-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
observertesthelper: move docker into separate package
A subsequent commit in this series needs to user observertesthelper's docker-related helpers in a test, but that creates an import cycle. Resolve the import cycle by moving docker-related helpers into a separate package and maintain backward compatibility by introducing deprecated stubs in observertesthelper that call into that new package. Signed-off-by: William Findlay <william.findlay@isovalent.com>
- Loading branch information
1 parent
9df054d
commit d9924e9
Showing
2 changed files
with
82 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Tetragon | ||
|
||
package docker | ||
|
||
import ( | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// DockerCreate creates a new docker container in the background. The container will | ||
// be killed and removed on test cleanup. | ||
// It returns the containerId on success, or an error if spawning the container failed. | ||
func DockerCreate(tb testing.TB, args ...string) (containerId string) { | ||
// note: we are not using `--rm` so we can choose to wait on the container | ||
// with `docker wait`. We remove it manually below in t.Cleanup instead | ||
args = append([]string{"create"}, args...) | ||
id, err := exec.Command("docker", args...).Output() | ||
if err != nil { | ||
tb.Fatalf("failed to spawn docker container %v: %s", args, err) | ||
} | ||
|
||
containerId = strings.TrimSpace(string(id)) | ||
tb.Cleanup(func() { | ||
err := exec.Command("docker", "rm", "--force", containerId).Run() | ||
if err != nil { | ||
tb.Logf("failed to remove container %s: %s", containerId, err) | ||
} | ||
}) | ||
|
||
return containerId | ||
} | ||
|
||
// DockerStart starts a new docker container with a given ID. | ||
func DockerStart(tb testing.TB, id string) { | ||
err := exec.Command("docker", "start", id).Run() | ||
if err != nil { | ||
tb.Fatalf("failed to start docker container %s: %s", id, err) | ||
} | ||
} | ||
|
||
// dockerRun starts a new docker container in the background. The container will | ||
// be killed and removed on test cleanup. | ||
// It returns the containerId on success, or an error if spawning the container failed. | ||
func DockerRun(tb testing.TB, args ...string) (containerId string) { | ||
// note: we are not using `--rm` so we can choose to wait on the container | ||
// with `docker wait`. We remove it manually below in t.Cleanup instead | ||
args = append([]string{"run", "--detach"}, args...) | ||
id, err := exec.Command("docker", args...).Output() | ||
if err != nil { | ||
tb.Fatalf("failed to spawn docker container %v: %s", args, err) | ||
} | ||
|
||
containerId = strings.TrimSpace(string(id)) | ||
tb.Cleanup(func() { | ||
err := exec.Command("docker", "rm", "--force", containerId).Run() | ||
if err != nil { | ||
tb.Logf("failed to remove container %s: %s", containerId, err) | ||
} | ||
}) | ||
|
||
return containerId | ||
} | ||
|
||
// dockerExec executes a command in a container. | ||
func DockerExec(tb testing.TB, id string, args ...string) { | ||
args = append([]string{"exec", id}, args...) | ||
err := exec.Command("docker", args...).Run() | ||
if err != nil { | ||
tb.Fatalf("failed to exec in docker container %v: %s", args, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters