Browse code

Fix tests that depend on clean environment

Signed-off-by: Christopher Crone <christopher.crone@docker.com>

Christopher Crone authored on 2017/09/09 00:16:15
Showing 10 changed files
... ...
@@ -81,6 +81,7 @@ func (s *DockerSuite) TestAPINetworkInspect(c *check.C) {
81 81
 	// Inspect default bridge network
82 82
 	nr := getNetworkResource(c, "bridge")
83 83
 	c.Assert(nr.Name, checker.Equals, "bridge")
84
+	connCount := len(nr.Containers)
84 85
 
85 86
 	// run a container and attach it to the default bridge network
86 87
 	out, _ := dockerCmd(c, "run", "-d", "--name", "test", "busybox", "top")
... ...
@@ -94,7 +95,7 @@ func (s *DockerSuite) TestAPINetworkInspect(c *check.C) {
94 94
 	c.Assert(nr.Internal, checker.Equals, false)
95 95
 	c.Assert(nr.EnableIPv6, checker.Equals, false)
96 96
 	c.Assert(nr.IPAM.Driver, checker.Equals, "default")
97
-	c.Assert(len(nr.Containers), checker.Equals, 1)
97
+	c.Assert(len(nr.Containers), checker.Equals, connCount+1)
98 98
 	c.Assert(nr.Containers[containerID], checker.NotNil)
99 99
 
100 100
 	ip, _, err := net.ParseCIDR(nr.Containers[containerID].IPv4Address)
... ...
@@ -291,9 +292,16 @@ func getNetworkIDByName(c *check.C, name string) string {
291 291
 	nJSON := []types.NetworkResource{}
292 292
 	err = json.NewDecoder(body).Decode(&nJSON)
293 293
 	c.Assert(err, checker.IsNil)
294
-	c.Assert(len(nJSON), checker.Equals, 1)
294
+	var res string
295
+	for _, n := range nJSON {
296
+		// Find exact match
297
+		if n.Name == name {
298
+			res = n.ID
299
+		}
300
+	}
301
+	c.Assert(res, checker.Not(checker.Equals), "")
295 302
 
296
-	return nJSON[0].ID
303
+	return res
297 304
 }
298 305
 
299 306
 func getNetworkResource(c *check.C, id string) *types.NetworkResource {
... ...
@@ -47,6 +47,9 @@ func (s *DockerAuthzV2Suite) TearDownTest(c *check.C) {
47 47
 
48 48
 func (s *DockerAuthzV2Suite) TestAuthZPluginAllowNonVolumeRequest(c *check.C) {
49 49
 	testRequires(c, DaemonIsLinux, IsAmd64, Network)
50
+
51
+	existingContainers := ExistingContainerIDs(c)
52
+
50 53
 	// Install authz plugin
51 54
 	_, err := s.d.Cmd("plugin", "install", "--grant-all-permissions", authzPluginNameWithTag)
52 55
 	c.Assert(err, checker.IsNil)
... ...
@@ -72,7 +75,7 @@ func (s *DockerAuthzV2Suite) TestAuthZPluginAllowNonVolumeRequest(c *check.C) {
72 72
 
73 73
 	out, err = s.d.Cmd("ps")
74 74
 	c.Assert(err, check.IsNil)
75
-	c.Assert(assertContainerList(out, []string{id}), check.Equals, true)
75
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), []string{id}), check.Equals, true)
76 76
 }
77 77
 
78 78
 func (s *DockerAuthzV2Suite) TestAuthZPluginDisable(c *check.C) {
... ...
@@ -204,6 +204,8 @@ func (s *DockerAuthzSuite) TearDownSuite(c *check.C) {
204 204
 }
205 205
 
206 206
 func (s *DockerAuthzSuite) TestAuthZPluginAllowRequest(c *check.C) {
207
+	existingContainers := ExistingContainerIDs(c)
208
+
207 209
 	// start the daemon and load busybox, --net=none build fails otherwise
208 210
 	// cause it needs to pull busybox
209 211
 	s.d.Start(c, "--authorization-plugin="+testAuthZPlugin)
... ...
@@ -221,7 +223,7 @@ func (s *DockerAuthzSuite) TestAuthZPluginAllowRequest(c *check.C) {
221 221
 
222 222
 	out, err = s.d.Cmd("ps")
223 223
 	c.Assert(err, check.IsNil)
224
-	c.Assert(assertContainerList(out, []string{id}), check.Equals, true)
224
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), []string{id}), check.Equals, true)
225 225
 	c.Assert(s.ctrl.psRequestCnt, check.Equals, 1)
226 226
 	c.Assert(s.ctrl.psResponseCnt, check.Equals, 1)
227 227
 }
... ...
@@ -407,6 +407,8 @@ func (s *DockerRegistrySuite) TestInspectImageWithDigests(c *check.C) {
407 407
 }
408 408
 
409 409
 func (s *DockerRegistrySuite) TestPsListContainersFilterAncestorImageByDigest(c *check.C) {
410
+	existingContainers := ExistingContainerIDs(c)
411
+
410 412
 	digest, err := setupImage(c)
411 413
 	c.Assert(err, checker.IsNil, check.Commentf("error setting up image"))
412 414
 
... ...
@@ -438,7 +440,7 @@ func (s *DockerRegistrySuite) TestPsListContainersFilterAncestorImageByDigest(c
438 438
 
439 439
 	// Valid imageReference
440 440
 	out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+imageReference)
441
-	checkPsAncestorFilterOutput(c, out, imageReference, expectedIDs)
441
+	checkPsAncestorFilterOutput(c, RemoveOutputForExistingElements(out, existingContainers), imageReference, expectedIDs)
442 442
 }
443 443
 
444 444
 func (s *DockerRegistrySuite) TestDeleteImageByIDOnlyPulledByDigest(c *check.C) {
... ...
@@ -39,6 +39,8 @@ func getHealth(c *check.C, name string) *types.Health {
39 39
 func (s *DockerSuite) TestHealth(c *check.C) {
40 40
 	testRequires(c, DaemonIsLinux) // busybox doesn't work on Windows
41 41
 
42
+	existingContainers := ExistingContainerNames(c)
43
+
42 44
 	imageName := "testhealth"
43 45
 	buildImageSuccessfully(c, imageName, build.WithDockerfile(`FROM busybox
44 46
 		RUN echo OK > /status
... ...
@@ -51,6 +53,7 @@ func (s *DockerSuite) TestHealth(c *check.C) {
51 51
 	name := "test_health"
52 52
 	dockerCmd(c, "create", "--name", name, imageName)
53 53
 	out, _ := dockerCmd(c, "ps", "-a", "--format={{.Status}}")
54
+	out = RemoveOutputForExistingElements(out, existingContainers)
54 55
 	c.Check(out, checker.Equals, "Created\n")
55 56
 
56 57
 	// Inspect the options
... ...
@@ -135,42 +135,48 @@ func (s *DockerSuite) TestInfoDiscoveryAdvertiseInterfaceName(c *check.C) {
135 135
 func (s *DockerSuite) TestInfoDisplaysRunningContainers(c *check.C) {
136 136
 	testRequires(c, DaemonIsLinux)
137 137
 
138
+	existing := existingContainerStates(c)
139
+
138 140
 	dockerCmd(c, "run", "-d", "busybox", "top")
139 141
 	out, _ := dockerCmd(c, "info")
140
-	c.Assert(out, checker.Contains, fmt.Sprintf("Containers: %d\n", 1))
141
-	c.Assert(out, checker.Contains, fmt.Sprintf(" Running: %d\n", 1))
142
-	c.Assert(out, checker.Contains, fmt.Sprintf(" Paused: %d\n", 0))
143
-	c.Assert(out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", 0))
142
+	c.Assert(out, checker.Contains, fmt.Sprintf("Containers: %d\n", existing["Containers"]+1))
143
+	c.Assert(out, checker.Contains, fmt.Sprintf(" Running: %d\n", existing["ContainersRunning"]+1))
144
+	c.Assert(out, checker.Contains, fmt.Sprintf(" Paused: %d\n", existing["ContainersPaused"]))
145
+	c.Assert(out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", existing["ContainersStopped"]))
144 146
 }
145 147
 
146 148
 func (s *DockerSuite) TestInfoDisplaysPausedContainers(c *check.C) {
147 149
 	testRequires(c, IsPausable)
148 150
 
151
+	existing := existingContainerStates(c)
152
+
149 153
 	out := runSleepingContainer(c, "-d")
150 154
 	cleanedContainerID := strings.TrimSpace(out)
151 155
 
152 156
 	dockerCmd(c, "pause", cleanedContainerID)
153 157
 
154 158
 	out, _ = dockerCmd(c, "info")
155
-	c.Assert(out, checker.Contains, fmt.Sprintf("Containers: %d\n", 1))
156
-	c.Assert(out, checker.Contains, fmt.Sprintf(" Running: %d\n", 0))
157
-	c.Assert(out, checker.Contains, fmt.Sprintf(" Paused: %d\n", 1))
158
-	c.Assert(out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", 0))
159
+	c.Assert(out, checker.Contains, fmt.Sprintf("Containers: %d\n", existing["Containers"]+1))
160
+	c.Assert(out, checker.Contains, fmt.Sprintf(" Running: %d\n", existing["ContainersRunning"]))
161
+	c.Assert(out, checker.Contains, fmt.Sprintf(" Paused: %d\n", existing["ContainersPaused"]+1))
162
+	c.Assert(out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", existing["ContainersStopped"]))
159 163
 }
160 164
 
161 165
 func (s *DockerSuite) TestInfoDisplaysStoppedContainers(c *check.C) {
162 166
 	testRequires(c, DaemonIsLinux)
163 167
 
168
+	existing := existingContainerStates(c)
169
+
164 170
 	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
165 171
 	cleanedContainerID := strings.TrimSpace(out)
166 172
 
167 173
 	dockerCmd(c, "stop", cleanedContainerID)
168 174
 
169 175
 	out, _ = dockerCmd(c, "info")
170
-	c.Assert(out, checker.Contains, fmt.Sprintf("Containers: %d\n", 1))
171
-	c.Assert(out, checker.Contains, fmt.Sprintf(" Running: %d\n", 0))
172
-	c.Assert(out, checker.Contains, fmt.Sprintf(" Paused: %d\n", 0))
173
-	c.Assert(out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", 1))
176
+	c.Assert(out, checker.Contains, fmt.Sprintf("Containers: %d\n", existing["Containers"]+1))
177
+	c.Assert(out, checker.Contains, fmt.Sprintf(" Running: %d\n", existing["ContainersRunning"]))
178
+	c.Assert(out, checker.Contains, fmt.Sprintf(" Paused: %d\n", existing["ContainersPaused"]))
179
+	c.Assert(out, checker.Contains, fmt.Sprintf(" Stopped: %d\n", existing["ContainersStopped"]+1))
174 180
 }
175 181
 
176 182
 func (s *DockerSuite) TestInfoDebug(c *check.C) {
... ...
@@ -237,3 +243,16 @@ func (s *DockerDaemonSuite) TestInfoLabels(c *check.C) {
237 237
 	c.Assert(err, checker.IsNil)
238 238
 	c.Assert(out, checker.Contains, "WARNING: labels with duplicate keys and conflicting values have been deprecated")
239 239
 }
240
+
241
+func existingContainerStates(c *check.C) map[string]int {
242
+	out, _ := dockerCmd(c, "info", "--format", "{{json .}}")
243
+	var m map[string]interface{}
244
+	err := json.Unmarshal([]byte(out), &m)
245
+	c.Assert(err, checker.IsNil)
246
+	res := map[string]int{}
247
+	res["Containers"] = int(m["Containers"].(float64))
248
+	res["ContainersRunning"] = int(m["ContainersRunning"].(float64))
249
+	res["ContainersPaused"] = int(m["ContainersPaused"].(float64))
250
+	res["ContainersStopped"] = int(m["ContainersStopped"].(float64))
251
+	return res
252
+}
... ...
@@ -5,6 +5,7 @@ import (
5 5
 	"net"
6 6
 	"regexp"
7 7
 	"sort"
8
+	"strconv"
8 9
 	"strings"
9 10
 
10 11
 	"github.com/docker/docker/integration-cli/checker"
... ...
@@ -148,9 +149,7 @@ func (s *DockerSuite) TestPortList(c *check.C) {
148 148
 
149 149
 	out, _ = dockerCmd(c, "port", ID)
150 150
 
151
-	err = assertPortList(c, out, []string{
152
-		"80/tcp -> 0.0.0.0:8000",
153
-		"80/udp -> 0.0.0.0:8000"})
151
+	err = assertPortRange(c, out, []int{8000, 8080}, []int{8000, 8080})
154 152
 	// Port list is not correct
155 153
 	c.Assert(err, checker.IsNil)
156 154
 	dockerCmd(c, "rm", "-f", ID)
... ...
@@ -173,6 +172,38 @@ func assertPortList(c *check.C, out string, expected []string) error {
173 173
 	return nil
174 174
 }
175 175
 
176
+func assertPortRange(c *check.C, out string, expectedTcp, expectedUdp []int) error {
177
+	lines := strings.Split(strings.Trim(out, "\n "), "\n")
178
+
179
+	var validTcp, validUdp bool
180
+	for _, l := range lines {
181
+		// 80/tcp -> 0.0.0.0:8015
182
+		port, err := strconv.Atoi(strings.Split(l, ":")[1])
183
+		if err != nil {
184
+			return err
185
+		}
186
+		if strings.Contains(l, "tcp") && expectedTcp != nil {
187
+			if port < expectedTcp[0] || port > expectedTcp[1] {
188
+				return fmt.Errorf("tcp port (%d) not in range expected range %d-%d", port, expectedTcp[0], expectedTcp[1])
189
+			}
190
+			validTcp = true
191
+		}
192
+		if strings.Contains(l, "udp") && expectedUdp != nil {
193
+			if port < expectedUdp[0] || port > expectedUdp[1] {
194
+				return fmt.Errorf("udp port (%d) not in range expected range %d-%d", port, expectedUdp[0], expectedUdp[1])
195
+			}
196
+			validUdp = true
197
+		}
198
+	}
199
+	if !validTcp {
200
+		return fmt.Errorf("tcp port not found")
201
+	}
202
+	if !validUdp {
203
+		return fmt.Errorf("udp port not found")
204
+	}
205
+	return nil
206
+}
207
+
176 208
 func stopRemoveContainer(id string, c *check.C) {
177 209
 	dockerCmd(c, "rm", "-f", id)
178 210
 }
... ...
@@ -19,6 +19,8 @@ import (
19 19
 )
20 20
 
21 21
 func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
22
+	existingContainers := ExistingContainerIDs(c)
23
+
22 24
 	out := runSleepingContainer(c, "-d")
23 25
 	firstID := strings.TrimSpace(out)
24 26
 
... ...
@@ -43,79 +45,79 @@ func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
43 43
 
44 44
 	// all
45 45
 	out, _ = dockerCmd(c, "ps", "-a")
46
-	c.Assert(assertContainerList(out, []string{fourthID, thirdID, secondID, firstID}), checker.Equals, true, check.Commentf("ALL: Container list is not in the correct order: \n%s", out))
46
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), []string{fourthID, thirdID, secondID, firstID}), checker.Equals, true, check.Commentf("ALL: Container list is not in the correct order: \n%s", out))
47 47
 
48 48
 	// running
49 49
 	out, _ = dockerCmd(c, "ps")
50
-	c.Assert(assertContainerList(out, []string{fourthID, secondID, firstID}), checker.Equals, true, check.Commentf("RUNNING: Container list is not in the correct order: \n%s", out))
50
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), []string{fourthID, secondID, firstID}), checker.Equals, true, check.Commentf("RUNNING: Container list is not in the correct order: \n%s", out))
51 51
 
52 52
 	// limit
53 53
 	out, _ = dockerCmd(c, "ps", "-n=2", "-a")
54 54
 	expected := []string{fourthID, thirdID}
55
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("LIMIT & ALL: Container list is not in the correct order: \n%s", out))
55
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("LIMIT & ALL: Container list is not in the correct order: \n%s", out))
56 56
 
57 57
 	out, _ = dockerCmd(c, "ps", "-n=2")
58
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("LIMIT: Container list is not in the correct order: \n%s", out))
58
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("LIMIT: Container list is not in the correct order: \n%s", out))
59 59
 
60 60
 	// filter since
61 61
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-a")
62 62
 	expected = []string{fourthID, thirdID, secondID}
63
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter & ALL: Container list is not in the correct order: \n%s", out))
63
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("SINCE filter & ALL: Container list is not in the correct order: \n%s", out))
64 64
 
65 65
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID)
66 66
 	expected = []string{fourthID, secondID}
67
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
67
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
68 68
 
69 69
 	out, _ = dockerCmd(c, "ps", "-f", "since="+thirdID)
70 70
 	expected = []string{fourthID}
71
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
71
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
72 72
 
73 73
 	// filter before
74 74
 	out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-a")
75 75
 	expected = []string{thirdID, secondID, firstID}
76
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter & ALL: Container list is not in the correct order: \n%s", out))
76
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("BEFORE filter & ALL: Container list is not in the correct order: \n%s", out))
77 77
 
78 78
 	out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID)
79 79
 	expected = []string{secondID, firstID}
80
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Container list is not in the correct order: \n%s", out))
80
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("BEFORE filter: Container list is not in the correct order: \n%s", out))
81 81
 
82 82
 	out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID)
83 83
 	expected = []string{secondID, firstID}
84
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
84
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
85 85
 
86 86
 	// filter since & before
87 87
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-a")
88 88
 	expected = []string{thirdID, secondID}
89
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter & ALL: Container list is not in the correct order: \n%s", out))
89
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter & ALL: Container list is not in the correct order: \n%s", out))
90 90
 
91 91
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID)
92 92
 	expected = []string{secondID}
93
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter: Container list is not in the correct order: \n%s", out))
93
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter: Container list is not in the correct order: \n%s", out))
94 94
 
95 95
 	// filter since & limit
96 96
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2", "-a")
97 97
 	expected = []string{fourthID, thirdID}
98 98
 
99
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
99
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("SINCE filter, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
100 100
 
101 101
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2")
102
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, LIMIT: Container list is not in the correct order: \n%s", out))
102
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("SINCE filter, LIMIT: Container list is not in the correct order: \n%s", out))
103 103
 
104 104
 	// filter before & limit
105 105
 	out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1", "-a")
106 106
 	expected = []string{thirdID}
107
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
107
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("BEFORE filter, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
108 108
 
109 109
 	out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1")
110
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter, LIMIT: Container list is not in the correct order: \n%s", out))
110
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("BEFORE filter, LIMIT: Container list is not in the correct order: \n%s", out))
111 111
 
112 112
 	// filter since & filter before & limit
113 113
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1", "-a")
114 114
 	expected = []string{thirdID}
115
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
115
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
116 116
 
117 117
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1")
118
-	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter, LIMIT: Container list is not in the correct order: \n%s", out))
118
+	c.Assert(assertContainerList(RemoveOutputForExistingElements(out, existingContainers), expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter, LIMIT: Container list is not in the correct order: \n%s", out))
119 119
 
120 120
 }
121 121
 
... ...
@@ -185,6 +187,8 @@ func (s *DockerSuite) TestPsListContainersSize(c *check.C) {
185 185
 }
186 186
 
187 187
 func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
188
+	existingContainers := ExistingContainerIDs(c)
189
+
188 190
 	// start exited container
189 191
 	out := cli.DockerCmd(c, "run", "-d", "busybox").Combined()
190 192
 	firstID := strings.TrimSpace(out)
... ...
@@ -199,11 +203,11 @@ func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
199 199
 	// filter containers by exited
200 200
 	out = cli.DockerCmd(c, "ps", "--no-trunc", "-q", "--filter=status=exited").Combined()
201 201
 	containerOut := strings.TrimSpace(out)
202
-	c.Assert(containerOut, checker.Equals, firstID)
202
+	c.Assert(RemoveOutputForExistingElements(containerOut, existingContainers), checker.Equals, firstID)
203 203
 
204 204
 	out = cli.DockerCmd(c, "ps", "-a", "--no-trunc", "-q", "--filter=status=running").Combined()
205 205
 	containerOut = strings.TrimSpace(out)
206
-	c.Assert(containerOut, checker.Equals, secondID)
206
+	c.Assert(RemoveOutputForExistingElements(containerOut, existingContainers), checker.Equals, secondID)
207 207
 
208 208
 	result := cli.Docker(cli.Args("ps", "-a", "-q", "--filter=status=rubbish"), cli.WithTimeout(time.Second*60))
209 209
 	result.Assert(c, icmd.Expected{
... ...
@@ -222,7 +226,7 @@ func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
222 222
 
223 223
 		out = cli.DockerCmd(c, "ps", "--no-trunc", "-q", "--filter=status=paused").Combined()
224 224
 		containerOut = strings.TrimSpace(out)
225
-		c.Assert(containerOut, checker.Equals, pausedID)
225
+		c.Assert(RemoveOutputForExistingElements(containerOut, existingContainers), checker.Equals, pausedID)
226 226
 	}
227 227
 }
228 228
 
... ...
@@ -305,6 +309,8 @@ func (s *DockerSuite) TestPsListContainersFilterName(c *check.C) {
305 305
 // - Run containers for each of those image (busybox, images_ps_filter_test1, images_ps_filter_test2)
306 306
 // - Filter them out :P
307 307
 func (s *DockerSuite) TestPsListContainersFilterAncestorImage(c *check.C) {
308
+	existingContainers := ExistingContainerIDs(c)
309
+
308 310
 	// Build images
309 311
 	imageName1 := "images_ps_filter_test1"
310 312
 	buildImageSuccessfully(c, imageName1, build.WithDockerfile(`FROM busybox
... ...
@@ -367,12 +373,12 @@ func (s *DockerSuite) TestPsListContainersFilterAncestorImage(c *check.C) {
367 367
 	var out string
368 368
 	for _, filter := range filterTestSuite {
369 369
 		out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+filter.filterName)
370
-		checkPsAncestorFilterOutput(c, out, filter.filterName, filter.expectedIDs)
370
+		checkPsAncestorFilterOutput(c, RemoveOutputForExistingElements(out, existingContainers), filter.filterName, filter.expectedIDs)
371 371
 	}
372 372
 
373 373
 	// Multiple ancestor filter
374 374
 	out, _ = dockerCmd(c, "ps", "-a", "-q", "--no-trunc", "--filter=ancestor="+imageName2, "--filter=ancestor="+imageName1Tagged)
375
-	checkPsAncestorFilterOutput(c, out, imageName2+","+imageName1Tagged, []string{fourthID, fifthID})
375
+	checkPsAncestorFilterOutput(c, RemoveOutputForExistingElements(out, existingContainers), imageName2+","+imageName1Tagged, []string{fourthID, fifthID})
376 376
 }
377 377
 
378 378
 func checkPsAncestorFilterOutput(c *check.C, out string, filterName string, expectedIDs []string) {
... ...
@@ -469,6 +475,9 @@ func (s *DockerSuite) TestPsListContainersFilterExited(c *check.C) {
469 469
 func (s *DockerSuite) TestPsRightTagName(c *check.C) {
470 470
 	// TODO Investigate further why this fails on Windows to Windows CI
471 471
 	testRequires(c, DaemonIsLinux)
472
+
473
+	existingContainers := ExistingContainerNames(c)
474
+
472 475
 	tag := "asybox:shmatest"
473 476
 	dockerCmd(c, "tag", "busybox", tag)
474 477
 
... ...
@@ -490,6 +499,7 @@ func (s *DockerSuite) TestPsRightTagName(c *check.C) {
490 490
 
491 491
 	out, _ = dockerCmd(c, "ps", "--no-trunc")
492 492
 	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
493
+	lines = RemoveLinesForExistingElements(lines, existingContainers)
493 494
 	// skip header
494 495
 	lines = lines[1:]
495 496
 	c.Assert(lines, checker.HasLen, 3, check.Commentf("There should be 3 running container, got %d", len(lines)))
... ...
@@ -581,12 +591,14 @@ func (s *DockerSuite) TestPsListContainersFilterCreated(c *check.C) {
581 581
 func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
582 582
 	// Problematic on Windows as it doesn't support link as of Jan 2016
583 583
 	testRequires(c, DaemonIsLinux)
584
+	existingContainers := ExistingContainerNames(c)
584 585
 	//create 2 containers and link them
585 586
 	dockerCmd(c, "run", "--name=child", "-d", "busybox", "top")
586 587
 	dockerCmd(c, "run", "--name=parent", "--link=child:linkedone", "-d", "busybox", "top")
587 588
 
588 589
 	//use the new format capabilities to only list the names and --no-trunc to get all names
589 590
 	out, _ := dockerCmd(c, "ps", "--format", "{{.Names}}", "--no-trunc")
591
+	out = RemoveOutputForExistingElements(out, existingContainers)
590 592
 	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
591 593
 	expected := []string{"parent", "child,parent/linkedone"}
592 594
 	var names []string
... ...
@@ -595,6 +607,7 @@ func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
595 595
 
596 596
 	//now list without turning off truncation and make sure we only get the non-link names
597 597
 	out, _ = dockerCmd(c, "ps", "--format", "{{.Names}}")
598
+	out = RemoveOutputForExistingElements(out, existingContainers)
598 599
 	lines = strings.Split(strings.TrimSpace(string(out)), "\n")
599 600
 	expected = []string{"parent", "child"}
600 601
 	var truncNames []string
... ...
@@ -604,12 +617,14 @@ func (s *DockerSuite) TestPsFormatMultiNames(c *check.C) {
604 604
 
605 605
 // Test for GitHub issue #21772
606 606
 func (s *DockerSuite) TestPsNamesMultipleTime(c *check.C) {
607
+	existingContainers := ExistingContainerNames(c)
607 608
 	runSleepingContainer(c, "--name=test1")
608 609
 	runSleepingContainer(c, "--name=test2")
609 610
 
610 611
 	//use the new format capabilities to list the names twice
611 612
 	out, _ := dockerCmd(c, "ps", "--format", "{{.Names}} {{.Names}}")
612 613
 	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
614
+	lines = RemoveLinesForExistingElements(lines, existingContainers)
613 615
 	expected := []string{"test2 test2", "test1 test1"}
614 616
 	var names []string
615 617
 	names = append(names, lines...)
... ...
@@ -628,6 +643,7 @@ func (s *DockerSuite) TestPsFormatHeaders(c *check.C) {
628 628
 }
629 629
 
630 630
 func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
631
+	existingContainers := ExistingContainerIDs(c)
631 632
 	config := `{
632 633
 		"psFormat": "default {{ .ID }}"
633 634
 }`
... ...
@@ -642,6 +658,7 @@ func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
642 642
 	id := strings.TrimSpace(out)
643 643
 
644 644
 	out, _ = dockerCmd(c, "--config", d, "ps", "-q")
645
+	out = RemoveOutputForExistingElements(out, existingContainers)
645 646
 	c.Assert(id, checker.HasPrefix, strings.TrimSpace(out), check.Commentf("Expected to print only the container id, got %v\n", out))
646 647
 }
647 648
 
... ...
@@ -652,6 +669,8 @@ func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) {
652 652
 	originalImageName := "busybox:TestPsImageIDAfterUpdate-original"
653 653
 	updatedImageName := "busybox:TestPsImageIDAfterUpdate-updated"
654 654
 
655
+	existingContainers := ExistingContainerIDs(c)
656
+
655 657
 	icmd.RunCommand(dockerBinary, "tag", "busybox:latest", originalImageName).Assert(c, icmd.Success)
656 658
 
657 659
 	originalImageID := getIDByName(c, originalImageName)
... ...
@@ -664,6 +683,7 @@ func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) {
664 664
 	result.Assert(c, icmd.Success)
665 665
 
666 666
 	lines := strings.Split(strings.TrimSpace(string(result.Combined())), "\n")
667
+	lines = RemoveLinesForExistingElements(lines, existingContainers)
667 668
 	// skip header
668 669
 	lines = lines[1:]
669 670
 	c.Assert(len(lines), checker.Equals, 1)
... ...
@@ -680,6 +700,7 @@ func (s *DockerSuite) TestPsImageIDAfterUpdate(c *check.C) {
680 680
 	result.Assert(c, icmd.Success)
681 681
 
682 682
 	lines = strings.Split(strings.TrimSpace(string(result.Combined())), "\n")
683
+	lines = RemoveLinesForExistingElements(lines, existingContainers)
683 684
 	// skip header
684 685
 	lines = lines[1:]
685 686
 	c.Assert(len(lines), checker.Equals, 1)
... ...
@@ -710,6 +731,8 @@ func (s *DockerSuite) TestPsNotShowPortsOfStoppedContainer(c *check.C) {
710 710
 }
711 711
 
712 712
 func (s *DockerSuite) TestPsShowMounts(c *check.C) {
713
+	existingContainers := ExistingContainerNames(c)
714
+
713 715
 	prefix, slash := getPrefixAndSlashFromDaemonPlatform()
714 716
 
715 717
 	mp := prefix + slash + "test"
... ...
@@ -736,6 +759,7 @@ func (s *DockerSuite) TestPsShowMounts(c *check.C) {
736 736
 	out, _ := dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}")
737 737
 
738 738
 	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
739
+	lines = RemoveLinesForExistingElements(lines, existingContainers)
739 740
 	c.Assert(lines, checker.HasLen, 3)
740 741
 
741 742
 	fields := strings.Fields(lines[0])
... ...
@@ -755,6 +779,7 @@ func (s *DockerSuite) TestPsShowMounts(c *check.C) {
755 755
 	out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume=ps-volume-test")
756 756
 
757 757
 	lines = strings.Split(strings.TrimSpace(string(out)), "\n")
758
+	lines = RemoveLinesForExistingElements(lines, existingContainers)
758 759
 	c.Assert(lines, checker.HasLen, 1)
759 760
 
760 761
 	fields = strings.Fields(lines[0])
... ...
@@ -768,6 +793,7 @@ func (s *DockerSuite) TestPsShowMounts(c *check.C) {
768 768
 	out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+mp)
769 769
 
770 770
 	lines = strings.Split(strings.TrimSpace(string(out)), "\n")
771
+	lines = RemoveLinesForExistingElements(lines, existingContainers)
771 772
 	c.Assert(lines, checker.HasLen, 2)
772 773
 
773 774
 	fields = strings.Fields(lines[0])
... ...
@@ -779,6 +805,7 @@ func (s *DockerSuite) TestPsShowMounts(c *check.C) {
779 779
 	out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+bindMountSource)
780 780
 
781 781
 	lines = strings.Split(strings.TrimSpace(string(out)), "\n")
782
+	lines = RemoveLinesForExistingElements(lines, existingContainers)
782 783
 	c.Assert(lines, checker.HasLen, 1)
783 784
 
784 785
 	fields = strings.Fields(lines[0])
... ...
@@ -790,6 +817,7 @@ func (s *DockerSuite) TestPsShowMounts(c *check.C) {
790 790
 	out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+bindMountDestination)
791 791
 
792 792
 	lines = strings.Split(strings.TrimSpace(string(out)), "\n")
793
+	lines = RemoveLinesForExistingElements(lines, existingContainers)
793 794
 	c.Assert(lines, checker.HasLen, 1)
794 795
 
795 796
 	fields = strings.Fields(lines[0])
... ...
@@ -820,6 +848,8 @@ func (s *DockerSuite) TestPsFormatSize(c *check.C) {
820 820
 }
821 821
 
822 822
 func (s *DockerSuite) TestPsListContainersFilterNetwork(c *check.C) {
823
+	existing := ExistingContainerIDs(c)
824
+
823 825
 	// TODO default network on Windows is not called "bridge", and creating a
824 826
 	// custom network fails on Windows fails with "Error response from daemon: plugin not found")
825 827
 	testRequires(c, DaemonIsLinux)
... ...
@@ -837,7 +867,7 @@ func (s *DockerSuite) TestPsListContainersFilterNetwork(c *check.C) {
837 837
 	lines = lines[1:]
838 838
 
839 839
 	// ps output should have no containers
840
-	c.Assert(lines, checker.HasLen, 0)
840
+	c.Assert(RemoveLinesForExistingElements(lines, existing), checker.HasLen, 0)
841 841
 
842 842
 	// Filter docker ps on network bridge
843 843
 	out, _ = dockerCmd(c, "ps", "--filter", "network=bridge")
... ...
@@ -849,7 +879,7 @@ func (s *DockerSuite) TestPsListContainersFilterNetwork(c *check.C) {
849 849
 	lines = lines[1:]
850 850
 
851 851
 	// ps output should have only one container
852
-	c.Assert(lines, checker.HasLen, 1)
852
+	c.Assert(RemoveLinesForExistingElements(lines, existing), checker.HasLen, 1)
853 853
 
854 854
 	// Making sure onbridgenetwork is on the output
855 855
 	c.Assert(containerOut, checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on network\n"))
... ...
@@ -864,7 +894,7 @@ func (s *DockerSuite) TestPsListContainersFilterNetwork(c *check.C) {
864 864
 	lines = lines[1:]
865 865
 
866 866
 	//ps output should have both the containers
867
-	c.Assert(lines, checker.HasLen, 2)
867
+	c.Assert(RemoveLinesForExistingElements(lines, existing), checker.HasLen, 2)
868 868
 
869 869
 	// Making sure onbridgenetwork and onnonenetwork is on the output
870 870
 	c.Assert(containerOut, checker.Contains, "onnonenetwork", check.Commentf("Missing the container on none network\n"))
... ...
@@ -885,11 +915,12 @@ func (s *DockerSuite) TestPsListContainersFilterNetwork(c *check.C) {
885 885
 	containerOut = strings.TrimSpace(string(out))
886 886
 
887 887
 	lines = strings.Split(containerOut, "\n")
888
+
888 889
 	// skip header
889 890
 	lines = lines[1:]
890 891
 
891 892
 	// ps output should have only one container
892
-	c.Assert(lines, checker.HasLen, 1)
893
+	c.Assert(RemoveLinesForExistingElements(lines, existing), checker.HasLen, 1)
893 894
 
894 895
 	// Making sure onbridgenetwork is on the output
895 896
 	c.Assert(containerOut, checker.Contains, "onbridgenetwork", check.Commentf("Missing the container on network\n"))
... ...
@@ -927,8 +958,10 @@ func (s *DockerSuite) TestPsFilterMissingArgErrorCode(c *check.C) {
927 927
 
928 928
 // Test case for 30291
929 929
 func (s *DockerSuite) TestPsFormatTemplateWithArg(c *check.C) {
930
+	existingContainers := ExistingContainerNames(c)
930 931
 	runSleepingContainer(c, "-d", "--name", "top", "--label", "some.label=label.foo-bar")
931 932
 	out, _ := dockerCmd(c, "ps", "--format", `{{.Names}} {{.Label "some.label"}}`)
933
+	out = RemoveOutputForExistingElements(out, existingContainers)
932 934
 	c.Assert(strings.TrimSpace(out), checker.Equals, "top label.foo-bar")
933 935
 }
934 936
 
... ...
@@ -969,11 +1002,14 @@ func (s *DockerSuite) TestPsListContainersFilterPorts(c *check.C) {
969 969
 func (s *DockerSuite) TestPsNotShowLinknamesOfDeletedContainer(c *check.C) {
970 970
 	testRequires(c, DaemonIsLinux)
971 971
 
972
+	existingContainers := ExistingContainerNames(c)
973
+
972 974
 	dockerCmd(c, "create", "--name=aaa", "busybox", "top")
973 975
 	dockerCmd(c, "create", "--name=bbb", "--link=aaa", "busybox", "top")
974 976
 
975 977
 	out, _ := dockerCmd(c, "ps", "--no-trunc", "-a", "--format", "{{.Names}}")
976 978
 	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
979
+	lines = RemoveLinesForExistingElements(lines, existingContainers)
977 980
 	expected := []string{"bbb", "aaa,bbb/aaa"}
978 981
 	var names []string
979 982
 	names = append(names, lines...)
... ...
@@ -982,5 +1018,6 @@ func (s *DockerSuite) TestPsNotShowLinknamesOfDeletedContainer(c *check.C) {
982 982
 	dockerCmd(c, "rm", "bbb")
983 983
 
984 984
 	out, _ = dockerCmd(c, "ps", "--no-trunc", "-a", "--format", "{{.Names}}")
985
+	out = RemoveOutputForExistingElements(out, existingContainers)
985 986
 	c.Assert(strings.TrimSpace(out), checker.Equals, "aaa")
986 987
 }
... ...
@@ -2813,23 +2813,27 @@ func (s *DockerSuite) TestRunVolumesFromRestartAfterRemoved(c *check.C) {
2813 2813
 
2814 2814
 // run container with --rm should remove container if exit code != 0
2815 2815
 func (s *DockerSuite) TestRunContainerWithRmFlagExitCodeNotEqualToZero(c *check.C) {
2816
+	existingContainers := ExistingContainerIDs(c)
2816 2817
 	name := "flowers"
2817 2818
 	cli.Docker(cli.Args("run", "--name", name, "--rm", "busybox", "ls", "/notexists")).Assert(c, icmd.Expected{
2818 2819
 		ExitCode: 1,
2819 2820
 	})
2820 2821
 
2821 2822
 	out := cli.DockerCmd(c, "ps", "-q", "-a").Combined()
2823
+	out = RemoveOutputForExistingElements(out, existingContainers)
2822 2824
 	if out != "" {
2823 2825
 		c.Fatal("Expected not to have containers", out)
2824 2826
 	}
2825 2827
 }
2826 2828
 
2827 2829
 func (s *DockerSuite) TestRunContainerWithRmFlagCannotStartContainer(c *check.C) {
2830
+	existingContainers := ExistingContainerIDs(c)
2828 2831
 	name := "sparkles"
2829 2832
 	cli.Docker(cli.Args("run", "--name", name, "--rm", "busybox", "commandNotFound")).Assert(c, icmd.Expected{
2830 2833
 		ExitCode: 127,
2831 2834
 	})
2832 2835
 	out := cli.DockerCmd(c, "ps", "-q", "-a").Combined()
2836
+	out = RemoveOutputForExistingElements(out, existingContainers)
2833 2837
 	if out != "" {
2834 2838
 		c.Fatal("Expected not to have containers", out)
2835 2839
 	}
... ...
@@ -7,7 +7,10 @@ import (
7 7
 	"path/filepath"
8 8
 	"strings"
9 9
 
10
+	"github.com/docker/docker/api/types"
11
+	"github.com/docker/docker/client"
10 12
 	"github.com/docker/docker/pkg/stringutils"
13
+	"github.com/go-check/check"
11 14
 	"github.com/gotestyourself/gotestyourself/icmd"
12 15
 	"github.com/pkg/errors"
13 16
 )
... ...
@@ -112,3 +115,71 @@ func RunCommandPipelineWithOutput(cmds ...*exec.Cmd) (output string, err error)
112 112
 	out, err := cmds[len(cmds)-1].CombinedOutput()
113 113
 	return string(out), err
114 114
 }
115
+
116
+type elementListOptions struct {
117
+	element, format string
118
+}
119
+
120
+func existingElements(c *check.C, opts elementListOptions) []string {
121
+	args := []string{}
122
+	switch opts.element {
123
+	case "container":
124
+		args = append(args, "ps", "-a")
125
+	case "image":
126
+		args = append(args, "images", "-a")
127
+	case "network":
128
+		args = append(args, "network", "ls")
129
+	case "plugin":
130
+		args = append(args, "plugin", "ls")
131
+	case "volume":
132
+		args = append(args, "volume", "ls")
133
+	}
134
+	if opts.format != "" {
135
+		args = append(args, "--format", opts.format)
136
+	}
137
+	out, _ := dockerCmd(c, args...)
138
+	lines := []string{}
139
+	for _, l := range strings.Split(out, "\n") {
140
+		if l != "" {
141
+			lines = append(lines, l)
142
+		}
143
+	}
144
+	return lines
145
+}
146
+
147
+// ExistingContainerIDs returns a list of currently existing container IDs.
148
+func ExistingContainerIDs(c *check.C) []string {
149
+	return existingElements(c, elementListOptions{element: "container", format: "{{.ID}}"})
150
+}
151
+
152
+// ExistingContainerNames returns a list of existing container names.
153
+func ExistingContainerNames(c *check.C) []string {
154
+	return existingElements(c, elementListOptions{element: "container", format: "{{.Names}}"})
155
+}
156
+
157
+// RemoveLinesForExistingElements removes existing elements from the output of a
158
+// docker command.
159
+// This function takes an output []string and returns a []string.
160
+func RemoveLinesForExistingElements(output, existing []string) []string {
161
+	for _, e := range existing {
162
+		index := -1
163
+		for i, line := range output {
164
+			if strings.Contains(line, e) {
165
+				index = i
166
+				break
167
+			}
168
+		}
169
+		if index != -1 {
170
+			output = append(output[:index], output[index+1:]...)
171
+		}
172
+	}
173
+	return output
174
+}
175
+
176
+// RemoveOutputForExistingElements removes existing elements from the output of
177
+// a docker command.
178
+// This function takes an output string and returns a string.
179
+func RemoveOutputForExistingElements(output string, existing []string) string {
180
+	res := RemoveLinesForExistingElements(strings.Split(output, "\n"), existing)
181
+	return strings.Join(res, "\n")
182
+}