Browse code

Use inspectField to simplify code

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>

Qiang Huang authored on 2015/05/18 11:06:13
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
 	}
... ...
@@ -302,12 +299,8 @@ func (s *DockerSuite) TestRunLinksContainerWithContainerName(c *check.C) {
302 302
 	if err != nil {
303 303
 		c.Fatalf("failed to run container: %v, output: %q", err, out)
304 304
 	}
305
-	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.NetworkSettings.IPAddress}}", "parent")
306
-	ip, _, _, err := runCommandWithStdoutStderr(cmd)
307
-	if err != nil {
308
-		c.Fatalf("failed to inspect container: %v, output: %q", err, ip)
309
-	}
310
-	ip = strings.TrimSpace(ip)
305
+	ip, err := inspectField("parent", "NetworkSettings.IPAddress")
306
+	c.Assert(err, check.IsNil)
311 307
 	cmd = exec.Command(dockerBinary, "run", "--link", "parent:test", "busybox", "/bin/cat", "/etc/hosts")
312 308
 	out, _, err = runCommandWithOutput(cmd)
313 309
 	if err != nil {
... ...
@@ -326,12 +319,8 @@ func (s *DockerSuite) TestRunLinksContainerWithContainerId(c *check.C) {
326 326
 		c.Fatalf("failed to run container: %v, output: %q", err, cID)
327 327
 	}
328 328
 	cID = strings.TrimSpace(cID)
329
-	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.NetworkSettings.IPAddress}}", cID)
330
-	ip, _, _, err := runCommandWithStdoutStderr(cmd)
331
-	if err != nil {
332
-		c.Fatalf("failed to inspect container: %v, output: %q", err, ip)
333
-	}
334
-	ip = strings.TrimSpace(ip)
329
+	ip, err := inspectField(cID, "NetworkSettings.IPAddress")
330
+	c.Assert(err, check.IsNil)
335 331
 	cmd = exec.Command(dockerBinary, "run", "--link", cID+":test", "busybox", "/bin/cat", "/etc/hosts")
336 332
 	out, _, err := runCommandWithOutput(cmd)
337 333
 	if err != nil {
... ...
@@ -1175,12 +1164,8 @@ func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
1175 1175
 		c.Skip("Your kernel does not support CPU cfs period, skip this test")
1176 1176
 	}
1177 1177
 
1178
-	cmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.CpuPeriod}}", "test")
1179
-	out, _, err = runCommandWithOutput(cmd)
1180
-	if err != nil {
1181
-		c.Fatalf("failed to inspect container: %s, %v", out, err)
1182
-	}
1183
-	out = strings.TrimSpace(out)
1178
+	out, err = inspectField("test", "HostConfig.CpuPeriod")
1179
+	c.Assert(err, check.IsNil)
1184 1180
 	if out != "50000" {
1185 1181
 		c.Errorf("setting the CPU CFS period failed")
1186 1182
 	}
... ...
@@ -1753,16 +1738,12 @@ func (s *DockerSuite) TestRunState(c *check.C) {
1753 1753
 	}
1754 1754
 	id := strings.TrimSpace(out)
1755 1755
 	state, err := inspectField(id, "State.Running")
1756
-	if err != nil {
1757
-		c.Fatal(err)
1758
-	}
1756
+	c.Assert(err, check.IsNil)
1759 1757
 	if state != "true" {
1760 1758
 		c.Fatal("Container state is 'not running'")
1761 1759
 	}
1762 1760
 	pid1, err := inspectField(id, "State.Pid")
1763
-	if err != nil {
1764
-		c.Fatal(err)
1765
-	}
1761
+	c.Assert(err, check.IsNil)
1766 1762
 	if pid1 == "0" {
1767 1763
 		c.Fatal("Container state Pid 0")
1768 1764
 	}
... ...
@@ -1773,16 +1754,12 @@ func (s *DockerSuite) TestRunState(c *check.C) {
1773 1773
 		c.Fatal(err, out)
1774 1774
 	}
1775 1775
 	state, err = inspectField(id, "State.Running")
1776
-	if err != nil {
1777
-		c.Fatal(err)
1778
-	}
1776
+	c.Assert(err, check.IsNil)
1779 1777
 	if state != "false" {
1780 1778
 		c.Fatal("Container state is 'running'")
1781 1779
 	}
1782 1780
 	pid2, err := inspectField(id, "State.Pid")
1783
-	if err != nil {
1784
-		c.Fatal(err)
1785
-	}
1781
+	c.Assert(err, check.IsNil)
1786 1782
 	if pid2 == pid1 {
1787 1783
 		c.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
1788 1784
 	}
... ...
@@ -1793,16 +1770,12 @@ func (s *DockerSuite) TestRunState(c *check.C) {
1793 1793
 		c.Fatal(err, out)
1794 1794
 	}
1795 1795
 	state, err = inspectField(id, "State.Running")
1796
-	if err != nil {
1797
-		c.Fatal(err)
1798
-	}
1796
+	c.Assert(err, check.IsNil)
1799 1797
 	if state != "true" {
1800 1798
 		c.Fatal("Container state is 'not running'")
1801 1799
 	}
