Skip to content

Commit

Permalink
observertesthelper: move docker into separate package
Browse files Browse the repository at this point in the history
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
will-isovalent committed Jan 24, 2025
1 parent 9df054d commit d9924e9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 43 deletions.
73 changes: 73 additions & 0 deletions pkg/observer/observertesthelper/docker/docker.go
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)
}
}
52 changes: 9 additions & 43 deletions pkg/observer/observertesthelper/observer_test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d9924e9

Please sign in to comment.