Browse code

Update integration test with Assert

Part of #16756

Update integration-cli/docker_cli_rmi_test.go:
Use Assert instead of condition judgement in test.

Signed-off-by: Zhang Wei <zhangwei555@huawei.com>

Zhang Wei authored on 2015/10/10 12:32:55
Showing 1 changed files
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"os/exec"
6 6
 	"strings"
7 7
 
8
+	"github.com/docker/docker/pkg/integration/checker"
8 9
 	"github.com/go-check/check"
9 10
 )
10 11
 
... ...
@@ -13,27 +14,21 @@ func (s *DockerSuite) TestRmiWithContainerFails(c *check.C) {
13 13
 	errSubstr := "is using it"
14 14
 
15 15
 	// create a container
16
-	out, _, err := dockerCmdWithError("run", "-d", "busybox", "true")
17
-	if err != nil {
18
-		c.Fatalf("failed to create a container: %s, %v", out, err)
19
-	}
16
+	out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
20 17
 
21 18
 	cleanedContainerID := strings.TrimSpace(out)
22 19
 
23 20
 	// try to delete the image
24
-	out, _, err = dockerCmdWithError("rmi", "busybox")
25
-	if err == nil {
26
-		c.Fatalf("Container %q is using image, should not be able to rmi: %q", cleanedContainerID, out)
27
-	}
28
-	if !strings.Contains(out, errSubstr) {
29
-		c.Fatalf("Container %q is using image, error message should contain %q: %v", cleanedContainerID, errSubstr, out)
30
-	}
21
+	out, _, err := dockerCmdWithError("rmi", "busybox")
22
+	// Container is using image, should not be able to rmi
23
+	c.Assert(err, checker.NotNil)
24
+	// Container is using image, error message should contain errSubstr
25
+	c.Assert(out, checker.Contains, errSubstr, check.Commentf("Container: %q", cleanedContainerID))
31 26
 
32 27
 	// make sure it didn't delete the busybox name
33 28
 	images, _ := dockerCmd(c, "images")
34
-	if !strings.Contains(images, "busybox") {
35
-		c.Fatalf("The name 'busybox' should not have been removed from images: %q", images)
36
-	}
29
+	// The name 'busybox' should not have been removed from images
30
+	c.Assert(images, checker.Contains, "busybox")
37 31
 }
38 32
 
39 33
 func (s *DockerSuite) TestRmiTag(c *check.C) {
... ...
@@ -44,97 +39,71 @@ func (s *DockerSuite) TestRmiTag(c *check.C) {
44 44
 	dockerCmd(c, "tag", "busybox", "utest:5000/docker:tag3")
45 45
 	{
46 46
 		imagesAfter, _ := dockerCmd(c, "images", "-a")
47
-		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+3 {
48
-			c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
49
-		}
47
+		c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+3, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
50 48
 	}
51 49
 	dockerCmd(c, "rmi", "utest/docker:tag2")
52 50
 	{
53 51
 		imagesAfter, _ := dockerCmd(c, "images", "-a")
54
-		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 {
55
-			c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
56
-		}
57
-
52
+		c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+2, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
58 53
 	}
59 54
 	dockerCmd(c, "rmi", "utest:5000/docker:tag3")
60 55
 	{
61 56
 		imagesAfter, _ := dockerCmd(c, "images", "-a")
62
-		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+1 {
63
-			c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
64
-		}
57
+		c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+1, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
65 58
 
66 59
 	}
67 60
 	dockerCmd(c, "rmi", "utest:tag1")
68 61
 	{
69 62
 		imagesAfter, _ := dockerCmd(c, "images", "-a")
70
-		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+0 {
71
-			c.Fatalf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter)
72
-		}
63
+		c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n"), check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
73 64
 
74 65
 	}
75 66
 }
76 67
 
