Browse code

integration-cli: DockerCLIUpdateSuite: replace dockerCmd and waitRun

Also adding some consts for fixed values.

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

Sebastiaan van Stijn authored on 2023/07/28 06:38:30
Showing 1 changed files
... ...
@@ -14,6 +14,7 @@ import (
14 14
 	"github.com/creack/pty"
15 15
 	"github.com/docker/docker/api/types"
16 16
 	"github.com/docker/docker/client"
17
+	"github.com/docker/docker/integration-cli/cli"
17 18
 	"github.com/docker/docker/testutil"
18 19
 	"github.com/docker/docker/testutil/request"
19 20
 	"gotest.tools/v3/assert"
... ...
@@ -31,14 +32,14 @@ func (s *DockerCLIUpdateSuite) TestUpdateRunningContainer(c *testing.T) {
31 31
 	testRequires(c, DaemonIsLinux)
32 32
 	testRequires(c, memoryLimitSupport)
33 33
 
34
-	name := "test-update-container"
35
-	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
36
-	dockerCmd(c, "update", "-m", "500M", name)
34
+	const name = "test-update-container"
35
+	cli.DockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
36
+	cli.DockerCmd(c, "update", "-m", "500M", name)
37 37
 
38 38
 	assert.Equal(c, inspectField(c, name, "HostConfig.Memory"), "524288000")
39 39
 
40
-	file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
41
-	out, _ := dockerCmd(c, "exec", name, "cat", file)
40
+	const file = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
41
+	out := cli.DockerCmd(c, "exec", name, "cat", file).Stdout()
42 42
 	assert.Equal(c, strings.TrimSpace(out), "524288000")
43 43
 }
44 44
 
... ...
@@ -46,15 +47,15 @@ func (s *DockerCLIUpdateSuite) TestUpdateRunningContainerWithRestart(c *testing.
46 46
 	testRequires(c, DaemonIsLinux)
47 47
 	testRequires(c, memoryLimitSupport)
48 48
 
49
-	name := "test-update-container"
50
-	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
51
-	dockerCmd(c, "update", "-m", "500M", name)
52
-	dockerCmd(c, "restart", name)
49
+	const name = "test-update-container"
50
+	cli.DockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "top")
51
+	cli.DockerCmd(c, "update", "-m", "500M", name)
52
+	cli.DockerCmd(c, "restart", name)
53 53
 
54 54
 	assert.Equal(c, inspectField(c, name, "HostConfig.Memory"), "524288000")
55 55
 
56
-	file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
57
-	out, _ := dockerCmd(c, "exec", name, "cat", file)
56
+	const file = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
57
+	out := cli.DockerCmd(c, "exec", name, "cat", file).Stdout()
58 58
 	assert.Equal(c, strings.TrimSpace(out), "524288000")
59 59
 }
60 60
 
... ...
@@ -62,14 +63,14 @@ func (s *DockerCLIUpdateSuite) TestUpdateStoppedContainer(c *testing.T) {
62 62
 	testRequires(c, DaemonIsLinux)
63 63
 	testRequires(c, memoryLimitSupport)
64 64
 
65
-	name := "test-update-container"
66
-	file := "/sys/fs/cgroup/memory/memory.limit_in_bytes"
67
-	dockerCmd(c, "run", "--name", name, "-m", "300M", "busybox", "cat", file)
68
-	dockerCmd(c, "update", "-m", "500M", name)
65
+	const name = "test-update-container"
66
+	const file = "/sys/fs/cgroup/memory/memory.limit_in_bytes"
67
+	cli.DockerCmd(c, "run", "--name", name, "-m", "300M", "busybox", "cat", file)
68
+	cli.DockerCmd(c, "update", "-m", "500M", name)
69 69
 
70 70
 	assert.Equal(c, inspectField(c, name, "HostConfig.Memory"), "524288000")
71 71
 
72
-	out, _ := dockerCmd(c, "start", "-a", name)
72
+	out := cli.DockerCmd(c, "start", "-a", name).Stdout()
73 73
 	assert.Equal(c, strings.TrimSpace(out), "524288000")
74 74
 }
75 75
 
