Przeglądaj źródła

Use inspectField to simplify code

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
Qiang Huang 10 lat temu
rodzic
commit
74f8a4eca4

+ 3 - 10
integration-cli/docker_cli_commit_test.go

@@ -85,16 +85,11 @@ func (s *DockerSuite) TestCommitPausedContainer(c *check.C) {
 		c.Fatalf("failed to commit container to image: %s, %v", out, err)
 	}
 
-	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Paused}}", cleanedContainerID)
-	out, _, _, err = runCommandWithStdoutStderr(cmd)
-	if err != nil {
-		c.Fatalf("failed to inspect container: %v, output: %q", err, out)
-	}
-
+	out, err = inspectField(cleanedContainerID, "State.Paused")
+	c.Assert(err, check.IsNil)
 	if !strings.Contains(out, "true") {
 		c.Fatalf("commit should not unpause a paused container")
 	}
-
 }
 
 func (s *DockerSuite) TestCommitNewFile(c *check.C) {
@@ -242,9 +237,7 @@ func (s *DockerSuite) TestCommitChange(c *check.C) {
 
 	for conf, value := range expected {
 		res, err := inspectField(imageId, conf)
-		if err != nil {
-			c.Errorf("failed to get value %s, error: %s", conf, err)
-		}
+		c.Assert(err, check.IsNil)
 		if res != value {
 			c.Errorf("%s('%s'), expected %s", conf, res, value)
 		}

+ 16 - 26
integration-cli/docker_cli_inspect_test.go

@@ -12,13 +12,10 @@ import (
 func (s *DockerSuite) TestInspectImage(c *check.C) {
 	imageTest := "emptyfs"
 	imageTestID := "511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158"
-	imagesCmd := exec.Command(dockerBinary, "inspect", "--format='{{.Id}}'", imageTest)
-	out, exitCode, err := runCommandWithOutput(imagesCmd)
-	if exitCode != 0 || err != nil {
-		c.Fatalf("failed to inspect image: %s, %v", out, err)
-	}
+	id, err := inspectField(imageTest, "Id")
+	c.Assert(err, check.IsNil)
 
-	if id := strings.TrimSuffix(out, "\n"); id != imageTestID {
+	if id != imageTestID {
 		c.Fatalf("Expected id: %s for image: %s but received id: %s", imageTestID, imageTest, id)
 	}
 
@@ -33,33 +30,28 @@ func (s *DockerSuite) TestInspectInt64(c *check.C) {
 
 	out = strings.TrimSpace(out)
 
-	inspectCmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.Memory}}", out)
-	inspectOut, _, err := runCommandWithOutput(inspectCmd)
-	if err != nil {
-		c.Fatalf("failed to inspect container: %v, output: %q", err, inspectOut)
-	}
+	inspectOut, err := inspectField(out, "HostConfig.Memory")
+	c.Assert(err, check.IsNil)
 
-	if strings.TrimSpace(inspectOut) != "314572800" {
+	if inspectOut != "314572800" {
 		c.Fatalf("inspect got wrong value, got: %q, expected: 314572800", inspectOut)
 	}
 }
 
 func (s *DockerSuite) TestInspectImageFilterInt(c *check.C) {
 	imageTest := "emptyfs"
-	imagesCmd := exec.Command(dockerBinary, "inspect", "--format='{{.Size}}'", imageTest)
-	out, exitCode, err := runCommandWithOutput(imagesCmd)
-	if exitCode != 0 || err != nil {
-		c.Fatalf("failed to inspect image: %s, %v", out, err)
-	}
-	size, err := strconv.Atoi(strings.TrimSuffix(out, "\n"))
+	out, err := inspectField(imageTest, "Size")
+	c.Assert(err, check.IsNil)
+
+	size, err := strconv.Atoi(out)
 	if err != nil {
 		c.Fatalf("failed to inspect size of the image: %s, %v", out, err)
 	}
 
 	//now see if the size turns out to be the same
 	formatStr := fmt.Sprintf("--format='{{eq .Size %d}}'", size)
-	imagesCmd = exec.Command(dockerBinary, "inspect", formatStr, imageTest)
-	out, exitCode, err = runCommandWithOutput(imagesCmd)
+	imagesCmd := exec.Command(dockerBinary, "inspect", formatStr, imageTest)
+	out, exitCode, err := runCommandWithOutput(imagesCmd)
 	if exitCode != 0 || err != nil {
 		c.Fatalf("failed to inspect image: %s, %v", out, err)
 	}
@@ -77,12 +69,10 @@ func (s *DockerSuite) TestInspectContainerFilterInt(c *check.C) {
 
 	id := strings.TrimSpace(out)
 
-	runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.ExitCode}}'", id)
-	out, _, err = runCommandWithOutput(runCmd)
-	if err != nil {
-		c.Fatalf("failed to inspect container: %s, %v", out, err)
-	}
-	exitCode, err := strconv.Atoi(strings.TrimSuffix(out, "\n"))
+	out, err = inspectField(id, "State.ExitCode")
+	c.Assert(err, check.IsNil)
+
+	exitCode, err := strconv.Atoi(out)
 	if err != nil {
 		c.Fatalf("failed to inspect exitcode of the container: %s, %v", out, err)
 	}

+ 3 - 6
integration-cli/docker_cli_nat_test.go

@@ -53,12 +53,9 @@ func getContainerLogs(c *check.C, containerID string) string {
 }
 
 func getContainerStatus(c *check.C, containerID string) string {
-	runCmd := exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", containerID)
-	out, _, err := runCommandWithOutput(runCmd)
-	if err != nil {
-		c.Fatal(out, err)
-	}
-	return strings.Trim(out, "\r\n")
+	out, err := inspectField(containerID, "State.Running")
+	c.Assert(err, check.IsNil)
+	return out
 }
 
 func (s *DockerSuite) TestNetworkNat(c *check.C) {

+ 10 - 30
integration-cli/docker_cli_restart_test.go

@@ -112,11 +112,8 @@ func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
 		c.Errorf("expect 1 volume received %s", out)
 	}
 
-	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
-	volumes, _, err := runCommandWithOutput(runCmd)
-	if err != nil {
-		c.Fatal(volumes, err)
-	}
+	volumes, err := inspectField(cleanedContainerID, ".Volumes")
+	c.Assert(err, check.IsNil)
 
 	runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID)
 	if out, _, err = runCommandWithOutput(runCmd); err != nil {
@@ -133,15 +130,10 @@ func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
 		c.Errorf("expect 1 volume after restart received %s", out)
 	}
 
-	runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ .Volumes }}", cleanedContainerID)
-	volumesAfterRestart, _, err := runCommandWithOutput(runCmd)
-	if err != nil {
-		c.Fatal(volumesAfterRestart, err)
-	}
+	volumesAfterRestart, err := inspectField(cleanedContainerID, ".Volumes")
+	c.Assert(err, check.IsNil)
 
 	if volumes != volumesAfterRestart {
-		volumes = strings.Trim(volumes, " \n\r")
-		volumesAfterRestart = strings.Trim(volumesAfterRestart, " \n\r")
 		c.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
 	}
 
@@ -157,9 +149,7 @@ func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
 
 	id := strings.TrimSpace(string(out))
 	name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
-	if err != nil {
-		c.Fatal(err, out)
-	}
+	c.Assert(err, check.IsNil)
 	if name != "no" {
 		c.Fatalf("Container restart policy name is %s, expected %s", name, "no")
 	}
@@ -176,17 +166,13 @@ func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
 
 	id := strings.TrimSpace(string(out))
 	name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
-	if err != nil {
-		c.Fatal(err, out)
-	}
+	c.Assert(err, check.IsNil)
 	if name != "always" {
 		c.Fatalf("Container restart policy name is %s, expected %s", name, "always")
 	}
 
 	MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 
 	// MaximumRetryCount=0 if the restart policy is always
 	if MaximumRetryCount != "0" {
@@ -205,9 +191,7 @@ func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
 
 	id := strings.TrimSpace(string(out))
 	name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
-	if err != nil {
-		c.Fatal(err, out)
-	}
+	c.Assert(err, check.IsNil)
 	if name != "on-failure" {
 		c.Fatalf("Container restart policy name is %s, expected %s", name, "on-failure")
 	}
@@ -226,16 +210,12 @@ func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) {
 		c.Fatal(err)
 	}
 	count, err := inspectField(id, "RestartCount")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if count != "0" {
 		c.Fatalf("Container was restarted %s times, expected %d", count, 0)
 	}
 	MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if MaximumRetryCount != "3" {
 		c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
 	}

+ 2 - 2
integration-cli/docker_cli_rmi_test.go

@@ -101,8 +101,8 @@ func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
 			c.Fatalf("tag busybox to create 4 more images with same imageID; docker images shows: %q\n", imagesAfter)
 		}
 	}
