Browse code

Merge pull request #13279 from hqhq/hq_use_inspectfield

Use inspectField to simplify code

Brian Goff authored on 2015/05/19 03:53:37
Showing 9 changed files
... ...
@@ -85,16 +85,11 @@ func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
85 85
 		c.Fatalf("failed to commit container to image: %s, %v", out, err)
86 86
 	}
87 87
 
88
-	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Paused}}", cleanedContainerID)
89
-	out, _, _, err = runCommandWithStdoutStderr(cmd)
90
-	if err != nil {
91
-		c.Fatalf("failed to inspect container: %v, output: %q", err, out)
92
-	}
93
-
88
+	out, err = inspectField(cleanedContainerID, "State.Paused")
89
+	c.Assert(err, check.IsNil)
94 90
 	if !strings.Contains(out, "true") {
95 91
 		c.Fatalf("commit should not unpause a paused container")
96 92
 	}
97
-
98 93
 }
99 94
 
100 95
 func (s *DockerSuite) TestCommitNewFile(c *check.C) {
... ...
@@ -242,9 +237,7 @@ func (s *DockerSuite) TestCommitChange(c *check.C) {
242 242
 
243 243
 	for conf, value := range expected {
244 244
 		res, err := inspectField(imageId, conf)
245
-		if err != nil {
246
-			c.Errorf("failed to get value %s, error: %s", conf, err)
247
-		}
245
+		c.Assert(err, check.IsNil)
248 246
 		if res != value {
249 247
 			c.Errorf("%s('%s'), expected %s", conf, res, value)
250 248
 		}
... ...
@@ -12,13 +12,10 @@ import (
12 12
 func (s *DockerSuite) TestInspectImage(c *check.C) {
13 13
 	imageTest := "emptyfs"
14 14
 	imageTestID := "511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158"
15
-	imagesCmd := exec.Command(dockerBinary, "inspect", "--format='{{.Id}}'", imageTest)
16
-	out, exitCode, err := runCommandWithOutput(imagesCmd)
17
-	if exitCode != 0 || err != nil {
18
-		c.Fatalf("failed to inspect image: %s, %v", out, err)
19
-	}
15
+	id, err := inspectField(imageTest, "Id")
16
+	c.Assert(err, check.IsNil)
20 17
 
21
-	if id := strings.TrimSuffix(out, "\n"); id != imageTestID {
18
+	if id != imageTestID {
22 19
 		c.Fatalf("Expected id: %s for image: %s but received id: %s", imageTestID, imageTest, id)
23 20
 	}
24 21
 
... ...
@@ -33,33 +30,28 @@ func (s *DockerSuite) TestInspectInt64(c *check.C) {
33 33
 
34 34
 	out = strings.TrimSpace(out)
35 35
 
36
-	inspectCmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.Memory}}", out)
37
-	inspectOut, _, err := runCommandWithOutput(inspectCmd)
38
-	if err != nil {
39
-		c.Fatalf("failed to inspect container: %v, output: %q", err, inspectOut)
40
-	}
36
+	inspectOut, err := inspectField(out, "HostConfig.Memory")
37
+	c.Assert(err, check.IsNil)
41 38
 
42
-	if strings.TrimSpace(inspectOut) != "314572800" {
39
+	if inspectOut != "314572800" {
43 40
 		c.Fatalf("inspect got wrong value, got: %q, expected: 314572800", inspectOut)
44 41
 	}
45 42
 }
46 43
 
47 44
 func (s *DockerSuite) TestInspectImageFilterInt(c *check.C) {
48 45
 	imageTest := "emptyfs"
49
-	imagesCmd := exec.Command(dockerBinary, "inspect", "--format='{{.Size}}'", imageTest)
50
-	out, exitCode, err := runCommandWithOutput(imagesCmd)
51
-	if exitCode != 0 || err != nil {
52
-		c.Fatalf("failed to inspect image: %s, %v", out, err)
53
-	}
54
-	size, err := strconv.Atoi(strings.TrimSuffix(out, "\n"))
46
+	out, err := inspectField(imageTest, "Size")
47
+	c.Assert(err, check.IsNil)
48
+
49
+	size, err := strconv.Atoi(out)
55 50
 	if err != nil {
56 51
 		c.Fatalf("failed to inspect size of the image: %s, %v", out, err)
57 52
 	}
58 53
 
59 54
 	//now see if the size turns out to be the same
60 55
 	formatStr := fmt.Sprintf("--format='{{eq .Size %d}}'", size)
61
-	imagesCmd = exec.Command(dockerBinary, "inspect", formatStr, imageTest)
62
-	out, exitCode, err = runCommandWithOutput(imagesCmd)
56
+	imagesCmd := exec.Command(dockerBinary, "inspect", formatStr, imageTest)
57
+	out, exitCode, err := runCommandWithOutput(imagesCmd)
63 58
 	if exitCode != 0 || err != nil {
64 59
 		c.Fatalf("failed to inspect image: %s, %v", out, err)
65 60
 	}
... ...
@@ -77,12 +69,10 @@ func (s *DockerSuite) TestInspectContainerFilterInt(c *check.C) {
77 77
 
78 78
 	id := strings.TrimSpace(out)
79 79
 
80
-	runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.ExitCode}}'", id)
81
-	out, _, err = runCommandWithOutput(runCmd)
82
-	if err != nil {
83
-		c.Fatalf("failed to inspect container: %s, %v", out, err)
84
-	}
85
-	exitCode, err := strconv.Atoi(strings.TrimSuffix(out, "\n"))
80
+	out, err = inspectField(id, "State.ExitCode")
81
+	c.Assert(err, check.IsNil)
82
+
83
+	exitCode, err := strconv.Atoi(out)
86 84
 	if err != nil {
87 85
 		c.Fatalf("failed to inspect exitcode of the container: %s, %v", out, err)
88 86
 	}
... ...
@@ -53,12 +53,9 @@ func getContainerLogs(c *check.C, containerID string) string {
53 53
 }
54 54
 
55 55
 func getContainerStatus(c *check.C, containerID string) string {
56
-	runCmd := exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", containerID)
57
-	out, _, err := runCommandWithOutput(runCmd)
58
-	if err != nil {
59
-		c.Fatal(out, err)
60
-	}
61
-	return strings.Trim(out, "\r\n")
56
+	out, err := inspectField(containerID, "State.Running")
57
+	c.Assert(err, check.IsNil)
58
+	return out
62 59
 }
63 60
 
64 61
 func (s *DockerSuite) TestNetworkNat(c *check.C) {
... ...
@@ -112,11 +112,8 @@ func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
112 112
 		c.Errorf("expect 1 volume received %s", out)
113 113
 	}
114 114
 
115
-	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
116
-	volumes, _, err := runCommandWithOutput(runCmd)
117
-	if err != nil {
118
-		c.Fatal(volumes, err)
119
-	}
115
+	volumes, err := inspectField(cleanedContainerID, ".Volumes")
116
+	c.Assert(err, check.IsNil)
120 117
 
121 118
 	runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
122 119
 	if out, _, err = runCommandWithOutput(runCmd); err != nil {
... ...
@@ -133,15 +130,10 @@ func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
133 133
 		c.Errorf("expect 1 volume after restart received %s", out)
134 134
 	}
135 135
 
136
-	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
137
-	volumesAfterRestart, _, err := runCommandWithOutput(runCmd)
138
-	if err != nil {
139
-		c.Fatal(volumesAfterRestart, err)
140
-	}
136
+	volumesAfterRestart, err := inspectField(cleanedContainerID, ".Volumes")
137
+	c.Assert(err, check.IsNil)
141 138
 
142 139
 	if volumes != volumesAfterRestart {
143
-		volumes = strings.Trim(volumes, " \n\r")
144
-		volumesAfterRestart = strings.Trim(volumesAfterRestart, " \n\r")
145 140
 		c.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
146 141
 	}
147 142
 
... ...
@@ -157,9 +149,7 @@ func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
157 157
 
158 158
 	id := strings.TrimSpace(string(out))
159 159
 	name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
160
-	if err != nil {
161
-		c.Fatal(err, out)
162
-	}
160
+	c.Assert(err, check.IsNil)
163 161
 	if name != "no" {
164 162
 		c.Fatalf("Container restart policy name is %s, expected %s", name, "no")
165 163
 	}
... ...
@@ -176,17 +166,13 @@ func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
176 176
 
177 177
 	id := strings.TrimSpace(string(out))
178 178
 	name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
179
-	if err != nil {
180
-		c.Fatal(err, out)
181
-	}
179
+	c.Assert(err, check.IsNil)
182 180
 	if name != "always" {
183 181
 		c.Fatalf("Container restart policy name is %s, expected %s", name, "always")
184 182
 	}
185 183
 
186 184
 	MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
187
-	if err != nil {
188
-		c.Fatal(err)
189
-	}
185
+	c.Assert(err, check.IsNil)
190 186
 
191 187
 	// MaximumRetryCount=0 if the restart policy is always
192 188
 	if MaximumRetryCount != "0" {
... ...
@@ -205,9 +191,7 @@ func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
205 205
 
206 206
 	id := strings.TrimSpace(string(out))
207 207
 	name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
208
-	if err != nil {
209
-		c.Fatal(err, out)
210
-	}
208
+	c.Assert(err, check.IsNil)
211 209
 	if name != "on-failure" {
212 210
 		c.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure")
213 211
 	}
... ...
@@ -226,16 +210,12 @@ func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) {
226 226
 		c.Fatal(err)
227 227
 	}
228 228
 	count, err := inspectField(id, "RestartCount")
229
-	if err != nil {
230
-		c.Fatal(err)
231
-	}
229
+	c.Assert(err, check.IsNil)
232 230
 	if count != "0" {
233 231
 		c.Fatalf("Container was restarted %s times, expected %d", count, 0)
234 232
 	}
235 233
 	MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
236
-	if err != nil {
237
-		c.Fatal(err)
238
-	}
234
+	c.Assert(err, check.IsNil)
239 235
 	if MaximumRetryCount != "3" {
240 236
 		c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
241 237
 	}
... ...
@@ -101,8 +101,8 @@ func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
101 101
 			c.Fatalf("tag busybox to create 4 more images with same imageID; docker images shows: %q\n", imagesAfter)
102 102
 		}
103 103
 	}
104
-	out, _ = dockerCmd(c, "inspect", "-f", "{{.Id}}", "busybox-test")
105
-	imgID := strings.TrimSpace(out)
104
+	imgID, err := inspectField("busybox-test", "Id")
105
+	c.Assert(err, check.IsNil)
106 106
 
107 107
 	// first checkout without force it fails
108 108
 	runCmd = exec.Command(dockerBinary, "rmi", imgID)
... ...
@@ -87,7 +87,7 @@ func (s *DockerSuite) TestRunEchoStdoutWithCPUAndMemoryLimit(c *check.C) {
87 87
 }
88 88
 
89 89
 // "test" should be printed
90
-func (s *DockerSuite) TestRunEchoStdoutWitCPUQuota(c *check.C) {
90
+func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
91 91
 	runCmd := exec.Command(dockerBinary, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test")
92 92
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
93 93
 	if err != nil {
... ...
@@ -101,12 +101,9 @@ func (s *DockerSuite) TestRunEchoStdoutWitCPUQuota(c *check.C) {
101 101
 		c.Errorf("container should've printed 'test'")
102 102
 	}
103 103
 
104
-	cmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.CpuQuota}}", "test")
105
-	out, _, err = runCommandWithOutput(cmd)
106
-	if err != nil {
107
-		c.Fatalf("failed to inspect container: %s, %v", out, err)
108
-	}
109
-	out = strings.TrimSpace(out)
104
+	out, err = inspectField("test", "HostConfig.CpuQuota")
105
+	c.Assert(err, check.IsNil)
106
+
110 107
 	if out != "8000" {
111 108
 		c.Errorf("setting the CPU CFS quota failed")
112 109
 	}
... ...
@@ -290,12 +287,8 @@ func (s *DockerSuite) TestRunLinksContainerWithContainerName(c *check.C) {
290 290
 	if err != nil {
291 291
 		c.Fatalf("failed to run container: %v, output: %q", err, out)
292 292
 	}
293
-	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.NetworkSettings.IPAddress}}", "parent")
294
-	ip, _, _, err := runCommandWithStdoutStderr(cmd)
295
-	if err != nil {
296
-		c.Fatalf("failed to inspect container: %v, output: %q", err, ip)
297
-	}
298
-	ip = strings.TrimSpace(ip)
293
+	ip, err := inspectField("parent", "NetworkSettings.IPAddress")
294
+	c.Assert(err, check.IsNil)
299 295
 	cmd = exec.Command(dockerBinary, "run", "--link", "parent:test", "busybox", "/bin/cat", "/etc/hosts")
300 296
 	out, _, err = runCommandWithOutput(cmd)
301 297
 	if err != nil {
... ...
@@ -314,12 +307,8 @@ func (s *DockerSuite) TestRunLinksContainerWithContainerId(c *check.C) {
314 314
 		c.Fatalf("failed to run container: %v, output: %q", err, cID)
315 315
 	}
316 316
 	cID = strings.TrimSpace(cID)
317
-	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.NetworkSettings.IPAddress}}", cID)
318
-	ip, _, _, err := runCommandWithStdoutStderr(cmd)
319
-	if err != nil {
320
-		c.Fatalf("failed to inspect container: %v, output: %q", err, ip)
321
-	}
322
-	ip = strings.TrimSpace(ip)
317
+	ip, err := inspectField(cID, "NetworkSettings.IPAddress")
318
+	c.Assert(err, check.IsNil)
323 319
 	cmd = exec.Command(dockerBinary, "run", "--link", cID+":test", "busybox", "/bin/cat", "/etc/hosts")
324 320
 	out, _, err := runCommandWithOutput(cmd)
325 321
 	if err != nil {
... ...
@@ -1163,12 +1152,8 @@ func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
1163 1163
 		c.Skip("Your kernel does not support CPU cfs period, skip this test")
1164 1164
 	}
1165 1165
 
1166
-	cmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.CpuPeriod}}", "test")
1167
-	out, _, err = runCommandWithOutput(cmd)
1168
-	if err != nil {
1169
-		c.Fatalf("failed to inspect container: %s, %v", out, err)
1170
-	}
1171
-	out = strings.TrimSpace(out)
1166
+	out, err = inspectField("test", "HostConfig.CpuPeriod")
1167
+	c.Assert(err, check.IsNil)
1172 1168
 	if out != "50000" {
1173 1169
 		c.Errorf("setting the CPU CFS period failed")
1174 1170
 	}