... ...
@@ -77,16 +78,16 @@ func (s *DockerCLIUpdateSuite) TestUpdatePausedContainer(c *testing.T) {
77 77
 	testRequires(c, DaemonIsLinux)
78 78
 	testRequires(c, cpuShare)
79 79
 
80
-	name := "test-update-container"
81
-	dockerCmd(c, "run", "-d", "--name", name, "--cpu-shares", "1000", "busybox", "top")
82
-	dockerCmd(c, "pause", name)
83
-	dockerCmd(c, "update", "--cpu-shares", "500", name)
80
+	const name = "test-update-container"
81
+	cli.DockerCmd(c, "run", "-d", "--name", name, "--cpu-shares", "1000", "busybox", "top")
82
+	cli.DockerCmd(c, "pause", name)
83
+	cli.DockerCmd(c, "update", "--cpu-shares", "500", name)
84 84
 
85 85
 	assert.Equal(c, inspectField(c, name, "HostConfig.CPUShares"), "500")
86 86
 
87
-	dockerCmd(c, "unpause", name)
88
-	file := "/sys/fs/cgroup/cpu/cpu.shares"
89
-	out, _ := dockerCmd(c, "exec", name, "cat", file)
87
+	cli.DockerCmd(c, "unpause", name)
88
+	const file = "/sys/fs/cgroup/cpu/cpu.shares"
89
+	out := cli.DockerCmd(c, "exec", name, "cat", file).Stdout()
90 90
 	assert.Equal(c, strings.TrimSpace(out), "500")
91 91
 }
92 92
 
... ...
@@ -95,16 +96,16 @@ func (s *DockerCLIUpdateSuite) TestUpdateWithUntouchedFields(c *testing.T) {
95 95
 	testRequires(c, memoryLimitSupport)
96 96
 	testRequires(c, cpuShare)
97 97
 
98
-	name := "test-update-container"
99
-	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "--cpu-shares", "800", "busybox", "top")
100
-	dockerCmd(c, "update", "-m", "500M", name)
98
+	const name = "test-update-container"
99
+	cli.DockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "--cpu-shares", "800", "busybox", "top")
100
+	cli.DockerCmd(c, "update", "-m", "500M", name)
101 101
 
102 102
 	// Update memory and not touch cpus, `cpuset.cpus` should still have the old value
103 103
 	out := inspectField(c, name, "HostConfig.CPUShares")
104 104
 	assert.Equal(c, out, "800")
105 105
 
106
-	file := "/sys/fs/cgroup/cpu/cpu.shares"
107
-	out, _ = dockerCmd(c, "exec", name, "cat", file)
106
+	const file = "/sys/fs/cgroup/cpu/cpu.shares"
107
+	out = cli.DockerCmd(c, "exec", name, "cat", file).Stdout()
108 108
 	assert.Equal(c, strings.TrimSpace(out), "800")
109 109
 }
110 110
 
... ...
@@ -112,8 +113,8 @@ func (s *DockerCLIUpdateSuite) TestUpdateContainerInvalidValue(c *testing.T) {
112 112
 	testRequires(c, DaemonIsLinux)
113 113
 	testRequires(c, memoryLimitSupport)
114 114
 
115
-	name := "test-update-container"
116
-	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
115
+	const name = "test-update-container"
116
+	cli.DockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
117 117
 	out, _, err := dockerCmdWithError("update", "-m", "2M", name)
118 118
 	assert.ErrorContains(c, err, "")
119 119
 	expected := "Minimum memory limit allowed is 6MB"
... ...
@@ -124,8 +125,8 @@ func (s *DockerCLIUpdateSuite) TestUpdateContainerWithoutFlags(c *testing.T) {
124 124
 	testRequires(c, DaemonIsLinux)
125 125
 	testRequires(c, memoryLimitSupport)
126 126
 
127
-	name := "test-update-container"
128
-	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
127
+	const name = "test-update-container"
128
+	cli.DockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
129 129
 	_, _, err := dockerCmdWithError("update", name)
130 130
 	assert.ErrorContains(c, err, "")
131 131
 }
... ...
@@ -135,14 +136,14 @@ func (s *DockerCLIUpdateSuite) TestUpdateSwapMemoryOnly(c *testing.T) {
135 135
 	testRequires(c, memoryLimitSupport)
136 136
 	testRequires(c, swapMemorySupport)
137 137
 
138
-	name := "test-update-container"
139
-	dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "--memory-swap", "500M", "busybox", "top")
140
-	dockerCmd(c, "update", "--memory-swap", "600M", name)
138
+	const name = "test-update-container"
139
+	cli.DockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "--memory-swap", "500M", "busybox", "top")
140
+	cli.DockerCmd(c, "update", "--memory-swap", "600M", name)
141 141
 
142 142
 	assert.Equal(c, inspectField(c, name, "HostConfig.MemorySwap"), "629145600")