-	out, _ = dockerCmd(c, "inspect", "-f", "{{.Id}}", "busybox-test")
-	imgID := strings.TrimSpace(out)
+	imgID, err := inspectField("busybox-test", "Id")
+	c.Assert(err, check.IsNil)
 
 	// first checkout without force it fails
 	runCmd = exec.Command(dockerBinary, "rmi", imgID)

+ 34 - 99
integration-cli/docker_cli_run_test.go

@@ -87,7 +87,7 @@ func (s *DockerSuite) TestRunEchoStdoutWithCPUAndMemoryLimit(c *check.C) {
 }
 
 // "test" should be printed
-func (s *DockerSuite) TestRunEchoStdoutWitCPUQuota(c *check.C) {
+func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
 	runCmd := exec.Command(dockerBinary, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test")
 	out, _, _, err := runCommandWithStdoutStderr(runCmd)
 	if err != nil {
@@ -101,12 +101,9 @@ func (s *DockerSuite) TestRunEchoStdoutWitCPUQuota(c *check.C) {
 		c.Errorf("container should've printed 'test'")
 	}
 
-	cmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.CpuQuota}}", "test")
-	out, _, err = runCommandWithOutput(cmd)
-	if err != nil {
-		c.Fatalf("failed to inspect container: %s, %v", out, err)
-	}
-	out = strings.TrimSpace(out)
+	out, err = inspectField("test", "HostConfig.CpuQuota")
+	c.Assert(err, check.IsNil)
+
 	if out != "8000" {
 		c.Errorf("setting the CPU CFS quota failed")
 	}
@@ -302,12 +299,8 @@ func (s *DockerSuite) TestRunLinksContainerWithContainerName(c *check.C) {
 	if err != nil {
 		c.Fatalf("failed to run container: %v, output: %q", err, out)
 	}
-	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.NetworkSettings.IPAddress}}", "parent")
-	ip, _, _, err := runCommandWithStdoutStderr(cmd)
-	if err != nil {
-		c.Fatalf("failed to inspect container: %v, output: %q", err, ip)
-	}
-	ip = strings.TrimSpace(ip)
+	ip, err := inspectField("parent", "NetworkSettings.IPAddress")
+	c.Assert(err, check.IsNil)
 	cmd = exec.Command(dockerBinary, "run", "--link", "parent:test", "busybox", "/bin/cat", "/etc/hosts")
 	out, _, err = runCommandWithOutput(cmd)
 	if err != nil {
@@ -326,12 +319,8 @@ func (s *DockerSuite) TestRunLinksContainerWithContainerId(c *check.C) {
 		c.Fatalf("failed to run container: %v, output: %q", err, cID)
 	}
 	cID = strings.TrimSpace(cID)
-	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.NetworkSettings.IPAddress}}", cID)
-	ip, _, _, err := runCommandWithStdoutStderr(cmd)
-	if err != nil {
-		c.Fatalf("failed to inspect container: %v, output: %q", err, ip)
-	}
-	ip = strings.TrimSpace(ip)
+	ip, err := inspectField(cID, "NetworkSettings.IPAddress")
+	c.Assert(err, check.IsNil)
 	cmd = exec.Command(dockerBinary, "run", "--link", cID+":test", "busybox", "/bin/cat", "/etc/hosts")
 	out, _, err := runCommandWithOutput(cmd)
 	if err != nil {
@@ -1175,12 +1164,8 @@ func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
 		c.Skip("Your kernel does not support CPU cfs period, skip this test")
 	}
 