1802 1800
 	pid3, err := inspectField(id, "State.Pid")
1803
-	if err != nil {
1804
-		c.Fatal(err)
1805
-	}
1801
+	c.Assert(err, check.IsNil)
1806 1802
 	if pid3 == pid1 {
1807 1803
 		c.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
1808 1804
 	}
... ...
@@ -2178,9 +2151,7 @@ func (s *DockerSuite) TestRunNetworkNotInitializedNoneMode(c *check.C) {
2178 2178
 	}
2179 2179
 	id := strings.TrimSpace(out)
2180 2180
 	res, err := inspectField(id, "NetworkSettings.IPAddress")
2181
-	if err != nil {
2182
-		c.Fatal(err)
2183
-	}
2181
+	c.Assert(err, check.IsNil)
2184 2182
 	if res != "" {
2185 2183
 		c.Fatalf("For 'none' mode network must not be initialized, but container got IP: %s", res)
2186 2184
 	}
... ...
@@ -2209,9 +2180,7 @@ func (s *DockerSuite) TestRunInspectMacAddress(c *check.C) {
2209 2209
 	}
2210 2210
 	id := strings.TrimSpace(out)
2211 2211
 	inspectedMac, err := inspectField(id, "NetworkSettings.MacAddress")
2212
-	if err != nil {
2213
-		c.Fatal(err)
2214
-	}
2212
+	c.Assert(err, check.IsNil)
2215 2213
 	if inspectedMac != mac {
2216 2214
 		c.Fatalf("docker inspect outputs wrong MAC address: %q, should be: %q", inspectedMac, mac)
2217 2215
 	}
... ...
@@ -2237,9 +2206,7 @@ func (s *DockerSuite) TestRunDeallocatePortOnMissingIptablesRule(c *check.C) {
2237 2237
 	}
2238 2238
 	id := strings.TrimSpace(out)
2239 2239
 	ip, err := inspectField(id, "NetworkSettings.IPAddress")