143 143
 
144
-	file := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
145
-	out, _ := dockerCmd(c, "exec", name, "cat", file)
144
+	const file = "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
145
+	out := cli.DockerCmd(c, "exec", name, "cat", file).Stdout()
146 146
 	assert.Equal(c, strings.TrimSpace(out), "629145600")
147 147
 }
148 148
 
... ...
@@ -151,8 +152,8 @@ func (s *DockerCLIUpdateSuite) TestUpdateInvalidSwapMemory(c *testing.T) {
151 151
 	testRequires(c, memoryLimitSupport)
152 152
 	testRequires(c, swapMemorySupport)
153 153
 
154
-	name := "test-update-container"
155
-	dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "--memory-swap", "500M", "busybox", "top")
154
+	const name = "test-update-container"
155
+	cli.DockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "--memory-swap", "500M", "busybox", "top")
156 156
 	_, _, err := dockerCmdWithError("update", "--memory-swap", "200M", name)
157 157
 	// Update invalid swap memory should fail.
158 158
 	// This will pass docker config validation, but failed at kernel validation
... ...
@@ -162,12 +163,12 @@ func (s *DockerCLIUpdateSuite) TestUpdateInvalidSwapMemory(c *testing.T) {
162 162
 	assert.Equal(c, inspectField(c, name, "HostConfig.Memory"), "314572800")
163 163
 	assert.Equal(c, inspectField(c, name, "HostConfig.MemorySwap"), "524288000")
164 164
 
165
-	dockerCmd(c, "update", "--memory-swap", "600M", name)
165
+	cli.DockerCmd(c, "update", "--memory-swap", "600M", name)
166 166
 
167 167
 	assert.Equal(c, inspectField(c, name, "HostConfig.MemorySwap"), "629145600")
168 168
 
169
-	file := "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
170
-	out, _ := dockerCmd(c, "exec", name, "cat", file)
169
+	const file = "/sys/fs/cgroup/memory/memory.memsw.limit_in_bytes"
170
+	out := cli.DockerCmd(c, "exec", name, "cat", file).Stdout()
171 171
 	assert.Equal(c, strings.TrimSpace(out), "629145600")
172 172
 }
173 173
 
... ...
@@ -175,10 +176,9 @@ func (s *DockerCLIUpdateSuite) TestUpdateStats(c *testing.T) {
175 175
 	testRequires(c, DaemonIsLinux)
176 176
 	testRequires(c, memoryLimitSupport)
177 177
 	testRequires(c, cpuCfsQuota)
178
-	name := "foo"
179
-	dockerCmd(c, "run", "-d", "-ti", "--name", name, "-m", "500m", "busybox")
180
-
181
-	assert.NilError(c, waitRun(name))
178
+	const name = "foo"
179
+	cli.DockerCmd(c, "run", "-d", "-ti", "--name", name, "-m", "500m", "busybox")
180
+	cli.WaitRun(c, name)
182 181
 
183 182
 	getMemLimit := func(id string) uint64 {
184 183
 		resp, body, err := request.Get(testutil.GetContext(c), fmt.Sprintf("/containers/%s/stats?stream=false", id))
... ...
@@ -194,7 +194,7 @@ func (s *DockerCLIUpdateSuite) TestUpdateStats(c *testing.T) {
194 194
 	}
195 195
 	preMemLimit := getMemLimit(name)
196 196
 
197
-	dockerCmd(c, "update", "--cpu-quota", "2000", name)
197
+	cli.DockerCmd(c, "update", "--cpu-quota", "2000", name)
198 198
 
199 199
 	curMemLimit := getMemLimit(name)
200 200
 	assert.Equal(c, preMemLimit, curMemLimit)
... ...
@@ -205,21 +205,21 @@ func (s *DockerCLIUpdateSuite) TestUpdateMemoryWithSwapMemory(c *testing.T) {
205 205
 	testRequires(c, memoryLimitSupport)
206 206
 	testRequires(c, swapMemorySupport)
207 207
 
208
-	name := "test-update-container"
209
-	dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "busybox", "top")
208
+	const name = "test-update-container"
209
+	cli.DockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "busybox", "top")
210 210
 	out, _, err := dockerCmdWithError("update", "--memory", "800M", name)
211 211
 	assert.ErrorContains(c, err, "")
212 212
 	assert.Assert(c, strings.Contains(out, "Memory limit should be smaller than already set memoryswap limit"))
213 213
 
214
-	dockerCmd(c, "update", "--memory", "800M", "--memory-swap", "1000M", name)
214
+	cli.DockerCmd(c, "update", "--memory", "800M", "--memory-swap", "1000M", name)
215 215
 }
