From d9924e98465cf6b648ed7369dd6be2dce84e7739 Mon Sep 17 00:00:00 2001 From: William Findlay Date: Fri, 24 Jan 2025 11:36:49 -0500 Subject: [PATCH] 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 --- .../observertesthelper/docker/docker.go | 73 +++++++++++++++++++ .../observer_test_helper.go | 52 +++---------- 2 files changed, 82 insertions(+), 43 deletions(-) create mode 100644 pkg/observer/observertesthelper/docker/docker.go diff --git a/pkg/observer/observertesthelper/docker/docker.go b/pkg/observer/observertesthelper/docker/docker.go new file mode 100644 index 00000000000..4cafa4f3ea8 --- /dev/null +++ b/pkg/observer/observertesthelper/docker/docker.go @@ -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) + } +} diff --git a/pkg/observer/observertesthelper/observer_test_helper.go b/pkg/observer/observertesthelper/observer_test_helper.go index ea5b92bd84d..5a9af505e0a 100644 --- a/pkg/observer/observertesthelper/observer_test_helper.go +++ b/pkg/observer/observertesthelper/observer_test_helper.go @@ -24,6 +24,7 @@ import ( "github.com/cilium/tetragon/pkg/encoder" "github.com/cilium/tetragon/pkg/metricsconfig" "github.com/cilium/tetragon/pkg/observer" + "github.com/cilium/tetragon/pkg/observer/observertesthelper/docker" "github.com/cilium/tetragon/pkg/policyfilter" "github.com/cilium/tetragon/pkg/tracingpolicy" "github.com/sirupsen/logrus" @@ -524,64 +525,29 @@ func ExecWGCurl(readyWG *sync.WaitGroup, retries uint, args ...string) error { // 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. +// DEPRECATED: use docker.DockerCreate instead. 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 + return docker.DockerCreate(tb, args...) } // DockerStart starts a new docker container with a given ID. +// DEPRECATED: use docker.DockerStart instead. 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) - } + docker.DockerStart(tb, id) } // 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. +// DEPRECATED: use docker.DockerRun instead. 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 + return docker.DockerRun(tb, args...) } // dockerExec executes a command in a container. +// DEPRECATED: use docker.DockerExec instead. 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) - } + docker.DockerExec(tb, id, args...) } type fakeK8sWatcher struct {