Browse code

Use check in params so we don't ignore errors

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2016/12/08 18:56:29
Showing 9 changed files
... ...
@@ -44,12 +44,14 @@ func (s *DockerSuite) OnTimeout(c *check.C) {
44 44
 }
45 45
 
46 46
 func (s *DockerSuite) TearDownTest(c *check.C) {
47
-	unpauseAllContainers()
48
-	deleteAllContainers()
49
-	deleteAllImages()
50
-	deleteAllVolumes()
51
-	deleteAllNetworks()
52
-	deleteAllPlugins()
47
+	unpauseAllContainers(c)
48
+	deleteAllContainers(c)
49
+	deleteAllImages(c)
50
+	deleteAllVolumes(c)
51
+	deleteAllNetworks(c)
52
+	if daemonPlatform == "linux" {
53
+		deleteAllPlugins(c)
54
+	}
53 55
 }
54 56
 
55 57
 func init() {
... ...
@@ -346,7 +346,7 @@ func (s *DockerSuite) TestGetStoppedContainerStats(c *check.C) {
346 346
 func (s *DockerSuite) TestContainerAPIPause(c *check.C) {
347 347
 	// Problematic on Windows as Windows does not support pause
348 348
 	testRequires(c, DaemonIsLinux)
349
-	defer unpauseAllContainers()
349
+	defer unpauseAllContainers(c)
350 350
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "sleep", "30")
351 351
 	ContainerID := strings.TrimSpace(out)
352 352
 
... ...
@@ -155,7 +155,7 @@ func (s *DockerSuite) TestAttachDisconnect(c *check.C) {
155 155
 
156 156
 func (s *DockerSuite) TestAttachPausedContainer(c *check.C) {
157 157
 	testRequires(c, IsPausable)
158
-	defer unpauseAllContainers()
158
+	defer unpauseAllContainers(c)
159 159
 	runSleepingContainer(c, "-d", "--name=test")
160 160
 	dockerCmd(c, "pause", "test")
161 161
 
... ...
@@ -39,7 +39,7 @@ func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
39 39
 //test commit a paused container should not unpause it after commit
40 40
 func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
41 41
 	testRequires(c, DaemonIsLinux)
42
-	defer unpauseAllContainers()
42
+	defer unpauseAllContainers(c)
43 43
 	out, _ := dockerCmd(c, "run", "-i", "-d", "busybox")
44 44
 
45 45
 	cleanedContainerID := strings.TrimSpace(out)
... ...
@@ -139,7 +139,7 @@ func (s *DockerSuite) TestExecExitStatus(c *check.C) {
139 139
 
140 140
 func (s *DockerSuite) TestExecPausedContainer(c *check.C) {
141 141
 	testRequires(c, IsPausable)
142
-	defer unpauseAllContainers()
142
+	defer unpauseAllContainers(c)
143 143
 
144 144
 	out, _ := runSleepingContainer(c, "-d", "--name", "testing")
145 145
 	ContainerID := strings.TrimSpace(out)
... ...
@@ -388,7 +388,7 @@ func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
388 388
 	// Not applicable on Windows to Windows CI.
389 389
 	testRequires(c, SameHostDaemon, DaemonIsLinux)
390 390
 	for _, fn := range []string{"resolv.conf", "hosts"} {
391
-		deleteAllContainers()
391
+		deleteAllContainers(c)
392 392
 
393 393
 		content, err := runCommandAndReadContainerFile(fn, exec.Command(dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn)))
394 394
 		c.Assert(err, checker.IsNil)
... ...
@@ -54,7 +54,7 @@ func (s *DockerSuite) TestInspectDefault(c *check.C) {
54 54
 
55 55
 func (s *DockerSuite) TestInspectStatus(c *check.C) {
56 56
 	if daemonPlatform != "windows" {
57
-		defer unpauseAllContainers()
57
+		defer unpauseAllContainers(c)
58 58
 	}
59 59
 	out, _ := runSleepingContainer(c, "-d")
60 60
 	out = strings.TrimSpace(out)
... ...
@@ -9,7 +9,7 @@ import (
9 9
 
10 10
 func (s *DockerSuite) TestPause(c *check.C) {
11 11
 	testRequires(c, IsPausable)
12
-	defer unpauseAllContainers()
12
+	defer unpauseAllContainers(c)
13 13
 
14 14
 	name := "testeventpause"
15 15
 	runSleepingContainer(c, "-d", "--name", name)
... ...
@@ -31,7 +31,7 @@ func (s *DockerSuite) TestPause(c *check.C) {
31 31
 
32 32
 func (s *DockerSuite) TestPauseMultipleContainers(c *check.C) {
33 33
 	testRequires(c, IsPausable)
34
-	defer unpauseAllContainers()
34
+	defer unpauseAllContainers(c)
35 35
 
36 36
 	containers := []string{
37 37
 		"testpausewithmorecontainers1",
... ...
@@ -96,7 +96,7 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
96 96
 func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
97 97
 	// Windows does not support pausing containers
98 98
 	testRequires(c, IsPausable)
99
-	defer unpauseAllContainers()
99
+	defer unpauseAllContainers(c)
100 100
 
101 101
 	runSleepingContainer(c, "-d", "--name", "testing")
102 102
 
... ...
@@ -26,6 +26,7 @@ import (
26 26
 	volumetypes "github.com/docker/docker/api/types/volume"
27 27
 	"github.com/docker/docker/opts"
28 28
 	"github.com/docker/docker/pkg/httputils"
29
+	"github.com/docker/docker/pkg/integration/checker"
29 30
 	icmd "github.com/docker/docker/pkg/integration/cmd"
30 31
 	"github.com/docker/docker/pkg/ioutils"
31 32
 	"github.com/docker/docker/pkg/stringutils"
... ...
@@ -245,29 +246,20 @@ func getAllContainers() (string, error) {
245 245
 	return out, err
246 246
 }
247 247
 
248
-func deleteAllContainers() error {
248
+func deleteAllContainers(c *check.C) {
249 249
 	containers, err := getAllContainers()
250
-	if err != nil {
251
-		fmt.Println(containers)
252
-		return err
253
-	}
254
-	if containers == "" {
255
-		return nil
256
-	}
250
+	c.Assert(err, checker.IsNil, check.Commentf("containers: %v", containers))
257 251
 
258
-	err = deleteContainer(strings.Split(strings.TrimSpace(containers), "\n")...)
259
-	if err != nil {
260
-		fmt.Println(err.Error())
252
+	if containers != "" {
253
+		err = deleteContainer(strings.Split(strings.TrimSpace(containers), "\n")...)
254
+		c.Assert(err, checker.IsNil)
261 255
 	}
262
-	return err
263 256
 }
264 257
 
265
-func deleteAllNetworks() error {
258
+func deleteAllNetworks(c *check.C) {
266 259
 	networks, err := getAllNetworks()
267
-	if err != nil {
268
-		return err
269
-	}
270
-	var errors []string
260
+	c.Assert(err, check.IsNil)
261
+	var errs []string
271 262
 	for _, n := range networks {
272 263
 		if n.Name == "bridge" || n.Name == "none" || n.Name == "host" {
273 264
 			continue
... ...
@@ -278,17 +270,14 @@ func deleteAllNetworks() error {
278 278
 		}
279 279
 		status, b, err := sockRequest("DELETE", "/networks/"+n.Name, nil)
280 280
 		if err != nil {
281
-			errors = append(errors, err.Error())
281
+			errs = append(errs, err.Error())
282 282
 			continue
283 283
 		}
284 284
 		if status != http.StatusNoContent {
285
-			errors = append(errors, fmt.Sprintf("error deleting network %s: %s", n.Name, string(b)))
285
+			errs = append(errs, fmt.Sprintf("error deleting network %s: %s", n.Name, string(b)))
286 286
 		}
287 287
 	}
288
-	if len(errors) > 0 {
289
-		return fmt.Errorf(strings.Join(errors, "\n"))
290
-	}
291
-	return nil
288
+	c.Assert(errs, checker.HasLen, 0, check.Commentf(strings.Join(errs, "\n")))
292 289
 }
293 290
 
294 291
 func getAllNetworks() ([]types.NetworkResource, error) {
... ...
@@ -303,26 +292,26 @@ func getAllNetworks() ([]types.NetworkResource, error) {
303 303
 	return networks, nil
304 304
 }
305 305
 
306
-func deleteAllPlugins() error {
306
+func deleteAllPlugins(c *check.C) {
307 307
 	plugins, err := getAllPlugins()
308
-	if err != nil {
309
-		return err
310
-	}
311
-	var errors []string
308
+	c.Assert(err, checker.IsNil)
309
+	var errs []string
312 310
 	for _, p := range plugins {
313
-		status, b, err := sockRequest("DELETE", "/plugins/"+p.Name+":"+p.Tag+"?force=1", nil)
311
+		pluginName := p.Name
312
+		tag := p.Tag
313
+		if tag == "" {
314
+			tag = "latest"
315
+		}
316
+		status, b, err := sockRequest("DELETE", "/plugins/"+pluginName+":"+tag+"?force=1", nil)
314 317
 		if err != nil {
315
-			errors = append(errors, err.Error())
318
+			errs = append(errs, err.Error())
316 319
 			continue
317 320
 		}
318
-		if status != http.StatusNoContent {
319
-			errors = append(errors, fmt.Sprintf("error deleting plugin %s: %s", p.Name, string(b)))
321
+		if status != http.StatusOK {
322
+			errs = append(errs, fmt.Sprintf("error deleting plugin %s: %s", p.Name, string(b)))
320 323
 		}
321 324
 	}
322
-	if len(errors) > 0 {
323
-		return fmt.Errorf(strings.Join(errors, "\n"))
324
-	}
325
-	return nil
325
+	c.Assert(errs, checker.HasLen, 0, check.Commentf(strings.Join(errs, "\n")))
326 326
 }
327 327
 
328 328
 func getAllPlugins() (types.PluginsListResponse, error) {
... ...
@@ -337,26 +326,21 @@ func getAllPlugins() (types.PluginsListResponse, error) {
337 337
 	return plugins, nil
338 338
 }
339 339
 
340
-func deleteAllVolumes() error {
340
+func deleteAllVolumes(c *check.C) {
341 341
 	volumes, err := getAllVolumes()
342
-	if err != nil {
343
-		return err
344
-	}
345
-	var errors []string
342
+	c.Assert(err, checker.IsNil)
343
+	var errs []string
346 344
 	for _, v := range volumes {
347 345
 		status, b, err := sockRequest("DELETE", "/volumes/"+v.Name, nil)
348 346
 		if err != nil {
349
-			errors = append(errors, err.Error())
347
+			errs = append(errs, err.Error())
350 348
 			continue
351 349
 		}
352 350
 		if status != http.StatusNoContent {
353
-			errors = append(errors, fmt.Sprintf("error deleting volume %s: %s", v.Name, string(b)))
351
+			errs = append(errs, fmt.Sprintf("error deleting volume %s: %s", v.Name, string(b)))
354 352
 		}
355 353
 	}
356
-	if len(errors) > 0 {
357
-		return fmt.Errorf(strings.Join(errors, "\n"))
358
-	}
359
-	return nil
354
+	c.Assert(errs, checker.HasLen, 0, check.Commentf(strings.Join(errs, "\n")))
360 355
 }
361 356
 
362 357
 func getAllVolumes() ([]*types.Volume, error) {
... ...
@@ -373,13 +357,11 @@ func getAllVolumes() ([]*types.Volume, error) {
373 373
 
374 374
 var protectedImages = map[string]struct{}{}
375 375
 
376
-func deleteAllImages() error {
376
+func deleteAllImages(c *check.C) {
377 377
 	cmd := exec.Command(dockerBinary, "images")
378 378
 	cmd.Env = appendBaseEnv(true)
379 379
 	out, err := cmd.CombinedOutput()
380
-	if err != nil {
381
-		return err
382
-	}
380
+	c.Assert(err, checker.IsNil)
383 381
 	lines := strings.Split(string(out), "\n")[1:]
384 382
 	var imgs []string
385 383
 	for _, l := range lines {
... ...
@@ -389,21 +371,16 @@ func deleteAllImages() error {
389 389
 		fields := strings.Fields(l)
390 390
 		imgTag := fields[0] + ":" + fields[1]
391 391
 		if _, ok := protectedImages[imgTag]; !ok {
392
-			if fields[0] == "<none>" {
392
+			if fields[0] == "<none>" || fields[1] == "<none>" {
393 393
 				imgs = append(imgs, fields[2])
394 394
 				continue
395 395
 			}
396 396
 			imgs = append(imgs, imgTag)
397 397
 		}
398 398
 	}
399
-	if len(imgs) == 0 {
400
-		return nil
399
+	if len(imgs) != 0 {
400
+		dockerCmd(c, append([]string{"rmi", "-f"}, imgs...)...)
401 401
 	}
402
-	args := append([]string{"rmi", "-f"}, imgs...)
403
-	if err := exec.Command(dockerBinary, args...).Run(); err != nil {
404
-		return err
405
-	}
406
-	return nil
407 402
 }
408 403
 
409 404
 func getPausedContainers() (string, error) {
... ...
@@ -428,28 +405,21 @@ func getSliceOfPausedContainers() ([]string, error) {
428 428
 	return []string{out}, err
429 429
 }
430 430
 
431
-func unpauseContainer(container string) error {
432
-	return icmd.RunCommand(dockerBinary, "unpause", container).Error
431
+func unpauseContainer(c *check.C, container string) {
432
+	dockerCmd(c, "unpause", container)
433 433
 }
434 434
 
435
-func unpauseAllContainers() error {
435
+func unpauseAllContainers(c *check.C) {
436 436
 	containers, err := getPausedContainers()
437
-	if err != nil {
438
-		fmt.Println(containers)
439
-		return err
440
-	}
437
+	c.Assert(err, checker.IsNil, check.Commentf("containers: %v", containers))
441 438
 
442 439
 	containers = strings.Replace(containers, "\n", " ", -1)
443 440
 	containers = strings.Trim(containers, " ")
444 441
 	containerList := strings.Split(containers, " ")
445 442
 
446 443
 	for _, value := range containerList {
447
-		if err = unpauseContainer(value); err != nil {
448
-			return err
449
-		}
444
+		unpauseContainer(c, value)
450 445
 	}
451
-
452
-	return nil
453 446
 }
454 447
 
455 448
 func deleteImages(images ...string) error {
... ...
@@ -490,10 +460,7 @@ func dockerCmdWithStdoutStderr(c *check.C, args ...string) (string, string, int)
490 490
 	}
491 491
 
492 492
 	result := icmd.RunCommand(dockerBinary, args...)
493
-	// TODO: why is c ever nil?
494
-	if c != nil {
495
-		c.Assert(result, icmd.Matches, icmd.Success)
496
-	}
493
+	c.Assert(result, icmd.Matches, icmd.Success)
497 494
 	return result.Stdout(), result.Stderr(), result.ExitCode
498 495
 }
499 496