From 71868228c787f54501abec6556364d2ceaa4e645 Mon Sep 17 00:00:00 2001 From: Hu Keping Date: Wed, 15 Jul 2015 04:15:00 +0800 Subject: [PATCH] Refactor : Use dockerCmd in integration-cli tests Signed-off-by: Hu Keping --- integration-cli/docker_cli_rename_test.go | 56 ++------ integration-cli/docker_cli_restart_test.go | 116 +++------------- integration-cli/docker_cli_rm_test.go | 54 ++------ integration-cli/docker_cli_rmi_test.go | 71 ++++------ integration-cli/docker_cli_run_unix_test.go | 138 +++++--------------- 5 files changed, 99 insertions(+), 336 deletions(-) diff --git a/integration-cli/docker_cli_rename_test.go b/integration-cli/docker_cli_rename_test.go index 156ea6eeb3..cac9f3aa9c 100644 --- a/integration-cli/docker_cli_rename_test.go +++ b/integration-cli/docker_cli_rename_test.go @@ -1,7 +1,6 @@ package main import ( - "os/exec" "strings" "github.com/docker/docker/pkg/stringid" @@ -9,28 +8,14 @@ import ( ) func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) { - runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh") - out, _, err := runCommandWithOutput(runCmd) - if err != nil { - c.Fatalf(out, err) - } + out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh") cleanedContainerID := strings.TrimSpace(out) - - runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID) - out, _, err = runCommandWithOutput(runCmd) - if err != nil { - c.Fatalf(out, err) - } + dockerCmd(c, "wait", cleanedContainerID) name, err := inspectField(cleanedContainerID, "Name") - newName := "new_name" + stringid.GenerateRandomID() - runCmd = exec.Command(dockerBinary, "rename", "first_name", newName) - out, _, err = runCommandWithOutput(runCmd) - if err != nil { - c.Fatalf(out, err) - } + dockerCmd(c, "rename", "first_name", newName) name, err = inspectField(cleanedContainerID, "Name") if err != nil { @@ -43,19 +28,11 @@ func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) { } func (s *DockerSuite) TestRenameRunningContainer(c *check.C) { - runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh") - out, _, err := runCommandWithOutput(runCmd) - if err != nil { - c.Fatalf(out, err) - } + out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh") newName := "new_name" + stringid.GenerateRandomID() cleanedContainerID := strings.TrimSpace(out) - runCmd = exec.Command(dockerBinary, "rename", "first_name", newName) - out, _, err = runCommandWithOutput(runCmd) - if err != nil { - c.Fatalf(out, err) - } + dockerCmd(c, "rename", "first_name", newName) name, err := inspectField(cleanedContainerID, "Name") if err != nil { @@ -67,18 +44,10 @@ func (s *DockerSuite) TestRenameRunningContainer(c *check.C) { } func (s *DockerSuite) TestRenameCheckNames(c *check.C) { - runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh") - out, _, err := runCommandWithOutput(runCmd) - if err != nil { - c.Fatalf(out, err) - } + dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh") newName := "new_name" + stringid.GenerateRandomID() - runCmd = exec.Command(dockerBinary, "rename", "first_name", newName) - out, _, err = runCommandWithOutput(runCmd) - if err != nil { - c.Fatalf(out, err) - } + dockerCmd(c, "rename", "first_name", newName) name, err := inspectField(newName, "Name") if err != nil { @@ -95,18 +64,13 @@ func (s *DockerSuite) TestRenameCheckNames(c *check.C) { } func (s *DockerSuite) TestRenameInvalidName(c *check.C) { - runCmd := exec.Command(dockerBinary, "run", "--name", "myname", "-d", "busybox", "top") - if out, _, err := runCommandWithOutput(runCmd); err != nil { - c.Fatalf(out, err) - } + dockerCmd(c, "run", "--name", "myname", "-d", "busybox", "top") - runCmd = exec.Command(dockerBinary, "rename", "myname", "new:invalid") - if out, _, err := runCommandWithOutput(runCmd); err == nil || !strings.Contains(out, "Invalid container name") { + if out, _, err := dockerCmdWithError(c, "rename", "myname", "new:invalid"); err == nil || !strings.Contains(out, "Invalid container name") { c.Fatalf("Renaming container to invalid name should have failed: %s\n%v", out, err) } - runCmd = exec.Command(dockerBinary, "ps", "-a") - if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "myname") { + if out, _, err := dockerCmdWithError(c, "ps", "-a"); err != nil || !strings.Contains(out, "myname") { c.Fatalf("Output of docker ps should have included 'myname': %s\n%v", out, err) } } diff --git a/integration-cli/docker_cli_restart_test.go b/integration-cli/docker_cli_restart_test.go index 7988017f30..c8bb0b1bcc 100644 --- a/integration-cli/docker_cli_restart_test.go +++ b/integration-cli/docker_cli_restart_test.go @@ -1,7 +1,6 @@ package main import ( - "os/exec" "strings" "time" @@ -9,104 +8,53 @@ import ( ) func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) { - - runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar") - out, _, err := runCommandWithOutput(runCmd) - if err != nil { - c.Fatal(out, err) - } + out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "foobar") cleanedContainerID := strings.TrimSpace(out) + dockerCmd(c, "wait", cleanedContainerID) - runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID) - if out, _, err = runCommandWithOutput(runCmd); err != nil { - c.Fatal(out, err) - } - - runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) - out, _, err = runCommandWithOutput(runCmd) - if err != nil { - c.Fatal(out, err) - } - + out, _ = dockerCmd(c, "logs", cleanedContainerID) if out != "foobar\n" { c.Errorf("container should've printed 'foobar'") } - runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID) - if out, _, err = runCommandWithOutput(runCmd); err != nil { - c.Fatal(out, err) - } - - runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) - out, _, err = runCommandWithOutput(runCmd) - if err != nil { - c.Fatal(out, err) - } + dockerCmd(c, "restart", cleanedContainerID) + out, _ = dockerCmd(c, "logs", cleanedContainerID) if out != "foobar\nfoobar\n" { c.Errorf("container should've printed 'foobar' twice") } - } func (s *DockerSuite) TestRestartRunningContainer(c *check.C) { - - runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'") - out, _, err := runCommandWithOutput(runCmd) - if err != nil { - c.Fatal(out, err) - } + out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'") cleanedContainerID := strings.TrimSpace(out) time.Sleep(1 * time.Second) - runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) - out, _, err = runCommandWithOutput(runCmd) - if err != nil { - c.Fatal(out, err) - } - + out, _ = dockerCmd(c, "logs", cleanedContainerID) if out != "foobar\n" { c.Errorf("container should've printed 'foobar'") } - runCmd = exec.Command(dockerBinary, "restart", "-t", "1", cleanedContainerID) - if out, _, err = runCommandWithOutput(runCmd); err != nil { - c.Fatal(out, err) - } + dockerCmd(c, "restart", "-t", "1", cleanedContainerID) - runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) - out, _, err = runCommandWithOutput(runCmd) - if err != nil { - c.Fatal(out, err) - } + out, _ = dockerCmd(c, "logs", cleanedContainerID) time.Sleep(1 * time.Second) if out != "foobar\nfoobar\n" { c.Errorf("container should've printed 'foobar' twice") } - } // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819. func (s *DockerSuite) TestRestartWithVolumes(c *check.C) { - - runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top") - out, _, err := runCommandWithOutput(runCmd) - if err != nil { - c.Fatal(out, err) - } + out, _ := dockerCmd(c, "run", "-d", "-v", "/test", "busybox", "top") cleanedContainerID := strings.TrimSpace(out) - - runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID) - out, _, err = runCommandWithOutput(runCmd) - if err != nil { - c.Fatal(out, err) - } + out, _ = dockerCmd(c, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID) if out = strings.Trim(out, " \n\r"); out != "1" { c.Errorf("expect 1 volume received %s", out) @@ -115,17 +63,9 @@ func (s *DockerSuite) TestRestartWithVolumes(c *check.C) { volumes, err := inspectField(cleanedContainerID, "Volumes") c.Assert(err, check.IsNil) - runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID) - if out, _, err = runCommandWithOutput(runCmd); err != nil { - c.Fatal(out, err) - } - - runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID) - out, _, err = runCommandWithOutput(runCmd) - if err != nil { - c.Fatal(out, err) - } + dockerCmd(c, "restart", cleanedContainerID) + out, _ = dockerCmd(c, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID) if out = strings.Trim(out, " \n\r"); out != "1" { c.Errorf("expect 1 volume after restart received %s", out) } @@ -136,16 +76,10 @@ func (s *DockerSuite) TestRestartWithVolumes(c *check.C) { if volumes != volumesAfterRestart { c.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart) } - } func (s *DockerSuite) TestRestartPolicyNO(c *check.C) { - - cmd := exec.Command(dockerBinary, "run", "-d", "--restart=no", "busybox", "false") - out, _, err := runCommandWithOutput(cmd) - if err != nil { - c.Fatal(err, out) - } + out, _ := dockerCmd(c, "run", "-d", "--restart=no", "busybox", "false") id := strings.TrimSpace(string(out)) name, err := inspectField(id, "HostConfig.RestartPolicy.Name") @@ -153,16 +87,10 @@ func (s *DockerSuite) TestRestartPolicyNO(c *check.C) { if name != "no" { c.Fatalf("Container restart policy name is %s, expected %s", name, "no") } - } func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) { - - cmd := exec.Command(dockerBinary, "run", "-d", "--restart=always", "busybox", "false") - out, _, err := runCommandWithOutput(cmd) - if err != nil { - c.Fatal(err, out) - } + out, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false") id := strings.TrimSpace(string(out)) name, err := inspectField(id, "HostConfig.RestartPolicy.Name") @@ -178,16 +106,10 @@ func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) { if MaximumRetryCount != "0" { c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "0") } - } func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) { - - cmd := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:1", "busybox", "false") - out, _, err := runCommandWithOutput(cmd) - if err != nil { - c.Fatal(err, out) - } + out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:1", "busybox", "false") id := strings.TrimSpace(string(out)) name, err := inspectField(id, "HostConfig.RestartPolicy.Name") @@ -201,10 +123,8 @@ func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) { // a good container with --restart=on-failure:3 // MaximumRetryCount!=0; RestartCount=0 func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) { - out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "true").CombinedOutput() - if err != nil { - c.Fatal(string(out), err) - } + out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true") + id := strings.TrimSpace(string(out)) if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5); err != nil { c.Fatal(err) diff --git a/integration-cli/docker_cli_rm_test.go b/integration-cli/docker_cli_rm_test.go index f5884dc0fc..0e57551b50 100644 --- a/integration-cli/docker_cli_rm_test.go +++ b/integration-cli/docker_cli_rm_test.go @@ -2,7 +2,6 @@ package main import ( "os" - "os/exec" "strings" "github.com/go-check/check" @@ -11,58 +10,34 @@ import ( func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) { testRequires(c, SameHostDaemon) - cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true") - if _, err := runCommand(cmd); err != nil { - c.Fatal(err) - } + dockerCmd(c, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true") if err := os.Remove("/tmp/testing"); err != nil { c.Fatal(err) } - cmd = exec.Command(dockerBinary, "rm", "-v", "losemyvolumes") - if out, _, err := runCommandWithOutput(cmd); err != nil { - c.Fatal(out, err) - } - + dockerCmd(c, "rm", "-v", "losemyvolumes") } func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) { + dockerCmd(c, "run", "--name", "foo", "-v", "/srv", "busybox", "true") - cmd := exec.Command(dockerBinary, "run", "--name", "foo", "-v", "/srv", "busybox", "true") - if _, err := runCommand(cmd); err != nil { - c.Fatal(err) - } - - cmd = exec.Command(dockerBinary, "rm", "-v", "foo") - if _, err := runCommand(cmd); err != nil { - c.Fatal(err) - } - + dockerCmd(c, "rm", "-v", "foo") } func (s *DockerSuite) TestRmRunningContainer(c *check.C) { - createRunningContainer(c, "foo") - // Test cannot remove running container - cmd := exec.Command(dockerBinary, "rm", "foo") - if _, err := runCommand(cmd); err == nil { + if _, _, err := dockerCmdWithError(c, "rm", "foo"); err == nil { c.Fatalf("Expected error, can't rm a running container") } - } func (s *DockerSuite) TestRmForceRemoveRunningContainer(c *check.C) { - createRunningContainer(c, "foo") // Stop then remove with -s - cmd := exec.Command(dockerBinary, "rm", "-f", "foo") - if _, err := runCommand(cmd); err != nil { - c.Fatal(err) - } - + dockerCmd(c, "rm", "-f", "foo") } func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) { @@ -80,40 +55,37 @@ func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) { c.Fatalf("Could not build image %s: %v", img, err) } // run container on first image - if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", img)); err != nil { + if out, _, err := dockerCmdWithError(c, "run", img); err != nil { c.Fatalf("Could not run image %s: %v: %s", img, err, out) } + // rebuild dockerfile with a small addition at the end if _, err := buildImage(img, dockerfile2, true); err != nil { c.Fatalf("Could not rebuild image %s: %v", img, err) } // try to remove the image, should error out. - if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", img)); err == nil { + if out, _, err := dockerCmdWithError(c, "rmi", img); err == nil { c.Fatalf("Expected to error out removing the image, but succeeded: %s", out) } + // check if we deleted the first image - out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "images", "-q", "--no-trunc")) + out, _, err := dockerCmdWithError(c, "images", "-q", "--no-trunc") if err != nil { c.Fatalf("%v: %s", err, out) } if !strings.Contains(out, img1) { c.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out) } - } func (s *DockerSuite) TestRmInvalidContainer(c *check.C) { - if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "unknown")); err == nil { + if out, _, err := dockerCmdWithError(c, "rm", "unknown"); err == nil { c.Fatal("Expected error on rm unknown container, got none") } else if !strings.Contains(out, "failed to remove containers") { c.Fatalf("Expected output to contain 'failed to remove containers', got %q", out) } - } func createRunningContainer(c *check.C, name string) { - cmd := exec.Command(dockerBinary, "run", "-dt", "--name", name, "busybox", "top") - if _, err := runCommand(cmd); err != nil { - c.Fatal(err) - } + dockerCmd(c, "run", "-dt", "--name", name, "busybox", "top") } diff --git a/integration-cli/docker_cli_rmi_test.go b/integration-cli/docker_cli_rmi_test.go index 3863f6006d..9f0288f126 100644 --- a/integration-cli/docker_cli_rmi_test.go +++ b/integration-cli/docker_cli_rmi_test.go @@ -12,8 +12,7 @@ func (s *DockerSuite) TestRmiWithContainerFails(c *check.C) { errSubstr := "is using it" // create a container - runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true") - out, _, err := runCommandWithOutput(runCmd) + out, _, err := dockerCmdWithError(c, "run", "-d", "busybox", "true") if err != nil { c.Fatalf("failed to create a container: %s, %v", out, err) } @@ -21,8 +20,7 @@ func (s *DockerSuite) TestRmiWithContainerFails(c *check.C) { cleanedContainerID := strings.TrimSpace(out) // try to delete the image - runCmd = exec.Command(dockerBinary, "rmi", "busybox") - out, _, err = runCommandWithOutput(runCmd) + out, _, err = dockerCmdWithError(c, "rmi", "busybox") if err == nil { c.Fatalf("Container %q is using image, should not be able to rmi: %q", cleanedContainerID, out) } @@ -75,14 +73,13 @@ func (s *DockerSuite) TestRmiTag(c *check.C) { } func (s *DockerSuite) TestRmiImgIDMultipleTag(c *check.C) { - runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'") - out, _, err := runCommandWithOutput(runCmd) + out, _, err := dockerCmdWithError(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'") if err != nil { c.Fatalf("failed to create a container:%s, %v", out, err) } + containerID := strings.TrimSpace(out) - runCmd = exec.Command(dockerBinary, "commit", containerID, "busybox-one") - out, _, err = runCommandWithOutput(runCmd) + out, _, err = dockerCmdWithError(c, "commit", containerID, "busybox-one") if err != nil { c.Fatalf("failed to commit a new busybox-one:%s, %v", out, err) } @@ -100,14 +97,15 @@ func (s *DockerSuite) TestRmiImgIDMultipleTag(c *check.C) { c.Assert(err, check.IsNil) // run a container with the image - out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "busybox-one", "top")) + out, _, err = dockerCmdWithError(c, "run", "-d", "busybox-one", "top") if err != nil { c.Fatalf("failed to create a container:%s, %v", out, err) } + containerID = strings.TrimSpace(out) // first checkout without force it fails - out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "rmi", imgID)) + out, _, err = dockerCmdWithError(c, "rmi", imgID) expected := fmt.Sprintf("Conflict, cannot delete %s because the running container %s is using it, stop it and use -f to force", imgID[:12], containerID[:12]) if err == nil || !strings.Contains(out, expected) { c.Fatalf("rmi tagged in multiple repos should have failed without force: %s, %v, expected: %s", out, err, expected) @@ -120,18 +118,16 @@ func (s *DockerSuite) TestRmiImgIDMultipleTag(c *check.C) { if strings.Contains(imagesAfter, imgID[:12]) { c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter) } - } func (s *DockerSuite) TestRmiImgIDForce(c *check.C) { - runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'") - out, _, err := runCommandWithOutput(runCmd) + out, _, err := dockerCmdWithError(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'") if err != nil { c.Fatalf("failed to create a container:%s, %v", out, err) } + containerID := strings.TrimSpace(out) - runCmd = exec.Command(dockerBinary, "commit", containerID, "busybox-test") - out, _, err = runCommandWithOutput(runCmd) + out, _, err = dockerCmdWithError(c, "commit", containerID, "busybox-test") if err != nil { c.Fatalf("failed to commit a new busybox-test:%s, %v", out, err) } @@ -151,8 +147,7 @@ func (s *DockerSuite) TestRmiImgIDForce(c *check.C) { c.Assert(err, check.IsNil) // first checkout without force it fails - runCmd = exec.Command(dockerBinary, "rmi", imgID) - out, _, err = runCommandWithOutput(runCmd) + out, _, err = dockerCmdWithError(c, "rmi", imgID) if err == nil || !strings.Contains(out, fmt.Sprintf("Conflict, cannot delete image %s because it is tagged in multiple repositories, use -f to force", imgID)) { c.Fatalf("rmi tagged in multiple repos should have failed without force:%s, %v", out, err) } @@ -163,7 +158,6 @@ func (s *DockerSuite) TestRmiImgIDForce(c *check.C) { if strings.Contains(imagesAfter, imgID[:12]) { c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter) } - } } @@ -171,24 +165,22 @@ func (s *DockerSuite) TestRmiTagWithExistingContainers(c *check.C) { container := "test-delete-tag" newtag := "busybox:newtag" bb := "busybox:latest" - if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "tag", bb, newtag)); err != nil { + if out, _, err := dockerCmdWithError(c, "tag", bb, newtag); err != nil { c.Fatalf("Could not tag busybox: %v: %s", err, out) } - if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", container, bb, "/bin/true")); err != nil { + if out, _, err := dockerCmdWithError(c, "run", "--name", container, bb, "/bin/true"); err != nil { c.Fatalf("Could not run busybox: %v: %s", err, out) } - out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", newtag)) + out, _, err := dockerCmdWithError(c, "rmi", newtag) if err != nil { c.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out) } if d := strings.Count(out, "Untagged: "); d != 1 { c.Fatalf("Expected 1 untagged entry got %d: %q", d, out) } - } func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) { - image := "busybox-clone" cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-") @@ -199,71 +191,60 @@ MAINTAINER foo`) c.Fatalf("Could not build %s: %s, %v", image, out, err) } - if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "test-force-rmi", image, "/bin/true")); err != nil { + if out, _, err := dockerCmdWithError(c, "run", "--name", "test-force-rmi", image, "/bin/true"); err != nil { c.Fatalf("Could not run container: %s, %v", out, err) } - out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", "-f", image)) - if err != nil { + if out, _, err := dockerCmdWithError(c, "rmi", "-f", image); err != nil { c.Fatalf("Could not remove image %s: %s, %v", image, out, err) } - } func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) { newRepo := "127.0.0.1:5000/busybox" oldRepo := "busybox" newTag := "busybox:test" - cmd := exec.Command(dockerBinary, "tag", oldRepo, newRepo) - out, _, err := runCommandWithOutput(cmd) + out, _, err := dockerCmdWithError(c, "tag", oldRepo, newRepo) if err != nil { c.Fatalf("Could not tag busybox: %v: %s", err, out) } - cmd = exec.Command(dockerBinary, "run", "--name", "test", oldRepo, "touch", "/home/abcd") - out, _, err = runCommandWithOutput(cmd) + + out, _, err = dockerCmdWithError(c, "run", "--name", "test", oldRepo, "touch", "/home/abcd") if err != nil { c.Fatalf("failed to run container: %v, output: %s", err, out) } - cmd = exec.Command(dockerBinary, "commit", "test", newTag) - out, _, err = runCommandWithOutput(cmd) + + out, _, err = dockerCmdWithError(c, "commit", "test", newTag) if err != nil { c.Fatalf("failed to commit container: %v, output: %s", err, out) } - cmd = exec.Command(dockerBinary, "rmi", newTag) - out, _, err = runCommandWithOutput(cmd) + + out, _, err = dockerCmdWithError(c, "rmi", newTag) if err != nil { c.Fatalf("failed to remove image: %v, output: %s", err, out) } if !strings.Contains(out, "Untagged: "+newTag) { c.Fatalf("Could not remove image %s: %s, %v", newTag, out, err) } - } func (s *DockerSuite) TestRmiBlank(c *check.C) { // try to delete a blank image name - runCmd := exec.Command(dockerBinary, "rmi", "") - out, _, err := runCommandWithOutput(runCmd) - + out, _, err := dockerCmdWithError(c, "rmi", "") if err == nil { c.Fatal("Should have failed to delete '' image") } - if strings.Contains(out, "No such image") { c.Fatalf("Wrong error message generated: %s", out) } - if !strings.Contains(out, "Image name can not be blank") { c.Fatalf("Expected error message not generated: %s", out) } - runCmd = exec.Command(dockerBinary, "rmi", " ") - out, _, err = runCommandWithOutput(runCmd) - + out, _, err = dockerCmdWithError(c, "rmi", " ") if err == nil { c.Fatal("Should have failed to delete '' image") } - if !strings.Contains(out, "No such image") { c.Fatalf("Expected error message not generated: %s", out) } diff --git a/integration-cli/docker_cli_run_unix_test.go b/integration-cli/docker_cli_run_unix_test.go index 2057182b41..8da0fc769b 100644 --- a/integration-cli/docker_cli_run_unix_test.go +++ b/integration-cli/docker_cli_run_unix_test.go @@ -88,11 +88,8 @@ func (s *DockerSuite) TestRunWithVolumesIsRecursive(c *check.C) { func (s *DockerSuite) TestRunWithUlimits(c *check.C) { testRequires(c, NativeExecDriver) - out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--name=testulimits", "--ulimit", "nofile=42", "busybox", "/bin/sh", "-c", "ulimit -n")) - if err != nil { - c.Fatal(err, out) - } + out, _ := dockerCmd(c, "run", "--name=testulimits", "--ulimit", "nofile=42", "busybox", "/bin/sh", "-c", "ulimit -n") ul := strings.TrimSpace(out) if ul != "42" { c.Fatalf("expected `ulimit -n` to be 42, got %s", ul) @@ -113,7 +110,7 @@ func (s *DockerSuite) TestRunContainerWithCgroupParent(c *check.C) { c.Fatalf("unable to find self cpu cgroup path. CgroupsPath: %v", selfCgroupPaths) } - out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup")) + out, _, err := dockerCmdWithError(c, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup") if err != nil { c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err) } @@ -138,8 +135,7 @@ func (s *DockerSuite) TestRunContainerWithCgroupParentAbsPath(c *check.C) { testRequires(c, NativeExecDriver) cgroupParent := "/cgroup-parent/test" - - out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup")) + out, _, err := dockerCmdWithError(c, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup") if err != nil { c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err) } @@ -163,8 +159,7 @@ func (s *DockerSuite) TestRunContainerWithCgroupMountRO(c *check.C) { testRequires(c, NativeExecDriver) filename := "/sys/fs/cgroup/devices/test123" - cmd := exec.Command(dockerBinary, "run", "busybox", "touch", filename) - out, _, err := runCommandWithOutput(cmd) + out, _, err := dockerCmdWithError(c, "run", "busybox", "touch", filename) if err == nil { c.Fatal("expected cgroup mount point to be read-only, touch file should fail") } @@ -176,24 +171,13 @@ func (s *DockerSuite) TestRunContainerWithCgroupMountRO(c *check.C) { func (s *DockerSuite) TestRunDeviceDirectory(c *check.C) { testRequires(c, NativeExecDriver) - cmd := exec.Command(dockerBinary, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/") - - out, _, err := runCommandWithOutput(cmd) - if err != nil { - c.Fatal(err, out) - } + out, _ := dockerCmd(c, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/") if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "timer") { c.Fatalf("expected output /dev/snd/timer, received %s", actual) } - cmd = exec.Command(dockerBinary, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/") - - out, _, err = runCommandWithOutput(cmd) - if err != nil { - c.Fatal(err, out) - } - + out, _ = dockerCmd(c, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/") if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "seq") { c.Fatalf("expected output /dev/othersnd/seq, received %s", actual) } @@ -269,8 +253,8 @@ func (s *DockerSuite) TestRunAttachDetach(c *check.C) { // "test" should be printed func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) { testRequires(c, CpuCfsQuota) - runCmd := exec.Command(dockerBinary, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test") - out, _, _, err := runCommandWithStdoutStderr(runCmd) + + out, _, err := dockerCmdWithError(c, "run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test") if err != nil { c.Fatalf("failed to run container: %v, output: %q", err, out) } @@ -289,8 +273,8 @@ func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) { func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) { testRequires(c, CpuCfsPeriod) - runCmd := exec.Command(dockerBinary, "run", "--cpu-period", "50000", "--name", "test", "busybox", "true") - if _, err := runCommand(runCmd); err != nil { + + if _, _, err := dockerCmdWithError(c, "run", "--cpu-period", "50000", "--name", "test", "busybox", "true"); err != nil { c.Fatalf("failed to run container: %v", err) } @@ -306,8 +290,7 @@ func (s *DockerSuite) TestRunOOMExitCode(c *check.C) { errChan := make(chan error) go func() { defer close(errChan) - runCmd := exec.Command(dockerBinary, "run", "-m", "4MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done") - out, exitCode, _ := runCommandWithOutput(runCmd) + out, exitCode, _ := dockerCmdWithError(c, "run", "-m", "4MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done") if expected := 137; exitCode != expected { errChan <- fmt.Errorf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out) } @@ -322,102 +305,63 @@ func (s *DockerSuite) TestRunOOMExitCode(c *check.C) { } func (s *DockerSuite) TestContainerNetworkModeToSelf(c *check.C) { - cmd := exec.Command(dockerBinary, "run", "--name=me", "--net=container:me", "busybox", "true") - out, _, err := runCommandWithOutput(cmd) + out, _, err := dockerCmdWithError(c, "run", "--name=me", "--net=container:me", "busybox", "true") if err == nil || !strings.Contains(out, "cannot join own network") { c.Fatalf("using container net mode to self should result in an error") } } func (s *DockerSuite) TestRunContainerNetModeWithDnsMacHosts(c *check.C) { - cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top") - out, _, err := runCommandWithOutput(cmd) + out, _, err := dockerCmdWithError(c, "run", "-d", "--name", "parent", "busybox", "top") if err != nil { c.Fatalf("failed to run container: %v, output: %q", err, out) } - cmd = exec.Command(dockerBinary, "run", "--dns", "1.2.3.4", "--net=container:parent", "busybox") - out, _, err = runCommandWithOutput(cmd) + out, _, err = dockerCmdWithError(c, "run", "--dns", "1.2.3.4", "--net=container:parent", "busybox") if err == nil || !strings.Contains(out, "Conflicting options: --dns and the network mode") { c.Fatalf("run --net=container with --dns should error out") } - cmd = exec.Command(dockerBinary, "run", "--mac-address", "92:d0:c6:0a:29:33", "--net=container:parent", "busybox") - out, _, err = runCommandWithOutput(cmd) + out, _, err = dockerCmdWithError(c, "run", "--mac-address", "92:d0:c6:0a:29:33", "--net=container:parent", "busybox") if err == nil || !strings.Contains(out, "--mac-address and the network mode") { c.Fatalf("run --net=container with --mac-address should error out") } - cmd = exec.Command(dockerBinary, "run", "--add-host", "test:192.168.2.109", "--net=container:parent", "busybox") - out, _, err = runCommandWithOutput(cmd) + out, _, err = dockerCmdWithError(c, "run", "--add-host", "test:192.168.2.109", "--net=container:parent", "busybox") if err == nil || !strings.Contains(out, "--add-host and the network mode") { c.Fatalf("run --net=container with --add-host should error out") } - } func (s *DockerSuite) TestRunContainerNetModeWithExposePort(c *check.C) { - cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top") - out, _, err := runCommandWithOutput(cmd) - if err != nil { - c.Fatalf("failed to run container: %v, output: %q", err, out) - } + dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top") - cmd = exec.Command(dockerBinary, "run", "-p", "5000:5000", "--net=container:parent", "busybox") - out, _, err = runCommandWithOutput(cmd) + out, _, err := dockerCmdWithError(c, "run", "-p", "5000:5000", "--net=container:parent", "busybox") if err == nil || !strings.Contains(out, "Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)") { c.Fatalf("run --net=container with -p should error out") } - cmd = exec.Command(dockerBinary, "run", "-P", "--net=container:parent", "busybox") - out, _, err = runCommandWithOutput(cmd) + out, _, err = dockerCmdWithError(c, "run", "-P", "--net=container:parent", "busybox") if err == nil || !strings.Contains(out, "Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)") { c.Fatalf("run --net=container with -P should error out") } - cmd = exec.Command(dockerBinary, "run", "--expose", "5000", "--net=container:parent", "busybox") - out, _, err = runCommandWithOutput(cmd) + out, _, err = dockerCmdWithError(c, "run", "--expose", "5000", "--net=container:parent", "busybox") if err == nil || !strings.Contains(out, "Conflicting options: --expose and the network mode (--expose)") { c.Fatalf("run --net=container with --expose should error out") } - } func (s *DockerSuite) TestRunLinkToContainerNetMode(c *check.C) { - cmd := exec.Command(dockerBinary, "run", "--name", "test", "-d", "busybox", "top") - out, _, err := runCommandWithOutput(cmd) - if err != nil { - c.Fatalf("failed to run container: %v, output: %q", err, out) - } - cmd = exec.Command(dockerBinary, "run", "--name", "parent", "-d", "--net=container:test", "busybox", "top") - out, _, err = runCommandWithOutput(cmd) - if err != nil { - c.Fatalf("failed to run container: %v, output: %q", err, out) - } - cmd = exec.Command(dockerBinary, "run", "-d", "--link=parent:parent", "busybox", "top") - out, _, err = runCommandWithOutput(cmd) - if err != nil { - c.Fatalf("failed to run container: %v, output: %q", err, out) - } - - cmd = exec.Command(dockerBinary, "run", "--name", "child", "-d", "--net=container:parent", "busybox", "top") - out, _, err = runCommandWithOutput(cmd) - if err != nil { - c.Fatalf("failed to run container: %v, output: %q", err, out) - } - cmd = exec.Command(dockerBinary, "run", "-d", "--link=child:child", "busybox", "top") - out, _, err = runCommandWithOutput(cmd) - if err != nil { - c.Fatalf("failed to run container: %v, output: %q", err, out) - } + dockerCmd(c, "run", "--name", "test", "-d", "busybox", "top") + dockerCmd(c, "run", "--name", "parent", "-d", "--net=container:test", "busybox", "top") + dockerCmd(c, "run", "-d", "--link=parent:parent", "busybox", "top") + dockerCmd(c, "run", "--name", "child", "-d", "--net=container:parent", "busybox", "top") + dockerCmd(c, "run", "-d", "--link=child:child", "busybox", "top") } func (s *DockerSuite) TestRunLoopbackOnlyExistsWhenNetworkingDisabled(c *check.C) { - cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up") - out, _, err := runCommandWithOutput(cmd) - if err != nil { - c.Fatal(err, out) - } + out, _ := dockerCmd(c, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up") var ( count = 0 @@ -441,41 +385,23 @@ func (s *DockerSuite) TestRunLoopbackOnlyExistsWhenNetworkingDisabled(c *check.C // Issue #4681 func (s *DockerSuite) TestRunLoopbackWhenNetworkDisabled(c *check.C) { - cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1") - if _, err := runCommand(cmd); err != nil { - c.Fatal(err) - } + dockerCmd(c, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1") } func (s *DockerSuite) TestRunModeNetContainerHostname(c *check.C) { testRequires(c, ExecSupport) - cmd := exec.Command(dockerBinary, "run", "-i", "-d", "--name", "parent", "busybox", "top") - out, _, err := runCommandWithOutput(cmd) - if err != nil { - c.Fatalf("failed to run container: %v, output: %q", err, out) - } - cmd = exec.Command(dockerBinary, "exec", "parent", "cat", "/etc/hostname") - out, _, err = runCommandWithOutput(cmd) - if err != nil { - c.Fatalf("failed to exec command: %v, output: %q", err, out) - } - cmd = exec.Command(dockerBinary, "run", "--net=container:parent", "busybox", "cat", "/etc/hostname") - out1, _, err := runCommandWithOutput(cmd) - if err != nil { - c.Fatalf("failed to run container: %v, output: %q", err, out1) - } + dockerCmd(c, "run", "-i", "-d", "--name", "parent", "busybox", "top") + out, _ := dockerCmd(c, "exec", "parent", "cat", "/etc/hostname") + out1, _ := dockerCmd(c, "run", "--net=container:parent", "busybox", "cat", "/etc/hostname") + if out1 != out { c.Fatal("containers with shared net namespace should have same hostname") } } func (s *DockerSuite) TestRunNetworkNotInitializedNoneMode(c *check.C) { - cmd := exec.Command(dockerBinary, "run", "-d", "--net=none", "busybox", "top") - out, _, err := runCommandWithOutput(cmd) - if err != nil { - c.Fatal(err) - } + out, _, err := dockerCmdWithError(c, "run", "-d", "--net=none", "busybox", "top") id := strings.TrimSpace(out) res, err := inspectField(id, "NetworkSettings.IPAddress") c.Assert(err, check.IsNil)