Browse code

Merge pull request #29224 from vdemeester/check-no-errors

Use check in params so we don't ignore errors

Sebastiaan van Stijn authored on 2016/12/09 08:26:20
Showing 12 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",
... ...
@@ -275,4 +275,7 @@ func (s *DockerSuite) TestRestartAutoRemoveContainer(c *check.C) {
275 275
 
276 276
 	out, _ = dockerCmd(c, "ps")
277 277
 	c.Assert(out, checker.Contains, id[:12], check.Commentf("container should be restarted instead of removed: %v", out))
278
+
279
+	// Kill the container to make sure it will be removed
280
+	dockerCmd(c, "kill", id)
278 281
 }
... ...
@@ -71,7 +71,7 @@ func (s *DockerSuite) TestRunLookupGoogleDNS(c *check.C) {
71 71
 		// nslookup isn't present in nanoserver. Hence just use PowerShell...
72 72
 		dockerCmd(c, "run", WindowsBaseImage, "powershell", "Resolve-DNSName", "google.com")
73 73
 	} else {
74
-		dockerCmd(c, "run", DefaultImage, "nslookup", "google.com")
74
+		dockerCmd(c, "run", "busybox", "nslookup", "google.com")
75 75
 	}
76 76
 
77 77
 }
... ...
@@ -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
 
... ...
@@ -22,16 +22,9 @@ var (
22 22
 	// path to containerd's ctr binary
23 23
 	ctrBinary = "docker-containerd-ctr"
24 24
 
25
-	// the private registry image to use for tests involving the registry
26
-	registryImageName = "registry"
27
-
28 25
 	// the private registry to use for tests
29 26
 	privateRegistryURL = "127.0.0.1:5000"
30 27
 
31
-	// TODO Windows CI. These are incorrect and need fixing into
32
-	// platform specific pieces.
33
-	runtimePath = "/var/run/docker"
34
-
35 28
 	workingDirectory string
36 29
 
37 30
 	// isLocalDaemon is true if the daemon under test is on the same
... ...
@@ -50,10 +43,6 @@ var (
50 50
 	// not support volumes, but TP4 did.
51 51
 	windowsDaemonKV int
52 52
 
53
-	// daemonDefaultImage is the name of the default image to use when running
54
-	// tests. This is platform dependent.
55
-	daemonDefaultImage string
56
-
57 53
 	// For a local daemon on Linux, these values will be used for testing
58 54
 	// user namespace support as the standard graph path(s) will be
59 55
 	// appended with the root remapped uid.gid prefix
... ...
@@ -81,12 +70,6 @@ var (
81 81
 	daemonPid int
82 82
 )
83 83
 
84
-const (
85
-	// DefaultImage is the name of the base image for the majority of tests that
86
-	// are run across suites
87
-	DefaultImage = "busybox"
88
-)
89
-
90 84
 func init() {
91 85
 	reexec.Init()
92 86
 	if dockerBin := os.Getenv("DOCKER_BINARY"); dockerBin != "" {
... ...
@@ -98,9 +81,6 @@ func init() {
98 98
 		fmt.Printf("ERROR: couldn't resolve full path to the Docker binary (%v)\n", err)
99 99
 		os.Exit(1)
100 100
 	}
101
-	if registryImage := os.Getenv("REGISTRY_IMAGE"); registryImage != "" {
102
-		registryImageName = registryImage
103
-	}
104 101
 	if registry := os.Getenv("REGISTRY_URL"); registry != "" {
105 102
 		privateRegistryURL = registry
106 103
 	}
... ...
@@ -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,15 +357,13 @@ func getAllVolumes() ([]*types.Volume, error) {
373 373
 
374 374
 var protectedImages = map[string]struct{}{}
375 375
 
376
-func deleteAllImages() error {
377
-	cmd := exec.Command(dockerBinary, "images")
376
+func deleteAllImages(c *check.C) {
377
+	cmd := exec.Command(dockerBinary, "images", "--digests")
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
-	var imgs []string
382
+	imgMap := map[string]struct{}{}
385 383
 	for _, l := range lines {
386 384
 		if l == "" {
387 385
 			continue
... ...
@@ -389,21 +371,25 @@ 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>" {
393
-				imgs = append(imgs, fields[2])
394
-				continue
392
+			if fields[0] == "<none>" || fields[1] == "<none>" {
393
+				if fields[2] != "<none>" {
394
+					imgMap[fields[0]+"@"+fields[2]] = struct{}{}
395
+				} else {
396
+					imgMap[fields[3]] = struct{}{}
397
+				}
398
+				// continue
399
+			} else {
400
+				imgMap[imgTag] = struct{}{}
395 401
 			}
396
-			imgs = append(imgs, imgTag)
397 402
 		}
398 403
 	}
399
-	if len(imgs) == 0 {
400
-		return nil
401
-	}
402
-	args := append([]string{"rmi", "-f"}, imgs...)
403
-	if err := exec.Command(dockerBinary, args...).Run(); err != nil {
404
-		return err
404
+	if len(imgMap) != 0 {
405
+		imgs := make([]string, 0, len(imgMap))
406
+		for k := range imgMap {
407
+			imgs = append(imgs, k)
408
+		}
409
+		dockerCmd(c, append([]string{"rmi", "-f"}, imgs...)...)
405 410
 	}
406
-	return nil
407 411
 }
408 412
 
409 413
 func getPausedContainers() (string, error) {
... ...
@@ -428,28 +414,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 +469,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