Browse code

Clean some function in docker_utils_test.go

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

Vincent Demeester authored on 2017/04/12 02:42:54
Showing 10 changed files
... ...
@@ -21,6 +21,7 @@ import (
21 21
 	mounttypes "github.com/docker/docker/api/types/mount"
22 22
 	networktypes "github.com/docker/docker/api/types/network"
23 23
 	"github.com/docker/docker/integration-cli/checker"
24
+	"github.com/docker/docker/integration-cli/cli"
24 25
 	"github.com/docker/docker/integration-cli/cli/build"
25 26
 	"github.com/docker/docker/integration-cli/request"
26 27
 	"github.com/docker/docker/pkg/ioutils"
... ...
@@ -347,25 +348,29 @@ func (s *DockerSuite) TestGetStoppedContainerStats(c *check.C) {
347 347
 func (s *DockerSuite) TestContainerAPIPause(c *check.C) {
348 348
 	// Problematic on Windows as Windows does not support pause
349 349
 	testRequires(c, DaemonIsLinux)
350
-	defer unpauseAllContainers(c)
351
-	out, _ := dockerCmd(c, "run", "-d", "busybox", "sleep", "30")
350
+
351
+	getPaused := func(c *check.C) []string {
352
+		return strings.Fields(cli.DockerCmd(c, "ps", "-f", "status=paused", "-q", "-a").Combined())
353
+	}
354
+
355
+	out := cli.DockerCmd(c, "run", "-d", "busybox", "sleep", "30").Combined()
352 356
 	ContainerID := strings.TrimSpace(out)
353 357
 
354
-	status, _, err := request.SockRequest("POST", "/containers/"+ContainerID+"/pause", nil, daemonHost())
358
+	resp, _, err := request.Post("/containers/" + ContainerID + "/pause")
355 359
 	c.Assert(err, checker.IsNil)
356
-	c.Assert(status, checker.Equals, http.StatusNoContent)
360
+	c.Assert(resp.StatusCode, checker.Equals, http.StatusNoContent)
357 361
 
358
-	pausedContainers := getPausedContainers(c)
362
+	pausedContainers := getPaused(c)
359 363
 
360 364
 	if len(pausedContainers) != 1 || stringid.TruncateID(ContainerID) != pausedContainers[0] {
361 365
 		c.Fatalf("there should be one paused container and not %d", len(pausedContainers))
362 366
 	}
363 367
 
364
-	status, _, err = request.SockRequest("POST", "/containers/"+ContainerID+"/unpause", nil, daemonHost())
368
+	resp, _, err = request.Post("/containers/" + ContainerID + "/unpause")
365 369
 	c.Assert(err, checker.IsNil)
366
-	c.Assert(status, checker.Equals, http.StatusNoContent)
370
+	c.Assert(resp.StatusCode, checker.Equals, http.StatusNoContent)
367 371
 
368
-	pausedContainers = getPausedContainers(c)
372
+	pausedContainers = getPaused(c)
369 373
 	c.Assert(pausedContainers, checker.HasLen, 0, check.Commentf("There should be no paused container."))
370 374
 }
371 375
 
... ...
@@ -1262,7 +1267,6 @@ func (s *DockerSuite) TestPutContainerArchiveErrSymlinkInVolumeToReadOnlyRootfs(
1262 1262
 		readOnly: true,
1263 1263
 		volumes:  defaultVolumes(testVol), // Our bind mount is at /vol2
1264 1264
 	})
1265
-	defer deleteContainer(cID)
1266 1265
 
1267 1266
 	// Attempt to extract to a symlink in the volume which points to a
1268 1267
 	// directory outside the volume. This should cause an error because the
... ...
@@ -155,7 +155,6 @@ 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(c)
159 158
 	runSleepingContainer(c, "-d", "--name=test")
160 159
 	dockerCmd(c, "pause", "test")
161 160
 
... ...
@@ -12,6 +12,7 @@ import (
12 12
 	"github.com/docker/distribution/manifest/schema2"
13 13
 	"github.com/docker/docker/api/types"
14 14
 	"github.com/docker/docker/integration-cli/checker"
15
+	"github.com/docker/docker/integration-cli/cli"
15 16
 	"github.com/docker/docker/integration-cli/cli/build"
16 17
 	"github.com/docker/docker/pkg/stringutils"
17 18
 	"github.com/go-check/check"
... ...
@@ -35,24 +36,20 @@ func setupImageWithTag(c *check.C, tag string) (digest.Digest, error) {
35 35
 	// new file is committed because this layer is used for detecting malicious
36 36
 	// changes. if this was committed as empty layer it would be skipped on pull
37 37
 	// and malicious changes would never be detected.
38
-	dockerCmd(c, "run", "-e", "digest=1", "--name", containerName, "busybox", "touch", "anewfile")
38
+	cli.DockerCmd(c, "run", "-e", "digest=1", "--name", containerName, "busybox", "touch", "anewfile")
39 39
 
40 40
 	// tag the image to upload it to the private registry
41 41
 	repoAndTag := repoName + ":" + tag
42
-	out, _, err := dockerCmdWithError("commit", containerName, repoAndTag)
43
-	c.Assert(err, checker.IsNil, check.Commentf("image tagging failed: %s", out))
42
+	cli.DockerCmd(c, "commit", containerName, repoAndTag)
44 43
 
45 44
 	// delete the container as we don't need it any more
46
-	err = deleteContainer(containerName)
47
-	c.Assert(err, checker.IsNil)
45
+	cli.DockerCmd(c, "rm", "-fv", containerName)
48 46
 
49 47
 	// push the image
50
-	out, _, err = dockerCmdWithError("push", repoAndTag)
51
-	c.Assert(err, checker.IsNil, check.Commentf("pushing the image to the private registry has failed: %s", out))
48
+	out := cli.DockerCmd(c, "push", repoAndTag).Combined()
52 49
 
53 50
 	// delete our local repo that we previously tagged
54
-	rmiout, _, err := dockerCmdWithError("rmi", repoAndTag)
55
-	c.Assert(err, checker.IsNil, check.Commentf("error deleting images prior to real test: %s", rmiout))
51
+	cli.DockerCmd(c, "rmi", repoAndTag)
56 52
 
57 53
 	matches := pushDigestRegex.FindStringSubmatch(out)
58 54
 	c.Assert(matches, checker.HasLen, 2, check.Commentf("unable to parse digest from push output: %s", out))
... ...
@@ -40,7 +40,6 @@ func (s *DockerSuite) TestCommitWithoutPause(c *check.C) {
40 40
 //test commit a paused container should not unpause it after commit
41 41
 func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
42 42
 	testRequires(c, DaemonIsLinux)
43
-	defer unpauseAllContainers(c)
44 43
 	out, _ := dockerCmd(c, "run", "-i", "-d", "busybox")
45 44
 
46 45
 	cleanedContainerID := strings.TrimSpace(out)
... ...
@@ -20,6 +20,7 @@ import (
20 20
 	"github.com/docker/docker/integration-cli/request"
21 21
 	icmd "github.com/docker/docker/pkg/testutil/cmd"
22 22
 	"github.com/go-check/check"
23
+	"github.com/docker/docker/integration-cli/cli"
23 24
 )
24 25
 
25 26
 func (s *DockerSuite) TestExec(c *check.C) {
... ...
@@ -137,7 +138,6 @@ func (s *DockerSuite) TestExecExitStatus(c *check.C) {
137 137
 
138 138
 func (s *DockerSuite) TestExecPausedContainer(c *check.C) {
139 139
 	testRequires(c, IsPausable)
140
-	defer unpauseAllContainers(c)
141 140
 
142 141
 	out, _ := runSleepingContainer(c, "-d", "--name", "testing")
143 142
 	ContainerID := strings.TrimSpace(out)
... ...
@@ -389,7 +389,10 @@ func (s *DockerSuite) TestRunMutableNetworkFiles(c *check.C) {
389 389
 	// Not applicable on Windows to Windows CI.
390 390
 	testRequires(c, SameHostDaemon, DaemonIsLinux)
391 391
 	for _, fn := range []string{"resolv.conf", "hosts"} {
392
-		deleteAllContainers(c)
392
+		containers := cli.DockerCmd(c, "ps", "-q", "-a").Combined()
393
+		if containers != "" {
394
+			cli.DockerCmd(c, append([]string{"rm", "-fv"}, strings.Split(strings.TrimSpace(containers), "\n")...)...)
395
+		}
393 396
 
394 397
 		content := runCommandAndReadContainerFile(c, fn, dockerBinary, "run", "-d", "--name", "c1", "busybox", "sh", "-c", fmt.Sprintf("echo success >/etc/%s && top", fn))
395 398
 
... ...
@@ -53,9 +53,6 @@ func (s *DockerSuite) TestInspectDefault(c *check.C) {
53 53
 }
54 54
 
55 55
 func (s *DockerSuite) TestInspectStatus(c *check.C) {
56
-	if testEnv.DaemonPlatform() != "windows" {
57
-		defer unpauseAllContainers(c)
58
-	}
59 56
 	out, _ := runSleepingContainer(c, "-d")
60 57
 	out = strings.TrimSpace(out)
61 58
 
... ...
@@ -4,23 +4,25 @@ import (
4 4
 	"strings"
5 5
 
6 6
 	"github.com/docker/docker/integration-cli/checker"
7
+	"github.com/docker/docker/integration-cli/cli"
7 8
 	"github.com/go-check/check"
8 9
 )
9 10
 
10 11
 func (s *DockerSuite) TestPause(c *check.C) {
11 12
 	testRequires(c, IsPausable)
12
-	defer unpauseAllContainers(c)
13 13
 
14 14
 	name := "testeventpause"
15 15
 	runSleepingContainer(c, "-d", "--name", name)
16 16
 
17
-	dockerCmd(c, "pause", name)
18
-	pausedContainers := getPausedContainers(c)
17
+	cli.DockerCmd(c, "pause", name)
18
+	pausedContainers := strings.Fields(
19
+		cli.DockerCmd(c, "ps", "-f", "status=paused", "-q", "-a").Combined(),
20
+	)
19 21
 	c.Assert(len(pausedContainers), checker.Equals, 1)
20 22
 
21
-	dockerCmd(c, "unpause", name)
23
+	cli.DockerCmd(c, "unpause", name)
22 24
 
23
-	out, _ := dockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c))
25
+	out := cli.DockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c)).Combined()
24 26
 	events := strings.Split(strings.TrimSpace(out), "\n")
25 27
 	actions := eventActionsByIDAndType(c, events, name, "container")
26 28
 
... ...
@@ -30,7 +32,6 @@ func (s *DockerSuite) TestPause(c *check.C) {
30 30
 
31 31
 func (s *DockerSuite) TestPauseMultipleContainers(c *check.C) {
32 32
 	testRequires(c, IsPausable)
33
-	defer unpauseAllContainers(c)
34 33
 
35 34
 	containers := []string{
36 35
 		"testpausewithmorecontainers1",
... ...
@@ -39,13 +40,15 @@ func (s *DockerSuite) TestPauseMultipleContainers(c *check.C) {
39 39
 	for _, name := range containers {
40 40
 		runSleepingContainer(c, "-d", "--name", name)
41 41
 	}
42
-	dockerCmd(c, append([]string{"pause"}, containers...)...)
43
-	pausedContainers := getPausedContainers(c)
42
+	cli.DockerCmd(c, append([]string{"pause"}, containers...)...)
43
+	pausedContainers := strings.Fields(
44
+		cli.DockerCmd(c, "ps", "-f", "status=paused", "-q", "-a").Combined(),
45
+	)
44 46
 	c.Assert(len(pausedContainers), checker.Equals, len(containers))
45 47
 
46
-	dockerCmd(c, append([]string{"unpause"}, containers...)...)
48
+	cli.DockerCmd(c, append([]string{"unpause"}, containers...)...)
47 49
 
48
-	out, _ := dockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c))
50
+	out := cli.DockerCmd(c, "events", "--since=0", "--until", daemonUnixTime(c)).Combined()
49 51
 	events := strings.Split(strings.TrimSpace(out), "\n")
50 52
 
51 53
 	for _, name := range containers {
... ...
@@ -2025,18 +2025,16 @@ func (s *DockerSuite) TestRunDeallocatePortOnMissingIptablesRule(c *check.C) {
2025 2025
 	// TODO Windows. Network settings are not propagated back to inspect.
2026 2026
 	testRequires(c, SameHostDaemon, DaemonIsLinux)
2027 2027
 
2028
-	out, _ := dockerCmd(c, "run", "-d", "-p", "23:23", "busybox", "top")
2028
+	out := cli.DockerCmd(c, "run", "-d", "-p", "23:23", "busybox", "top").Combined()
2029 2029
 
2030 2030
 	id := strings.TrimSpace(out)
2031 2031
 	ip := inspectField(c, id, "NetworkSettings.Networks.bridge.IPAddress")
2032 2032
 	icmd.RunCommand("iptables", "-D", "DOCKER", "-d", fmt.Sprintf("%s/32", ip),
2033 2033
 		"!", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-m", "tcp", "--dport", "23", "-j", "ACCEPT").Assert(c, icmd.Success)
2034 2034
 
2035
-	if err := deleteContainer(id); err != nil {
2036
-		c.Fatal(err)
2037
-	}
2035
+	cli.DockerCmd(c, "rm", "-fv", id)
2038 2036
 
2039
-	dockerCmd(c, "run", "-d", "-p", "23:23", "busybox", "top")
2037
+	cli.DockerCmd(c, "run", "-d", "-p", "23:23", "busybox", "top")
2040 2038
 }
2041 2039
 
2042 2040
 func (s *DockerSuite) TestRunPortInUse(c *check.C) {
... ...
@@ -2817,12 +2815,11 @@ func (s *DockerSuite) TestRunVolumesFromRestartAfterRemoved(c *check.C) {
2817 2817
 // run container with --rm should remove container if exit code != 0
2818 2818
 func (s *DockerSuite) TestRunContainerWithRmFlagExitCodeNotEqualToZero(c *check.C) {
2819 2819
 	name := "flowers"
2820
-	out, _, err := dockerCmdWithError("run", "--name", name, "--rm", "busybox", "ls", "/notexists")
2821
-	if err == nil {
2822
-		c.Fatal("Expected docker run to fail", out, err)
2823
-	}
2820
+	cli.Docker(cli.Args("run", "--name", name, "--rm", "busybox", "ls", "/notexists")).Assert(c, icmd.Expected{
2821
+		ExitCode: 1,
2822
+	})
2824 2823
 
2825
-	out = getAllContainers(c)
2824
+	out := cli.DockerCmd(c, "ps", "-q", "-a").Combined()
2826 2825
 	if out != "" {
2827 2826
 		c.Fatal("Expected not to have containers", out)
2828 2827
 	}
... ...
@@ -2830,12 +2827,10 @@ func (s *DockerSuite) TestRunContainerWithRmFlagExitCodeNotEqualToZero(c *check.
2830 2830
 
2831 2831
 func (s *DockerSuite) TestRunContainerWithRmFlagCannotStartContainer(c *check.C) {
2832 2832
 	name := "sparkles"
2833
-	out, _, err := dockerCmdWithError("run", "--name", name, "--rm", "busybox", "commandNotFound")
2834
-	if err == nil {
2835
-		c.Fatal("Expected docker run to fail", out, err)
2836
-	}
2837
-
2838
-	out = getAllContainers(c)
2833
+	cli.Docker(cli.Args("run", "--name", name, "--rm", "busybox", "commandNotFound")).Assert(c, icmd.Expected{
2834
+		ExitCode: 127,
2835
+	})
2836
+	out := cli.DockerCmd(c, "ps", "-q", "-a").Combined()
2839 2837
 	if out != "" {
2840 2838
 		c.Fatal("Expected not to have containers", out)
2841 2839
 	}
... ...
@@ -94,7 +94,6 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
94 94
 func (s *DockerSuite) TestStartPausedContainer(c *check.C) {
95 95
 	// Windows does not support pausing containers
96 96
 	testRequires(c, IsPausable)
97
-	defer unpauseAllContainers(c)
98 97
 
99 98
 	runSleepingContainer(c, "-d", "--name", "testing")
100 99
 
... ...
@@ -33,44 +33,6 @@ func daemonHost() string {
33 33
 	return request.DaemonHost()
34 34
 }
35 35
 
36
-// FIXME(vdemeester) move this away are remove ignoreNoSuchContainer bool
37
-func deleteContainer(container ...string) error {
38
-	return icmd.RunCommand(dockerBinary, append([]string{"rm", "-fv"}, container...)...).Compare(icmd.Success)
39
-}
40
-
41
-func getAllContainers(c *check.C) string {
42
-	result := icmd.RunCommand(dockerBinary, "ps", "-q", "-a")
43
-	result.Assert(c, icmd.Success)
44
-	return result.Combined()
45
-}
46
-
47
-// Deprecated
48
-func deleteAllContainers(c *check.C) {
49
-	containers := getAllContainers(c)
50
-	if containers != "" {
51
-		err := deleteContainer(strings.Split(strings.TrimSpace(containers), "\n")...)
52
-		c.Assert(err, checker.IsNil)
53
-	}
54
-}
55
-
56
-func getPausedContainers(c *check.C) []string {
57
-	result := icmd.RunCommand(dockerBinary, "ps", "-f", "status=paused", "-q", "-a")
58
-	result.Assert(c, icmd.Success)
59
-	return strings.Fields(result.Combined())
60
-}
61
-
62
-func unpauseContainer(c *check.C, container string) {
63
-	dockerCmd(c, "unpause", container)
64
-}
65
-
66
-// Deprecated
67
-func unpauseAllContainers(c *check.C) {
68
-	containers := getPausedContainers(c)
69
-	for _, value := range containers {
70
-		unpauseContainer(c, value)
71
-	}
72
-}
73
-
74 36
 func deleteImages(images ...string) error {
75 37
 	args := []string{dockerBinary, "rmi", "-f"}
76 38
 	return icmd.RunCmd(icmd.Cmd{Command: append(args, images...)}).Error