... ...
@@ -1741,16 +1726,12 @@ func (s *DockerSuite) TestRunState(c *check.C) {
1741 1741
 	}
1742 1742
 	id := strings.TrimSpace(out)
1743 1743
 	state, err := inspectField(id, "State.Running")
1744
-	if err != nil {
1745
-		c.Fatal(err)
1746
-	}
1744
+	c.Assert(err, check.IsNil)
1747 1745
 	if state != "true" {
1748 1746
 		c.Fatal("Container state is 'not running'")
1749 1747
 	}
1750 1748
 	pid1, err := inspectField(id, "State.Pid")
1751
-	if err != nil {
1752
-		c.Fatal(err)
1753
-	}
1749
+	c.Assert(err, check.IsNil)
1754 1750
 	if pid1 == "0" {
1755 1751
 		c.Fatal("Container state Pid 0")
1756 1752
 	}
... ...
@@ -1761,16 +1742,12 @@ func (s *DockerSuite) TestRunState(c *check.C) {
1761 1761
 		c.Fatal(err, out)
1762 1762
 	}
1763 1763
 	state, err = inspectField(id, "State.Running")
1764
-	if err != nil {
1765
-		c.Fatal(err)
1766
-	}
1764
+	c.Assert(err, check.IsNil)
1767 1765
 	if state != "false" {
1768 1766
 		c.Fatal("Container state is 'running'")
1769 1767
 	}
