Skip to content

Commit 4270987

Browse files
authored
Merge pull request docker#9567 from milas/e2e-cmd
e2e: ensure all Compose cmds standalone compatible
2 parents 4ea3ba7 + 3248b7b commit 4270987

15 files changed

+65
-53
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: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,33 @@ func TestLocalComposeExec(t *testing.T) {
2929

3030
const projectName = "compose-e2e-exec"
3131

32-
c.RunDockerComposeCmd(t, "--project-directory", "fixtures/simple-composefile", "--project-name", projectName, "up",
33-
"-d")
32+
cmdArgs := func(cmd string, args ...string) []string {
33+
ret := []string{"--project-directory", "fixtures/simple-composefile", "--project-name", projectName, cmd}
34+
ret = append(ret, args...)
35+
return ret
36+
}
37+
38+
c.RunDockerComposeCmd(t, cmdArgs("up", "-d")...)
3439

3540
t.Run("exec true", func(t *testing.T) {
36-
res := c.RunDockerOrExitError(t, "exec", "compose-e2e-exec-simple-1", "/bin/true")
37-
res.Assert(t, icmd.Expected{ExitCode: 0})
41+
c.RunDockerComposeCmd(t, cmdArgs("exec", "simple", "/bin/true")...)
3842
})
3943

4044
t.Run("exec false", func(t *testing.T) {
41-
res := c.RunDockerOrExitError(t, "exec", "compose-e2e-exec-simple-1", "/bin/false")
45+
res := c.RunDockerComposeCmdNoCheck(t, cmdArgs("exec", "simple", "/bin/false")...)
4246
res.Assert(t, icmd.Expected{ExitCode: 1})
4347
})
4448

4549
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"),
50+
res := icmd.RunCmd(c.NewDockerComposeCmd(t, cmdArgs("exec", "-e", "FOO", "simple", "/usr/bin/env")...),
4751
func(cmd *icmd.Cmd) {
4852
cmd.Env = append(cmd.Env, "FOO=BAR")
4953
})
5054
res.Assert(t, icmd.Expected{Out: "FOO=BAR"})
5155
})
5256

5357
t.Run("exec without env set", func(t *testing.T) {
54-
res := c.RunDockerOrExitError(t, "exec", "-e", "FOO", "compose-e2e-exec-simple-1", "/usr/bin/env")
55-
res.Assert(t, icmd.Expected{ExitCode: 0})
56-
assert.Check(t, !strings.Contains(res.Stdout(), "FOO="))
58+
res := c.RunDockerComposeCmd(t, cmdArgs("exec", "-e", "FOO", "simple", "/usr/bin/env")...)
59+
assert.Check(t, !strings.Contains(res.Stdout(), "FOO="), res.Combined())
5760
})
5861
}

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)