2240
-	if err != nil {
2241
-		c.Fatal(err)
2242
-	}
2240
+	c.Assert(err, check.IsNil)
2243 2241
 	iptCmd := exec.Command("iptables", "-D", "DOCKER", "-d", fmt.Sprintf("%s/32", ip),
2244 2242
 		"!", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-m", "tcp", "--dport", "23", "-j", "ACCEPT")
2245 2243
 	out, _, err = runCommandWithOutput(iptCmd)
... ...
@@ -2499,32 +2466,24 @@ func (s *DockerSuite) TestRunVolumesCleanPaths(c *check.C) {
2499 2499
 	}
2500 2500
 
2501 2501
 	out, err := inspectFieldMap("dark_helmet", "Volumes", "/foo/")
2502
-	if err != nil {
2503
-		c.Fatal(err)
2504
-	}
2502
+	c.Assert(err, check.IsNil)
2505 2503
 	if out != "" {
2506 2504
 		c.Fatalf("Found unexpected volume entry for '/foo/' in volumes\n%q", out)
2507 2505
 	}
2508 2506
 
2509 2507
 	out, err = inspectFieldMap("dark_helmet", "Volumes", "/foo")
2510
-	if err != nil {
2511
-		c.Fatal(err)
2512
-	}
2508
+	c.Assert(err, check.IsNil)
2513 2509
 	if !strings.Contains(out, volumesStoragePath) {
2514 2510
 		c.Fatalf("Volume was not defined for /foo\n%q", out)
2515 2511
 	}
2516 2512
 
2517 2513
 	out, err = inspectFieldMap("dark_helmet", "Volumes", "/bar/")
2518
-	if err != nil {
2519
-		c.Fatal(err)
2520
-	}
2514
+	c.Assert(err, check.IsNil)
2521 2515
 	if out != "" {
2522 2516
 		c.Fatalf("Found unexpected volume entry for '/bar/' in volumes\n%q", out)
2523 2517
 	}
2524 2518
 	out, err = inspectFieldMap("dark_helmet", "Volumes", "/bar")
2525
-	if err != nil {
2526
-		c.Fatal(err)
2527
-	}
2519
+	c.Assert(err, check.IsNil)
2528 2520
 	if !strings.Contains(out, volumesStoragePath) {
2529 2521
 		c.Fatalf("Volume was not defined for /bar\n%q", out)
2530 2522
 	}
... ...
@@ -2561,9 +2520,7 @@ func (s *DockerSuite) TestRunAllowPortRangeThroughExpose(c *check.C) {
2561 2561
 	}
2562 2562
 	id := strings.TrimSpace(out)
2563 2563
 	portstr, err := inspectFieldJSON(id, "NetworkSettings.Ports")
2564
-	if err != nil {
2565
-		c.Fatal(err)
2566
-	}
2564
+	c.Assert(err, check.IsNil)
2567 2565
 	var ports nat.PortMap
2568 2566
 	if err = unmarshalJSON([]byte(portstr), &ports); err != nil {
2569 2567
 		c.Fatal(err)
... ...
@@ -2604,14 +2561,8 @@ func (s *DockerSuite) TestRunUnknownCommand(c *check.C) {
2604 2604
 	runCmd = exec.Command(dockerBinary, "start", cID)
2605 2605
 	_, _, _, _ = runCommandWithStdoutStderr(runCmd)
2606 2606
 
2607
-	runCmd = exec.Command(dockerBinary, "inspect", "--format={{.State.ExitCode}}", cID)
2608
-	rc, _, _, err2 := runCommandWithStdoutStderr(runCmd)
2609
-	rc = strings.TrimSpace(rc)
2610
-
2611
-	if err2 != nil {
2612
-		c.Fatalf("Error getting status of container: %v", err2)
2613
-	}
2614
-
2607
+	rc, err := inspectField(cID, "State.ExitCode")
2608
+	c.Assert(err, check.IsNil)
2615 2609
 	if rc == "0" {
2616 2610
 		c.Fatalf("ExitCode(%v) cannot be 0", rc)
2617 2611
 	}
... ...
@@ -2658,16 +2609,12 @@ func (s *DockerSuite) TestRunModeIpcContainer(c *check.C) {
2658 2658
 	}
2659 2659
 	id := strings.TrimSpace(out)
2660 2660
 	state, err := inspectField(id, "State.Running")
2661
-	if err != nil {
2662
-		c.Fatal(err)
2663
-	}
2661
+	c.Assert(err, check.IsNil)
2664 2662
 	if state != "true" {
2665 2663
 		c.Fatal("Container state is 'not running'")
2666 2664
 	}
2667 2665
 	pid1, err := inspectField(id, "State.Pid")
2668
-	if err != nil {
2669
-		c.Fatal(err)
2670
-	}
2666
+	c.Assert(err, check.IsNil)
2671 2667
 
2672 2668
 	parentContainerIpc, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/ipc", pid1))
2673 2669
 	if err != nil {
... ...
@@ -2706,9 +2653,7 @@ func (s *DockerSuite) TestContainerNetworkMode(c *check.C) {
2706 2706
 		c.Fatal(err)
2707 2707
 	}
2708 2708
 	pid1, err := inspectField(id, "State.Pid")
2709
-	if err != nil {
2710
-		c.Fatal(err)
2711
-	}
2709
+	c.Assert(err, check.IsNil)
2712 2710
 
2713 2711
 	parentContainerNet, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/net", pid1))
2714 2712
 	if err != nil {
... ...
@@ -2963,9 +2908,7 @@ func (s *DockerSuite) TestRunAllowPortRangeThroughPublish(c *check.C) {
2963 2963
 
2964 2964
 	id := strings.TrimSpace(out)
2965 2965
 	portstr, err := inspectFieldJSON(id, "NetworkSettings.Ports")
2966
-	if err != nil {
2967
-		c.Fatal(err)
2968
-	}
2966
+	c.Assert(err, check.IsNil)
2969 2967
 	var ports nat.PortMap
2970 2968
 	err = unmarshalJSON([]byte(portstr), &ports)
2971 2969
 	for port, binding := range ports {
... ...
@@ -3003,12 +2946,8 @@ func (s *DockerSuite) TestRunSetDefaultRestartPolicy(c *check.C) {
3003 3003
 	if out, _, err := runCommandWithOutput(runCmd); err != nil {
3004 3004
 		c.Fatalf("failed to run container: %v, output: %q", err, out)
3005 3005
 	}
3006
-	cmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.RestartPolicy.Name}}", "test")
3007
-	out, _, err := runCommandWithOutput(cmd)
3008
-	if err != nil {
3009
-		c.Fatalf("failed to inspect container: %v, output: %q", err, out)
3010
-	}
3011
-	out = strings.Trim(out, "\r\n")
3006
+	out, err := inspectField("test", "HostConfig.RestartPolicy.Name")
3007
+	c.Assert(err, check.IsNil)
3012 3008
 	if out != "no" {
3013 3009
 		c.Fatalf("Set default restart policy failed")
3014 3010
 	}
... ...
@@ -3024,16 +2963,12 @@ func (s *DockerSuite) TestRunRestartMaxRetries(c *check.C) {
3024 3024
 		c.Fatal(err)
3025 3025
 	}
3026 3026
 	count, err := inspectField(id, "RestartCount")
3027
-	if err != nil {
3028
-		c.Fatal(err)
3029
-	}
3027
+	c.Assert(err, check.IsNil)
3030 3028
 	if count != "3" {
3031 3029
 		c.Fatalf("Container was restarted %s times, expected %d", count, 3)
3032 3030
 	}
3033 3031
 	MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
3034
-	if err != nil {
3035
-		c.Fatal(err)
3036
-	}
3032
+	c.Assert(err, check.IsNil)
3037 3033
 	if MaximumRetryCount != "3" {
3038 3034
 		c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
3039 3035
 	}
... ...
@@ -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 {