1770 1768
 	pid2, err := inspectField(id, "State.Pid")
1771
-	if err != nil {
1772
-		c.Fatal(err)
1773
-	}
1769
+	c.Assert(err, check.IsNil)
1774 1770
 	if pid2 == pid1 {
1775 1771
 		c.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
1776 1772
 	}
... ...
@@ -1781,16 +1758,12 @@ func (s *DockerSuite) TestRunState(c *check.C) {
1781 1781
 		c.Fatal(err, out)
1782 1782
 	}
1783 1783
 	state, err = inspectField(id, "State.Running")
1784
-	if err != nil {
1785
-		c.Fatal(err)
1786
-	}
1784
+	c.Assert(err, check.IsNil)
1787 1785
 	if state != "true" {
1788 1786
 		c.Fatal("Container state is 'not running'")
1789 1787
 	}
1790 1788
 	pid3, err := inspectField(id, "State.Pid")
1791
-	if err != nil {
1792
-		c.Fatal(err)
1793
-	}
1789
+	c.Assert(err, check.IsNil)
1794 1790
 	if pid3 == pid1 {
1795 1791
 		c.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
1796 1792
 	}
... ...
@@ -2166,9 +2139,7 @@ func (s *DockerSuite) TestRunNetworkNotInitializedNoneMode(c *check.C) {
2166 2166
 	}