-	cmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.CpuPeriod}}", "test")
-	out, _, err = runCommandWithOutput(cmd)
-	if err != nil {
-		c.Fatalf("failed to inspect container: %s, %v", out, err)
-	}
-	out = strings.TrimSpace(out)
+	out, err = inspectField("test", "HostConfig.CpuPeriod")
+	c.Assert(err, check.IsNil)
 	if out != "50000" {
 		c.Errorf("setting the CPU CFS period failed")
 	}
@@ -1753,16 +1738,12 @@ func (s *DockerSuite) TestRunState(c *check.C) {
 	}
 	id := strings.TrimSpace(out)
 	state, err := inspectField(id, "State.Running")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if state != "true" {
 		c.Fatal("Container state is 'not running'")
 	}
 	pid1, err := inspectField(id, "State.Pid")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if pid1 == "0" {
 		c.Fatal("Container state Pid 0")
 	}
@@ -1773,16 +1754,12 @@ func (s *DockerSuite) TestRunState(c *check.C) {
 		c.Fatal(err, out)
 	}
 	state, err = inspectField(id, "State.Running")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if state != "false" {
 		c.Fatal("Container state is 'running'")
 	}
 	pid2, err := inspectField(id, "State.Pid")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if pid2 == pid1 {
 		c.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
 	}
