Skip to content

Commit 9622395

Browse files
committed
e2e: ensure all Compose cmds standalone compatible
The E2E tests can be run in plugin (`docker compose`) or standalone (`docker-compose`) mode. Existing logic was in place to ensure that the helper method is always used, which will invoke the right one based on how tests are being executed. However, this logic was too easy to (unintentionally) bypass given the myriad of ways that commands can be run. The check has been made stricter and pushed to a lower-level to more aggressively catch instances. As a result, a bunch of calls to `RunDockerCmd` are now updated to be `RunDockerComposeCmd`, which will ensure that the invocation is correct based on test mode. Signed-off-by: Milas Bowman <[email protected]>
1 parent bec4ea4 commit 9622395

15 files changed

+54
-45
lines changed

pkg/e2e/cancel_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ func TestComposeCancel(t *testing.T) {
4141

4242
// require a separate groupID from the process running tests, in order to simulate ctrl+C from a terminal.
4343
// sending kill signal
44-
cmd, stdout, stderr, err := StartWithNewGroupID(c.NewDockerCmd("compose", "-f", buildProjectPath, "build", "--progress", "plain"))
44+
cmd, stdout, stderr, err := StartWithNewGroupID(c.NewDockerComposeCmd(t, "-f", buildProjectPath, "build",
45+
"--progress", "plain"))
4546
assert.NilError(t, err)
4647

4748
c.WaitForCondition(t, func() (bool, string) {

pkg/e2e/cascade_stop_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ func TestCascadeStop(t *testing.T) {
2828
const projectName = "e2e-cascade-stop"
2929

3030
t.Run("abort-on-container-exit", func(t *testing.T) {
31-
res := c.RunDockerOrExitError(t, "compose", "-f", "./fixtures/cascade-stop-test/compose.yaml", "--project-name", projectName, "up", "--abort-on-container-exit")
31+
res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/cascade-stop-test/compose.yaml", "--project-name", projectName, "up", "--abort-on-container-exit")
3232
res.Assert(t, icmd.Expected{ExitCode: 1, Out: `should_fail-1 exited with code 1`})
3333
res.Assert(t, icmd.Expected{ExitCode: 1, Out: `Aborting on container exit...`})
3434
})
3535

3636
t.Run("exit-code-from", func(t *testing.T) {
37-
res := c.RunDockerOrExitError(t, "compose", "-f", "./fixtures/cascade-stop-test/compose.yaml", "--project-name", projectName, "up", "--exit-code-from=sleep")
37+
res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/cascade-stop-test/compose.yaml", "--project-name", projectName, "up", "--exit-code-from=sleep")
3838
res.Assert(t, icmd.Expected{ExitCode: 137, Out: `should_fail-1 exited with code 1`})
3939
res.Assert(t, icmd.Expected{ExitCode: 137, Out: `Aborting on container exit...`})
4040
})
4141

4242
t.Run("exit-code-from unknown", func(t *testing.T) {
43-
res := c.RunDockerOrExitError(t, "compose", "-f", "./fixtures/cascade-stop-test/compose.yaml", "--project-name", projectName, "up", "--exit-code-from=unknown")
43+
res := c.RunDockerComposeCmdNoCheck(t, "-f", "./fixtures/cascade-stop-test/compose.yaml", "--project-name", projectName, "up", "--exit-code-from=unknown")
4444
res.Assert(t, icmd.Expected{ExitCode: 1, Err: `no such service: unknown`})
4545
})
4646

pkg/e2e/compose_build_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ func TestLocalComposeBuild(t *testing.T) {
5757
c.RunDockerOrExitError(t, "rmi", "build-test_nginx")
5858
c.RunDockerOrExitError(t, "rmi", "custom-nginx")
5959

60-
icmd.RunCmd(c.NewDockerCmd("compose", "--project-directory", "fixtures/build-test", "build", "--build-arg", "FOO"),
60+
icmd.RunCmd(c.NewDockerComposeCmd(t,
61+
"--project-directory",
62+
"fixtures/build-test",
63+
"build",
64+
"--build-arg",
65+
"FOO"),
6166
func(cmd *icmd.Cmd) {
6267
cmd.Env = append(cmd.Env, "FOO=BAR")
6368
})
@@ -69,7 +74,7 @@ func TestLocalComposeBuild(t *testing.T) {
6974
t.Run("build with multiple build-args ", func(t *testing.T) {
7075
// ensure local test run does not reuse previously build image
7176
c.RunDockerOrExitError(t, "rmi", "-f", "multi-args_multiargs")
72-
cmd := c.NewDockerCmd("compose", "--project-directory", "fixtures/build-test/multi-args", "build")
77+
cmd := c.NewDockerComposeCmd(t, "--project-directory", "fixtures/build-test/multi-args", "build")
7378

7479
icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
7580
cmd.Env = append(cmd.Env, "DOCKER_BUILDKIT=0")

pkg/e2e/compose_down_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestDown(t *testing.T) {
2828
const projectName = "e2e-down"
2929

3030
t.Run("no resource to remove", func(t *testing.T) {
31-
res := c.RunDockerOrExitError(t, "compose", "--project-name", projectName, "down")
31+
res := c.RunDockerComposeCmd(t, "--project-name", projectName, "down")
3232
res.Assert(t, icmd.Expected{ExitCode: 0, Err: `No resource found to remove for project "e2e-down"`})
3333
})
3434
}

pkg/e2e/compose_exec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestLocalComposeExec(t *testing.T) {
4343
})
4444

4545
t.Run("exec with env set", func(t *testing.T) {
46-
res := icmd.RunCmd(c.NewDockerCmd("exec", "-e", "FOO", "compose-e2e-exec-simple-1", "/usr/bin/env"),
46+
res := icmd.RunCmd(c.NewDockerCmd(t, "exec", "-e", "FOO", "compose-e2e-exec-simple-1", "/usr/bin/env"),
4747
func(cmd *icmd.Cmd) {
4848
cmd.Env = append(cmd.Env, "FOO=BAR")
4949
})

pkg/e2e/compose_run_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,15 @@ func TestLocalComposeRun(t *testing.T) {
108108
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
109109
assert.Assert(t, strings.Contains(res.Combined(), "orphan"))
110110

111-
cmd := c.NewDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
111+
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "run", "back", "echo", "Hello")
112112
res = icmd.RunCmd(cmd, func(cmd *icmd.Cmd) {
113113
cmd.Env = append(cmd.Env, "COMPOSE_IGNORE_ORPHANS=True")
114114
})
115115
assert.Assert(t, !strings.Contains(res.Combined(), "orphan"))
116116
})
117117

118118
t.Run("down", func(t *testing.T) {
119-
cmd := c.NewDockerCmd("compose", "-f", "./fixtures/run-test/compose.yaml", "down")
119+
cmd := c.NewDockerComposeCmd(t, "-f", "./fixtures/run-test/compose.yaml", "down")
120120
icmd.RunCmd(cmd, func(c *icmd.Cmd) {
121121
c.Env = append(c.Env, "COMPOSE_REMOVE_ORPHANS=True")
122122
})

pkg/e2e/compose_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func TestLocalComposeUp(t *testing.T) {
8888
})
8989

9090
t.Run("check healthcheck output", func(t *testing.T) {
91-
c.WaitForCmdResult(t, c.NewDockerCmd("compose", "-p", projectName, "ps", "--format", "json"),
91+
c.WaitForCmdResult(t, c.NewDockerComposeCmd(t, "-p", projectName, "ps", "--format", "json"),
9292
StdoutContains(`"Name":"compose-e2e-demo-web-1","Command":"/dispatcher","Project":"compose-e2e-demo","Service":"web","State":"running","Health":"healthy"`),
9393
5*time.Second, 1*time.Second)
9494

@@ -123,7 +123,7 @@ func TestLocalComposeUp(t *testing.T) {
123123
func TestComposePull(t *testing.T) {
124124
c := NewParallelCLI(t)
125125

126-
res := c.RunDockerOrExitError(t, "compose", "--project-directory", "fixtures/simple-composefile", "pull")
126+
res := c.RunDockerComposeCmd(t, "--project-directory", "fixtures/simple-composefile", "pull")
127127
output := res.Combined()
128128

129129
assert.Assert(t, strings.Contains(output, "simple Pulled"))
@@ -148,9 +148,9 @@ func TestDownComposefileInParentFolder(t *testing.T) {
148148
func TestAttachRestart(t *testing.T) {
149149
c := NewParallelCLI(t)
150150

151-
cmd := c.NewDockerCmd("compose", "--ansi=never", "--project-directory", "./fixtures/attach-restart", "up")
151+
cmd := c.NewDockerComposeCmd(t, "--ansi=never", "--project-directory", "./fixtures/attach-restart", "up")
152152
res := icmd.StartCmd(cmd)
153-
defer c.RunDockerOrExitError(t, "compose", "-p", "attach-restart", "down")
153+
defer c.RunDockerComposeCmd(t, "-p", "attach-restart", "down")
154154

155155
c.WaitForCondition(t, func() (bool, string) {
156156
debug := res.Combined()
@@ -165,8 +165,8 @@ func TestAttachRestart(t *testing.T) {
165165
func TestInitContainer(t *testing.T) {
166166
c := NewParallelCLI(t)
167167

168-
res := c.RunDockerOrExitError(t, "compose", "--ansi=never", "--project-directory", "./fixtures/init-container", "up")
169-
defer c.RunDockerOrExitError(t, "compose", "-p", "init-container", "down")
168+
res := c.RunDockerComposeCmd(t, "--ansi=never", "--project-directory", "./fixtures/init-container", "up")
169+
defer c.RunDockerComposeCmd(t, "-p", "init-container", "down")
170170
testify.Regexp(t, "foo-1 | hello(?m:.*)bar-1 | world", res.Stdout())
171171
}
172172

pkg/e2e/framework.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,19 @@ func (c *CLI) MetricsSocket() string {
228228
}
229229

230230
// NewDockerCmd creates a docker cmd without running it
231-
func (c *CLI) NewDockerCmd(args ...string) icmd.Cmd {
231+
func (c *CLI) NewDockerCmd(t testing.TB, args ...string) icmd.Cmd {
232+
for _, arg := range args {
233+
if arg == compose.PluginName {
234+
t.Fatal("This test called 'RunDockerCmd' for 'compose'. Please prefer 'RunDockerComposeCmd' to be able to test as a plugin and standalone")
235+
}
236+
}
232237
return c.NewCmd(DockerExecutableName, args...)
233238
}
234239

235240
// RunDockerOrExitError runs a docker command and returns a result
236241
func (c *CLI) RunDockerOrExitError(t testing.TB, args ...string) *icmd.Result {
237242
fmt.Printf("\t[%s] docker %s\n", t.Name(), strings.Join(args, " "))
238-
return icmd.RunCmd(c.NewDockerCmd(args...))
243+
return icmd.RunCmd(c.NewDockerCmd(t, args...))
239244
}
240245

241246
// RunCmd runs a command, expects no error and returns a result
@@ -260,9 +265,6 @@ func (c *CLI) RunCmdInDir(t testing.TB, dir string, args ...string) *icmd.Result
260265

261266
// RunDockerCmd runs a docker command, expects no error and returns a result
262267
func (c *CLI) RunDockerCmd(t testing.TB, args ...string) *icmd.Result {
263-
if len(args) > 0 && args[0] == compose.PluginName {
264-
t.Fatal("This test called 'RunDockerCmd' for 'compose'. Please prefer 'RunDockerComposeCmd' to be able to test as a plugin and standalone")
265-
}
266268
res := c.RunDockerOrExitError(t, args...)
267269
res.Assert(t, icmd.Success)
268270
return res

pkg/e2e/metrics_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,29 @@ func TestComposeMetrics(t *testing.T) {
2727
c := NewParallelCLI(t)
2828

2929
t.Run("catch specific failure metrics", func(t *testing.T) {
30-
res := c.RunDockerOrExitError(t, "compose", "-f", "fixtures/does-not-exist/compose.yaml", "build")
30+
res := c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/does-not-exist/compose.yaml", "build")
3131
expectedErr := "fixtures/does-not-exist/compose.yaml: no such file or directory"
3232
if runtime.GOOS == "windows" {
3333
expectedErr = "does-not-exist\\compose.yaml: The system cannot find the path specified"
3434
}
3535
res.Assert(t, icmd.Expected{ExitCode: 14, Err: expectedErr})
36-
res = c.RunDockerOrExitError(t, "compose", "-f", "fixtures/wrong-composefile/compose.yaml", "up", "-d")
36+
res = c.RunDockerComposeCmdNoCheck(t, "-f", "fixtures/wrong-composefile/compose.yaml", "up", "-d")
3737
res.Assert(t, icmd.Expected{ExitCode: 15, Err: "services.simple Additional property wrongField is not allowed"})
38-
res = c.RunDockerOrExitError(t, "compose", "up")
38+
res = c.RunDockerComposeCmdNoCheck(t, "up")
3939
res.Assert(t, icmd.Expected{ExitCode: 14, Err: "no configuration file provided: not found"})
40-
res = c.RunDockerOrExitError(t, "compose", "up", "-f", "fixtures/wrong-composefile/compose.yaml")
40+
res = c.RunDockerComposeCmdNoCheck(t, "up", "-f", "fixtures/wrong-composefile/compose.yaml")
4141
res.Assert(t, icmd.Expected{ExitCode: 16, Err: "unknown shorthand flag: 'f' in -f"})
42-
res = c.RunDockerOrExitError(t, "compose", "up", "--file", "fixtures/wrong-composefile/compose.yaml")
42+
res = c.RunDockerComposeCmdNoCheck(t, "up", "--file", "fixtures/wrong-composefile/compose.yaml")
4343
res.Assert(t, icmd.Expected{ExitCode: 16, Err: "unknown flag: --file"})
44-
res = c.RunDockerOrExitError(t, "compose", "donw", "--file", "fixtures/wrong-composefile/compose.yaml")
44+
res = c.RunDockerComposeCmdNoCheck(t, "donw", "--file", "fixtures/wrong-composefile/compose.yaml")
4545
res.Assert(t, icmd.Expected{ExitCode: 16, Err: `unknown docker command: "compose donw"`})
46-
res = c.RunDockerOrExitError(t, "compose", "--file", "fixtures/wrong-composefile/build-error.yml", "build")
46+
res = c.RunDockerComposeCmdNoCheck(t, "--file", "fixtures/wrong-composefile/build-error.yml", "build")
4747
res.Assert(t, icmd.Expected{ExitCode: 17, Err: `line 17: unknown instruction: WRONG`})
48-
res = c.RunDockerOrExitError(t, "compose", "--file", "fixtures/wrong-composefile/build-error.yml", "up")
48+
res = c.RunDockerComposeCmdNoCheck(t, "--file", "fixtures/wrong-composefile/build-error.yml", "up")
4949
res.Assert(t, icmd.Expected{ExitCode: 17, Err: `line 17: unknown instruction: WRONG`})
50-
res = c.RunDockerOrExitError(t, "compose", "--file", "fixtures/wrong-composefile/unknown-image.yml", "pull")
50+
res = c.RunDockerComposeCmdNoCheck(t, "--file", "fixtures/wrong-composefile/unknown-image.yml", "pull")
5151
res.Assert(t, icmd.Expected{ExitCode: 18, Err: `pull access denied for unknownimage, repository does not exist or may require 'docker login'`})
52-
res = c.RunDockerOrExitError(t, "compose", "--file", "fixtures/wrong-composefile/unknown-image.yml", "up")
52+
res = c.RunDockerComposeCmdNoCheck(t, "--file", "fixtures/wrong-composefile/unknown-image.yml", "up")
5353
res.Assert(t, icmd.Expected{ExitCode: 18, Err: `pull access denied for unknownimage, repository does not exist or may require 'docker login'`})
5454
})
5555
}

pkg/e2e/networks_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func TestNetworkModes(t *testing.T) {
150150
const projectName = "network_mode_service_run"
151151

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

156156
})

0 commit comments

Comments
 (0)