2167 2167
 	id := strings.TrimSpace(out)
2168 2168
 	res, err := inspectField(id, "NetworkSettings.IPAddress")
2169
-	if err != nil {
2170
-		c.Fatal(err)
2171
-	}
2169
+	c.Assert(err, check.IsNil)
2172 2170
 	if res != "" {
2173 2171
 		c.Fatalf("For 'none' mode network must not be initialized, but container got IP: %s", res)
2174 2172
 	}
... ...
@@ -2197,9 +2168,7 @@ func (s *DockerSuite) TestRunInspectMacAddress(c *check.C) {
2197 2197
 	}
2198 2198
 	id := strings.TrimSpace(out)
2199 2199
 	inspectedMac, err := inspectField(id, "NetworkSettings.MacAddress")
2200
-	if err != nil {
2201
-		c.Fatal(err)
2202
-	}
2200
+	c.Assert(err, check.IsNil)
2203 2201
 	if inspectedMac != mac {
2204 2202
 		c.Fatalf("docker inspect outputs wrong MAC address: %q, should be: %q", inspectedMac, mac)
2205 2203
 	}
... ...
@@ -2225,9 +2194,7 @@ func (s *DockerSuite) TestRunDeallocatePortOnMissingIptablesRule(c *check.C) {
2225 2225
 	}
