Browse code

integration-cli: DockerCLIRestartSuite: replace dockerCmd and waitRun

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2023/07/28 16:43:38
Showing 1 changed files
... ...
@@ -9,6 +9,7 @@ import (
9 9
 	"time"
10 10
 
11 11
 	"github.com/docker/docker/integration-cli/checker"
12
+	"github.com/docker/docker/integration-cli/cli"
12 13
 	"gotest.tools/v3/assert"
13 14
 	is "gotest.tools/v3/assert/cmp"
14 15
 	"gotest.tools/v3/poll"
... ...
@@ -28,39 +29,37 @@ func (s *DockerCLIRestartSuite) OnTimeout(c *testing.T) {
28 28
 }
29 29
 
30 30
 func (s *DockerCLIRestartSuite) TestRestartStoppedContainer(c *testing.T) {
31
-	dockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar")
32
-	cleanedContainerID := getIDByName(c, "test")
31
+	cli.DockerCmd(c, "run", "--name=test", "busybox", "echo", "foobar")
32
+	cID := getIDByName(c, "test")
33 33
 
34
-	out, _ := dockerCmd(c, "logs", cleanedContainerID)
34
+	out := cli.DockerCmd(c, "logs", cID).Combined()
35 35
 	assert.Equal(c, out, "foobar\n")
36 36
 
37
-	dockerCmd(c, "restart", cleanedContainerID)
37
+	cli.DockerCmd(c, "restart", cID)
38 38
 
39 39
 	// Wait until the container has stopped
40
-	err := waitInspect(cleanedContainerID, "{{.State.Running}}", "false", 20*time.Second)
40
+	err := waitInspect(cID, "{{.State.Running}}", "false", 20*time.Second)
41 41
 	assert.NilError(c, err)
42 42
 
43
-	out, _ = dockerCmd(c, "logs", cleanedContainerID)
43
+	out = cli.DockerCmd(c, "logs", cID).Combined()
44 44
 	assert.Equal(c, out, "foobar\nfoobar\n")
45 45
 }
46 46
 
47 47
 func (s *DockerCLIRestartSuite) TestRestartRunningContainer(c *testing.T) {
48
-	out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
49
-
50
-	cleanedContainerID := strings.TrimSpace(out)
51
-
52
-	assert.NilError(c, waitRun(cleanedContainerID))
48
+	cID := cli.DockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'").Stdout()
49
+	cID = strings.TrimSpace(cID)
50
+	cli.WaitRun(c, cID)
53 51
 
54 52
 	getLogs := func(c *testing.T) (interface{}, string) {
55
-		out, _ := dockerCmd(c, "logs", cleanedContainerID)
53
+		out := cli.DockerCmd(c, "logs", cID).Combined()
56 54
 		return out, ""
57 55
 	}
58 56
 
59 57
 	// Wait 10 seconds for the 'echo' to appear in the logs
60 58
 	poll.WaitOn(c, pollCheck(c, getLogs, checker.Equals("foobar\n")), poll.WithTimeout(10*time.Second))
61 59
 
62
-	dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
63
-	assert.NilError(c, waitRun(cleanedContainerID))
60
+	cli.DockerCmd(c, "restart", "-t", "1", cID)
61
+	cli.WaitRun(c, cID)
64 62
 
65 63
 	// Wait 10 seconds for first 'echo' appear (again) in the logs
66 64
 	poll.WaitOn(c, pollCheck(c, getLogs, checker.Equals("foobar\nfoobar\n")), poll.WithTimeout(10*time.Second))
... ...
@@ -69,25 +68,23 @@ func (s *DockerCLIRestartSuite) TestRestartRunningContainer(c *testing.T) {
69 69
 // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
70 70
 func (s *DockerCLIRestartSuite) TestRestartWithVolumes(c *testing.T) {
71 71
 	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
72
-	out := runSleepingContainer(c, "-d", "-v", prefix+slash+"test")
73
-
74
-	cleanedContainerID := strings.TrimSpace(out)
75
-	out, err := inspectFilter(cleanedContainerID, "len .Mounts")
76
-	assert.NilError(c, err, "failed to inspect %s: %s", cleanedContainerID, out)
72
+	cID := runSleepingContainer(c, "-d", "-v", prefix+slash+"test")
73
+	out, err := inspectFilter(cID, "len .Mounts")
74
+	assert.NilError(c, err, "failed to inspect %s: %s", cID, out)
77 75
 	out = strings.Trim(out, " \n\r")
78 76
 	assert.Equal(c, out, "1")
79 77
 
80
-	source, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
78
+	source, err := inspectMountSourceField(cID, prefix+slash+"test")
81 79
 	assert.NilError(c, err)
82 80
 
83
-	dockerCmd(c, "restart", cleanedContainerID)
81
+	cli.DockerCmd(c, "restart", cID)
84 82
 
85
-	out, err = inspectFilter(cleanedContainerID, "len .Mounts")
86
-	assert.NilError(c, err, "failed to inspect %s: %s", cleanedContainerID, out)
83
+	out, err = inspectFilter(cID, "len .Mounts")
84
+	assert.NilError(c, err, "failed to inspect %s: %s", cID, out)
87 85
 	out = strings.Trim(out, " \n\r")
88 86
 	assert.Equal(c, out, "1")
89 87
 
90
-	sourceAfterRestart, err := inspectMountSourceField(cleanedContainerID, prefix+slash+"test")
88
+	sourceAfterRestart, err := inspectMountSourceField(cID, prefix+slash+"test")
91 89
 	assert.NilError(c, err)
92 90
 	assert.Equal(c, source, sourceAfterRestart)
93 91
 }
... ...
@@ -96,31 +93,29 @@ func (s *DockerCLIRestartSuite) TestRestartDisconnectedContainer(c *testing.T) {
96 96
 	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon, NotUserNamespace)
97 97
 
98 98
 	// Run a container on the default bridge network
99
-	out, _ := dockerCmd(c, "run", "-d", "--name", "c0", "busybox", "top")
100
-	cleanedContainerID := strings.TrimSpace(out)
101
-	assert.NilError(c, waitRun(cleanedContainerID))
99
+	cID := cli.DockerCmd(c, "run", "-d", "--name", "c0", "busybox", "top").Stdout()
100
+	cID = strings.TrimSpace(cID)
101
+	cli.WaitRun(c, cID)
102 102
 
103 103
 	// Disconnect the container from the network
104
-	out, exitCode := dockerCmd(c, "network", "disconnect", "bridge", "c0")
105
-	assert.Assert(c, exitCode == 0, out)
104
+	result := cli.DockerCmd(c, "network", "disconnect", "bridge", "c0")
105
+	assert.Assert(c, result.ExitCode == 0, result.Combined())
106 106
 
107 107
 	// Restart the container
108
-	out, exitCode = dockerCmd(c, "restart", "c0")
109
-	assert.Assert(c, exitCode == 0, out)
108
+	result = cli.DockerCmd(c, "restart", "c0")
109
+	assert.Assert(c, result.ExitCode == 0, result.Combined())
110 110
 }
111 111
 
112 112
 func (s *DockerCLIRestartSuite) TestRestartPolicyNO(c *testing.T) {
113
-	out, _ := dockerCmd(c, "create", "--restart=no", "busybox")
114
-
115
-	id := strings.TrimSpace(out)
116
-	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
113
+	cID := cli.DockerCmd(c, "create", "--restart=no", "busybox").Stdout()
114
+	cID = strings.TrimSpace(cID)
115
+	name := inspectField(c, cID, "HostConfig.RestartPolicy.Name")
117 116
 	assert.Equal(c, name, "no")
118 117
 }
119 118
 
120 119
 func (s *DockerCLIRestartSuite) TestRestartPolicyAlways(c *testing.T) {
121
-	out, _ := dockerCmd(c, "create", "--restart=always", "busybox")
122
-
123
-	id := strings.TrimSpace(out)
120
+	id := cli.DockerCmd(c, "create", "--restart=always", "busybox").Stdout()
121
+	id = strings.TrimSpace(id)
124 122
 	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
125 123
 	assert.Equal(c, name, "always")
126 124
 
... ...
@@ -135,30 +130,24 @@ func (s *DockerCLIRestartSuite) TestRestartPolicyOnFailure(c *testing.T) {
135 135
 	assert.ErrorContains(c, err, "", out)
136 136
 	assert.Assert(c, strings.Contains(out, "maximum retry count cannot be negative"))
137 137
 
138
-	out, _ = dockerCmd(c, "create", "--restart=on-failure:1", "busybox")
139
-
140
-	id := strings.TrimSpace(out)
138
+	id := cli.DockerCmd(c, "create", "--restart=on-failure:1", "busybox").Stdout()
139
+	id = strings.TrimSpace(id)
141 140
 	name := inspectField(c, id, "HostConfig.RestartPolicy.Name")
142 141
 	maxRetry := inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
143
-
144 142
 	assert.Equal(c, name, "on-failure")
145 143
 	assert.Equal(c, maxRetry, "1")
146 144
 
147
-	out, _ = dockerCmd(c, "create", "--restart=on-failure:0", "busybox")
148
-
149
-	id = strings.TrimSpace(out)
145
+	id = cli.DockerCmd(c, "create", "--restart=on-failure:0", "busybox").Stdout()
146
+	id = strings.TrimSpace(id)
150 147
 	name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
151 148
 	maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
152
-
153 149
 	assert.Equal(c, name, "on-failure")
154 150
 	assert.Equal(c, maxRetry, "0")
155 151
 
156
-	out, _ = dockerCmd(c, "create", "--restart=on-failure", "busybox")
157
-
158
-	id = strings.TrimSpace(out)
152
+	id = cli.DockerCmd(c, "create", "--restart=on-failure", "busybox").Stdout()
153
+	id = strings.TrimSpace(id)
159 154
 	name = inspectField(c, id, "HostConfig.RestartPolicy.Name")
160 155
 	maxRetry = inspectField(c, id, "HostConfig.RestartPolicy.MaximumRetryCount")
161
-
162 156
 	assert.Equal(c, name, "on-failure")
163 157
 	assert.Equal(c, maxRetry, "0")
164 158
 }
... ...
@@ -166,9 +155,8 @@ func (s *DockerCLIRestartSuite) TestRestartPolicyOnFailure(c *testing.T) {
166 166
 // a good container with --restart=on-failure:3
167 167
 // MaximumRetryCount!=0; RestartCount=0
168 168
 func (s *DockerCLIRestartSuite) TestRestartContainerwithGoodContainer(c *testing.T) {
169
-	out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
170
-
171
-	id := strings.TrimSpace(out)
169
+	id := cli.DockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true").Stdout()
170
+	id = strings.TrimSpace(id)
172 171
 	err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 30*time.Second)
173 172
 	assert.NilError(c, err)
174 173
 
... ...
@@ -189,9 +177,8 @@ func (s *DockerCLIRestartSuite) TestRestartContainerSuccess(c *testing.T) {
189 189
 		testRequires(c, testEnv.DaemonInfo.Isolation.IsProcess)
190 190
 	}
191 191
 
192
-	out := runSleepingContainer(c, "-d", "--restart=always")
193
-	id := strings.TrimSpace(out)
194
-	assert.NilError(c, waitRun(id))
192
+	id := runSleepingContainer(c, "-d", "--restart=always")
193
+	cli.WaitRun(c, id)
195 194
 
196 195
 	pidStr := inspectField(c, id, "State.Pid")
197 196
 
... ...
@@ -215,14 +202,13 @@ func (s *DockerCLIRestartSuite) TestRestartContainerSuccess(c *testing.T) {
215 215
 func (s *DockerCLIRestartSuite) TestRestartWithPolicyUserDefinedNetwork(c *testing.T) {
216 216
 	// TODO Windows. This may be portable following HNS integration post TP5.
217 217
 	testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon, NotUserNamespace)
218
-	dockerCmd(c, "network", "create", "-d", "bridge", "udNet")
218
+	cli.DockerCmd(c, "network", "create", "-d", "bridge", "udNet")
219 219
 
220
-	dockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top")
221
-	assert.NilError(c, waitRun("first"))
220
+	cli.DockerCmd(c, "run", "-d", "--net=udNet", "--name=first", "busybox", "top")
221
+	cli.WaitRun(c, "first")
222 222
 
223
-	dockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second",
224
-		"--link=first:foo", "busybox", "top")
225
-	assert.NilError(c, waitRun("second"))
223
+	cli.DockerCmd(c, "run", "-d", "--restart=always", "--net=udNet", "--name=second", "--link=first:foo", "busybox", "top")
224
+	cli.WaitRun(c, "second")
226 225
 
227 226
 	// ping to first and its alias foo must succeed
228 227
 	_, _, err := dockerCmdWithError("exec", "second", "ping", "-c", "1", "first")
... ...
@@ -266,13 +252,11 @@ func (s *DockerCLIRestartSuite) TestRestartPolicyAfterRestart(c *testing.T) {
266 266
 		testRequires(c, testEnv.DaemonInfo.Isolation.IsProcess)
267 267
 	}
268 268
 
269
-	out := runSleepingContainer(c, "-d", "--restart=always")
270
-	id := strings.TrimSpace(out)
271
-	assert.NilError(c, waitRun(id))
272
-
273
-	dockerCmd(c, "restart", id)
269
+	id := runSleepingContainer(c, "-d", "--restart=always")
270
+	cli.WaitRun(c, id)
274 271
 
275
-	assert.NilError(c, waitRun(id))
272
+	cli.DockerCmd(c, "restart", id)
273
+	cli.WaitRun(c, id)
276 274
 
277 275
 	pidStr := inspectField(c, id, "State.Pid")
278 276
 
... ...
@@ -294,11 +278,11 @@ func (s *DockerCLIRestartSuite) TestRestartPolicyAfterRestart(c *testing.T) {
294 294
 }
295 295
 
296 296
 func (s *DockerCLIRestartSuite) TestRestartContainerwithRestartPolicy(c *testing.T) {
297
-	out1, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false")
298
-	out2, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
297
+	id1 := cli.DockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "false").Stdout()
298
+	id1 = strings.TrimSpace(id1)
299
+	id2 := cli.DockerCmd(c, "run", "-d", "--restart=always", "busybox", "false").Stdout()
300
+	id2 = strings.TrimSpace(id2)
299 301
 
300
-	id1 := strings.TrimSpace(out1)
301
-	id2 := strings.TrimSpace(out2)
302 302
 	waitTimeout := 15 * time.Second
303 303
 	if testEnv.DaemonInfo.OSType == "windows" {
304 304
 		waitTimeout = 150 * time.Second
... ...
@@ -306,18 +290,18 @@ func (s *DockerCLIRestartSuite) TestRestartContainerwithRestartPolicy(c *testing
306 306
 	err := waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
307 307
 	assert.NilError(c, err)
308 308
 
309
-	dockerCmd(c, "restart", id1)
310
-	dockerCmd(c, "restart", id2)
309
+	cli.DockerCmd(c, "restart", id1)
310
+	cli.DockerCmd(c, "restart", id2)
311 311
 
312 312
 	// Make sure we can stop/start (regression test from a705e166cf3bcca62543150c2b3f9bfeae45ecfa)
313
-	dockerCmd(c, "stop", id1)
314
-	dockerCmd(c, "stop", id2)
315
-	dockerCmd(c, "start", id1)
316
-	dockerCmd(c, "start", id2)
313
+	cli.DockerCmd(c, "stop", id1)
314
+	cli.DockerCmd(c, "stop", id2)
315
+	cli.DockerCmd(c, "start", id1)
316
+	cli.DockerCmd(c, "start", id2)
317 317
 
318 318
 	// Kill the containers, making sure they are stopped at the end of the test
319
-	dockerCmd(c, "kill", id1)
320
-	dockerCmd(c, "kill", id2)
319
+	cli.DockerCmd(c, "kill", id1)
320
+	cli.DockerCmd(c, "kill", id2)
321 321
 	err = waitInspect(id1, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
322 322
 	assert.NilError(c, err)
323 323
 	err = waitInspect(id2, "{{ .State.Restarting }} {{ .State.Running }}", "false false", waitTimeout)
... ...
@@ -325,16 +309,14 @@ func (s *DockerCLIRestartSuite) TestRestartContainerwithRestartPolicy(c *testing
325 325
 }
326 326
 
327 327
 func (s *DockerCLIRestartSuite) TestRestartAutoRemoveContainer(c *testing.T) {
328
-	out := runSleepingContainer(c, "--rm")
329
-
330
-	id := strings.TrimSpace(out)
331
-	dockerCmd(c, "restart", id)
328
+	id := runSleepingContainer(c, "--rm")
329
+	cli.DockerCmd(c, "restart", id)
332 330
 	err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false true", 15*time.Second)
333 331
 	assert.NilError(c, err)
334 332
 
335
-	out, _ = dockerCmd(c, "ps")
333
+	out := cli.DockerCmd(c, "ps").Stdout()
336 334
 	assert.Assert(c, is.Contains(out, id[:12]), "container should be restarted instead of removed: %v", out)
337 335
 
338 336
 	// Kill the container to make sure it will be removed
339
-	dockerCmd(c, "kill", id)
337
+	cli.DockerCmd(c, "kill", id)
340 338
 }