be sure everything has been cleanup at the end of each tests

Signed-off-by: Guillaume Lours <705411+glours@users.noreply.github.com>
This commit is contained in:
Guillaume Lours 2024-12-04 11:22:22 +01:00
parent e4222bff53
commit cbff0e5559
12 changed files with 26 additions and 19 deletions

View File

@ -33,7 +33,7 @@ import (
func TestLocalComposeBuild(t *testing.T) {
for _, env := range []string{"DOCKER_BUILDKIT=0", "DOCKER_BUILDKIT=1", "DOCKER_BUILDKIT=1,COMPOSE_BAKE=1"} {
for _, env := range []string{"DOCKER_BUILDKIT=0", "DOCKER_BUILDKIT=1", "DOCKER_BUILDKIT=1,COMPOSE-BAKE=1"} {
c := NewCLI(t, WithEnv(strings.Split(env, ",")...))
t.Run(env+" build named and unnamed images", func(t *testing.T) {
@ -118,7 +118,7 @@ func TestLocalComposeBuild(t *testing.T) {
})
t.Run(env+" rebuild when up --build", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "up", "-d", "--build")
res := c.RunDockerComposeCmd(t, "--workdir", "fixtures/build-test", "up", "-d", "--build")
res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})

View File

@ -70,18 +70,13 @@ func TestLocalComposeExecOneOff(t *testing.T) {
c := NewParallelCLI(t)
const projectName = "compose-e2e-exec-one-off"
defer c.cleanupWithDown(t, projectName)
cmdArgs := func(cmd string, args ...string) []string {
ret := []string{"--project-directory", "fixtures/simple-composefile", "--project-name", projectName, cmd}
ret = append(ret, args...)
return ret
}
cleanup := func() {
c.RunDockerComposeCmd(t, cmdArgs("down", "--timeout=0")...)
}
cleanup()
t.Cleanup(cleanup)
c.RunDockerComposeCmd(t, cmdArgs("run", "-d", "simple")...)
t.Run("exec in one-off container", func(t *testing.T) {
@ -93,4 +88,7 @@ func TestLocalComposeExecOneOff(t *testing.T) {
res := c.RunDockerComposeCmdNoCheck(t, cmdArgs("exec", "--index", "1", "-e", "FOO", "simple", "/usr/bin/env")...)
res.Assert(t, icmd.Expected{ExitCode: 1, Err: "service \"simple\" is not running container #1"})
})
cmdResult := c.RunDockerCmd(t, "ps", "-q", "--filter", "label=com.docker.compose.project=compose-e2e-exec-one-off").Stdout()
containerIDs := strings.Split(cmdResult, "\n")
_ = c.RunDockerOrExitError(t, append([]string{"stop"}, containerIDs...)...)
}

View File

@ -27,6 +27,7 @@ import (
func TestLocalComposeRun(t *testing.T) {
c := NewParallelCLI(t)
defer c.cleanupWithDown(t, "run-test")
t.Run("compose run", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back")

View File

@ -320,6 +320,7 @@ func TestRemoveOrphaned(t *testing.T) {
func TestComposeFileSetByDotEnv(t *testing.T) {
c := NewCLI(t)
defer c.cleanupWithDown(t, "dotenv")
cmd := c.NewDockerComposeCmd(t, "config")
cmd.Dir = filepath.Join(".", "fixtures", "dotenv")
@ -335,6 +336,7 @@ func TestComposeFileSetByDotEnv(t *testing.T) {
func TestComposeFileSetByProjectDirectory(t *testing.T) {
c := NewCLI(t)
defer c.cleanupWithDown(t, "dotenv")
dir := filepath.Join(".", "fixtures", "dotenv", "development")
cmd := c.NewDockerComposeCmd(t, "--project-directory", dir, "config")
@ -347,6 +349,7 @@ func TestComposeFileSetByProjectDirectory(t *testing.T) {
func TestComposeFileSetByEnvFile(t *testing.T) {
c := NewCLI(t)
defer c.cleanupWithDown(t, "dotenv")
dotEnv, err := os.CreateTemp(t.TempDir(), ".env")
assert.NilError(t, err)
@ -370,6 +373,7 @@ COMPOSE_PROFILES=test
func TestNestedDotEnv(t *testing.T) {
c := NewCLI(t)
defer c.cleanupWithDown(t, "nested")
cmd := c.NewDockerComposeCmd(t, "run", "echo")
cmd.Dir = filepath.Join(".", "fixtures", "nested")
@ -381,6 +385,7 @@ func TestNestedDotEnv(t *testing.T) {
cmd = c.NewDockerComposeCmd(t, "run", "echo")
cmd.Dir = filepath.Join(".", "fixtures", "nested", "sub")
defer c.cleanupWithDown(t, "nested")
res = icmd.RunCmd(cmd)
res.Assert(t, icmd.Expected{
ExitCode: 0,
@ -392,9 +397,7 @@ func TestNestedDotEnv(t *testing.T) {
func TestUnnecessaryResources(t *testing.T) {
const projectName = "compose-e2e-unnecessary-resources"
c := NewParallelCLI(t)
t.Cleanup(func() {
c.RunDockerComposeCmd(t, "-p", projectName, "down", "-t=0")
})
defer c.cleanupWithDown(t, projectName)
res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/external/compose.yaml", "-p", projectName, "up", "-d")
res.Assert(t, icmd.Expected{

View File

@ -94,6 +94,7 @@ func TestStdoutStderr(t *testing.T) {
func TestLoggingDriver(t *testing.T) {
c := NewCLI(t)
const projectName = "e2e-logging-driver"
defer c.cleanupWithDown(t, projectName)
host := "HOST=127.0.0.1"
res := c.RunDockerCmd(t, "info", "-f", "{{.OperatingSystem}}")

View File

@ -24,6 +24,7 @@ import (
func TestConfigFromEnv(t *testing.T) {
c := NewParallelCLI(t)
defer c.cleanupWithDown(t, "configs")
t.Run("config from file", func(t *testing.T) {
res := icmd.RunCmd(c.NewDockerComposeCmd(t, "-f", "./fixtures/configs/compose.yaml", "run", "from_file"))

View File

@ -25,6 +25,7 @@ import (
func TestRawEnvFile(t *testing.T) {
c := NewParallelCLI(t)
defer c.cleanupWithDown(t, "dotenv")
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/dotenv/raw.yaml", "run", "test")
assert.Equal(t, strings.TrimSpace(res.Stdout()), "'{\"key\": \"value\"}'")

View File

@ -494,3 +494,8 @@ func HTTPGetWithRetry(
}
return ""
}
func (c *CLI) cleanupWithDown(t testing.TB, project string, args ...string) {
t.Helper()
c.RunDockerComposeCmd(t, append([]string{"-p", project, "down", "-v", "--remove-orphans"}, args...)...)
}

View File

@ -63,6 +63,7 @@ func TestNetworkAliases(t *testing.T) {
c := NewParallelCLI(t)
const projectName = "network_alias_e2e"
defer c.cleanupWithDown(t, projectName)
t.Run("up", func(t *testing.T) {
c.RunDockerComposeCmd(t, "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName, "up",
@ -136,6 +137,7 @@ func TestNetworkModes(t *testing.T) {
c := NewCLI(t)
const projectName = "network_mode_service_run"
defer c.cleanupWithDown(t, projectName)
t.Run("run with service mode dependency", func(t *testing.T) {
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/network-test/compose.yaml", "--project-name", projectName, "run", "-T", "mydb", "echo", "success")
@ -143,9 +145,6 @@ func TestNetworkModes(t *testing.T) {
})
t.Run("down", func(t *testing.T) {
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
})
}
func TestNetworkConfigChanged(t *testing.T) {

View File

@ -27,6 +27,7 @@ func TestRemoveOrphans(t *testing.T) {
c := NewCLI(t)
const projectName = "compose-e2e-orphans"
defer c.cleanupWithDown(t, projectName)
c.RunDockerComposeCmd(t, "-f", "./fixtures/orphans/compose.yaml", "-p", projectName, "run", "orphan")
res := c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--all")

View File

@ -24,6 +24,7 @@ import (
func TestSecretFromEnv(t *testing.T) {
c := NewParallelCLI(t)
defer c.cleanupWithDown(t, "env-secret")
t.Run("compose run", func(t *testing.T) {
res := icmd.RunCmd(c.NewDockerComposeCmd(t, "-f", "./fixtures/env-secret/compose.yaml", "run", "foo"),

View File

@ -165,11 +165,7 @@ func doTest(t *testing.T, svcName string) {
cli := NewCLI(t, WithEnv(env...))
// important that --rmi is used to prune the images and ensure that watch builds on launch
cleanup := func() {
cli.RunDockerComposeCmd(t, "down", svcName, "--remove-orphans", "--volumes", "--rmi=local")
}
cleanup()
t.Cleanup(cleanup)
defer cli.cleanupWithDown(t, projName, "--rmi=local")
cmd := cli.NewDockerComposeCmd(t, "--verbose", "watch", svcName)
// stream output since watch runs in the background