77 68
 func (s *DockerSuite) TestRmiImgIDMultipleTag(c *check.C) {
78 69
 	testRequires(c, DaemonIsLinux)
79
-	out, _, err := dockerCmdWithError("run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'")
80
-	if err != nil {
81
-		c.Fatalf("failed to create a container:%s, %v", out, err)
82
-	}
70
+	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'")
83 71
 
84 72
 	containerID := strings.TrimSpace(out)
85
-	out, _, err = dockerCmdWithError("commit", containerID, "busybox-one")
86
-	if err != nil {
87
-		c.Fatalf("failed to commit a new busybox-one:%s, %v", out, err)
88
-	}
73
+	dockerCmd(c, "commit", containerID, "busybox-one")
89 74
 
90 75
 	imagesBefore, _ := dockerCmd(c, "images", "-a")
91 76
 	dockerCmd(c, "tag", "busybox-one", "busybox-one:tag1")
92 77
 	dockerCmd(c, "tag", "busybox-one", "busybox-one:tag2")
93 78
 
94 79
 	imagesAfter, _ := dockerCmd(c, "images", "-a")
95
-	if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+2 {
96
-		c.Fatalf("tag busybox to create 2 more images with same imageID; docker images shows: %q\n", imagesAfter)
97
-	}
80
+	// tag busybox to create 2 more images with same imageID
81
+	c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+2, check.Commentf("docker images shows: %q\n", imagesAfter))
98 82
 
99 83
 	imgID, err := inspectField("busybox-one:tag1", "Id")
100
-	c.Assert(err, check.IsNil)
84
+	c.Assert(err, checker.IsNil)
101 85
 
102 86
 	// run a container with the image
103
-	out, _, err = dockerCmdWithError("run", "-d", "busybox-one", "top")
104
-	if err != nil {
105
-		c.Fatalf("failed to create a container:%s, %v", out, err)
106
-	}
87
+	out, _ = dockerCmd(c, "run", "-d", "busybox-one", "top")
107 88
 
108 89
 	containerID = strings.TrimSpace(out)
109 90
 
110 91
 	// first checkout without force it fails
111 92
 	out, _, err = dockerCmdWithError("rmi", imgID)
112 93
 	expected := fmt.Sprintf("conflict: unable to delete %s (cannot be forced) - image is being used by running container %s", imgID[:12], containerID[:12])
113
-	if err == nil || !strings.Contains(out, expected) {
114
-		c.Fatalf("rmi tagged in multiple repos should have failed without force: %s, %v, expected: %s", out, err, expected)
115
-	}
94
+	// rmi tagged in multiple repos should have failed without force
95
+	c.Assert(err, checker.NotNil)
96
+	c.Assert(out, checker.Contains, expected)
116 97
 
117 98
 	dockerCmd(c, "stop", containerID)
118 99
 	dockerCmd(c, "rmi", "-f", imgID)
119 100
 
120 101
 	imagesAfter, _ = dockerCmd(c, "images", "-a")
121
-	if strings.Contains(imagesAfter, imgID[:12]) {
122
-		c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter)
123
-	}
102
+	// rmi -f failed, image still exists
103
+	c.Assert(imagesAfter, checker.Not(checker.Contains), imgID[:12], check.Commentf("ImageID:%q; ImagesAfter: %q", imgID, imagesAfter))
124 104
 }
125 105
 
126 106
 func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
127 107
 	testRequires(c, DaemonIsLinux)