@@ -1793,16 +1770,12 @@ func (s *DockerSuite) TestRunState(c *check.C) {
 		c.Fatal(err, out)
 	}
 	state, err = inspectField(id, "State.Running")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if state != "true" {
 		c.Fatal("Container state is 'not running'")
 	}
 	pid3, err := inspectField(id, "State.Pid")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if pid3 == pid1 {
 		c.Fatalf("Container state Pid %s, but expected %s", pid2, pid1)
 	}
@@ -2178,9 +2151,7 @@ func (s *DockerSuite) TestRunNetworkNotInitializedNoneMode(c *check.C) {
 	}
 	id := strings.TrimSpace(out)
 	res, err := inspectField(id, "NetworkSettings.IPAddress")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if res != "" {
 		c.Fatalf("For 'none' mode network must not be initialized, but container got IP: %s", res)
 	}
@@ -2209,9 +2180,7 @@ func (s *DockerSuite) TestRunInspectMacAddress(c *check.C) {
 	}
 	id := strings.TrimSpace(out)
 	inspectedMac, err := inspectField(id, "NetworkSettings.MacAddress")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if inspectedMac != mac {
 		c.Fatalf("docker inspect outputs wrong MAC address: %q, should be: %q", inspectedMac, mac)
 	}
@@ -2237,9 +2206,7 @@ func (s *DockerSuite) TestRunDeallocatePortOnMissingIptablesRule(c *check.C) {
 	}
 	id := strings.TrimSpace(out)
 	ip, err := inspectField(id, "NetworkSettings.IPAddress")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	iptCmd := exec.Command("iptables", "-D", "DOCKER", "-d", fmt.Sprintf("%s/32", ip),
 		"!", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-m", "tcp", "--dport", "23", "-j", "ACCEPT")
 	out, _, err = runCommandWithOutput(iptCmd)
@@ -2499,32 +2466,24 @@ func (s *DockerSuite) TestRunVolumesCleanPaths(c *check.C) {
 	}
 
 	out, err := inspectFieldMap("dark_helmet", "Volumes", "/foo/")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if out != "" {
 		c.Fatalf("Found unexpected volume entry for '/foo/' in volumes\n%q", out)
 	}
 
 	out, err = inspectFieldMap("dark_helmet", "Volumes", "/foo")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if !strings.Contains(out, volumesStoragePath) {
 		c.Fatalf("Volume was not defined for /foo\n%q", out)
 	}
 
 	out, err = inspectFieldMap("dark_helmet", "Volumes", "/bar/")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if out != "" {
 		c.Fatalf("Found unexpected volume entry for '/bar/' in volumes\n%q", out)
 	}
 	out, err = inspectFieldMap("dark_helmet", "Volumes", "/bar")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if !strings.Contains(out, volumesStoragePath) {
 		c.Fatalf("Volume was not defined for /bar\n%q", out)
 	}
@@ -2561,9 +2520,7 @@ func (s *DockerSuite) TestRunAllowPortRangeThroughExpose(c *check.C) {
 	}
 	id := strings.TrimSpace(out)
 	portstr, err := inspectFieldJSON(id, "NetworkSettings.Ports")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	var ports nat.PortMap
 	if err = unmarshalJSON([]byte(portstr), &ports); err != nil {
 		c.Fatal(err)
@@ -2604,14 +2561,8 @@ func (s *DockerSuite) TestRunUnknownCommand(c *check.C) {
 	runCmd = exec.Command(dockerBinary, "start", cID)
 	_, _, _, _ = runCommandWithStdoutStderr(runCmd)
 
-	runCmd = exec.Command(dockerBinary, "inspect", "--format={{.State.ExitCode}}", cID)
-	rc, _, _, err2 := runCommandWithStdoutStderr(runCmd)
-	rc = strings.TrimSpace(rc)
-
-	if err2 != nil {
-		c.Fatalf("Error getting status of container: %v", err2)
-	}
-
+	rc, err := inspectField(cID, "State.ExitCode")
+	c.Assert(err, check.IsNil)
 	if rc == "0" {
 		c.Fatalf("ExitCode(%v) cannot be 0", rc)
 	}
@@ -2658,16 +2609,12 @@ func (s *DockerSuite) TestRunModeIpcContainer(c *check.C) {
 	}
 	id := strings.TrimSpace(out)
 	state, err := inspectField(id, "State.Running")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if state != "true" {
 		c.Fatal("Container state is 'not running'")
 	}
 	pid1, err := inspectField(id, "State.Pid")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 
 	parentContainerIpc, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/ipc", pid1))
 	if err != nil {
@@ -2706,9 +2653,7 @@ func (s *DockerSuite) TestContainerNetworkMode(c *check.C) {
 		c.Fatal(err)
 	}
 	pid1, err := inspectField(id, "State.Pid")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 
 	parentContainerNet, err := os.Readlink(fmt.Sprintf("/proc/%s/ns/net", pid1))
 	if err != nil {
@@ -2963,9 +2908,7 @@ func (s *DockerSuite) TestRunAllowPortRangeThroughPublish(c *check.C) {
 
 	id := strings.TrimSpace(out)
 	portstr, err := inspectFieldJSON(id, "NetworkSettings.Ports")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	var ports nat.PortMap
 	err = unmarshalJSON([]byte(portstr), &ports)
 	for port, binding := range ports {
@@ -3003,12 +2946,8 @@ func (s *DockerSuite) TestRunSetDefaultRestartPolicy(c *check.C) {
 	if out, _, err := runCommandWithOutput(runCmd); err != nil {
 		c.Fatalf("failed to run container: %v, output: %q", err, out)
 	}
-	cmd := exec.Command(dockerBinary, "inspect", "-f", "{{.HostConfig.RestartPolicy.Name}}", "test")
-	out, _, err := runCommandWithOutput(cmd)
-	if err != nil {
-		c.Fatalf("failed to inspect container: %v, output: %q", err, out)
-	}
-	out = strings.Trim(out, "\r\n")
+	out, err := inspectField("test", "HostConfig.RestartPolicy.Name")
+	c.Assert(err, check.IsNil)
 	if out != "no" {
 		c.Fatalf("Set default restart policy failed")
 	}
@@ -3024,16 +2963,12 @@ func (s *DockerSuite) TestRunRestartMaxRetries(c *check.C) {
 		c.Fatal(err)
 	}
 	count, err := inspectField(id, "RestartCount")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if count != "3" {
 		c.Fatalf("Container was restarted %s times, expected %d", count, 3)
 	}
 	MaximumRetryCount, err := inspectField(id, "HostConfig.RestartPolicy.MaximumRetryCount")
-	if err != nil {
-		c.Fatal(err)
-	}
+	c.Assert(err, check.IsNil)
 	if MaximumRetryCount != "3" {
 		c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "3")
 	}

+ 8 - 24
integration-cli/docker_cli_start_test.go

@@ -98,9 +98,7 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
 	// when container runs successfully, we should not have state.Error
 	dockerCmd(c, "run", "-d", "-p", "9999:9999", "--name", "test", "busybox", "top")
 	stateErr, err := inspectField("test", "State.Error")
-	if err != nil {
-		c.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
-	}
+	c.Assert(err, check.IsNil)
 	if stateErr != "" {
 		c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
 	}
@@ -111,9 +109,7 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
 		c.Fatalf("Expected error but got none, output %q", out)
 	}
 	stateErr, err = inspectField("test2", "State.Error")
-	if err != nil {
-		c.Fatalf("Failed to inspect %q state's error, got error %q", "test2", err)
-	}
+	c.Assert(err, check.IsNil)
 	expected := "port is already allocated"
 	if stateErr == "" || !strings.Contains(stateErr, expected) {
 		c.Fatalf("State.Error(%q) does not include %q", stateErr, expected)
@@ -123,9 +119,7 @@ func (s *DockerSuite) TestStartRecordError(c *check.C) {
 	dockerCmd(c, "stop", "test")
 	dockerCmd(c, "start", "test2")
 	stateErr, err = inspectField("test2", "State.Error")
-	if err != nil {
-		c.Fatalf("Failed to inspect %q state's error, got error %q", "test", err)
-	}
+	c.Assert(err, check.IsNil)
 	if stateErr != "" {
 		c.Fatalf("Expected to not have state error but got state.Error(%q)", stateErr)
 	}
@@ -196,12 +190,8 @@ func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
 	if out, _, err := runCommandWithOutput(cmd); err != nil {
 		c.Fatal(out, err)
 	}
-	cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", "parent")
-	out, _, err := runCommandWithOutput(cmd)
-	if err != nil {
-		c.Fatal(out, err)
-	}
-	out = strings.Trim(out, "\r\n")
+	out, err := inspectField("parent", "State.Running")
+	c.Assert(err, check.IsNil)
 	if out != "false" {
 		c.Fatal("Container should be stopped")
 	}
@@ -215,12 +205,8 @@ func (s *DockerSuite) TestStartMultipleContainers(c *check.C) {
 	}
 
 	for container, expected := range map[string]string{"parent": "true", "child_first": "false", "child_second": "true"} {
-		cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", container)
-		out, _, err = runCommandWithOutput(cmd)
-		if err != nil {
-			c.Fatal(out, err)
-		}
-		out = strings.Trim(out, "\r\n")
+		out, err := inspectField(container, "State.Running")
+		c.Assert(err, check.IsNil)
 		if out != expected {
 			c.Fatal("Container running state wrong")
 		}
@@ -260,12 +246,10 @@ func (s *DockerSuite) TestStartAttachMultipleContainers(c *check.C) {
 
 	// confirm the state of all the containers be stopped
 	for container, expected := range map[string]string{"test1": "false", "test2": "false", "test3": "false"} {
-		cmd = exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", container)
-		out, _, err := runCommandWithOutput(cmd)
+		out, err := inspectField(container, "State.Running")
 		if err != nil {
 			c.Fatal(out, err)
 		}
-		out = strings.Trim(out, "\r\n")
 		if out != expected {
 			c.Fatal("Container running state wrong")
 		}

+ 4 - 9
integration-cli/docker_cli_tag_test.go

@@ -22,15 +22,10 @@ func (s *DockerSuite) TestTagUnprefixedRepoByName(c *check.C) {
 
 // tagging an image by ID in a new unprefixed repo should work
 func (s *DockerSuite) TestTagUnprefixedRepoByID(c *check.C) {
-	getIDCmd := exec.Command(dockerBinary, "inspect", "-f", "{{.Id}}", "busybox")
-	out, _, err := runCommandWithOutput(getIDCmd)
-	if err != nil {
-		c.Fatalf("failed to get the image ID of busybox: %s, %v", out, err)
-	}
-
-	cleanedImageID := strings.TrimSpace(out)
-	tagCmd := exec.Command(dockerBinary, "tag", cleanedImageID, "testfoobarbaz")
-	if out, _, err = runCommandWithOutput(tagCmd); err != nil {
+	imageID, err := inspectField("busybox", "Id")
+	c.Assert(err, check.IsNil)
+	tagCmd := exec.Command(dockerBinary, "tag", imageID, "testfoobarbaz")
+	if out, _, err := runCommandWithOutput(tagCmd); err != nil {
 		c.Fatal(out, err)
 	}
 }

+ 4 - 12
integration-cli/docker_cli_wait_test.go

@@ -21,12 +21,8 @@ func (s *DockerSuite) TestWaitNonBlockedExitZero(c *check.C) {
 
 	status := "true"
 	for i := 0; status != "false"; i++ {
-		runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.Running}}'", containerID)
-		status, _, err = runCommandWithOutput(runCmd)
-		if err != nil {
-			c.Fatal(status, err)
-		}
-		status = strings.TrimSpace(status)
+		status, err = inspectField(containerID, "State.Running")
+		c.Assert(err, check.IsNil)
 
 		time.Sleep(time.Second)
 		if i >= 60 {
@@ -84,12 +80,8 @@ func (s *DockerSuite) TestWaitNonBlockedExitRandom(c *check.C) {
 
 	status := "true"
 	for i := 0; status != "false"; i++ {
-		runCmd = exec.Command(dockerBinary, "inspect", "--format='{{.State.Running}}'", containerID)
-		status, _, err = runCommandWithOutput(runCmd)
-		if err != nil {
-			c.Fatal(status, err)
-		}
-		status = strings.TrimSpace(status)
+		status, err = inspectField(containerID, "State.Running")
+		c.Assert(err, check.IsNil)
 
 		time.Sleep(time.Second)
 		if i >= 60 {