Browse code

Merge pull request #10909 from ahmetalpbalkan/run_test-defer

integration-cli: Better test cleanup with defer

Jessie Frazelle authored on 2015/02/21 10:28:08
Showing 16 changed files
... ...
@@ -14,6 +14,8 @@ import (
14 14
 )
15 15
 
16 16
 func TestContainerApiGetAll(t *testing.T) {
17
+	defer deleteAllContainers()
18
+
17 19
 	startCount, err := getContainerCount()
18 20
 	if err != nil {
19 21
 		t.Fatalf("Cannot query container count: %v", err)
... ...
@@ -46,12 +48,12 @@ func TestContainerApiGetAll(t *testing.T) {
46 46
 		t.Fatalf("Container Name mismatch. Expected: %q, received: %q\n", "/"+name, actual)
47 47
 	}
48 48
 
49
-	deleteAllContainers()
50
-
51 49
 	logDone("container REST API - check GET json/all=1")
52 50
 }
53 51
 
54 52
 func TestContainerApiGetExport(t *testing.T) {
53
+	defer deleteAllContainers()
54
+
55 55
 	name := "exportcontainer"
56 56
 	runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "touch", "/test")
57 57
 	out, _, err := runCommandWithOutput(runCmd)
... ...
@@ -82,12 +84,13 @@ func TestContainerApiGetExport(t *testing.T) {
82 82
 	if !found {
83 83
 		t.Fatalf("The created test file has not been found in the exported image")
84 84
 	}
85
-	deleteAllContainers()
86 85
 
87 86
 	logDone("container REST API - check GET containers/export")
88 87
 }
89 88
 
90 89
 func TestContainerApiGetChanges(t *testing.T) {
90
+	defer deleteAllContainers()
91
+
91 92
 	name := "changescontainer"
92 93
 	runCmd := exec.Command(dockerBinary, "run", "--name", name, "busybox", "rm", "/etc/passwd")
93 94
 	out, _, err := runCommandWithOutput(runCmd)
... ...
@@ -119,8 +122,6 @@ func TestContainerApiGetChanges(t *testing.T) {
119 119
 		t.Fatalf("/etc/passwd has been removed but is not present in the diff")
120 120
 	}
121 121
 
122
-	deleteAllContainers()
123
-
124 122
 	logDone("container REST API - check GET containers/changes")
125 123
 }
126 124
 
... ...
@@ -7,6 +7,8 @@ import (
7 7
 )
8 8
 
9 9
 func TestInspectApiContainerResponse(t *testing.T) {
10
+	defer deleteAllContainers()
11
+
10 12
 	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
11 13
 	out, _, err := runCommandWithOutput(runCmd)
12 14
 	if err != nil {
... ...
@@ -53,7 +55,5 @@ func TestInspectApiContainerResponse(t *testing.T) {
53 53
 		}
54 54
 	}
55 55
 
56
-	deleteAllContainers()
57
-
58 56
 	logDone("container json - check keys in container json response")
59 57
 }
... ...
@@ -73,6 +73,8 @@ func TestCommitWithoutPause(t *testing.T) {
73 73
 }
74 74
 
75 75
 func TestCommitNewFile(t *testing.T) {
76
+	defer deleteAllContainers()
77
+
76 78
 	cmd := exec.Command(dockerBinary, "run", "--name", "commiter", "busybox", "/bin/sh", "-c", "echo koye > /foo")
77 79
 	if _, err := runCommand(cmd); err != nil {
78 80
 		t.Fatal(err)
... ...
@@ -84,6 +86,7 @@ func TestCommitNewFile(t *testing.T) {
84 84
 		t.Fatal(err)
85 85
 	}
86 86
 	imageID = strings.Trim(imageID, "\r\n")
87
+	defer deleteImages(imageID)
87 88
 
88 89
 	cmd = exec.Command(dockerBinary, "run", imageID, "cat", "/foo")
89 90
 
... ...
@@ -95,13 +98,12 @@ func TestCommitNewFile(t *testing.T) {
95 95
 		t.Fatalf("expected output koye received %q", actual)
96 96
 	}
97 97
 
98
-	deleteAllContainers()
99
-	deleteImages(imageID)
100
-
101 98
 	logDone("commit - commit file and read")
102 99
 }
103 100
 
104 101
 func TestCommitHardlink(t *testing.T) {
102
+	defer deleteAllContainers()
103
+
105 104
 	cmd := exec.Command(dockerBinary, "run", "-t", "--name", "hardlinks", "busybox", "sh", "-c", "touch file1 && ln file1 file2 && ls -di file1 file2")
106 105
 	firstOuput, _, err := runCommandWithOutput(cmd)
107 106
 	if err != nil {
... ...
@@ -127,6 +129,7 @@ func TestCommitHardlink(t *testing.T) {
127 127
 		t.Fatal(imageID, err)
128 128
 	}
129 129
 	imageID = strings.Trim(imageID, "\r\n")
130
+	defer deleteImages(imageID)
130 131
 
131 132
 	cmd = exec.Command(dockerBinary, "run", "-t", "hardlinks", "ls", "-di", "file1", "file2")
132 133
 	secondOuput, _, err := runCommandWithOutput(cmd)
... ...
@@ -147,9 +150,6 @@ func TestCommitHardlink(t *testing.T) {
147 147
 		t.Fatalf("Failed to create hardlink in a container. Expected to find %q in %q", inode, chunks[1:])
148 148
 	}
149 149
 
150
-	deleteAllContainers()
151
-	deleteImages(imageID)
152
-
153 150
 	logDone("commit - commit hardlinks")
154 151
 }
155 152
 
... ...
@@ -178,6 +178,8 @@ func TestCommitTTY(t *testing.T) {
178 178
 }
179 179
 
180 180
 func TestCommitWithHostBindMount(t *testing.T) {
181
+	defer deleteAllContainers()
182
+
181 183
 	cmd := exec.Command(dockerBinary, "run", "--name", "bind-commit", "-v", "/dev/null:/winning", "busybox", "true")
182 184
 	if _, err := runCommand(cmd); err != nil {
183 185
 		t.Fatal(err)
... ...
@@ -190,6 +192,7 @@ func TestCommitWithHostBindMount(t *testing.T) {
190 190
 	}
191 191
 
192 192
 	imageID = strings.Trim(imageID, "\r\n")
193
+	defer deleteImages(imageID)
193 194
 
194 195
 	cmd = exec.Command(dockerBinary, "run", "bindtest", "true")
195 196
 
... ...
@@ -197,8 +200,5 @@ func TestCommitWithHostBindMount(t *testing.T) {
197 197
 		t.Fatal(err)
198 198
 	}
199 199
 
200
-	deleteAllContainers()
201
-	deleteImages(imageID)
202
-
203 200
 	logDone("commit - commit bind mounted file")
204 201
 }
... ...
@@ -11,6 +11,8 @@ import (
11 11
 
12 12
 // Make sure we can create a simple container with some args
13 13
 func TestCreateArgs(t *testing.T) {
14
+	defer deleteAllContainers()
15
+
14 16
 	runCmd := exec.Command(dockerBinary, "create", "busybox", "command", "arg1", "arg2", "arg with space")
15 17
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
16 18
 	if err != nil {
... ...
@@ -56,13 +58,13 @@ func TestCreateArgs(t *testing.T) {
56 56
 		t.Fatalf("Unexpected args. Expected %v, received: %v", expected, c.Args)
57 57
 	}
58 58
 
59
-	deleteAllContainers()
60
-
61 59
 	logDone("create - args")
62 60
 }
63 61
 
64 62
 // Make sure we can set hostconfig options too
65 63
 func TestCreateHostConfig(t *testing.T) {
64
+	defer deleteAllContainers()
65
+
66 66
 	runCmd := exec.Command(dockerBinary, "create", "-P", "busybox", "echo")
67 67
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
68 68
 	if err != nil {
... ...
@@ -98,12 +100,12 @@ func TestCreateHostConfig(t *testing.T) {
98 98
 		t.Fatalf("Expected PublishAllPorts, got false")
99 99
 	}
100 100
 
101
-	deleteAllContainers()
102
-
103 101
 	logDone("create - hostconfig")
104 102
 }
105 103
 
106 104
 func TestCreateWithPortRange(t *testing.T) {
105
+	defer deleteAllContainers()
106
+
107 107
 	runCmd := exec.Command(dockerBinary, "create", "-p", "3300-3303:3300-3303/tcp", "busybox", "echo")
108 108
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
109 109
 	if err != nil {
... ...
@@ -147,12 +149,12 @@ func TestCreateWithPortRange(t *testing.T) {
147 147
 		}
148 148
 	}
149 149
 
150
-	deleteAllContainers()
151
-
152 150
 	logDone("create - port range")
153 151
 }
154 152
 
155 153
 func TestCreateWithiLargePortRange(t *testing.T) {
154
+	defer deleteAllContainers()
155
+
156 156
 	runCmd := exec.Command(dockerBinary, "create", "-p", "1-65535:1-65535/tcp", "busybox", "echo")
157 157
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
158 158
 	if err != nil {
... ...
@@ -196,13 +198,13 @@ func TestCreateWithiLargePortRange(t *testing.T) {
196 196
 		}
197 197
 	}
198 198
 
199
-	deleteAllContainers()
200
-
201 199
 	logDone("create - large port range")
202 200
 }
203 201
 
204 202
 // "test123" should be printed by docker create + start
205 203
 func TestCreateEchoStdout(t *testing.T) {
204
+	defer deleteAllContainers()
205
+
206 206
 	runCmd := exec.Command(dockerBinary, "create", "busybox", "echo", "test123")
207 207
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
208 208
 	if err != nil {
... ...
@@ -221,12 +223,12 @@ func TestCreateEchoStdout(t *testing.T) {
221 221
 		t.Errorf("container should've printed 'test123', got %q", out)
222 222
 	}
223 223
 
224
-	deleteAllContainers()
225
-
226 224
 	logDone("create - echo test123")
227 225
 }
228 226
 
229 227
 func TestCreateVolumesCreated(t *testing.T) {
228
+	defer deleteAllContainers()
229
+
230 230
 	name := "test_create_volume"
231 231
 	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "create", "--name", name, "-v", "/foo", "busybox")); err != nil {
232 232
 		t.Fatal(out, err)
... ...
@@ -137,6 +137,8 @@ func TestDaemonStartBridgeWithoutIPAssociation(t *testing.T) {
137 137
 }
138 138
 
139 139
 func TestDaemonIptablesClean(t *testing.T) {
140
+	defer deleteAllContainers()
141
+
140 142
 	d := NewDaemon(t)
141 143
 	if err := d.StartWithBusybox(); err != nil {
142 144
 		t.Fatalf("Could not start daemon with busybox: %v", err)
... ...
@@ -174,12 +176,12 @@ func TestDaemonIptablesClean(t *testing.T) {
174 174
 		t.Fatalf("iptables output should not have contained %q, but was %q", ipTablesSearchString, out)
175 175
 	}
176 176
 
177
-	deleteAllContainers()
178
-
179 177
 	logDone("daemon - run,iptables - iptables rules cleaned after daemon restart")
180 178
 }
181 179
 
182 180
 func TestDaemonIptablesCreate(t *testing.T) {
181
+	defer deleteAllContainers()
182
+
183 183
 	d := NewDaemon(t)
184 184
 	if err := d.StartWithBusybox(); err != nil {
185 185
 		t.Fatalf("Could not start daemon with busybox: %v", err)
... ...
@@ -226,8 +228,6 @@ func TestDaemonIptablesCreate(t *testing.T) {
226 226
 		t.Fatalf("iptables output after restart should have contained %q, but was %q", ipTablesSearchString, out)
227 227
 	}
228 228
 
229
-	deleteAllContainers()
230
-
231 229
 	logDone("daemon - run,iptables - iptables rules for always restarted container created after daemon restart")
232 230
 }
233 231
 
... ...
@@ -17,6 +17,8 @@ import (
17 17
 )
18 18
 
19 19
 func TestExec(t *testing.T) {
20
+	defer deleteAllContainers()
21
+
20 22
 	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
21 23
 	if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
22 24
 		t.Fatal(out, err)
... ...
@@ -34,8 +36,6 @@ func TestExec(t *testing.T) {
34 34
 		t.Errorf("container exec should've printed %q but printed %q", expected, out)
35 35
 	}
36 36
 
37
-	deleteAllContainers()
38
-
39 37
 	logDone("exec - basic test")
40 38
 }
41 39
 
... ...
@@ -80,6 +80,8 @@ func TestExecInteractiveStdinClose(t *testing.T) {
80 80
 }
81 81
 
82 82
 func TestExecInteractive(t *testing.T) {
83
+	defer deleteAllContainers()
84
+
83 85
 	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "testing", "busybox", "sh", "-c", "echo test > /tmp/file && sleep 100")
84 86
 	if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
85 87
 		t.Fatal(out, err)
... ...
@@ -127,12 +129,12 @@ func TestExecInteractive(t *testing.T) {
127 127
 		t.Fatal("docker exec failed to exit on stdin close")
128 128
 	}
129 129
 
130
-	deleteAllContainers()
131
-
132 130
 	logDone("exec - Interactive test")
133 131
 }
134 132
 
135 133
 func TestExecAfterContainerRestart(t *testing.T) {
134
+	defer deleteAllContainers()
135
+
136 136
 	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
137 137
 	out, _, err := runCommandWithOutput(runCmd)
138 138
 	if err != nil {
... ...
@@ -157,12 +159,12 @@ func TestExecAfterContainerRestart(t *testing.T) {
157 157
 		t.Errorf("container should've printed hello, instead printed %q", outStr)
158 158
 	}
159 159
 
160
-	deleteAllContainers()
161
-
162 160
 	logDone("exec - exec running container after container restart")
163 161
 }
164 162
 
165 163
 func TestExecAfterDaemonRestart(t *testing.T) {
164
+	defer deleteAllContainers()
165
+
166 166
 	d := NewDaemon(t)
167 167
 	if err := d.StartWithBusybox(); err != nil {
168 168
 		t.Fatalf("Could not start daemon with busybox: %v", err)
... ...
@@ -222,6 +224,8 @@ func TestExecEnv(t *testing.T) {
222 222
 }
223 223
 
224 224
 func TestExecExitStatus(t *testing.T) {
225
+	defer deleteAllContainers()
226
+
225 227
 	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "top", "busybox", "top")
226 228
 	if out, _, _, err := runCommandWithStdoutStderr(runCmd); err != nil {
227 229
 		t.Fatal(out, err)
... ...
@@ -239,7 +243,6 @@ func TestExecExitStatus(t *testing.T) {
239 239
 }
240 240
 
241 241
 func TestExecPausedContainer(t *testing.T) {
242
-
243 242
 	defer deleteAllContainers()
244 243
 	defer unpauseAllContainers()
245 244
 
... ...
@@ -493,6 +496,8 @@ func TestInspectExecID(t *testing.T) {
493 493
 }
494 494
 
495 495
 func TestLinksPingLinkedContainersOnRename(t *testing.T) {
496
+	defer deleteAllContainers()
497
+
496 498
 	var out string
497 499
 	out, _, _ = dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
498 500
 	idA := stripTrailingCharacters(out)
... ...
@@ -519,8 +524,6 @@ func TestLinksPingLinkedContainersOnRename(t *testing.T) {
519 519
 		t.Fatal(out, err)
520 520
 	}
521 521
 
522
-	deleteAllContainers()
523
-
524 522
 	logDone("links - ping linked container upon rename")
525 523
 }
526 524
 
... ...
@@ -15,6 +15,8 @@ import (
15 15
 )
16 16
 
17 17
 func TestLinksEtcHostsRegularFile(t *testing.T) {
18
+	defer deleteAllContainers()
19
+
18 20
 	runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
19 21
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
20 22
 	if err != nil {
... ...
@@ -24,13 +26,12 @@ func TestLinksEtcHostsRegularFile(t *testing.T) {
24 24
 	if !strings.HasPrefix(out, "-") {
25 25
 		t.Errorf("/etc/hosts should be a regular file")
26 26
 	}
27
-
28
-	deleteAllContainers()
29
-
30 27
 	logDone("link - /etc/hosts is a regular file")
31 28
 }
32 29
 
33 30
 func TestLinksEtcHostsContentMatch(t *testing.T) {
31
+	defer deleteAllContainers()
32
+
34 33
 	testRequires(t, SameHostDaemon)
35 34
 
36 35
 	runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hosts")
... ...
@@ -48,8 +49,6 @@ func TestLinksEtcHostsContentMatch(t *testing.T) {
48 48
 		t.Errorf("container")
49 49
 	}
50 50
 
51
-	deleteAllContainers()
52
-
53 51
 	logDone("link - /etc/hosts matches hosts copy")
54 52
 }
55 53
 
... ...
@@ -68,6 +67,8 @@ func TestLinksPingUnlinkedContainers(t *testing.T) {
68 68
 
69 69
 // Test for appropriate error when calling --link with an invalid target container
70 70
 func TestLinksInvalidContainerTarget(t *testing.T) {
71
+	defer deleteAllContainers()
72
+
71 73
 	runCmd := exec.Command(dockerBinary, "run", "--link", "bogus:alias", "busybox", "true")
72 74
 	out, _, err := runCommandWithOutput(runCmd)
73 75
 
... ...
@@ -77,12 +78,13 @@ func TestLinksInvalidContainerTarget(t *testing.T) {
77 77
 	if !strings.Contains(out, "Could not get container") {
78 78
 		t.Fatal("error output expected 'Could not get container', but got %q instead; err: %v", out, err)
79 79
 	}
80
-	deleteAllContainers()
81 80
 
82 81
 	logDone("links - linking to non-existent container should not work")
83 82
 }
84 83
 
85 84
 func TestLinksPingLinkedContainers(t *testing.T) {
85
+	defer deleteAllContainers()
86
+
86 87
 	runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
87 88
 	if _, err := runCommand(runCmd); err != nil {
88 89
 		t.Fatal(err)
... ...
@@ -103,11 +105,12 @@ func TestLinksPingLinkedContainers(t *testing.T) {
103 103
 	// 3. Ping by hostname
104 104
 	dockerCmd(t, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
105 105
 
106
-	deleteAllContainers()
107 106
 	logDone("links - ping linked container")
108 107
 }
109 108
 
110 109
 func TestLinksPingLinkedContainersAfterRename(t *testing.T) {
110
+	defer deleteAllContainers()
111
+
111 112
 	out, _, _ := dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
112 113
 	idA := stripTrailingCharacters(out)
113 114
 	out, _, _ = dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
... ...
@@ -116,12 +119,13 @@ func TestLinksPingLinkedContainersAfterRename(t *testing.T) {
116 116
 	dockerCmd(t, "run", "--rm", "--link", "container_new:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
117 117
 	dockerCmd(t, "kill", idA)
118 118
 	dockerCmd(t, "kill", idB)
119
-	deleteAllContainers()
120 119
 
121 120
 	logDone("links - ping linked container after rename")
122 121
 }
123 122
 
124 123
 func TestLinksIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
124
+	defer deleteAllContainers()
125
+
125 126
 	testRequires(t, SameHostDaemon)
126 127
 
127 128
 	dockerCmd(t, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "sleep", "10")
... ...
@@ -143,7 +147,6 @@ func TestLinksIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
143 143
 
144 144
 	dockerCmd(t, "kill", "child")
145 145
 	dockerCmd(t, "kill", "parent")
146
-	deleteAllContainers()
147 146
 
148 147
 	logDone("link - verify iptables when link and unlink")
149 148
 }
... ...
@@ -10,6 +10,7 @@ import (
10 10
 
11 11
 func TestNetworkNat(t *testing.T) {
12 12
 	testRequires(t, SameHostDaemon)
13
+	defer deleteAllContainers()
13 14
 
14 15
 	iface, err := net.InterfaceByName("eth0")
15 16
 	if err != nil {
... ...
@@ -56,7 +57,6 @@ func TestNetworkNat(t *testing.T) {
56 56
 	if out, _, err = runCommandWithOutput(killCmd); err != nil {
57 57
 		t.Fatalf("failed to kill container: %s, %v", out, err)
58 58
 	}
59
-	deleteAllContainers()
60 59
 
61 60
 	logDone("network - make sure nat works through the host")
62 61
 }
... ...
@@ -8,6 +8,8 @@ import (
8 8
 )
9 9
 
10 10
 func TestPortList(t *testing.T) {
11
+	defer deleteAllContainers()
12
+
11 13
 	// one port
12 14
 	runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "9876:80", "busybox", "top")
13 15
 	out, _, err := runCommandWithOutput(runCmd)
... ...
@@ -121,8 +123,6 @@ func TestPortList(t *testing.T) {
121 121
 		t.Fatal(out, err)
122 122
 	}
123 123
 
124
-	deleteAllContainers()
125
-
126 124
 	logDone("port - test port list")
127 125
 }
128 126
 
... ...
@@ -11,6 +11,8 @@ import (
11 11
 )
12 12
 
13 13
 func TestPsListContainers(t *testing.T) {
14
+	defer deleteAllContainers()
15
+
14 16
 	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
15 17
 	out, _, err := runCommandWithOutput(runCmd)
16 18
 	if err != nil {
... ...
@@ -223,8 +225,6 @@ func TestPsListContainers(t *testing.T) {
223 223
 		t.Error("Container list is not in the correct order")
224 224
 	}
225 225
 
226
-	deleteAllContainers()
227
-
228 226
 	logDone("ps - test ps options")
229 227
 }
230 228
 
... ...
@@ -246,6 +246,8 @@ func assertContainerList(out string, expected []string) bool {
246 246
 }
247 247
 
248 248
 func TestPsListContainersSize(t *testing.T) {
249
+	defer deleteAllContainers()
250
+
249 251
 	cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "hello")
250 252
 	runCommandWithOutput(cmd)
251 253
 	cmd = exec.Command(dockerBinary, "ps", "-s", "-n=1")
... ...
@@ -296,14 +298,14 @@ func TestPsListContainersSize(t *testing.T) {
296 296
 		t.Fatalf("Expected size %q, got %q", expectedSize, foundSize)
297 297
 	}
298 298
 
299
-	deleteAllContainers()
300 299
 	logDone("ps - test ps size")
301 300
 }
302 301
 
303 302
 func TestPsListContainersFilterStatus(t *testing.T) {
304 303
 	// FIXME: this should test paused, but it makes things hang and its wonky
305 304
 	// this is because paused containers can't be controlled by signals
306
-	deleteAllContainers()
305
+	defer deleteAllContainers()
306
+
307 307
 	// start exited container
308 308
 	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
309 309
 	out, _, err := runCommandWithOutput(runCmd)
... ...
@@ -347,12 +349,12 @@ func TestPsListContainersFilterStatus(t *testing.T) {
347 347
 		t.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
348 348
 	}
349 349
 
350
-	deleteAllContainers()
351
-
352 350
 	logDone("ps - test ps filter status")
353 351
 }
354 352
 
355 353
 func TestPsListContainersFilterID(t *testing.T) {
354
+	defer deleteAllContainers()
355
+
356 356
 	// start container
357 357
 	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox")
358 358
 	out, _, err := runCommandWithOutput(runCmd)
... ...
@@ -377,12 +379,12 @@ func TestPsListContainersFilterID(t *testing.T) {
377 377
 		t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
378 378
 	}
379 379
 
380
-	deleteAllContainers()
381
-
382 380
 	logDone("ps - test ps filter id")
383 381
 }
384 382
 
385 383
 func TestPsListContainersFilterName(t *testing.T) {
384
+	defer deleteAllContainers()
385
+
386 386
 	// start container
387 387
 	runCmd := exec.Command(dockerBinary, "run", "-d", "--name=a_name_to_match", "busybox")
388 388
 	out, _, err := runCommandWithOutput(runCmd)
... ...
@@ -407,8 +409,6 @@ func TestPsListContainersFilterName(t *testing.T) {
407 407
 		t.Fatalf("Expected id %s, got %s for exited filter, output: %q", firstID[:12], containerOut, out)
408 408
 	}
409 409
 
410
-	deleteAllContainers()
411
-
412 410
 	logDone("ps - test ps filter name")
413 411
 }
414 412
 
... ...
@@ -7,6 +7,8 @@ import (
7 7
 )
8 8
 
9 9
 func TestRenameStoppedContainer(t *testing.T) {
10
+	defer deleteAllContainers()
11
+
10 12
 	runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
11 13
 	out, _, err := runCommandWithOutput(runCmd)
12 14
 	if err != nil {
... ...
@@ -36,12 +38,13 @@ func TestRenameStoppedContainer(t *testing.T) {
36 36
 	if name != "new_name" {
37 37
 		t.Fatal("Failed to rename container ", name)
38 38
 	}
39
-	deleteAllContainers()
40 39
 
41 40
 	logDone("rename - stopped container")
42 41
 }
43 42
 
44 43
 func TestRenameRunningContainer(t *testing.T) {
44
+	defer deleteAllContainers()
45
+
45 46
 	runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
46 47
 	out, _, err := runCommandWithOutput(runCmd)
47 48
 	if err != nil {
... ...
@@ -62,12 +65,13 @@ func TestRenameRunningContainer(t *testing.T) {
62 62
 	if name != "new_name" {
63 63
 		t.Fatal("Failed to rename container ")
64 64
 	}
65
-	deleteAllContainers()
66 65
 
67 66
 	logDone("rename - running container")
68 67
 }
69 68
 
70 69
 func TestRenameCheckNames(t *testing.T) {
70
+	defer deleteAllContainers()
71
+
71 72
 	runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh")
72 73
 	out, _, err := runCommandWithOutput(runCmd)
73 74
 	if err != nil {
... ...
@@ -93,8 +97,6 @@ func TestRenameCheckNames(t *testing.T) {
93 93
 		t.Fatal(err)
94 94
 	}
95 95
 
96
-	deleteAllContainers()
97
-
98 96
 	logDone("rename - running container")
99 97
 }
100 98
 
... ...
@@ -8,6 +8,8 @@ import (
8 8
 )
9 9
 
10 10
 func TestRestartStoppedContainer(t *testing.T) {
11
+	defer deleteAllContainers()
12
+
11 13
 	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar")
12 14
 	out, _, err := runCommandWithOutput(runCmd)
13 15
 	if err != nil {
... ...
@@ -46,12 +48,12 @@ func TestRestartStoppedContainer(t *testing.T) {
46 46
 		t.Errorf("container should've printed 'foobar' twice")
47 47
 	}
48 48
 
49
-	deleteAllContainers()
50
-
51 49
 	logDone("restart - echo foobar for stopped container")
52 50
 }
53 51
 
54 52
 func TestRestartRunningContainer(t *testing.T) {
53
+	defer deleteAllContainers()
54
+
55 55
 	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
56 56
 	out, _, err := runCommandWithOutput(runCmd)
57 57
 	if err != nil {
... ...
@@ -89,13 +91,13 @@ func TestRestartRunningContainer(t *testing.T) {
89 89
 		t.Errorf("container should've printed 'foobar' twice")
90 90
 	}
91 91
 
92
-	deleteAllContainers()
93
-
94 92
 	logDone("restart - echo foobar for running container")
95 93
 }
96 94
 
97 95
 // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
98 96
 func TestRestartWithVolumes(t *testing.T) {
97
+	defer deleteAllContainers()
98
+
99 99
 	runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
100 100
 	out, _, err := runCommandWithOutput(runCmd)
101 101
 	if err != nil {
... ...
@@ -147,8 +149,6 @@ func TestRestartWithVolumes(t *testing.T) {
147 147
 		t.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
148 148
 	}
149 149
 
150
-	deleteAllContainers()
151
-
152 150
 	logDone("restart - does not create a new volume on restart")
153 151
 }
154 152
 
... ...
@@ -8,6 +8,8 @@ import (
8 8
 )
9 9
 
10 10
 func TestRmContainerWithRemovedVolume(t *testing.T) {
11
+	defer deleteAllContainers()
12
+
11 13
 	cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
12 14
 	if _, err := runCommand(cmd); err != nil {
13 15
 		t.Fatal(err)
... ...
@@ -22,12 +24,12 @@ func TestRmContainerWithRemovedVolume(t *testing.T) {
22 22
 		t.Fatal(out, err)
23 23
 	}
24 24
 
25
-	deleteAllContainers()
26
-
27 25
 	logDone("rm - removed volume")
28 26
 }
29 27
 
30 28
 func TestRmContainerWithVolume(t *testing.T) {
29
+	defer deleteAllContainers()
30
+
31 31
 	cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true")
32 32
 	if _, err := runCommand(cmd); err != nil {
33 33
 		t.Fatal(err)
... ...
@@ -38,12 +40,12 @@ func TestRmContainerWithVolume(t *testing.T) {
38 38
 		t.Fatal(err)
39 39
 	}
40 40
 
41
-	deleteAllContainers()
42
-
43 41
 	logDone("rm - volume")
44 42
 }
45 43
 
46 44
 func TestRmRunningContainer(t *testing.T) {
45
+	defer deleteAllContainers()
46
+
47 47
 	createRunningContainer(t, "foo")
48 48
 
49 49
 	// Test cannot remove running container
... ...
@@ -52,12 +54,12 @@ func TestRmRunningContainer(t *testing.T) {
52 52
 		t.Fatalf("Expected error, can't rm a running container")
53 53
 	}
54 54
 
55
-	deleteAllContainers()
56
-
57 55
 	logDone("rm - running container")
58 56
 }
59 57
 
60 58
 func TestRmRunningContainerCheckError409(t *testing.T) {
59
+	defer deleteAllContainers()
60
+
61 61
 	createRunningContainer(t, "foo")
62 62
 
63 63
 	endpoint := "/containers/foo"
... ...
@@ -70,12 +72,12 @@ func TestRmRunningContainerCheckError409(t *testing.T) {
70 70
 		t.Fatalf("Expected error to contain '409 Conflict' but found %s", err)
71 71
 	}
72 72
 
73
-	deleteAllContainers()
74
-
75 73
 	logDone("rm - running container")
76 74
 }
77 75
 
78 76
 func TestRmForceRemoveRunningContainer(t *testing.T) {
77
+	defer deleteAllContainers()
78
+
79 79
 	createRunningContainer(t, "foo")
80 80
 
81 81
 	// Stop then remove with -s
... ...
@@ -84,12 +86,12 @@ func TestRmForceRemoveRunningContainer(t *testing.T) {
84 84
 		t.Fatal(err)
85 85
 	}
86 86
 
87
-	deleteAllContainers()
88
-
89 87
 	logDone("rm - running container with --force=true")
90 88
 }
91 89
 
92 90
 func TestRmContainerOrphaning(t *testing.T) {
91
+	defer deleteAllContainers()
92
+
93 93
 	dockerfile1 := `FROM busybox:latest
94 94
 	ENTRYPOINT ["/bin/true"]`
95 95
 	img := "test-container-orphaning"
... ...
@@ -99,6 +101,7 @@ func TestRmContainerOrphaning(t *testing.T) {
99 99
 
100 100
 	// build first dockerfile
101 101
 	img1, err := buildImage(img, dockerfile1, true)
102
+	defer deleteImages(img1)
102 103
 	if err != nil {
103 104
 		t.Fatalf("Could not build image %s: %v", img, err)
104 105
 	}
... ...
@@ -123,9 +126,6 @@ func TestRmContainerOrphaning(t *testing.T) {
123 123
 		t.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
124 124
 	}
125 125
 
126
-	deleteAllContainers()
127
-	deleteImages(img1)
128
-
129 126
 	logDone("rm - container orphaning")
130 127
 }
131 128
 
... ...
@@ -78,6 +78,8 @@ func TestRmiTag(t *testing.T) {
78 78
 }
79 79
 
80 80
 func TestRmiTagWithExistingContainers(t *testing.T) {
81
+	defer deleteAllContainers()
82
+
81 83
 	container := "test-delete-tag"
82 84
 	newtag := "busybox:newtag"
83 85
 	bb := "busybox:latest"
... ...
@@ -95,12 +97,12 @@ func TestRmiTagWithExistingContainers(t *testing.T) {
95 95
 		t.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
96 96
 	}
97 97
 
98
-	deleteAllContainers()
99
-
100 98
 	logDone("rmi - delete tag with existing containers")
101 99
 }
102 100
 
103 101
 func TestRmiForceWithExistingContainers(t *testing.T) {
102
+	defer deleteAllContainers()
103
+
104 104
 	image := "busybox-clone"
105 105
 
106 106
 	cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-")
... ...
@@ -120,7 +122,5 @@ MAINTAINER foo`)
120 120
 		t.Fatalf("Could not remove image %s:  %s, %v", image, out, err)
121 121
 	}
122 122
 
123
-	deleteAllContainers()
124
-
125 123
 	logDone("rmi - force delete with existing containers")
126 124
 }
... ...
@@ -25,6 +25,7 @@ import (
25 25
 
26 26
 // "test123" should be printed by docker run
27 27
 func TestRunEchoStdout(t *testing.T) {
28
+	defer deleteAllContainers()
28 29
 	runCmd := exec.Command(dockerBinary, "run", "busybox", "echo", "test123")
29 30
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
30 31
 	if err != nil {
... ...
@@ -35,13 +36,12 @@ func TestRunEchoStdout(t *testing.T) {
35 35
 		t.Errorf("container should've printed 'test123'")
36 36
 	}
37 37
 
38
-	deleteAllContainers()
39
-
40 38
 	logDone("run - echo test123")
41 39
 }
42 40
 
43 41
 // "test" should be printed
44 42
 func TestRunEchoStdoutWithMemoryLimit(t *testing.T) {
43
+	defer deleteAllContainers()
45 44
 	runCmd := exec.Command(dockerBinary, "run", "-m", "16m", "busybox", "echo", "test")
46 45
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
47 46
 	if err != nil {
... ...
@@ -55,26 +55,26 @@ func TestRunEchoStdoutWithMemoryLimit(t *testing.T) {
55 55
 
56 56
 	}
57 57
 
58
-	deleteAllContainers()
59
-
60 58
 	logDone("run - echo with memory limit")
61 59
 }
62 60
 
63 61
 // should run without memory swap
64 62
 func TestRunWithoutMemoryswapLimit(t *testing.T) {
63
+	defer deleteAllContainers()
64
+
65 65
 	runCmd := exec.Command(dockerBinary, "run", "-m", "16m", "--memory-swap", "-1", "busybox", "true")
66 66
 	out, _, err := runCommandWithOutput(runCmd)
67 67
 	if err != nil {
68 68
 		t.Fatalf("failed to run container, output: %q", out)
69 69
 	}
70 70
 
71
-	deleteAllContainers()
72
-
73 71
 	logDone("run - without memory swap limit")
74 72
 }
75 73
 
76 74
 // "test" should be printed
77 75
 func TestRunEchoStdoutWitCPULimit(t *testing.T) {
76
+	defer deleteAllContainers()
77
+
78 78
 	runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "busybox", "echo", "test")
79 79
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
80 80
 	if err != nil {
... ...
@@ -85,13 +85,13 @@ func TestRunEchoStdoutWitCPULimit(t *testing.T) {
85 85
 		t.Errorf("container should've printed 'test'")
86 86
 	}
87 87
 
88
-	deleteAllContainers()
89
-
90 88
 	logDone("run - echo with CPU limit")
91 89
 }
92 90
 
93 91
 // "test" should be printed
94 92
 func TestRunEchoStdoutWithCPUAndMemoryLimit(t *testing.T) {
93
+	defer deleteAllContainers()
94
+
95 95
 	runCmd := exec.Command(dockerBinary, "run", "-c", "1000", "-m", "16m", "busybox", "echo", "test")
96 96
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
97 97
 	if err != nil {
... ...
@@ -102,13 +102,13 @@ func TestRunEchoStdoutWithCPUAndMemoryLimit(t *testing.T) {
102 102
 		t.Errorf("container should've printed 'test', got %q instead", out)
103 103
 	}
104 104
 
105
-	deleteAllContainers()
106
-
107 105
 	logDone("run - echo with CPU and memory limit")
108 106
 }
109 107
 
110 108
 // "test" should be printed
111 109
 func TestRunEchoNamedContainer(t *testing.T) {
110
+	defer deleteAllContainers()
111
+
112 112
 	runCmd := exec.Command(dockerBinary, "run", "--name", "testfoonamedcontainer", "busybox", "echo", "test")
113 113
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
114 114
 	if err != nil {
... ...
@@ -123,13 +123,13 @@ func TestRunEchoNamedContainer(t *testing.T) {
123 123
 		t.Errorf("failed to remove the named container: %v", err)
124 124
 	}
125 125
 
126
-	deleteAllContainers()
127
-
128 126
 	logDone("run - echo with named container")
129 127
 }
130 128
 
131 129
 // docker run should not leak file descriptors
132 130
 func TestRunLeakyFileDescriptors(t *testing.T) {
131
+	defer deleteAllContainers()
132
+
133 133
 	runCmd := exec.Command(dockerBinary, "run", "busybox", "ls", "-C", "/proc/self/fd")
134 134
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
135 135
 	if err != nil {
... ...
@@ -141,41 +141,41 @@ func TestRunLeakyFileDescriptors(t *testing.T) {
141 141
 		t.Errorf("container should've printed '0  1  2  3', not: %s", out)
142 142
 	}
143 143
 
144
-	deleteAllContainers()
145
-
146 144
 	logDone("run - check file descriptor leakage")
147 145
 }
148 146
 
149 147
 // it should be possible to ping Google DNS resolver
150 148
 // this will fail when Internet access is unavailable
151 149
 func TestRunPingGoogle(t *testing.T) {
150
+	defer deleteAllContainers()
151
+
152 152
 	runCmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "8.8.8.8")
153 153
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
154 154
 	if err != nil {
155 155
 		t.Fatalf("failed to run container: %v, output: %q", err, out)
156 156
 	}
157 157
 
158
-	deleteAllContainers()
159
-
160 158
 	logDone("run - ping 8.8.8.8")
161 159
 }
162 160
 
163 161
 // the exit code should be 0
164 162
 // some versions of lxc might make this test fail
165 163
 func TestRunExitCodeZero(t *testing.T) {
164
+	defer deleteAllContainers()
165
+
166 166
 	runCmd := exec.Command(dockerBinary, "run", "busybox", "true")
167 167
 	if out, _, err := runCommandWithOutput(runCmd); err != nil {
168 168
 		t.Errorf("container should've exited with exit code 0: %s, %v", out, err)
169 169
 	}
170 170
 
171
-	deleteAllContainers()
172
-
173 171
 	logDone("run - exit with 0")
174 172
 }
175 173
 
176 174
 // the exit code should be 1
177 175
 // some versions of lxc might make this test fail
178 176
 func TestRunExitCodeOne(t *testing.T) {
177
+	defer deleteAllContainers()
178
+
179 179
 	runCmd := exec.Command(dockerBinary, "run", "busybox", "false")
180 180
 	exitCode, err := runCommand(runCmd)
181 181
 	if err != nil && !strings.Contains("exit status 1", fmt.Sprintf("%s", err)) {
... ...
@@ -185,14 +185,14 @@ func TestRunExitCodeOne(t *testing.T) {
185 185
 		t.Errorf("container should've exited with exit code 1")
186 186
 	}
187 187
 
188
-	deleteAllContainers()
189
-
190 188
 	logDone("run - exit with 1")
191 189
 }
192 190
 
193 191
 // it should be possible to pipe in data via stdin to a process running in a container
194 192
 // some versions of lxc might make this test fail
195 193
 func TestRunStdinPipe(t *testing.T) {
194
+	defer deleteAllContainers()
195
+
196 196
 	runCmd := exec.Command("bash", "-c", `echo "blahblah" | docker run -i -a stdin busybox cat`)
197 197
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
198 198
 	if err != nil {
... ...
@@ -228,13 +228,13 @@ func TestRunStdinPipe(t *testing.T) {
228 228
 		t.Fatalf("rm failed to remove container: %s, %v", out, err)
229 229
 	}
230 230
 
231
-	deleteAllContainers()
232
-
233 231
 	logDone("run - pipe in with -i -a stdin")
234 232
 }
235 233
 
236 234
 // the container's ID should be printed when starting a container in detached mode
237 235
 func TestRunDetachedContainerIDPrinting(t *testing.T) {
236
+	defer deleteAllContainers()
237
+
238 238
 	runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
239 239
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
240 240
 	if err != nil {
... ...
@@ -264,13 +264,13 @@ func TestRunDetachedContainerIDPrinting(t *testing.T) {
264 264
 		t.Errorf("rm didn't print the container ID %s %s", out, rmOut)
265 265
 	}
266 266
 
267
-	deleteAllContainers()
268
-
269 267
 	logDone("run - print container ID in detached mode")
270 268
 }
271 269
 
272 270
 // the working directory should be set correctly
273 271
 func TestRunWorkingDirectory(t *testing.T) {
272
+	defer deleteAllContainers()
273
+
274 274
 	runCmd := exec.Command(dockerBinary, "run", "-w", "/root", "busybox", "pwd")
275 275
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
276 276
 	if err != nil {
... ...
@@ -295,14 +295,14 @@ func TestRunWorkingDirectory(t *testing.T) {
295 295
 		t.Errorf("--workdir failed to set working directory")
296 296
 	}
297 297
 
298
-	deleteAllContainers()
299
-
300 298
 	logDone("run - run with working directory set by -w")
301 299
 	logDone("run - run with working directory set by --workdir")
302 300
 }
303 301
 
304 302
 // pinging Google's DNS resolver should fail when we disable the networking
305 303
 func TestRunWithoutNetworking(t *testing.T) {
304
+	defer deleteAllContainers()
305
+
306 306
 	runCmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "8.8.8.8")
307 307
 	out, _, exitCode, err := runCommandWithStdoutStderr(runCmd)
308 308
 	if err != nil && exitCode != 1 {
... ...
@@ -321,14 +321,14 @@ func TestRunWithoutNetworking(t *testing.T) {
321 321
 		t.Errorf("-n=false should've disabled the network; the container shouldn't have been able to ping 8.8.8.8")
322 322
 	}
323 323
 
324
-	deleteAllContainers()
325
-
326 324
 	logDone("run - disable networking with --net=none")
327 325
 	logDone("run - disable networking with -n=false")
328 326
 }
329 327
 
330 328
 //test --link use container name to link target
331 329
 func TestRunLinksContainerWithContainerName(t *testing.T) {
330
+	defer deleteAllContainers()
331
+
332 332
 	cmd := exec.Command(dockerBinary, "run", "-i", "-t", "-d", "--name", "parent", "busybox")
333 333
 	out, _, _, err := runCommandWithStdoutStderr(cmd)
334 334
 	if err != nil {
... ...
@@ -348,13 +348,14 @@ func TestRunLinksContainerWithContainerName(t *testing.T) {
348 348
 	if !strings.Contains(out, ip+"	test") {
349 349
 		t.Fatalf("use a container name to link target failed")
350 350
 	}
351
-	deleteAllContainers()
352 351
 
353 352
 	logDone("run - use a container name to link target work")
354 353
 }
355 354
 
356 355
 //test --link use container id to link target
357 356
 func TestRunLinksContainerWithContainerId(t *testing.T) {
357
+	defer deleteAllContainers()
358
+
358 359
 	cmd := exec.Command(dockerBinary, "run", "-i", "-t", "-d", "busybox")
359 360
 	cID, _, _, err := runCommandWithStdoutStderr(cmd)
360 361
 	if err != nil {
... ...
@@ -376,13 +377,13 @@ func TestRunLinksContainerWithContainerId(t *testing.T) {
376 376
 		t.Fatalf("use a container id to link target failed")
377 377
 	}
378 378
 
379
-	deleteAllContainers()
380
-
381 379
 	logDone("run - use a container id to link target work")
382 380
 }
383 381
 
384 382
 // Regression test for #4741
385 383
 func TestRunWithVolumesAsFiles(t *testing.T) {
384
+	defer deleteAllContainers()
385
+
386 386
 	runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/etc/hosts:/target-file", "busybox", "true")
387 387
 	out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
388 388
 	if err != nil && exitCode != 0 {
... ...
@@ -394,13 +395,14 @@ func TestRunWithVolumesAsFiles(t *testing.T) {
394 394
 	if err != nil && exitCode != 0 {
395 395
 		t.Fatal("2", out, stderr, err)
396 396
 	}
397
-	deleteAllContainers()
398 397
 
399 398
 	logDone("run - regression test for #4741 - volumes from as files")
400 399
 }
401 400
 
402 401
 // Regression test for #4979
403 402
 func TestRunWithVolumesFromExited(t *testing.T) {
403
+	defer deleteAllContainers()
404
+
404 405
 	runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", "/some/dir", "busybox", "touch", "/some/dir/file")
405 406
 	out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
406 407
 	if err != nil && exitCode != 0 {
... ...
@@ -412,31 +414,30 @@ func TestRunWithVolumesFromExited(t *testing.T) {
412 412
 	if err != nil && exitCode != 0 {
413 413
 		t.Fatal("2", out, stderr, err)
414 414
 	}
415
-	deleteAllContainers()
416 415
 
417 416
 	logDone("run - regression test for #4979 - volumes-from on exited container")
418 417
 }
419 418
 
420 419
 // Regression test for #4830
421 420
 func TestRunWithRelativePath(t *testing.T) {
421
+	defer deleteAllContainers()
422
+
422 423
 	runCmd := exec.Command(dockerBinary, "run", "-v", "tmp:/other-tmp", "busybox", "true")
423 424
 	if _, _, _, err := runCommandWithStdoutStderr(runCmd); err == nil {
424 425
 		t.Fatalf("relative path should result in an error")
425 426
 	}
426 427
 
427
-	deleteAllContainers()
428
-
429 428
 	logDone("run - volume with relative path")
430 429
 }
431 430
 
432 431
 func TestRunVolumesMountedAsReadonly(t *testing.T) {
432
+	defer deleteAllContainers()
433
+
433 434
 	cmd := exec.Command(dockerBinary, "run", "-v", "/test:/test:ro", "busybox", "touch", "/test/somefile")
434 435
 	if code, err := runCommand(cmd); err == nil || code == 0 {
435 436
 		t.Fatalf("run should fail because volume is ro: exit code %d", code)
436 437
 	}
437 438
 
438
-	deleteAllContainers()
439
-
440 439
 	logDone("run - volumes as readonly mount")
441 440
 }
442 441
 
... ...
@@ -508,6 +509,7 @@ func TestVolumesFromGetsProperMode(t *testing.T) {
508 508
 
509 509
 // Test for GH#10618
510 510
 func TestRunNoDupVolumes(t *testing.T) {
511
+	defer deleteAllContainers()
511 512
 
512 513
 	bindPath1, err := ioutil.TempDir("", "test1")
513 514
 	if err != nil {
... ...
@@ -532,13 +534,14 @@ func TestRunNoDupVolumes(t *testing.T) {
532 532
 			t.Fatalf("Expected 'duplicate volume' error, got %v", err)
533 533
 		}
534 534
 	}
535
-	deleteAllContainers()
536 535
 
537 536
 	logDone("run - don't allow multiple (bind) volumes on the same container target")
538 537
 }
539 538
 
540 539
 // Test for #1351
541 540
 func TestRunApplyVolumesFromBeforeVolumes(t *testing.T) {
541
+	defer deleteAllContainers()
542
+
542 543
 	cmd := exec.Command(dockerBinary, "run", "--name", "parent", "-v", "/test", "busybox", "touch", "/test/foo")
543 544
 	if _, err := runCommand(cmd); err != nil {
544 545
 		t.Fatal(err)
... ...
@@ -549,12 +552,12 @@ func TestRunApplyVolumesFromBeforeVolumes(t *testing.T) {
549 549
 		t.Fatal(out, err)
550 550
 	}
551 551
 
552
-	deleteAllContainers()
553
-
554 552
 	logDone("run - volumes from mounted first")
555 553
 }
556 554
 
557 555
 func TestRunMultipleVolumesFrom(t *testing.T) {
556
+	defer deleteAllContainers()
557
+
558 558
 	cmd := exec.Command(dockerBinary, "run", "--name", "parent1", "-v", "/test", "busybox", "touch", "/test/foo")
559 559
 	if _, err := runCommand(cmd); err != nil {
560 560
 		t.Fatal(err)
... ...
@@ -571,13 +574,13 @@ func TestRunMultipleVolumesFrom(t *testing.T) {
571 571
 		t.Fatal(err)
572 572
 	}
573 573
 
574
-	deleteAllContainers()
575
-
576 574
 	logDone("run - multiple volumes from")
577 575
 }
578 576
 
579 577
 // this tests verifies the ID format for the container
580 578
 func TestRunVerifyContainerID(t *testing.T) {
579
+	defer deleteAllContainers()
580
+
581 581
 	cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
582 582
 	out, exit, err := runCommandWithOutput(cmd)
583 583
 	if err != nil {
... ...
@@ -594,36 +597,39 @@ func TestRunVerifyContainerID(t *testing.T) {
594 594
 		t.Fatalf("Invalid container ID: %s", out)
595 595
 	}
596 596
 
597
-	deleteAllContainers()
598
-
599 597
 	logDone("run - verify container ID")
600 598
 }
601 599
 
602 600
 // Test that creating a container with a volume doesn't crash. Regression test for #995.
603 601
 func TestRunCreateVolume(t *testing.T) {
602
+	defer deleteAllContainers()
603
+
604 604
 	cmd := exec.Command(dockerBinary, "run", "-v", "/var/lib/data", "busybox", "true")
605 605
 	if _, err := runCommand(cmd); err != nil {
606 606
 		t.Fatal(err)
607 607
 	}
608 608
 
609
-	deleteAllContainers()
610
-
611 609
 	logDone("run - create docker managed volume")
612 610
 }
613 611
 
614 612
 // Test that creating a volume with a symlink in its path works correctly. Test for #5152.
615 613
 // Note that this bug happens only with symlinks with a target that starts with '/'.
616 614
 func TestRunCreateVolumeWithSymlink(t *testing.T) {
617
-	buildCmd := exec.Command(dockerBinary, "build", "-t", "docker-test-createvolumewithsymlink", "-")
615
+	defer deleteAllContainers()
616
+
617
+	image := "docker-test-createvolumewithsymlink"
618
+	defer deleteImages(image)
619
+
620
+	buildCmd := exec.Command(dockerBinary, "build", "-t", image, "-")
618 621
 	buildCmd.Stdin = strings.NewReader(`FROM busybox
619 622
 		RUN ln -s home /bar`)
620 623
 	buildCmd.Dir = workingDirectory
621 624
 	err := buildCmd.Run()
622 625
 	if err != nil {
623
-		t.Fatalf("could not build 'docker-test-createvolumewithsymlink': %v", err)
626
+		t.Fatalf("could not build '%s': %v", image, err)
624 627
 	}
625 628
 
626
-	cmd := exec.Command(dockerBinary, "run", "-v", "/bar/foo", "--name", "test-createvolumewithsymlink", "docker-test-createvolumewithsymlink", "sh", "-c", "mount | grep -q /home/foo")
629
+	cmd := exec.Command(dockerBinary, "run", "-v", "/bar/foo", "--name", "test-createvolumewithsymlink", image, "sh", "-c", "mount | grep -q /home/foo")
627 630
 	exitCode, err := runCommand(cmd)
628 631
 	if err != nil || exitCode != 0 {
629 632
 		t.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
... ...
@@ -648,15 +654,16 @@ func TestRunCreateVolumeWithSymlink(t *testing.T) {
648 648
 		t.Fatalf("[open] (expecting 'file does not exist' error) err: %v, volPath: %s", err, volPath)
649 649
 	}
650 650
 
651
-	deleteImages("docker-test-createvolumewithsymlink")
652
-	deleteAllContainers()
653
-
654 651
 	logDone("run - create volume with symlink")
655 652
 }
656 653
 
657 654
 // Tests that a volume path that has a symlink exists in a container mounting it with `--volumes-from`.
658 655
 func TestRunVolumesFromSymlinkPath(t *testing.T) {
656
+	defer deleteAllContainers()
657
+
659 658
 	name := "docker-test-volumesfromsymlinkpath"
659
+	defer deleteImages(name)
660
+
660 661
 	buildCmd := exec.Command(dockerBinary, "build", "-t", name, "-")
661 662
 	buildCmd.Stdin = strings.NewReader(`FROM busybox
662 663
 		RUN ln -s home /foo
... ...
@@ -679,13 +686,12 @@ func TestRunVolumesFromSymlinkPath(t *testing.T) {
679 679
 		t.Fatalf("[run] err: %v, exitcode: %d", err, exitCode)
680 680
 	}
681 681
 
682
-	deleteAllContainers()
683
-	deleteImages(name)
684
-
685 682
 	logDone("run - volumes-from symlink path")
686 683
 }
687 684
 
688 685
 func TestRunExitCode(t *testing.T) {
686
+	defer deleteAllContainers()
687
+
689 688
 	cmd := exec.Command(dockerBinary, "run", "busybox", "/bin/sh", "-c", "exit 72")
690 689
 
691 690
 	exit, err := runCommand(cmd)
... ...
@@ -696,14 +702,13 @@ func TestRunExitCode(t *testing.T) {
696 696
 		t.Fatalf("expected exit code 72 received %d", exit)
697 697
 	}
698 698
 
699
-	deleteAllContainers()
700
-
701 699
 	logDone("run - correct exit code")
702 700
 }
703 701
 
704 702
 func TestRunUserDefaultsToRoot(t *testing.T) {
705
-	cmd := exec.Command(dockerBinary, "run", "busybox", "id")
703
+	defer deleteAllContainers()
706 704
 
705
+	cmd := exec.Command(dockerBinary, "run", "busybox", "id")
707 706
 	out, _, err := runCommandWithOutput(cmd)
708 707
 	if err != nil {
709 708
 		t.Fatal(err, out)
... ...
@@ -711,14 +716,14 @@ func TestRunUserDefaultsToRoot(t *testing.T) {
711 711
 	if !strings.Contains(out, "uid=0(root) gid=0(root)") {
712 712
 		t.Fatalf("expected root user got %s", out)
713 713
 	}
714
-	deleteAllContainers()
715 714
 
716 715
 	logDone("run - default user")
717 716
 }
718 717
 
719 718
 func TestRunUserByName(t *testing.T) {
720
-	cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id")
719
+	defer deleteAllContainers()
721 720
 
721
+	cmd := exec.Command(dockerBinary, "run", "-u", "root", "busybox", "id")
722 722
 	out, _, err := runCommandWithOutput(cmd)
723 723
 	if err != nil {
724 724
 		t.Fatal(err, out)
... ...
@@ -726,14 +731,14 @@ func TestRunUserByName(t *testing.T) {
726 726
 	if !strings.Contains(out, "uid=0(root) gid=0(root)") {
727 727
 		t.Fatalf("expected root user got %s", out)
728 728
 	}
729
-	deleteAllContainers()
730 729
 
731 730
 	logDone("run - user by name")
732 731
 }
733 732
 
734 733
 func TestRunUserByID(t *testing.T) {
735
-	cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id")
734
+	defer deleteAllContainers()
736 735
 
736
+	cmd := exec.Command(dockerBinary, "run", "-u", "1", "busybox", "id")
737 737
 	out, _, err := runCommandWithOutput(cmd)
738 738
 	if err != nil {
739 739
 		t.Fatal(err, out)
... ...
@@ -741,14 +746,14 @@ func TestRunUserByID(t *testing.T) {
741 741
 	if !strings.Contains(out, "uid=1(daemon) gid=1(daemon)") {
742 742
 		t.Fatalf("expected daemon user got %s", out)
743 743
 	}
744
-	deleteAllContainers()
745 744
 
746 745
 	logDone("run - user by id")
747 746
 }
748 747
 
749 748
 func TestRunUserByIDBig(t *testing.T) {
750
-	cmd := exec.Command(dockerBinary, "run", "-u", "2147483648", "busybox", "id")
749
+	defer deleteAllContainers()
751 750
 
751
+	cmd := exec.Command(dockerBinary, "run", "-u", "2147483648", "busybox", "id")
752 752
 	out, _, err := runCommandWithOutput(cmd)
753 753
 	if err == nil {
754 754
 		t.Fatal("No error, but must be.", out)
... ...
@@ -756,14 +761,14 @@ func TestRunUserByIDBig(t *testing.T) {
756 756
 	if !strings.Contains(out, "Uids and gids must be in range") {
757 757
 		t.Fatalf("expected error about uids range, got %s", out)
758 758
 	}
759
-	deleteAllContainers()
760 759
 
761 760
 	logDone("run - user by id, id too big")
762 761
 }
763 762
 
764 763
 func TestRunUserByIDNegative(t *testing.T) {
765
-	cmd := exec.Command(dockerBinary, "run", "-u", "-1", "busybox", "id")
764
+	defer deleteAllContainers()
766 765
 
766
+	cmd := exec.Command(dockerBinary, "run", "-u", "-1", "busybox", "id")
767 767
 	out, _, err := runCommandWithOutput(cmd)
768 768
 	if err == nil {
769 769
 		t.Fatal("No error, but must be.", out)
... ...
@@ -771,14 +776,14 @@ func TestRunUserByIDNegative(t *testing.T) {
771 771
 	if !strings.Contains(out, "Uids and gids must be in range") {
772 772
 		t.Fatalf("expected error about uids range, got %s", out)
773 773
 	}
774
-	deleteAllContainers()
775 774
 
776 775
 	logDone("run - user by id, id negative")
777 776
 }
778 777
 
779 778
 func TestRunUserByIDZero(t *testing.T) {
780
-	cmd := exec.Command(dockerBinary, "run", "-u", "0", "busybox", "id")
779
+	defer deleteAllContainers()
781 780
 
781
+	cmd := exec.Command(dockerBinary, "run", "-u", "0", "busybox", "id")
782 782
 	out, _, err := runCommandWithOutput(cmd)
783 783
 	if err != nil {
784 784
 		t.Fatal(err, out)
... ...
@@ -786,24 +791,25 @@ func TestRunUserByIDZero(t *testing.T) {
786 786
 	if !strings.Contains(out, "uid=0(root) gid=0(root) groups=10(wheel)") {
787 787
 		t.Fatalf("expected daemon user got %s", out)
788 788
 	}
789
-	deleteAllContainers()
790 789
 
791 790
 	logDone("run - user by id, zero uid")
792 791
 }
793 792
 
794 793
 func TestRunUserNotFound(t *testing.T) {
795
-	cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id")
794
+	defer deleteAllContainers()
796 795
 
796
+	cmd := exec.Command(dockerBinary, "run", "-u", "notme", "busybox", "id")
797 797
 	_, err := runCommand(cmd)
798 798
 	if err == nil {
799 799
 		t.Fatal("unknown user should cause container to fail")
800 800
 	}
801
-	deleteAllContainers()
802 801
 
803 802
 	logDone("run - user not found")
804 803
 }
805 804
 
806 805
 func TestRunTwoConcurrentContainers(t *testing.T) {
806
+	defer deleteAllContainers()
807
+
807 808
 	group := sync.WaitGroup{}
808 809
 	group.Add(2)
809 810
 
... ...
@@ -819,12 +825,12 @@ func TestRunTwoConcurrentContainers(t *testing.T) {
819 819
 
820 820
 	group.Wait()
821 821
 
822
-	deleteAllContainers()
823
-
824 822
 	logDone("run - two concurrent containers")
825 823
 }
826 824
 
827 825
 func TestRunEnvironment(t *testing.T) {
826
+	defer deleteAllContainers()
827
+
828 828
 	cmd := exec.Command(dockerBinary, "run", "-h", "testing", "-e=FALSE=true", "-e=TRUE", "-e=TRICKY", "-e=HOME=", "busybox", "env")
829 829
 	cmd.Env = append(os.Environ(),
830 830
 		"TRUE=false",
... ...
@@ -865,8 +871,6 @@ func TestRunEnvironment(t *testing.T) {
865 865
 		}
866 866
 	}
867 867
 
868
-	deleteAllContainers()
869
-
870 868
 	logDone("run - verify environment")
871 869
 }
872 870
 
... ...
@@ -874,6 +878,8 @@ func TestRunEnvironmentErase(t *testing.T) {
874 874
 	// Test to make sure that when we use -e on env vars that are
875 875
 	// not set in our local env that they're removed (if present) in
876 876
 	// the container
877
+	defer deleteAllContainers()
878
+
877 879
 	cmd := exec.Command(dockerBinary, "run", "-e", "FOO", "-e", "HOSTNAME", "busybox", "env")
878 880
 	cmd.Env = appendDockerHostEnv([]string{})
879 881
 
... ...
@@ -905,14 +911,14 @@ func TestRunEnvironmentErase(t *testing.T) {
905 905
 		}
906 906
 	}
907 907
 
908
-	deleteAllContainers()
909
-
910 908
 	logDone("run - verify environment erase")
911 909
 }
912 910
 
913 911
 func TestRunEnvironmentOverride(t *testing.T) {
914 912
 	// Test to make sure that when we use -e on env vars that are
915 913
 	// already in the env that we're overriding them
914
+	defer deleteAllContainers()
915
+
916 916
 	cmd := exec.Command(dockerBinary, "run", "-e", "HOSTNAME", "-e", "HOME=/root2", "busybox", "env")
917 917
 	cmd.Env = appendDockerHostEnv([]string{"HOSTNAME=bar"})
918 918
 
... ...
@@ -945,49 +951,48 @@ func TestRunEnvironmentOverride(t *testing.T) {
945 945
 		}
946 946
 	}
947 947
 
948
-	deleteAllContainers()
949
-
950 948
 	logDone("run - verify environment override")
951 949
 }
952 950
 
953 951
 func TestRunContainerNetwork(t *testing.T) {
952
+	defer deleteAllContainers()
953
+
954 954
 	cmd := exec.Command(dockerBinary, "run", "busybox", "ping", "-c", "1", "127.0.0.1")
955 955
 	if _, err := runCommand(cmd); err != nil {
956 956
 		t.Fatal(err)
957 957
 	}
958 958
 
959
-	deleteAllContainers()
960
-
961 959
 	logDone("run - test container network via ping")
962 960
 }
963 961
 
964 962
 // Issue #4681
965 963
 func TestRunLoopbackWhenNetworkDisabled(t *testing.T) {
964
+	defer deleteAllContainers()
965
+
966 966
 	cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
967 967
 	if _, err := runCommand(cmd); err != nil {
968 968
 		t.Fatal(err)
969 969
 	}
970 970
 
971
-	deleteAllContainers()
972
-
973 971
 	logDone("run - test container loopback when networking disabled")
974 972
 }
975 973
 
976 974
 func TestRunNetHostNotAllowedWithLinks(t *testing.T) {
977
-	_, _, err := dockerCmd(t, "run", "--name", "linked", "busybox", "true")
975
+	defer deleteAllContainers()
978 976
 
977
+	_, _, err := dockerCmd(t, "run", "--name", "linked", "busybox", "true")
979 978
 	cmd := exec.Command(dockerBinary, "run", "--net=host", "--link", "linked:linked", "busybox", "true")
980 979
 	_, _, err = runCommandWithOutput(cmd)
981 980
 	if err == nil {
982 981
 		t.Fatal("Expected error")
983 982
 	}
984 983
 
985
-	deleteAllContainers()
986
-
987 984
 	logDone("run - don't allow --net=host to be used with links")
988 985
 }
989 986
 
990 987
 func TestRunLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) {
988
+	defer deleteAllContainers()
989
+
991 990
 	cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
992 991
 	out, _, err := runCommandWithOutput(cmd)
993 992
 	if err != nil {
... ...
@@ -1013,8 +1018,6 @@ func TestRunLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) {
1013 1013
 		t.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
1014 1014
 	}
1015 1015
 
1016
-	deleteAllContainers()
1017
-
1018 1016
 	logDone("run - test loopback only exists when networking disabled")
1019 1017
 }
1020 1018
 
... ...
@@ -1024,6 +1027,8 @@ func TestRunLoopbackOnlyExistsWhenNetworkingDisabled(t *testing.T) {
1024 1024
 // codepath is executed with "docker run -h <hostname>".  Both were manually
1025 1025
 // tested, but this testcase takes the simpler path of using "run -h .."
1026 1026
 func TestRunFullHostnameSet(t *testing.T) {
1027
+	defer deleteAllContainers()
1028
+
1027 1029
 	cmd := exec.Command(dockerBinary, "run", "-h", "foo.bar.baz", "busybox", "hostname")
1028 1030
 	out, _, err := runCommandWithOutput(cmd)
1029 1031
 	if err != nil {
... ...
@@ -1033,12 +1038,13 @@ func TestRunFullHostnameSet(t *testing.T) {
1033 1033
 	if actual := strings.Trim(out, "\r\n"); actual != "foo.bar.baz" {
1034 1034
 		t.Fatalf("expected hostname 'foo.bar.baz', received %s", actual)
1035 1035
 	}
1036
-	deleteAllContainers()
1037 1036
 
1038 1037
 	logDone("run - test fully qualified hostname set with -h")
1039 1038
 }
1040 1039
 
1041 1040
 func TestRunPrivilegedCanMknod(t *testing.T) {
1041
+	defer deleteAllContainers()
1042
+
1042 1043
 	cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
1043 1044
 	out, _, err := runCommandWithOutput(cmd)
1044 1045
 	if err != nil {
... ...
@@ -1048,12 +1054,13 @@ func TestRunPrivilegedCanMknod(t *testing.T) {
1048 1048
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
1049 1049
 		t.Fatalf("expected output ok received %s", actual)
1050 1050
 	}
1051
-	deleteAllContainers()
1052 1051
 
1053 1052
 	logDone("run - test privileged can mknod")
1054 1053
 }
1055 1054
 
1056 1055
 func TestRunUnPrivilegedCanMknod(t *testing.T) {
1056
+	defer deleteAllContainers()
1057
+
1057 1058
 	cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
1058 1059
 	out, _, err := runCommandWithOutput(cmd)
1059 1060
 	if err != nil {
... ...
@@ -1063,7 +1070,6 @@ func TestRunUnPrivilegedCanMknod(t *testing.T) {
1063 1063
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
1064 1064
 		t.Fatalf("expected output ok received %s", actual)
1065 1065
 	}
1066
-	deleteAllContainers()
1067 1066
 
1068 1067
 	logDone("run - test un-privileged can mknod")
1069 1068
 }
... ...
@@ -1080,6 +1086,8 @@ func TestRunCapDropInvalid(t *testing.T) {
1080 1080
 }
1081 1081
 
1082 1082
 func TestRunCapDropCannotMknod(t *testing.T) {
1083
+	defer deleteAllContainers()
1084
+
1083 1085
 	cmd := exec.Command(dockerBinary, "run", "--cap-drop=MKNOD", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
1084 1086
 	out, _, err := runCommandWithOutput(cmd)
1085 1087
 	if err == nil {
... ...
@@ -1089,12 +1097,13 @@ func TestRunCapDropCannotMknod(t *testing.T) {
1089 1089
 	if actual := strings.Trim(out, "\r\n"); actual == "ok" {
1090 1090
 		t.Fatalf("expected output not ok received %s", actual)
1091 1091
 	}
1092
-	deleteAllContainers()
1093 1092
 
1094 1093
 	logDone("run - test --cap-drop=MKNOD cannot mknod")
1095 1094
 }
1096 1095
 
1097 1096
 func TestRunCapDropCannotMknodLowerCase(t *testing.T) {
1097
+	defer deleteAllContainers()
1098
+
1098 1099
 	cmd := exec.Command(dockerBinary, "run", "--cap-drop=mknod", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
1099 1100
 	out, _, err := runCommandWithOutput(cmd)
1100 1101
 	if err == nil {
... ...
@@ -1104,12 +1113,13 @@ func TestRunCapDropCannotMknodLowerCase(t *testing.T) {
1104 1104
 	if actual := strings.Trim(out, "\r\n"); actual == "ok" {
1105 1105
 		t.Fatalf("expected output not ok received %s", actual)
1106 1106
 	}
1107
-	deleteAllContainers()
1108 1107
 
1109 1108
 	logDone("run - test --cap-drop=mknod cannot mknod lowercase")
1110 1109
 }
1111 1110
 
1112 1111
 func TestRunCapDropALLCannotMknod(t *testing.T) {
1112
+	defer deleteAllContainers()
1113
+
1113 1114
 	cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "--cap-add=SETGID", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
1114 1115
 	out, _, err := runCommandWithOutput(cmd)
1115 1116
 	if err == nil {
... ...
@@ -1119,12 +1129,13 @@ func TestRunCapDropALLCannotMknod(t *testing.T) {
1119 1119
 	if actual := strings.Trim(out, "\r\n"); actual == "ok" {
1120 1120
 		t.Fatalf("expected output not ok received %s", actual)
1121 1121
 	}
1122
-	deleteAllContainers()
1123 1122
 
1124 1123
 	logDone("run - test --cap-drop=ALL cannot mknod")
1125 1124
 }
1126 1125
 
1127 1126
 func TestRunCapDropALLAddMknodCanMknod(t *testing.T) {
1127
+	defer deleteAllContainers()
1128
+
1128 1129
 	cmd := exec.Command(dockerBinary, "run", "--cap-drop=ALL", "--cap-add=MKNOD", "--cap-add=SETGID", "busybox", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok")
1129 1130
 	out, _, err := runCommandWithOutput(cmd)
1130 1131
 	if err != nil {
... ...
@@ -1134,7 +1145,6 @@ func TestRunCapDropALLAddMknodCanMknod(t *testing.T) {
1134 1134
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
1135 1135
 		t.Fatalf("expected output ok received %s", actual)
1136 1136
 	}
1137
-	deleteAllContainers()
1138 1137
 
1139 1138
 	logDone("run - test --cap-drop=ALL --cap-add=MKNOD can mknod")
1140 1139
 }
... ...
@@ -1152,6 +1162,8 @@ func TestRunCapAddInvalid(t *testing.T) {
1152 1152
 }
1153 1153
 
1154 1154
 func TestRunCapAddCanDownInterface(t *testing.T) {
1155
+	defer deleteAllContainers()
1156
+
1155 1157
 	cmd := exec.Command(dockerBinary, "run", "--cap-add=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
1156 1158
 	out, _, err := runCommandWithOutput(cmd)
1157 1159
 	if err != nil {
... ...
@@ -1161,12 +1173,13 @@ func TestRunCapAddCanDownInterface(t *testing.T) {
1161 1161
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
1162 1162
 		t.Fatalf("expected output ok received %s", actual)
1163 1163
 	}
1164
-	deleteAllContainers()
1165 1164
 
1166 1165
 	logDone("run - test --cap-add=NET_ADMIN can set eth0 down")
1167 1166
 }
1168 1167
 
1169 1168
 func TestRunCapAddALLCanDownInterface(t *testing.T) {
1169
+	defer deleteAllContainers()
1170
+
1170 1171
 	cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
1171 1172
 	out, _, err := runCommandWithOutput(cmd)
1172 1173
 	if err != nil {
... ...
@@ -1176,12 +1189,13 @@ func TestRunCapAddALLCanDownInterface(t *testing.T) {
1176 1176
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
1177 1177
 		t.Fatalf("expected output ok received %s", actual)
1178 1178
 	}
1179
-	deleteAllContainers()
1180 1179
 
1181 1180
 	logDone("run - test --cap-add=ALL can set eth0 down")
1182 1181
 }
1183 1182
 
1184 1183
 func TestRunCapAddALLDropNetAdminCanDownInterface(t *testing.T) {
1184
+	defer deleteAllContainers()
1185
+
1185 1186
 	cmd := exec.Command(dockerBinary, "run", "--cap-add=ALL", "--cap-drop=NET_ADMIN", "busybox", "sh", "-c", "ip link set eth0 down && echo ok")
1186 1187
 	out, _, err := runCommandWithOutput(cmd)
1187 1188
 	if err == nil {
... ...
@@ -1191,14 +1205,14 @@ func TestRunCapAddALLDropNetAdminCanDownInterface(t *testing.T) {
1191 1191
 	if actual := strings.Trim(out, "\r\n"); actual == "ok" {
1192 1192
 		t.Fatalf("expected output not ok received %s", actual)
1193 1193
 	}
1194
-	deleteAllContainers()
1195 1194
 
1196 1195
 	logDone("run - test --cap-add=ALL --cap-drop=NET_ADMIN cannot set eth0 down")
1197 1196
 }
1198 1197
 
1199 1198
 func TestRunPrivilegedCanMount(t *testing.T) {
1200
-	cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
1199
+	defer deleteAllContainers()
1201 1200
 
1201
+	cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
1202 1202
 	out, _, err := runCommandWithOutput(cmd)
1203 1203
 	if err != nil {
1204 1204
 		t.Fatal(err)
... ...
@@ -1207,14 +1221,14 @@ func TestRunPrivilegedCanMount(t *testing.T) {
1207 1207
 	if actual := strings.Trim(out, "\r\n"); actual != "ok" {
1208 1208
 		t.Fatalf("expected output ok received %s", actual)
1209 1209
 	}
1210
-	deleteAllContainers()
1211 1210
 
1212 1211
 	logDone("run - test privileged can mount")
1213 1212
 }
1214 1213
 
1215 1214
 func TestRunUnPrivilegedCannotMount(t *testing.T) {
1216
-	cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
1215
+	defer deleteAllContainers()
1217 1216
 
1217
+	cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "mount -t tmpfs none /tmp && echo ok")
1218 1218
 	out, _, err := runCommandWithOutput(cmd)
1219 1219
 	if err == nil {
1220 1220
 		t.Fatal(err, out)
... ...
@@ -1223,69 +1237,68 @@ func TestRunUnPrivilegedCannotMount(t *testing.T) {
1223 1223
 	if actual := strings.Trim(out, "\r\n"); actual == "ok" {
1224 1224
 		t.Fatalf("expected output not ok received %s", actual)
1225 1225
 	}
1226
-	deleteAllContainers()
1227 1226
 
1228 1227
 	logDone("run - test un-privileged cannot mount")
1229 1228
 }
1230 1229
 
1231 1230
 func TestRunSysNotWritableInNonPrivilegedContainers(t *testing.T) {
1231
+	defer deleteAllContainers()
1232
+
1232 1233
 	cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/sys/kernel/profiling")
1233 1234
 	if code, err := runCommand(cmd); err == nil || code == 0 {
1234 1235
 		t.Fatal("sys should not be writable in a non privileged container")
1235 1236
 	}
1236 1237
 
1237
-	deleteAllContainers()
1238
-
1239 1238
 	logDone("run - sys not writable in non privileged container")
1240 1239
 }
1241 1240
 
1242 1241
 func TestRunSysWritableInPrivilegedContainers(t *testing.T) {
1242
+	defer deleteAllContainers()
1243
+
1243 1244
 	cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/sys/kernel/profiling")
1244 1245
 	if code, err := runCommand(cmd); err != nil || code != 0 {
1245 1246
 		t.Fatalf("sys should be writable in privileged container")
1246 1247
 	}
1247 1248
 
1248
-	deleteAllContainers()
1249
-
1250 1249
 	logDone("run - sys writable in privileged container")
1251 1250
 }
1252 1251
 
1253 1252
 func TestRunProcNotWritableInNonPrivilegedContainers(t *testing.T) {
1253
+	defer deleteAllContainers()
1254
+
1254 1255
 	cmd := exec.Command(dockerBinary, "run", "busybox", "touch", "/proc/sysrq-trigger")
1255 1256
 	if code, err := runCommand(cmd); err == nil || code == 0 {
1256 1257
 		t.Fatal("proc should not be writable in a non privileged container")
1257 1258
 	}
1258 1259
 
1259
-	deleteAllContainers()
1260
-
1261 1260
 	logDone("run - proc not writable in non privileged container")
1262 1261
 }
1263 1262
 
1264 1263
 func TestRunProcWritableInPrivilegedContainers(t *testing.T) {
1264
+	defer deleteAllContainers()
1265
+
1265 1266
 	cmd := exec.Command(dockerBinary, "run", "--privileged", "busybox", "touch", "/proc/sysrq-trigger")
1266 1267
 	if code, err := runCommand(cmd); err != nil || code != 0 {
1267 1268
 		t.Fatalf("proc should be writable in privileged container")
1268 1269
 	}
1269
-
1270
-	deleteAllContainers()
1271
-
1272 1270
 	logDone("run - proc writable in privileged container")
1273 1271
 }
1274 1272
 
1275 1273
 func TestRunWithCpuset(t *testing.T) {
1274
+	defer deleteAllContainers()
1275
+
1276 1276
 	cmd := exec.Command(dockerBinary, "run", "--cpuset", "0", "busybox", "true")
1277 1277
 	if code, err := runCommand(cmd); err != nil || code != 0 {
1278 1278
 		t.Fatalf("container should run successfuly with cpuset of 0: %s", err)
1279 1279
 	}
1280 1280
 
1281
-	deleteAllContainers()
1282
-
1283 1281
 	logDone("run - cpuset 0")
1284 1282
 }
1285 1283
 
1286 1284
 func TestRunDeviceNumbers(t *testing.T) {
1287
-	cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "ls -l /dev/null")
1285
+	defer deleteAllContainers()
1288 1286
 
1287
+	cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "ls -l /dev/null")
1289 1288
 	out, _, err := runCommandWithOutput(cmd)
1290 1289
 	if err != nil {
1291 1290
 		t.Fatal(err, out)
... ...
@@ -1299,14 +1312,14 @@ func TestRunDeviceNumbers(t *testing.T) {
1299 1299
 	if !(reflect.DeepEqual(deviceLineFields, expected)) {
1300 1300
 		t.Fatalf("expected output\ncrw-rw-rw- 1 root root 1, 3 May 24 13:29 /dev/null\n received\n %s\n", out)
1301 1301
 	}
1302
-	deleteAllContainers()
1303 1302
 
1304 1303
 	logDone("run - test device numbers")
1305 1304
 }
1306 1305
 
1307 1306
 func TestRunThatCharacterDevicesActLikeCharacterDevices(t *testing.T) {
1308
-	cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero")
1307
+	defer deleteAllContainers()
1309 1308
 
1309
+	cmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", "dd if=/dev/zero of=/zero bs=1k count=5 2> /dev/null ; du -h /zero")
1310 1310
 	out, _, err := runCommandWithOutput(cmd)
1311 1311
 	if err != nil {
1312 1312
 		t.Fatal(err, out)
... ...
@@ -1315,26 +1328,25 @@ func TestRunThatCharacterDevicesActLikeCharacterDevices(t *testing.T) {
1315 1315
 	if actual := strings.Trim(out, "\r\n"); actual[0] == '0' {
1316 1316
 		t.Fatalf("expected a new file called /zero to be create that is greater than 0 bytes long, but du says: %s", actual)
1317 1317
 	}
1318
-	deleteAllContainers()
1319 1318
 
1320 1319
 	logDone("run - test that character devices work.")
1321 1320
 }
1322 1321
 
1323 1322
 func TestRunUnprivilegedWithChroot(t *testing.T) {
1324
-	cmd := exec.Command(dockerBinary, "run", "busybox", "chroot", "/", "true")
1323
+	defer deleteAllContainers()
1325 1324
 
1325
+	cmd := exec.Command(dockerBinary, "run", "busybox", "chroot", "/", "true")
1326 1326
 	if _, err := runCommand(cmd); err != nil {
1327 1327
 		t.Fatal(err)
1328 1328
 	}
1329 1329
 
1330
-	deleteAllContainers()
1331
-
1332 1330
 	logDone("run - unprivileged with chroot")
1333 1331
 }
1334 1332
 
1335 1333
 func TestRunAddingOptionalDevices(t *testing.T) {
1336
-	cmd := exec.Command(dockerBinary, "run", "--device", "/dev/zero:/dev/nulo", "busybox", "sh", "-c", "ls /dev/nulo")
1334
+	defer deleteAllContainers()
1337 1335
 
1336
+	cmd := exec.Command(dockerBinary, "run", "--device", "/dev/zero:/dev/nulo", "busybox", "sh", "-c", "ls /dev/nulo")
1338 1337
 	out, _, err := runCommandWithOutput(cmd)
1339 1338
 	if err != nil {
1340 1339
 		t.Fatal(err, out)
... ...
@@ -1343,14 +1355,14 @@ func TestRunAddingOptionalDevices(t *testing.T) {
1343 1343
 	if actual := strings.Trim(out, "\r\n"); actual != "/dev/nulo" {
1344 1344
 		t.Fatalf("expected output /dev/nulo, received %s", actual)
1345 1345
 	}
1346
-	deleteAllContainers()
1347 1346
 
1348 1347
 	logDone("run - test --device argument")
1349 1348
 }
1350 1349
 
1351 1350
 func TestRunModeHostname(t *testing.T) {
1352
-	cmd := exec.Command(dockerBinary, "run", "-h=testhostname", "busybox", "cat", "/etc/hostname")
1351
+	defer deleteAllContainers()
1353 1352
 
1353
+	cmd := exec.Command(dockerBinary, "run", "-h=testhostname", "busybox", "cat", "/etc/hostname")
1354 1354
 	out, _, err := runCommandWithOutput(cmd)
1355 1355
 	if err != nil {
1356 1356
 		t.Fatal(err, out)
... ...
@@ -1374,12 +1386,12 @@ func TestRunModeHostname(t *testing.T) {
1374 1374
 		t.Fatalf("expected %q, but says: %q", hostname, actual)
1375 1375
 	}
1376 1376
 
1377
-	deleteAllContainers()
1378
-
1379 1377
 	logDone("run - hostname and several network modes")
1380 1378
 }
1381 1379
 
1382 1380
 func TestRunRootWorkdir(t *testing.T) {
1381
+	defer deleteAllContainers()
1382
+
1383 1383
 	s, _, err := dockerCmd(t, "run", "--workdir", "/", "busybox", "pwd")
1384 1384
 	if err != nil {
1385 1385
 		t.Fatal(s, err)
... ...
@@ -1388,36 +1400,35 @@ func TestRunRootWorkdir(t *testing.T) {
1388 1388
 		t.Fatalf("pwd returned %q (expected /\\n)", s)
1389 1389
 	}
1390 1390
 
1391
-	deleteAllContainers()
1392
-
1393 1391
 	logDone("run - workdir /")
1394 1392
 }
1395 1393
 
1396 1394
 func TestRunAllowBindMountingRoot(t *testing.T) {
1395
+	defer deleteAllContainers()
1396
+
1397 1397
 	s, _, err := dockerCmd(t, "run", "-v", "/:/host", "busybox", "ls", "/host")
1398 1398
 	if err != nil {
1399 1399
 		t.Fatal(s, err)
1400 1400
 	}
1401 1401
 
1402
-	deleteAllContainers()
1403
-
1404 1402
 	logDone("run - bind mount / as volume")
1405 1403
 }
1406 1404
 
1407 1405
 func TestRunDisallowBindMountingRootToRoot(t *testing.T) {
1406
+	defer deleteAllContainers()
1407
+
1408 1408
 	cmd := exec.Command(dockerBinary, "run", "-v", "/:/", "busybox", "ls", "/host")
1409 1409
 	out, _, err := runCommandWithOutput(cmd)
1410 1410
 	if err == nil {
1411 1411
 		t.Fatal(out, err)
1412 1412
 	}
1413 1413
 
1414
-	deleteAllContainers()
1415
-
1416 1414
 	logDone("run - bind mount /:/ as volume should not work")
1417 1415
 }
1418 1416
 
1419 1417
 // Verify that a container gets default DNS when only localhost resolvers exist
1420 1418
 func TestRunDnsDefaultOptions(t *testing.T) {
1419
+	defer deleteAllContainers()
1421 1420
 
1422 1421
 	// preserve original resolv.conf for restoring after test
1423 1422
 	origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
... ...
@@ -1454,12 +1465,12 @@ func TestRunDnsDefaultOptions(t *testing.T) {
1454 1454
 		t.Fatalf("expected resolv.conf be: %q, but was: %q", expected, actual)
1455 1455
 	}
1456 1456
 
1457
-	deleteAllContainers()
1458
-
1459 1457
 	logDone("run - dns default options")
1460 1458
 }
1461 1459
 
1462 1460
 func TestRunDnsOptions(t *testing.T) {
1461
+	defer deleteAllContainers()
1462
+
1463 1463
 	cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "--dns-search=mydomain", "busybox", "cat", "/etc/resolv.conf")
1464 1464
 
1465 1465
 	out, _, err := runCommandWithOutput(cmd)
... ...
@@ -1488,6 +1499,8 @@ func TestRunDnsOptions(t *testing.T) {
1488 1488
 }
1489 1489
 
1490 1490
 func TestRunDnsOptionsBasedOnHostResolvConf(t *testing.T) {
1491
+	defer deleteAllContainers()
1492
+
1491 1493
 	var out string
1492 1494
 
1493 1495
 	origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
... ...
@@ -1577,8 +1590,7 @@ func TestRunDnsOptionsBasedOnHostResolvConf(t *testing.T) {
1577 1577
 			t.Fatalf("expected %q domain, but says: %q", actualSearch[i], hostSearch[i])
1578 1578
 		}
1579 1579
 	}
1580
-
1581
-	deleteAllContainers()
1580
+	defer deleteAllContainers()
1582 1581
 
1583 1582
 	logDone("run - dns options based on host resolv.conf")
1584 1583
 }
... ...
@@ -1753,8 +1765,9 @@ func TestRunAddHost(t *testing.T) {
1753 1753
 
1754 1754
 // Regression test for #6983
1755 1755
 func TestRunAttachStdErrOnlyTTYMode(t *testing.T) {
1756
-	cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stderr", "busybox", "true")
1756
+	defer deleteAllContainers()
1757 1757
 
1758
+	cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stderr", "busybox", "true")
1758 1759
 	exitCode, err := runCommand(cmd)
1759 1760
 	if err != nil {
1760 1761
 		t.Fatal(err)
... ...
@@ -1762,13 +1775,13 @@ func TestRunAttachStdErrOnlyTTYMode(t *testing.T) {
1762 1762
 		t.Fatalf("Container should have exited with error code 0")
1763 1763
 	}
1764 1764
 
1765
-	deleteAllContainers()
1766
-
1767 1765
 	logDone("run - Attach stderr only with -t")
1768 1766
 }
1769 1767
 
1770 1768
 // Regression test for #6983
1771 1769
 func TestRunAttachStdOutOnlyTTYMode(t *testing.T) {
1770
+	defer deleteAllContainers()
1771
+
1772 1772
 	cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "busybox", "true")
1773 1773
 
1774 1774
 	exitCode, err := runCommand(cmd)
... ...
@@ -1778,15 +1791,14 @@ func TestRunAttachStdOutOnlyTTYMode(t *testing.T) {
1778 1778
 		t.Fatalf("Container should have exited with error code 0")
1779 1779
 	}
1780 1780
 
1781
-	deleteAllContainers()
1782
-
1783 1781
 	logDone("run - Attach stdout only with -t")
1784 1782
 }
1785 1783
 
1786 1784
 // Regression test for #6983
1787 1785
 func TestRunAttachStdOutAndErrTTYMode(t *testing.T) {
1788
-	cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "-a", "stderr", "busybox", "true")
1786
+	defer deleteAllContainers()
1789 1787
 
1788
+	cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stdout", "-a", "stderr", "busybox", "true")
1790 1789
 	exitCode, err := runCommand(cmd)
1791 1790
 	if err != nil {
1792 1791
 		t.Fatal(err)
... ...
@@ -1794,8 +1806,6 @@ func TestRunAttachStdOutAndErrTTYMode(t *testing.T) {
1794 1794
 		t.Fatalf("Container should have exited with error code 0")
1795 1795
 	}
1796 1796
 
1797
-	deleteAllContainers()
1798
-
1799 1797
 	logDone("run - Attach stderr and stdout with -t")
1800 1798
 }
1801 1799
 
... ...
@@ -1805,7 +1815,6 @@ func TestRunAttachWithDettach(t *testing.T) {
1805 1805
 	defer deleteAllContainers()
1806 1806
 
1807 1807
 	cmd := exec.Command(dockerBinary, "run", "-d", "--attach", "stdout", "busybox", "true")
1808
-
1809 1808
 	_, stderr, _, err := runCommandWithStdoutStderr(cmd)
1810 1809
 	if err == nil {
1811 1810
 		t.Fatalf("Container should have exited with error code different than 0", err)
... ...
@@ -2232,6 +2241,8 @@ func TestRunBindMounts(t *testing.T) {
2232 2232
 // Ensure that CIDFile gets deleted if it's empty
2233 2233
 // Perform this test by making `docker run` fail
2234 2234
 func TestRunCidFileCleanupIfEmpty(t *testing.T) {
2235
+	defer deleteAllContainers()
2236
+
2235 2237
 	tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
2236 2238
 	if err != nil {
2237 2239
 		t.Fatal(err)
... ...
@@ -2248,7 +2259,6 @@ func TestRunCidFileCleanupIfEmpty(t *testing.T) {
2248 2248
 	if _, err := os.Stat(tmpCidFile); err == nil {
2249 2249
 		t.Fatalf("empty CIDFile %q should've been deleted", tmpCidFile)
2250 2250
 	}
2251
-	deleteAllContainers()
2252 2251
 	logDone("run - cleanup empty cidfile on error")
2253 2252
 }
2254 2253
 
... ...
@@ -2256,6 +2266,8 @@ func TestRunCidFileCleanupIfEmpty(t *testing.T) {
2256 2256
 //sudo docker run --cidfile /tmp/docker_test.cid ubuntu echo "test"
2257 2257
 // TestRunCidFile tests that run --cidfile returns the longid
2258 2258
 func TestRunCidFileCheckIDLength(t *testing.T) {
2259
+	defer deleteAllContainers()
2260
+
2259 2261
 	tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
2260 2262
 	if err != nil {
2261 2263
 		t.Fatal(err)
... ...
@@ -2279,11 +2291,13 @@ func TestRunCidFileCheckIDLength(t *testing.T) {
2279 2279
 	if cid != id {
2280 2280
 		t.Fatalf("cid must be equal to %s, got %s", id, cid)
2281 2281
 	}
2282
-	deleteAllContainers()
2282
+
2283 2283
 	logDone("run - cidfile contains long id")
2284 2284
 }
2285 2285
 
2286 2286
 func TestRunNetworkNotInitializedNoneMode(t *testing.T) {
2287
+	defer deleteAllContainers()
2288
+
2287 2289
 	cmd := exec.Command(dockerBinary, "run", "-d", "--net=none", "busybox", "top")
2288 2290
 	out, _, err := runCommandWithOutput(cmd)
2289 2291
 	if err != nil {
... ...
@@ -2297,7 +2311,7 @@ func TestRunNetworkNotInitializedNoneMode(t *testing.T) {
2297 2297
 	if res != "" {
2298 2298
 		t.Fatalf("For 'none' mode network must not be initialized, but container got IP: %s", res)
2299 2299
 	}
2300
-	deleteAllContainers()
2300
+
2301 2301
 	logDone("run - network must not be initialized in 'none' mode")
2302 2302
 }
2303 2303
 
... ...
@@ -2319,6 +2333,8 @@ func TestRunSetMacAddress(t *testing.T) {
2319 2319
 }
2320 2320
 
2321 2321
 func TestRunInspectMacAddress(t *testing.T) {
2322
+	defer deleteAllContainers()
2323
+
2322 2324
 	mac := "12:34:56:78:9a:bc"
2323 2325
 	cmd := exec.Command(dockerBinary, "run", "-d", "--mac-address="+mac, "busybox", "top")
2324 2326
 	out, _, err := runCommandWithOutput(cmd)
... ...
@@ -2333,11 +2349,13 @@ func TestRunInspectMacAddress(t *testing.T) {
2333 2333
 	if inspectedMac != mac {
2334 2334
 		t.Fatalf("docker inspect outputs wrong MAC address: %q, should be: %q", inspectedMac, mac)
2335 2335
 	}
2336
-	deleteAllContainers()
2336
+
2337 2337
 	logDone("run - inspecting MAC address")
2338 2338
 }
2339 2339
 
2340 2340
 func TestRunDeallocatePortOnMissingIptablesRule(t *testing.T) {
2341
+	defer deleteAllContainers()
2342
+
2341 2343
 	cmd := exec.Command(dockerBinary, "run", "-d", "-p", "23:23", "busybox", "top")
2342 2344
 	out, _, err := runCommandWithOutput(cmd)
2343 2345
 	if err != nil {
... ...
@@ -2362,11 +2380,13 @@ func TestRunDeallocatePortOnMissingIptablesRule(t *testing.T) {
2362 2362
 	if err != nil {
2363 2363
 		t.Fatal(err, out)
2364 2364
 	}
2365
-	deleteAllContainers()
2365
+
2366 2366
 	logDone("run - port should be deallocated even on iptables error")
2367 2367
 }
2368 2368
 
2369 2369
 func TestRunPortInUse(t *testing.T) {
2370
+	defer deleteAllContainers()
2371
+
2370 2372
 	port := "1234"
2371 2373
 	l, err := net.Listen("tcp", ":"+port)
2372 2374
 	if err != nil {
... ...
@@ -2382,7 +2402,6 @@ func TestRunPortInUse(t *testing.T) {
2382 2382
 		t.Fatalf("Out must be about \"address already in use\", got %s", out)
2383 2383
 	}
2384 2384
 
2385
-	deleteAllContainers()
2386 2385
 	logDone("run - error out if port already in use")
2387 2386
 }
2388 2387
 
... ...
@@ -2421,6 +2440,8 @@ func TestRunPortProxy(t *testing.T) {
2421 2421
 
2422 2422
 // Regression test for #7792
2423 2423
 func TestRunMountOrdering(t *testing.T) {
2424
+	defer deleteAllContainers()
2425
+
2424 2426
 	tmpDir, err := ioutil.TempDir("", "docker_nested_mount_test")
2425 2427
 	if err != nil {
2426 2428
 		t.Fatal(err)
... ...
@@ -2457,12 +2478,13 @@ func TestRunMountOrdering(t *testing.T) {
2457 2457
 		t.Fatal(out, err)
2458 2458
 	}
2459 2459
 
2460
-	deleteAllContainers()
2461 2460
 	logDone("run - volumes are mounted in the correct order")
2462 2461
 }
2463 2462
 
2464 2463
 // Regression test for https://github.com/docker/docker/issues/8259
2465 2464
 func TestRunReuseBindVolumeThatIsSymlink(t *testing.T) {
2465
+	defer deleteAllContainers()
2466
+
2466 2467
 	tmpDir, err := ioutil.TempDir(os.TempDir(), "testlink")
2467 2468
 	if err != nil {
2468 2469
 		t.Fatal(err)
... ...
@@ -2488,12 +2510,13 @@ func TestRunReuseBindVolumeThatIsSymlink(t *testing.T) {
2488 2488
 		t.Fatal(err, out)
2489 2489
 	}
2490 2490
 
2491
-	deleteAllContainers()
2492 2491
 	logDone("run - can remount old bindmount volume")
2493 2492
 }
2494 2493
 
2495 2494
 //test create /etc volume
2496 2495
 func TestRunCreateVolumeEtc(t *testing.T) {
2496
+	defer deleteAllContainers()
2497
+
2497 2498
 	cmd := exec.Command(dockerBinary, "run", "--dns=127.0.0.1", "-v", "/etc", "busybox", "cat", "/etc/resolv.conf")
2498 2499
 	out, _, err := runCommandWithOutput(cmd)
2499 2500
 	if err != nil {
... ...
@@ -2522,8 +2545,6 @@ func TestRunCreateVolumeEtc(t *testing.T) {
2522 2522
 		t.Fatal("failed: create /etc volume cover /etc/hosts", out)
2523 2523
 	}
2524 2524
 
2525
-	deleteAllContainers()
2526
-
2527 2525
 	logDone("run - create /etc volume success")
2528 2526
 }
2529 2527
 
... ...
@@ -2686,6 +2707,8 @@ func TestRunSlowStdoutConsumer(t *testing.T) {
2686 2686
 }
2687 2687
 
2688 2688
 func TestRunAllowPortRangeThroughExpose(t *testing.T) {
2689
+	defer deleteAllContainers()
2690
+
2689 2691
 	cmd := exec.Command(dockerBinary, "run", "-d", "--expose", "3000-3003", "-P", "busybox", "top")
2690 2692
 	out, _, err := runCommandWithOutput(cmd)
2691 2693
 	if err != nil {
... ...
@@ -2715,6 +2738,8 @@ func TestRunAllowPortRangeThroughExpose(t *testing.T) {
2715 2715
 
2716 2716
 // test docker run expose a invalid port
2717 2717
 func TestRunExposePort(t *testing.T) {
2718
+	defer deleteAllContainers()
2719
+
2718 2720
 	runCmd := exec.Command(dockerBinary, "run", "--expose", "80000", "busybox")
2719 2721
 	out, _, err := runCommandWithOutput(runCmd)
2720 2722
 	//expose a invalid port should with a error out
... ...
@@ -2722,8 +2747,6 @@ func TestRunExposePort(t *testing.T) {
2722 2722
 		t.Fatalf("run --expose a invalid port should with error out")
2723 2723
 	}
2724 2724
 
2725
-	deleteAllContainers()
2726
-
2727 2725
 	logDone("run - can't expose a invalid port")
2728 2726
 }
2729 2727
 
... ...
@@ -2755,6 +2778,8 @@ func TestRunUnknownCommand(t *testing.T) {
2755 2755
 }
2756 2756
 
2757 2757
 func TestRunModeIpcHost(t *testing.T) {
2758
+	defer deleteAllContainers()
2759
+
2758 2760
 	hostIpc, err := os.Readlink("/proc/1/ns/ipc")
2759 2761
 	if err != nil {
2760 2762
 		t.Fatal(err)
... ...
@@ -2781,12 +2806,13 @@ func TestRunModeIpcHost(t *testing.T) {
2781 2781
 	if hostIpc == out2 {
2782 2782
 		t.Fatalf("IPC should be different without --ipc=host %s == %s\n", hostIpc, out2)
2783 2783
 	}
2784
-	deleteAllContainers()
2785 2784
 
2786 2785
 	logDone("run - ipc host mode")
2787 2786
 }
2788 2787
 
2789 2788
 func TestRunModeIpcContainer(t *testing.T) {
2789
+	defer deleteAllContainers()
2790
+
2790 2791
 	cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
2791 2792
 	out, _, err := runCommandWithOutput(cmd)
2792 2793
 	if err != nil {
... ...
@@ -2819,12 +2845,13 @@ func TestRunModeIpcContainer(t *testing.T) {
2819 2819
 	if parentContainerIpc != out2 {
2820 2820
 		t.Fatalf("IPC different with --ipc=container:%s %s != %s\n", id, parentContainerIpc, out2)
2821 2821
 	}
2822
-	deleteAllContainers()
2823 2822
 
2824 2823
 	logDone("run - ipc container mode")
2825 2824
 }
2826 2825
 
2827 2826
 func TestContainerNetworkMode(t *testing.T) {
2827
+	defer deleteAllContainers()
2828
+
2828 2829
 	cmd := exec.Command(dockerBinary, "run", "-d", "busybox", "top")
2829 2830
 	out, _, err := runCommandWithOutput(cmd)
2830 2831
 	if err != nil {
... ...
@@ -2853,12 +2880,13 @@ func TestContainerNetworkMode(t *testing.T) {
2853 2853
 	if parentContainerNet != out2 {
2854 2854
 		t.Fatalf("NET different with --net=container:%s %s != %s\n", id, parentContainerNet, out2)
2855 2855
 	}
2856
-	deleteAllContainers()
2857 2856
 
2858 2857
 	logDone("run - container shared network namespace")
2859 2858
 }
2860 2859
 
2861 2860
 func TestRunModePidHost(t *testing.T) {
2861
+	defer deleteAllContainers()
2862
+
2862 2863
 	hostPid, err := os.Readlink("/proc/1/ns/pid")
2863 2864
 	if err != nil {
2864 2865
 		t.Fatal(err)
... ...
@@ -2885,7 +2913,6 @@ func TestRunModePidHost(t *testing.T) {
2885 2885
 	if hostPid == out2 {
2886 2886
 		t.Fatalf("PID should be different without --pid=host %s == %s\n", hostPid, out2)
2887 2887
 	}
2888
-	deleteAllContainers()
2889 2888
 
2890 2889
 	logDone("run - pid host mode")
2891 2890
 }
... ...
@@ -3034,9 +3061,10 @@ func TestRunNetHost(t *testing.T) {
3034 3034
 }
3035 3035
 
3036 3036
 func TestRunAllowPortRangeThroughPublish(t *testing.T) {
3037
+	defer deleteAllContainers()
3038
+
3037 3039
 	cmd := exec.Command(dockerBinary, "run", "-d", "--expose", "3000-3003", "-p", "3000-3003", "busybox", "top")
3038 3040
 	out, _, err := runCommandWithOutput(cmd)
3039
-	defer deleteAllContainers()
3040 3041
 
3041 3042
 	id := strings.TrimSpace(out)
3042 3043
 	portstr, err := inspectFieldJSON(id, "NetworkSettings.Ports")
... ...
@@ -56,6 +56,8 @@ func TestRunRedirectStdout(t *testing.T) {
56 56
 
57 57
 // Test recursive bind mount works by default
58 58
 func TestRunWithVolumesIsRecursive(t *testing.T) {
59
+	defer deleteAllContainers()
60
+
59 61
 	tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
60 62
 	if err != nil {
61 63
 		t.Fatal(err)
... ...
@@ -87,7 +89,5 @@ func TestRunWithVolumesIsRecursive(t *testing.T) {
87 87
 		t.Fatal("Recursive bind mount test failed. Expected file not found")
88 88
 	}
89 89
 
90
-	deleteAllContainers()
91
-
92 90
 	logDone("run - volumes are bind mounted recursively")
93 91
 }