128
-	out, _, err := dockerCmdWithError("run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'")
129
-	if err != nil {
130
-		c.Fatalf("failed to create a container:%s, %v", out, err)
131
-	}
108
+	out, _ := dockerCmd(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'")
132 109
 
133 110
 	containerID := strings.TrimSpace(out)
134
-	out, _, err = dockerCmdWithError("commit", containerID, "busybox-test")
135
-	if err != nil {
136
-		c.Fatalf("failed to commit a new busybox-test:%s, %v", out, err)
137
-	}
111
+	dockerCmd(c, "commit", containerID, "busybox-test")
138 112
 
139 113
 	imagesBefore, _ := dockerCmd(c, "images", "-a")
140 114
 	dockerCmd(c, "tag", "busybox-test", "utest:tag1")
... ...
@@ -143,25 +112,23 @@ func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
143 143
 	dockerCmd(c, "tag", "busybox-test", "utest:5000/docker:tag4")
144 144
 	{
145 145
 		imagesAfter, _ := dockerCmd(c, "images", "-a")
146
-		if strings.Count(imagesAfter, "\n") != strings.Count(imagesBefore, "\n")+4 {
147
-			c.Fatalf("tag busybox to create 4 more images with same imageID; docker images shows: %q\n", imagesAfter)
148
-		}
146
+		c.Assert(strings.Count(imagesAfter, "\n"), checker.Equals, strings.Count(imagesBefore, "\n")+4, check.Commentf("before: %q\n\nafter: %q\n", imagesBefore, imagesAfter))
149 147
 	}
150 148
 	imgID, err := inspectField("busybox-test", "Id")
151
-	c.Assert(err, check.IsNil)
149
+	c.Assert(err, checker.IsNil)
152 150
 
153 151
 	// first checkout without force it fails
154 152
 	out, _, err = dockerCmdWithError("rmi", imgID)
155
-	if err == nil || !strings.Contains(out, "(must be forced) - image is referenced in one or more repositories") {
156
-		c.Fatalf("rmi tagged in multiple repos should have failed without force:%s, %v", out, err)
157
-	}
153
+	// rmi tagged in multiple repos should have failed without force
154
+	c.Assert(err, checker.NotNil)
155
+	// rmi tagged in multiple repos should have failed without force
156
+	c.Assert(out, checker.Contains, "(must be forced) - image is referenced in one or more repositories", check.Commentf("out: %s; err: %v;", out, err))
158 157
 
159 158
 	dockerCmd(c, "rmi", "-f", imgID)
160 159
 	{
161 160
 		imagesAfter, _ := dockerCmd(c, "images", "-a")
162
-		if strings.Contains(imagesAfter, imgID[:12]) {
163
-			c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter)
164
-		}
161
+		// rmi failed, image still exists
162
+		c.Assert(imagesAfter, checker.Not(checker.Contains), imgID[:12])
165 163
 	}
166 164
 }
167 165
 
... ...
@@ -170,17 +137,16 @@ func (s *DockerSuite) TestRmiImageIDForceWithRunningContainersAndMultipleTags(c
170 170
 	testRequires(c, DaemonIsLinux)
171 171
 	dockerfile := "FROM busybox\nRUN echo test 14116\n"
172 172
 	imgID, err := buildImage("test-14116", dockerfile, false)
173
-	c.Assert(err, check.IsNil)
173
+	c.Assert(err, checker.IsNil)
174 174
 
175 175
 	newTag := "newtag"
176 176
 	dockerCmd(c, "tag", imgID, newTag)
177 177
 	dockerCmd(c, "run", "-d", imgID, "top")
178 178
 
179 179
 	out, _, err := dockerCmdWithError("rmi", "-f", imgID)
180
-	if err == nil || !strings.Contains(out, "(cannot be forced) - image is being used by running container") {
181
-		c.Log(out)
182
-		c.Fatalf("rmi -f should not delete image with running containers")
183
-	}
180
+	// rmi -f should not delete image with running containers
181
+	c.Assert(err, checker.NotNil)
182
+	c.Assert(out, checker.Contains, "(cannot be forced) - image is being used by running container")
184 183
 }
185 184
 
186 185
 func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) {
... ...
@@ -188,19 +154,12 @@ func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) {
188 188
 	container := "test-delete-tag"
189 189
 	newtag := "busybox:newtag"
190 190
 	bb := "busybox:latest"
191
-	if out, _, err := dockerCmdWithError("tag", bb, newtag); err != nil {
192
-		c.Fatalf("Could not tag busybox: %v: %s", err, out)
193
-	}
194
-	if out, _, err := dockerCmdWithError("run", "--name", container, bb, "/bin/true"); err != nil {
195
-		c.Fatalf("Could not run busybox: %v: %s", err, out)
196
-	}
197
-	out, _, err := dockerCmdWithError("rmi", newtag)
198
-	if err != nil {
199
-		c.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out)
200
-	}
201
-	if d := strings.Count(out, "Untagged: "); d != 1 {
202
-		c.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
203
-	}
191
+	dockerCmd(c, "tag", bb, newtag)
192
+
193
+	dockerCmd(c, "run", "--name", container, bb, "/bin/true")
194
+
195
+	out, _ := dockerCmd(c, "rmi", newtag)
196
+	c.Assert(strings.Count(out, "Untagged: "), checker.Equals, 1)
204 197
 }