216 216
 
217 217
 func (s *DockerCLIUpdateSuite) TestUpdateNotAffectMonitorRestartPolicy(c *testing.T) {
218 218
 	testRequires(c, DaemonIsLinux, cpuShare)
219 219
 
220
-	out, _ := dockerCmd(c, "run", "-tid", "--restart=always", "busybox", "sh")
221
-	id := strings.TrimSpace(out)
222
-	dockerCmd(c, "update", "--cpu-shares", "512", id)
220
+	id := cli.DockerCmd(c, "run", "-tid", "--restart=always", "busybox", "sh").Stdout()
221
+	id = strings.TrimSpace(id)
222
+	cli.DockerCmd(c, "update", "--cpu-shares", "512", id)
223 223
 
224 224
 	cpty, tty, err := pty.Open()
225 225
 	assert.NilError(c, err)
... ...
@@ -239,19 +239,19 @@ func (s *DockerCLIUpdateSuite) TestUpdateNotAffectMonitorRestartPolicy(c *testin
239 239
 	// container should restart again and keep running
240 240
 	err = waitInspect(id, "{{.RestartCount}}", "1", 30*time.Second)
241 241
 	assert.NilError(c, err)
242
-	assert.NilError(c, waitRun(id))
242
+	cli.WaitRun(c, id)
243 243
 }
244 244
 
245 245
 func (s *DockerCLIUpdateSuite) TestUpdateWithNanoCPUs(c *testing.T) {
246 246
 	testRequires(c, cpuCfsQuota, cpuCfsPeriod)
247 247
 
248
-	file1 := "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
249
-	file2 := "/sys/fs/cgroup/cpu/cpu.cfs_period_us"
248
+	const file1 = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
249
+	const file2 = "/sys/fs/cgroup/cpu/cpu.cfs_period_us"
250 250
 
251
-	out, _ := dockerCmd(c, "run", "-d", "--cpus", "0.5", "--name", "top", "busybox", "top")
251
+	out := cli.DockerCmd(c, "run", "-d", "--cpus", "0.5", "--name", "top", "busybox", "top").Stdout()
252 252
 	assert.Assert(c, strings.TrimSpace(out) != "")
253 253
 
254
-	out, _ = dockerCmd(c, "exec", "top", "sh", "-c", fmt.Sprintf("cat %s && cat %s", file1, file2))
254
+	out = cli.DockerCmd(c, "exec", "top", "sh", "-c", fmt.Sprintf("cat %s && cat %s", file1, file2)).Combined()
255 255
 	assert.Equal(c, strings.TrimSpace(out), "50000\n100000")
256 256
 
257 257
 	clt, err := client.NewClientWithOpts(client.FromEnv)
... ...
@@ -269,7 +269,7 @@ func (s *DockerCLIUpdateSuite) TestUpdateWithNanoCPUs(c *testing.T) {
269 269
 	assert.ErrorContains(c, err, "")
270 270
 	assert.Assert(c, strings.Contains(out, "Conflicting options: CPU Quota cannot be updated as NanoCPUs has already been set"))
271 271
 
272
-	dockerCmd(c, "update", "--cpus", "0.8", "top")
272
+	cli.DockerCmd(c, "update", "--cpus", "0.8", "top")
273 273
 	inspect, err = clt.ContainerInspect(testutil.GetContext(c), "top")
274 274
 	assert.NilError(c, err)
275 275
 	assert.Equal(c, inspect.HostConfig.NanoCPUs, int64(800000000))
... ...
@@ -279,6 +279,6 @@ func (s *DockerCLIUpdateSuite) TestUpdateWithNanoCPUs(c *testing.T) {
279 279
 	out = inspectField(c, "top", "HostConfig.CpuPeriod")
280 280
 	assert.Equal(c, out, "0", "CPU CFS period should be 0")
281 281
 
282
-	out, _ = dockerCmd(c, "exec", "top", "sh", "-c", fmt.Sprintf("cat %s && cat %s", file1, file2))
282
+	out = cli.DockerCmd(c, "exec", "top", "sh", "-c", fmt.Sprintf("cat %s && cat %s", file1, file2)).Combined()
283 283
 	assert.Equal(c, strings.TrimSpace(out), "80000\n100000")
284 284
 }