Skip to content

Commit cbff0e5

Browse files
committed
be sure everything has been cleanup at the end of each tests
Signed-off-by: Guillaume Lours <[email protected]>
1 parent e4222bf commit cbff0e5

12 files changed

+26
-19
lines changed

pkg/e2e/build_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333

3434
func TestLocalComposeBuild(t *testing.T) {
3535

36-
for _, env := range []string{"DOCKER_BUILDKIT=0", "DOCKER_BUILDKIT=1", "DOCKER_BUILDKIT=1,COMPOSE_BAKE=1"} {
36+
for _, env := range []string{"DOCKER_BUILDKIT=0", "DOCKER_BUILDKIT=1", "DOCKER_BUILDKIT=1,COMPOSE-BAKE=1"} {
3737
c := NewCLI(t, WithEnv(strings.Split(env, ",")...))
3838

3939
t.Run(env+" build named and unnamed images", func(t *testing.T) {
@@ -118,7 +118,7 @@ func TestLocalComposeBuild(t *testing.T) {
118118
})
119119

120120
t.Run(env+" rebuild when up --build", func(t *testing.T) {
121-
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/build-test", "up", "-d", "--build")
121+
res := c.RunDockerComposeCmd(t, "--workdir", "fixtures/build-test", "up", "-d", "--build")
122122

123123
res.Assert(t, icmd.Expected{Out: "COPY static /usr/share/nginx/html"})
124124
res.Assert(t, icmd.Expected{Out: "COPY static2 /usr/share/nginx/html"})

pkg/e2e/compose_exec_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,13 @@ func TestLocalComposeExecOneOff(t *testing.T) {
7070
c := NewParallelCLI(t)
7171

7272
const projectName = "compose-e2e-exec-one-off"
73+
defer c.cleanupWithDown(t, projectName)
7374
cmdArgs := func(cmd string, args ...string) []string {
7475
ret := []string{"--project-directory", "fixtures/simple-composefile", "--project-name", projectName, cmd}
7576
ret = append(ret, args...)
7677
return ret
7778
}
7879

79-
cleanup := func() {
80-
c.RunDockerComposeCmd(t, cmdArgs("down", "--timeout=0")...)
81-
}
82-
cleanup()
83-
t.Cleanup(cleanup)
84-
8580
c.RunDockerComposeCmd(t, cmdArgs("run", "-d", "simple")...)
8681

8782
t.Run("exec in one-off container", func(t *testing.T) {
@@ -93,4 +88,7 @@ func TestLocalComposeExecOneOff(t *testing.T) {
9388
res := c.RunDockerComposeCmdNoCheck(t, cmdArgs("exec", "--index", "1", "-e", "FOO", "simple", "/usr/bin/env")...)
9489
res.Assert(t, icmd.Expected{ExitCode: 1, Err: "service \"simple\" is not running container #1"})
9590
})
91+
cmdResult := c.RunDockerCmd(t, "ps", "-q", "--filter", "label=com.docker.compose.project=compose-e2e-exec-one-off").Stdout()
92+
containerIDs := strings.Split(cmdResult, "\n")
93+
_ = c.RunDockerOrExitError(t, append([]string{"stop"}, containerIDs...)...)
9694
}

pkg/e2e/compose_run_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
func TestLocalComposeRun(t *testing.T) {
2929
c := NewParallelCLI(t)
30+
defer c.cleanupWithDown(t, "run-test")
3031

3132
t.Run("compose run", func(t *testing.T) {
3233
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back")

pkg/e2e/compose_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ func TestRemoveOrphaned(t *testing.T) {
320320

321321
func TestComposeFileSetByDotEnv(t *testing.T) {
322322
c := NewCLI(t)
323+
defer c.cleanupWithDown(t, "dotenv")
323324

324325
cmd := c.NewDockerComposeCmd(t, "config")
325326
cmd.Dir = filepath.Join(".", "fixtures", "dotenv")
@@ -335,6 +336,7 @@ func TestComposeFileSetByDotEnv(t *testing.T) {
335336

336337
func TestComposeFileSetByProjectDirectory(t *testing.T) {
337338
c := NewCLI(t)
339+
defer c.cleanupWithDown(t, "dotenv")
338340

339341
dir := filepath.Join(".", "fixtures", "dotenv", "development")
340342
cmd := c.NewDockerComposeCmd(t, "--project-directory", dir, "config")
@@ -347,6 +349,7 @@ func TestComposeFileSetByProjectDirectory(t *testing.T) {
347349

348350
func TestComposeFileSetByEnvFile(t *testing.T) {
349351
c := NewCLI(t)
352+
defer c.cleanupWithDown(t, "dotenv")
350353

351354
dotEnv, err := os.CreateTemp(t.TempDir(), ".env")
352355
assert.NilError(t, err)
@@ -370,6 +373,7 @@ COMPOSE_PROFILES=test
370373

371374
func TestNestedDotEnv(t *testing.T) {
372375
c := NewCLI(t)
376+
defer c.cleanupWithDown(t, "nested")
373377

374378
cmd := c.NewDockerComposeCmd(t, "run", "echo")
375379
cmd.Dir = filepath.Join(".", "fixtures", "nested")
@@ -381,6 +385,7 @@ func TestNestedDotEnv(t *testing.T) {
381385

382386
cmd = c.NewDockerComposeCmd(t, "run", "echo")
383387
cmd.Dir = filepath.Join(".", "fixtures", "nested", "sub")
388+
defer c.cleanupWithDown(t, "nested")
384389
res = icmd.RunCmd(cmd)
385390
res.Assert(t, icmd.Expected{
386391
ExitCode: 0,
@@ -392,9 +397,7 @@ func TestNestedDotEnv(t *testing.T) {
392397
func TestUnnecessaryResources(t *testing.T) {
393398
const projectName = "compose-e2e-unnecessary-resources"
394399
c := NewParallelCLI(t)
395-
t.Cleanup(func() {
396-
c.RunDockerComposeCmd(t, "-p", projectName, "down", "-t=0")
397-
})
400+
defer c.cleanupWithDown(t, projectName)
398401

399402
res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/external/compose.yaml", "-p", projectName, "up", "-d")
400403
res.Assert(t, icmd.Expected{

pkg/e2e/compose_up_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func TestStdoutStderr(t *testing.T) {
9494
func TestLoggingDriver(t *testing.T) {
9595
c := NewCLI(t)
9696
const projectName = "e2e-logging-driver"
97+
defer c.cleanupWithDown(t, projectName)
9798

9899
host := "HOST=127.0.0.1"
99100
res := c.RunDockerCmd(t, "info", "-f", "{{.OperatingSystem}}")

pkg/e2e/configs_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
func TestConfigFromEnv(t *testing.T) {
2626
c := NewParallelCLI(t)
27+
defer c.cleanupWithDown(t, "configs")
2728

2829
t.Run("config from file", func(t *testing.T) {
2930
res := icmd.RunCmd(c.NewDockerComposeCmd(t, "-f", "./fixtures/configs/compose.yaml", "run", "from_file"))

pkg/e2e/env_file_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
func TestRawEnvFile(t *testing.T) {
2727
c := NewParallelCLI(t)
28+
defer c.cleanupWithDown(t, "dotenv")
2829

2930
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/dotenv/raw.yaml", "run", "test")
3031
assert.Equal(t, strings.TrimSpace(res.Stdout()), "'{\"key\": \"value\"}'")

pkg/e2e/framework.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,8 @@ func HTTPGetWithRetry(
494494
}
495495
return ""
496496
}
497+
498+
func (c *CLI) cleanupWithDown(t testing.TB, project string, args ...string) {
499+
t.Helper()
500+
c.RunDockerComposeCmd(t, append([]string{"-p", project, "down", "-v", "--remove-orphans"}, args...)...)
501+
}

pkg/e2e/networks_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func TestNetworkAliases(t *testing.T) {
6363
c := NewParallelCLI(t)
6464

6565
const projectName = "network_alias_e2e"
66+
defer c.cleanupWithDown(t, projectName)
6667

6768
t.Run("up", func(t *testing.T) {
6869
c.RunDockerComposeCmd(t, "-f", "./fixtures/network-alias/compose.yaml", "--project-name", projectName, "up",
@@ -136,16 +137,14 @@ func TestNetworkModes(t *testing.T) {
136137
c := NewCLI(t)
137138

138139
const projectName = "network_mode_service_run"
140+
defer c.cleanupWithDown(t, projectName)
139141

140142
t.Run("run with service mode dependency", func(t *testing.T) {
141143
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/network-test/compose.yaml", "--project-name", projectName, "run", "-T", "mydb", "echo", "success")
142144
res.Assert(t, icmd.Expected{Out: "success"})
143145

144146
})
145147

146-
t.Run("down", func(t *testing.T) {
147-
_ = c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
148-
})
149148
}
150149

151150
func TestNetworkConfigChanged(t *testing.T) {

pkg/e2e/orphans_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func TestRemoveOrphans(t *testing.T) {
2727
c := NewCLI(t)
2828

2929
const projectName = "compose-e2e-orphans"
30+
defer c.cleanupWithDown(t, projectName)
3031

3132
c.RunDockerComposeCmd(t, "-f", "./fixtures/orphans/compose.yaml", "-p", projectName, "run", "orphan")
3233
res := c.RunDockerComposeCmd(t, "-p", projectName, "ps", "--all")

0 commit comments

Comments
 (0)