205 198
 
206 199
 func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) {
... ...
@@ -211,17 +170,12 @@ func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) {
211 211
 	cmd.Stdin = strings.NewReader(`FROM busybox
212 212
 MAINTAINER foo`)
213 213
 
214
-	if out, _, err := runCommandWithOutput(cmd); err != nil {
215
-		c.Fatalf("Could not build %s: %s, %v", image, out, err)
216
-	}
214
+	out, _, err := runCommandWithOutput(cmd)
215
+	c.Assert(err, checker.IsNil, check.Commentf("Could not build %s: %s", image, out))
217 216
 
218
-	if out, _, err := dockerCmdWithError("run", "--name", "test-force-rmi", image, "/bin/true"); err != nil {
219
-		c.Fatalf("Could not run container: %s, %v", out, err)
220
-	}
217
+	dockerCmd(c, "run", "--name", "test-force-rmi", image, "/bin/true")
221 218
 
222
-	if out, _, err := dockerCmdWithError("rmi", "-f", image); err != nil {
223
-		c.Fatalf("Could not remove image %s:  %s, %v", image, out, err)
224
-	}
219
+	dockerCmd(c, "rmi", "-f", image)
225 220
 }
226 221
 
227 222
 func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) {
... ...
@@ -229,51 +183,32 @@ func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) {
229 229
 	newRepo := "127.0.0.1:5000/busybox"
230 230
 	oldRepo := "busybox"
231 231
 	newTag := "busybox:test"
232
-	out, _, err := dockerCmdWithError("tag", oldRepo, newRepo)
233
-	if err != nil {
234
-		c.Fatalf("Could not tag busybox: %v: %s", err, out)
235
-	}
232
+	dockerCmd(c, "tag", oldRepo, newRepo)
236 233
 
237
-	out, _, err = dockerCmdWithError("run", "--name", "test", oldRepo, "touch", "/home/abcd")
238
-	if err != nil {
239
-		c.Fatalf("failed to run container: %v, output: %s", err, out)
240
-	}
234
+	dockerCmd(c, "run", "--name", "test", oldRepo, "touch", "/home/abcd")
241 235
 
242
-	out, _, err = dockerCmdWithError("commit", "test", newTag)
243
-	if err != nil {
244
-		c.Fatalf("failed to commit container: %v, output: %s", err, out)
245
-	}
236
+	dockerCmd(c, "commit", "test", newTag)
246 237
 
247
-	out, _, err = dockerCmdWithError("rmi", newTag)
248
-	if err != nil {
249
-		c.Fatalf("failed to remove image: %v, output: %s", err, out)
250
-	}
251
-	if !strings.Contains(out, "Untagged: "+newTag) {
252
-		c.Fatalf("Could not remove image %s: %s, %v", newTag, out, err)
253
-	}
238
+	out, _ := dockerCmd(c, "rmi", newTag)
239
+	c.Assert(out, checker.Contains, "Untagged: "+newTag)
254 240
 }
255 241
 
256 242
 func (s *DockerSuite) TestRmiBlank(c *check.C) {
257 243
 	testRequires(c, DaemonIsLinux)
258 244
 	// try to delete a blank image name
259 245
 	out, _, err := dockerCmdWithError("rmi", "")
260
-	if err == nil {
261
-		c.Fatal("Should have failed to delete '' image")
262
-	}
263
-	if strings.Contains(out, "no such id") {
264
-		c.Fatalf("Wrong error message generated: %s", out)
265
-	}
266
-	if !strings.Contains(out, "image name cannot be blank") {
267
-		c.Fatalf("Expected error message not generated: %s", out)
268
-	}
246
+	// Should have failed to delete '' image
247
+	c.Assert(err, checker.NotNil)
248
+	// Wrong error message generated
249
+	c.Assert(out, checker.Not(checker.Contains), "no such id", check.Commentf("out: %s", out))
250
+	// Expected error message not generated
251
+	c.Assert(out, checker.Contains, "image name cannot be blank", check.Commentf("out: %s", out))
269 252
 
270 253
 	out, _, err = dockerCmdWithError("rmi", " ")
271
-	if err == nil {
272
-		c.Fatal("Should have failed to delete '' image")
273
-	}
274
-	if !strings.Contains(out, "no such id") {
275
-		c.Fatalf("Expected error message not generated: %s", out)
276
-	}
254
+	// Should have failed to delete '' image
255
+	c.Assert(err, checker.NotNil)
256
+	// Expected error message not generated
257
+	c.Assert(out, checker.Contains, "no such id", check.Commentf("out: %s", out))
277 258
 }