2226 2226
 	id := strings.TrimSpace(out)
2227 2227
 	ip, err := inspectField(id, "NetworkSettings.IPAddress")
2228
-	if err != nil {
2229
-		c.Fatal(err)
2230
-	}
2228
+	c.Assert(err, check.IsNil)
2231 2229
 	iptCmd := exec.Command("iptables", "-D", "DOCKER", "-d", fmt.Sprintf("%s/32", ip),
2232 2230
 		"!", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-m", "tcp", "--dport", "23", "-j", "ACCEPT")
2233 2231
 	out, _, err = runCommandWithOutput(iptCmd)
... ...
@@ -2487,32 +2454,24 @@ func (s *DockerSuite) TestRunVolumesCleanPaths(c *check.C) {
2487 2487
 	}
2488 2488
 
2489 2489
 	out, err := inspectFieldMap("dark_helmet", "Volumes", "/foo/")
2490
-	if err != nil {
2491
-		c.Fatal(err)
2492
-	}
2490
+	c.Assert(err, check.IsNil)
2493 2491
 	if out != "" {
2494 2492
 		c.Fatalf("Found unexpected volume entry for '/foo/' in volumes\n%q", out)
2495 2493
 	}
2496 2494
 
2497 2495
 	out, err = inspectFieldMap("dark_helmet", "Volumes", "/foo")
2498
-	if err != nil {
2499
-		c.Fatal(err)
2500
-	}
2496
+	c.Assert(err, check.IsNil)
2501 2497
 	if !strings.Contains(out, volumesStoragePath) {
2502 2498
 		c.Fatalf("Volume was not defined for /foo\n%q", out)
2503 2499
 	}
2504 2500
 
2505 2501
 	out, err = inspectFieldMap("dark_helmet", "Volumes", "/bar/")
2506
-	if err != nil {
2507
-		c.Fatal(err)
2508
-	}
2502
+	c.Assert(err, check.IsNil)
2509 2503
 	if out != "" {
2510 2504
 		c.Fatalf("Found unexpected volume entry for '/bar/' in volumes\n%q", out)
2511 2505
 	}
2512 2506
 	out, err = inspectFieldMap("dark_helmet", "Volumes", "/bar")
2513
-	if err != nil {
2514
-		c.Fatal(err)
2515
-	}
2507
+	c.Assert(err, check.IsNil)
2516 2508
 	if !strings.Contains(out, volumesStoragePath) {
2517 2509
 		c.Fatalf("Volume was not defined for /bar\n%q", out)
2518 2510
 	}
... ...
@@ -2549,9 +2508,7 @@ func (s *DockerSuite) TestRunAllowPortRangeThroughExpose(c *check.C) {
2549 2549
 	}
2550 2550
 	id := strings.TrimSpace(out)
2551 2551
 	portstr, err := inspectFieldJSON(id, "NetworkSettings.Ports")
2552
-	if err != nil {
2553
-		c.Fatal(err)
2554
-	}
2552
+	c.Assert(err, check.IsNil)
2555 2553
 	var ports nat.PortMap
2556 2554
 	if err = unmarshalJSON([]byte(portstr), &ports); err != nil {
2557 2555
 		c.Fatal(err)
... ...
@@ -2592,14 +2549,8 @@ func (s *DockerSuite) TestRunUnknownCommand(c *check.C) {
2592 2592
 	runCmd = exec.Command(dockerBinary, "start", cID)
2593 2593
 	_, _, _, _ = runCommandWithStdoutStderr(runCmd)
2594 2594
 
2595
-	runCmd = exec.Command(dockerBinary, "inspect", "--format={{.State.ExitCode}}", cID)
2596
-	rc, _, _, err2 := runCommandWithStdoutStderr(runCmd)
2597
-	rc = strings.TrimSpace(rc)
2598
-
2599
-	if err2 != nil {
2600
-		c.Fatalf("Error getting status of container: %v", err2)
2601
-	}
2602
-
2595
+	rc, err := inspectField(cID, "State.ExitCode")
2596
+	c.Assert(err, check.IsNil)
2603 2597
 	if rc == "0" {
2604 2598
 		c.Fatalf("ExitCode(%v) cannot be 0", rc)
2605 2599
 	}
... ...
@@ -2646,16 +2597,12 @@ func (s *DockerSuite) TestRunModeIpcContainer(c *check.C) {
2646 2646
 	}
2647 2647
 	id := strings.TrimSpace(out)
2648 2648
 	state, err := inspectField(id, "State.Running")
2649
-	if err != nil {
2650
-		c.Fatal(err)
2651
-	}
2649
+	c.Assert(err, check.IsNil)
2652 2650
 	if state != "true" {
2653 2651
 		c.Fatal("Container state is 'not running'")
2654 2652
 	}
2655 2653
 	pid1, err := inspectField(id, "State.Pid")
2656
-	if err != nil {
2657
-		c.Fatal(err)
2658
-	}
2654
+	c.Assert(err, check.IsNil)
2659 2655
 
2660 2656
 	parentContainerIpc, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/ipc", pid1))
2661 2657
 	if err != nil {
... ...
@@ -2694,9 +2641,7 @@ func (s *DockerSuite) TestContainerNetworkMode(c *check.C) {
2694 2694
 		c.Fatal(err)
2695 2695
 	}
2696 2696
 	pid1, err := inspectField(id, "State.Pid")
2697
-	if err != nil {
2698
-		c.Fatal(err)
2699
-	}
2697
+	c.Assert(err, check.IsNil)
2700 2698
 
2701 2699
 	parentContainerNet, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/net", pid1))
2702 2700
 	if err != nil {
... ...
@@ -2951,9 +2896,7 @@ func (s *DockerSuite) TestRunAllowPortRangeThroughPublish(c *check.C) {
2951 2951
 
2952 2952
 	id := strings.TrimSpace(out)
2953 2953
 	portstr, err := inspectFieldJSON(id, "NetworkSettings.Ports")
2954
-	if err != nil {
2955
-		c.Fatal(err)
2956
-	}
2954
+	c.Assert(err, check.IsNil)
2957 2955
 	var ports nat.PortMap
2958 2956
 	err = unmarshalJSON([]byte(portstr), &ports)
2959 2957
 	for port, binding := range ports {
... ...
@@ -2991,12 +2934,8 @@ func (s *DockerSuite) TestRunSetDefaultRestartPolicy(c *check.C) {
2991 2991
 	if out, _, err := runCommandWithOutput(runCmd); err != nil {
2992 2992
 		c.Fatalf("failed to run container: %v, output: %q", err, out)
2993 2993
 	}
2994
-	cmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.RestartPolicy.Name}}", "test")
2995
-	out, _, err := runCommandWithOutput(cmd)
2996
-	if err != nil {
2997
-		c.Fatalf("failed to inspect container: %v, output: %q", err, out)
2998
-	}
2999
-	out = strings.Trim(out, "\r\n")
2994
+	out, err := inspectField("test", "HostConfig.RestartPolicy.Name")
2995
+	c.Assert(err, check.IsNil)
3000 2996
 	if out != "no" {
3001 2997
 		c.Fatalf("Set default restart policy failed")
3002 2998
 	}
... ...
@@ -3012,16 +2951,12 @@ func (s *DockerSuite) TestRunRestartMaxRetries(c *check.C) {
3012 3012
 		c.Fatal(err)
3013 3013
 	}
3014 3014
 	count, err := inspectField(id, "RestartCount")
3015
-	if err != nil {
3016
-		c.Fatal(err)
3017
-	}
3015
+	c.Assert(err, check.IsNil)
3018 3016
 	if count != "3" {
3019 3017
 		c.Fatalf("Container was restarted %s times, expected %d", count, 3)
3020 3018
 	}
3021 3019
 	MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
3022
-	if err != nil {
3023
-		c.Fatal(err)
3024
-	}
3020
+	c.Assert(err, check.IsNil)
3025 3021
 	if MaximumRetryCount != "3" {
3026 3022
 		c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
3027 3023
 	}
... ...
@@ -98,9 +98,7 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
98 98
 	// when container runs successfully, we should not have state.Error
99 99
 	dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
100 100
 	stateErr, err := inspectField("test", "State.Error")
101
-	if err != nil {
102
-		c.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
103
-	}
101
+	c.Assert(err, check.IsNil)
104 102
 	if stateErr != "" {
105 103
 		c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
106 104
 	}
... ...
@@ -111,9 +109,7 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
111 111
 		c.Fatalf("Expected error but got none, output %q", out)
112 112
 	}
113 113
 	stateErr, err = inspectField("test2", "State.Error")
114
-	if err != nil {
115
-		c.Fatalf("Failed to inspect %q state's error, got error %q", "test2", err)
116
-	}
114
+	c.Assert(err, check.IsNil)
117 115
 	expected := "port is already allocated"
118 116
 	if stateErr == "" || !strings.Contains(stateErr, expected) {
119 117
 		c.Fatalf("State.Error(%q) does not include %q", stateErr, expected)
... ...
@@ -123,9 +119,7 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
123 123
 	dockerCmd(c, "stop", "test")
124 124
 	dockerCmd(c, "start", "test2")
125 125
 	stateErr, err = inspectField("test2", "State.Error")
126
-	if err != nil {
127
-		c.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
128
-	}
126
+	c.Assert(err, check.IsNil)
129 127
 	if stateErr != "" {
130 128
 		c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
131 129
 	}
... ...
@@ -196,12 +190,8 @@ func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
196 196
 	if out, _, err := runCommandWithOutput(cmd); err != nil {
197 197
 		c.Fatal(out, err)
198 198
 	}
199
-	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", "parent")
200
-	out, _, err := runCommandWithOutput(cmd)
201
-	if err != nil {
202
-		c.Fatal(out, err)
203
-	}
204
-	out = strings.Trim(out, "\r\n")
199
+	out, err := inspectField("parent", "State.Running")
200
+	c.Assert(err, check.IsNil)
205 201
 	if out != "false" {
206 202
 		c.Fatal("Container should be stopped")
207 203
 	}
... ...
@@ -215,12 +205,8 @@ func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
215 215
 	}
216 216
 
217 217
 	for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
218
-		cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", container)
219
-		out, _, err = runCommandWithOutput(cmd)
220
-		if err != nil {
221
-			c.Fatal(out, err)
222
-		}
223
-		out = strings.Trim(out, "\r\n")
218
+		out, err := inspectField(container, "State.Running")
219
+		c.Assert(err, check.IsNil)
224 220
 		if out != expected {
225 221
 			c.Fatal("Container running state wrong")
226 222
 		}
... ...
@@ -260,12 +246,10 @@ func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
260 260
 
261 261
 	// confirm the state of all the containers be stopped
262 262
 	for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
263
-		cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", container)
264
-		out, _, err := runCommandWithOutput(cmd)
263
+		out, err := inspectField(container, "State.Running")
265 264
 		if err != nil {
266 265
 			c.Fatal(out, err)
267 266
 		}
268
-		out = strings.Trim(out, "\r\n")
269 267
 		if out != expected {
270 268
 			c.Fatal("Container running state wrong")
271 269
 		}
... ...
@@ -22,15 +22,10 @@ func (s *DockerSuite) TestTagUnprefixedRepoByName(c *check.C) {
22 22
 
23 23
 // tagging an image by ID in a new unprefixed repo should work
24 24
 func (s *DockerSuite) TestTagUnprefixedRepoByID(c *check.C) {
25
-	getIDCmd := exec.Command(dockerBinary, "inspect", "-f", "{{.Id}}", "busybox")
26
-	out, _, err := runCommandWithOutput(getIDCmd)
27
-	if err != nil {
28
-		c.Fatalf("failed to get the image ID of busybox: %s, %v", out, err)
29
-	}
30
-
31
-	cleanedImageID := strings.TrimSpace(out)
32
-	tagCmd := exec.Command(dockerBinary, "tag", cleanedImageID, "testfoobarbaz")
33
-	if out, _, err = runCommandWithOutput(tagCmd); err != nil {
25
+	imageID, err := inspectField("busybox", "Id")
26
+	c.Assert(err, check.IsNil)
27
+	tagCmd := exec.Command(dockerBinary, "tag", imageID, "testfoobarbaz")
28
+	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
34 29
 		c.Fatal(out, err)
35 30
 	}
36 31
 }
... ...
@@ -21,12 +21,8 @@ func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
21 21
 
22 22
 	status := "true"
23 23
 	for i := 0; status != "false"; i++ {
24
-		runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.Running}}'", containerID)
25
-		status, _, err = runCommandWithOutput(runCmd)
26
-		if err != nil {
27
-			c.Fatal(status, err)
28
-		}
29
-		status = strings.TrimSpace(status)
24
+		status, err = inspectField(containerID, "State.Running")
25
+		c.Assert(err, check.IsNil)
30 26
 
31 27
 		time.Sleep(time.Second)
32 28
 		if i >= 60 {
... ...
@@ -84,12 +80,8 @@ func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
84 84
 
85 85
 	status := "true"
86 86
 	for i := 0; status != "false"; i++ {
87
-		runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.Running}}'", containerID)
88
-		status, _, err = runCommandWithOutput(runCmd)
89
-		if err != nil {
90
-			c.Fatal(status, err)
91
-		}
92
-		status = strings.TrimSpace(status)
87
+		status, err = inspectField(containerID, "State.Running")
88
+		c.Assert(err, check.IsNil)
93 89
 
94 90
 		time.Sleep(time.Second)
95 91
 		if i >= 60 {