278 259
 
279 260
 func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) {
... ...
@@ -284,7 +219,7 @@ func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) {
284 284
 	for i, name := range imageNames {
285 285
 		dockerfile := fmt.Sprintf("FROM busybox\nMAINTAINER %s\nRUN echo %s\n", name, name)
286 286
 		id, err := buildImage(name, dockerfile, false)
287
-		c.Assert(err, check.IsNil)
287
+		c.Assert(err, checker.IsNil)
288 288
 		imageIds[i] = id
289 289
 	}
290 290
 
... ...
@@ -297,10 +232,9 @@ func (s *DockerSuite) TestRmiContainerImageNotFound(c *check.C) {
297 297
 
298 298
 	// Try to remove the image of the running container and see if it fails as expected.
299 299
 	out, _, err := dockerCmdWithError("rmi", "-f", imageIds[0])
300
-	if err == nil || !strings.Contains(out, "image is being used by running container") {
301
-		c.Log(out)
302
-		c.Fatal("The image of the running container should not be removed.")
303
-	}
300
+	// The image of the running container should not be removed.
301
+	c.Assert(err, checker.NotNil)
302
+	c.Assert(out, checker.Contains, "image is being used by running container", check.Commentf("out: %s", out))
304 303
 }
305 304
 
306 305
 // #13422
... ...
@@ -315,7 +249,7 @@ RUN echo 1 #layer1
315 315
 RUN echo 2 #layer2
316 316
 `
317 317
 	_, err := buildImage(image, dockerfile, false)
318
-	c.Assert(err, check.IsNil)
318
+	c.Assert(err, checker.IsNil)
319 319
 
320 320
 	out, _ := dockerCmd(c, "history", "-q", image)
321 321
 	ids := strings.Split(out, "\n")
... ...
@@ -329,10 +263,8 @@ RUN echo 2 #layer2
329 329
 
330 330
 	// See if the "tmp2" can be untagged.
331 331
 	out, _ = dockerCmd(c, "rmi", newTag)
332
-	if d := strings.Count(out, "Untagged: "); d != 1 {
333
-		c.Log(out)
334
-		c.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
335
-	}
332
+	// Expected 1 untagged entry
333
+	c.Assert(strings.Count(out, "Untagged: "), checker.Equals, 1, check.Commentf("out: %s", out))
336 334
 
337 335
 	// Now let's add the tag again and create a container based on it.
338 336
 	dockerCmd(c, "tag", idToTag, newTag)
... ...
@@ -342,15 +274,13 @@ RUN echo 2 #layer2
342 342
 	// At this point we have 2 containers, one based on layer2 and another based on layer0.
343 343
 	// Try to untag "tmp2" without the -f flag.
344 344
 	out, _, err = dockerCmdWithError("rmi", newTag)
345
-	if err == nil || !strings.Contains(out, cid[:12]) || !strings.Contains(out, "(must force)") {
346
-		c.Log(out)
347
-		c.Fatalf("%q should not be untagged without the -f flag", newTag)
348
-	}
345
+	// should not be untagged without the -f flag
346
+	c.Assert(err, checker.NotNil)
347
+	c.Assert(out, checker.Contains, cid[:12])
348
+	c.Assert(out, checker.Contains, "(must force)")
349 349
 
350 350
 	// Add the -f flag and test again.
351 351
 	out, _ = dockerCmd(c, "rmi", "-f", newTag)
352
-	if !strings.Contains(out, fmt.Sprintf("Untagged: %s:latest", newTag)) {
353
-		c.Log(out)
354
-		c.Fatalf("%q should be allowed to untag with the -f flag", newTag)
355
-	}
352
+	// should be allowed to untag with the -f flag
353
+	c.Assert(out, checker.Contains, fmt.Sprintf("Untagged: %s:latest", newTag))
356 354
 }