Merge pull request #14623 from HuKeping/refactor-dockercmd

Refactor : Use dockerCmd in integration-cli tests
This commit is contained in:
David Calavera 2015-07-16 09:24:59 -07:00
commit ac8299ae15
5 changed files with 99 additions and 336 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"os/exec"
"strings" "strings"
"github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/stringid"
@ -9,28 +8,14 @@ import (
) )
func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) { func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh") out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
cleanedContainerID := strings.TrimSpace(out) cleanedContainerID := strings.TrimSpace(out)
dockerCmd(c, "wait", cleanedContainerID)
runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
name, err := inspectField(cleanedContainerID, "Name") name, err := inspectField(cleanedContainerID, "Name")
newName := "new_name" + stringid.GenerateRandomID() newName := "new_name" + stringid.GenerateRandomID()
runCmd = exec.Command(dockerBinary, "rename", "first_name", newName) dockerCmd(c, "rename", "first_name", newName)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
name, err = inspectField(cleanedContainerID, "Name") name, err = inspectField(cleanedContainerID, "Name")
if err != nil { if err != nil {
@ -43,19 +28,11 @@ func (s *DockerSuite) TestRenameStoppedContainer(c *check.C) {
} }
func (s *DockerSuite) TestRenameRunningContainer(c *check.C) { func (s *DockerSuite) TestRenameRunningContainer(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh") out, _ := dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
newName := "new_name" + stringid.GenerateRandomID() newName := "new_name" + stringid.GenerateRandomID()
cleanedContainerID := strings.TrimSpace(out) cleanedContainerID := strings.TrimSpace(out)
runCmd = exec.Command(dockerBinary, "rename", "first_name", newName) dockerCmd(c, "rename", "first_name", newName)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
name, err := inspectField(cleanedContainerID, "Name") name, err := inspectField(cleanedContainerID, "Name")
if err != nil { if err != nil {
@ -67,18 +44,10 @@ func (s *DockerSuite) TestRenameRunningContainer(c *check.C) {
} }
func (s *DockerSuite) TestRenameCheckNames(c *check.C) { func (s *DockerSuite) TestRenameCheckNames(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "--name", "first_name", "-d", "busybox", "sh") dockerCmd(c, "run", "--name", "first_name", "-d", "busybox", "sh")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
newName := "new_name" + stringid.GenerateRandomID() newName := "new_name" + stringid.GenerateRandomID()
runCmd = exec.Command(dockerBinary, "rename", "first_name", newName) dockerCmd(c, "rename", "first_name", newName)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatalf(out, err)
}
name, err := inspectField(newName, "Name") name, err := inspectField(newName, "Name")
if err != nil { if err != nil {
@ -95,18 +64,13 @@ func (s *DockerSuite) TestRenameCheckNames(c *check.C) {
} }
func (s *DockerSuite) TestRenameInvalidName(c *check.C) { func (s *DockerSuite) TestRenameInvalidName(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "--name", "myname", "-d", "busybox", "top") dockerCmd(c, "run", "--name", "myname", "-d", "busybox", "top")
if out, _, err := runCommandWithOutput(runCmd); err != nil {
c.Fatalf(out, err)
}
runCmd = exec.Command(dockerBinary, "rename", "myname", "new:invalid") if out, _, err := dockerCmdWithError(c, "rename", "myname", "new:invalid"); err == nil || !strings.Contains(out, "Invalid container name") {
if out, _, err := runCommandWithOutput(runCmd); err == nil || !strings.Contains(out, "Invalid container name") {
c.Fatalf("Renaming container to invalid name should have failed: %s\n%v", out, err) c.Fatalf("Renaming container to invalid name should have failed: %s\n%v", out, err)
} }
runCmd = exec.Command(dockerBinary, "ps", "-a") if out, _, err := dockerCmdWithError(c, "ps", "-a"); err != nil || !strings.Contains(out, "myname") {
if out, _, err := runCommandWithOutput(runCmd); err != nil || !strings.Contains(out, "myname") {
c.Fatalf("Output of docker ps should have included 'myname': %s\n%v", out, err) c.Fatalf("Output of docker ps should have included 'myname': %s\n%v", out, err)
} }
} }

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"os/exec"
"strings" "strings"
"time" "time"
@ -9,104 +8,53 @@ import (
) )
func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) { func (s *DockerSuite) TestRestartStoppedContainer(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "busybox", "echo", "foobar")
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "echo", "foobar")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatal(out, err)
}
cleanedContainerID := strings.TrimSpace(out) cleanedContainerID := strings.TrimSpace(out)
dockerCmd(c, "wait", cleanedContainerID)
runCmd = exec.Command(dockerBinary, "wait", cleanedContainerID) out, _ = dockerCmd(c, "logs", 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)
}
if out != "foobar\n" { if out != "foobar\n" {
c.Errorf("container should've printed 'foobar'") c.Errorf("container should've printed 'foobar'")
} }
runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID) dockerCmd(c, "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)
}
out, _ = dockerCmd(c, "logs", cleanedContainerID)
if out != "foobar\nfoobar\n" { if out != "foobar\nfoobar\n" {
c.Errorf("container should've printed 'foobar' twice") c.Errorf("container should've printed 'foobar' twice")
} }
} }
func (s *DockerSuite) TestRestartRunningContainer(c *check.C) { func (s *DockerSuite) TestRestartRunningContainer(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "busybox", "sh", "-c", "echo foobar && sleep 30 && echo 'should not print this'")
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)
}
cleanedContainerID := strings.TrimSpace(out) cleanedContainerID := strings.TrimSpace(out)
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) out, _ = dockerCmd(c, "logs", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatal(out, err)
}
if out != "foobar\n" { if out != "foobar\n" {
c.Errorf("container should've printed 'foobar'") c.Errorf("container should've printed 'foobar'")
} }
runCmd = exec.Command(dockerBinary, "restart", "-t", "1", cleanedContainerID) dockerCmd(c, "restart", "-t", "1", cleanedContainerID)
if out, _, err = runCommandWithOutput(runCmd); err != nil {
c.Fatal(out, err)
}
runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) out, _ = dockerCmd(c, "logs", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatal(out, err)
}
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
if out != "foobar\nfoobar\n" { if out != "foobar\nfoobar\n" {
c.Errorf("container should've printed 'foobar' twice") 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. // 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) { func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "-v", "/test", "busybox", "top")
runCmd := exec.Command(dockerBinary, "run", "-d", "-v", "/test", "busybox", "top")
out, _, err := runCommandWithOutput(runCmd)
if err != nil {
c.Fatal(out, err)
}
cleanedContainerID := strings.TrimSpace(out) cleanedContainerID := strings.TrimSpace(out)
out, _ = dockerCmd(c, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
runCmd = exec.Command(dockerBinary, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd)
if err != nil {
c.Fatal(out, err)
}
if out = strings.Trim(out, " \n\r"); out != "1" { if out = strings.Trim(out, " \n\r"); out != "1" {
c.Errorf("expect 1 volume received %s", out) c.Errorf("expect 1 volume received %s", out)
@ -115,17 +63,9 @@ func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
volumes, err := inspectField(cleanedContainerID, "Volumes") volumes, err := inspectField(cleanedContainerID, "Volumes")
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)
runCmd = exec.Command(dockerBinary, "restart", cleanedContainerID) dockerCmd(c, "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)
}
out, _ = dockerCmd(c, "inspect", "--format", "{{ len .Volumes }}", cleanedContainerID)
if out = strings.Trim(out, " \n\r"); out != "1" { if out = strings.Trim(out, " \n\r"); out != "1" {
c.Errorf("expect 1 volume after restart received %s", out) c.Errorf("expect 1 volume after restart received %s", out)
} }
@ -136,16 +76,10 @@ func (s *DockerSuite) TestRestartWithVolumes(c *check.C) {
if volumes != volumesAfterRestart { if volumes != volumesAfterRestart {
c.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart) c.Errorf("expected volume path: %s Actual path: %s", volumes, volumesAfterRestart)
} }
} }
func (s *DockerSuite) TestRestartPolicyNO(c *check.C) { func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "--restart=no", "busybox", "false")
cmd := exec.Command(dockerBinary, "run", "-d", "--restart=no", "busybox", "false")
out, _, err := runCommandWithOutput(cmd)
if err != nil {
c.Fatal(err, out)
}
id := strings.TrimSpace(string(out)) id := strings.TrimSpace(string(out))
name, err := inspectField(id, "HostConfig.RestartPolicy.Name") name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
@ -153,16 +87,10 @@ func (s *DockerSuite) TestRestartPolicyNO(c *check.C) {
if name != "no" { if name != "no" {
c.Fatalf("Container restart policy name is %s, expected %s", name, "no") c.Fatalf("Container restart policy name is %s, expected %s", name, "no")
} }
} }
func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) { func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "--restart=always", "busybox", "false")
cmd := exec.Command(dockerBinary, "run", "-d", "--restart=always", "busybox", "false")
out, _, err := runCommandWithOutput(cmd)
if err != nil {
c.Fatal(err, out)
}
id := strings.TrimSpace(string(out)) id := strings.TrimSpace(string(out))
name, err := inspectField(id, "HostConfig.RestartPolicy.Name") name, err := inspectField(id, "HostConfig.RestartPolicy.Name")
@ -178,16 +106,10 @@ func (s *DockerSuite) TestRestartPolicyAlways(c *check.C) {
if MaximumRetryCount != "0" { if MaximumRetryCount != "0" {
c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "0") c.Fatalf("Container Maximum Retry Count is %s, expected %s", MaximumRetryCount, "0")
} }
} }
func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) { func (s *DockerSuite) TestRestartPolicyOnFailure(c *check.C) {
out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:1", "busybox", "false")
cmd := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:1", "busybox", "false")
out, _, err := runCommandWithOutput(cmd)
if err != nil {
c.Fatal(err, out)
}
id := strings.TrimSpace(string(out)) id := strings.TrimSpace(string(out))
name, err := inspectField(id, "HostConfig.RestartPolicy.Name") 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 // a good container with --restart=on-failure:3
// MaximumRetryCount!=0; RestartCount=0 // MaximumRetryCount!=0; RestartCount=0
func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) { func (s *DockerSuite) TestContainerRestartwithGoodContainer(c *check.C) {
out, err := exec.Command(dockerBinary, "run", "-d", "--restart=on-failure:3", "busybox", "true").CombinedOutput() out, _ := dockerCmd(c, "run", "-d", "--restart=on-failure:3", "busybox", "true")
if err != nil {
c.Fatal(string(out), err)
}
id := strings.TrimSpace(string(out)) id := strings.TrimSpace(string(out))
if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5); err != nil { if err := waitInspect(id, "{{ .State.Restarting }} {{ .State.Running }}", "false false", 5); err != nil {
c.Fatal(err) c.Fatal(err)

View File

@ -2,7 +2,6 @@ package main
import ( import (
"os" "os"
"os/exec"
"strings" "strings"
"github.com/go-check/check" "github.com/go-check/check"
@ -11,58 +10,34 @@ import (
func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) { func (s *DockerSuite) TestRmContainerWithRemovedVolume(c *check.C) {
testRequires(c, SameHostDaemon) testRequires(c, SameHostDaemon)
cmd := exec.Command(dockerBinary, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true") dockerCmd(c, "run", "--name", "losemyvolumes", "-v", "/tmp/testing:/test", "busybox", "true")
if _, err := runCommand(cmd); err != nil {
c.Fatal(err)
}
if err := os.Remove("/tmp/testing"); err != nil { if err := os.Remove("/tmp/testing"); err != nil {
c.Fatal(err) c.Fatal(err)
} }
cmd = exec.Command(dockerBinary, "rm", "-v", "losemyvolumes") dockerCmd(c, "rm", "-v", "losemyvolumes")
if out, _, err := runCommandWithOutput(cmd); err != nil {
c.Fatal(out, err)
}
} }
func (s *DockerSuite) TestRmContainerWithVolume(c *check.C) { 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") dockerCmd(c, "rm", "-v", "foo")
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)
}
} }
func (s *DockerSuite) TestRmRunningContainer(c *check.C) { func (s *DockerSuite) TestRmRunningContainer(c *check.C) {
createRunningContainer(c, "foo") createRunningContainer(c, "foo")
// Test cannot remove running container if _, _, err := dockerCmdWithError(c, "rm", "foo"); err == nil {
cmd := exec.Command(dockerBinary, "rm", "foo")
if _, err := runCommand(cmd); err == nil {
c.Fatalf("Expected error, can't rm a running container") c.Fatalf("Expected error, can't rm a running container")
} }
} }
func (s *DockerSuite) TestRmForceRemoveRunningContainer(c *check.C) { func (s *DockerSuite) TestRmForceRemoveRunningContainer(c *check.C) {
createRunningContainer(c, "foo") createRunningContainer(c, "foo")
// Stop then remove with -s // Stop then remove with -s
cmd := exec.Command(dockerBinary, "rm", "-f", "foo") dockerCmd(c, "rm", "-f", "foo")
if _, err := runCommand(cmd); err != nil {
c.Fatal(err)
}
} }
func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) { 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) c.Fatalf("Could not build image %s: %v", img, err)
} }
// run container on first image // 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) c.Fatalf("Could not run image %s: %v: %s", img, err, out)
} }
// rebuild dockerfile with a small addition at the end // rebuild dockerfile with a small addition at the end
if _, err := buildImage(img, dockerfile2, true); err != nil { if _, err := buildImage(img, dockerfile2, true); err != nil {
c.Fatalf("Could not rebuild image %s: %v", img, err) c.Fatalf("Could not rebuild image %s: %v", img, err)
} }
// try to remove the image, should error out. // 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) c.Fatalf("Expected to error out removing the image, but succeeded: %s", out)
} }
// check if we deleted the first image // 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 { if err != nil {
c.Fatalf("%v: %s", err, out) c.Fatalf("%v: %s", err, out)
} }
if !strings.Contains(out, img1) { if !strings.Contains(out, img1) {
c.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out) c.Fatalf("Orphaned container (could not find %q in docker images): %s", img1, out)
} }
} }
func (s *DockerSuite) TestRmInvalidContainer(c *check.C) { 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") c.Fatal("Expected error on rm unknown container, got none")
} else if !strings.Contains(out, "failed to remove containers") { } else if !strings.Contains(out, "failed to remove containers") {
c.Fatalf("Expected output to contain 'failed to remove containers', got %q", out) c.Fatalf("Expected output to contain 'failed to remove containers', got %q", out)
} }
} }
func createRunningContainer(c *check.C, name string) { func createRunningContainer(c *check.C, name string) {
cmd := exec.Command(dockerBinary, "run", "-dt", "--name", name, "busybox", "top") dockerCmd(c, "run", "-dt", "--name", name, "busybox", "top")
if _, err := runCommand(cmd); err != nil {
c.Fatal(err)
}
} }

View File

@ -12,8 +12,7 @@ func (s *DockerSuite) TestRmiWithContainerFails(c *check.C) {
errSubstr := "is using it" errSubstr := "is using it"
// create a container // create a container
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true") out, _, err := dockerCmdWithError(c, "run", "-d", "busybox", "true")
out, _, err := runCommandWithOutput(runCmd)
if err != nil { if err != nil {
c.Fatalf("failed to create a container: %s, %v", out, err) 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) cleanedContainerID := strings.TrimSpace(out)
// try to delete the image // try to delete the image
runCmd = exec.Command(dockerBinary, "rmi", "busybox") out, _, err = dockerCmdWithError(c, "rmi", "busybox")
out, _, err = runCommandWithOutput(runCmd)
if err == nil { if err == nil {
c.Fatalf("Container %q is using image, should not be able to rmi: %q", cleanedContainerID, out) 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) { func (s *DockerSuite) TestRmiImgIDMultipleTag(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'") out, _, err := dockerCmdWithError(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-one'")
out, _, err := runCommandWithOutput(runCmd)
if err != nil { if err != nil {
c.Fatalf("failed to create a container:%s, %v", out, err) c.Fatalf("failed to create a container:%s, %v", out, err)
} }
containerID := strings.TrimSpace(out) containerID := strings.TrimSpace(out)
runCmd = exec.Command(dockerBinary, "commit", containerID, "busybox-one") out, _, err = dockerCmdWithError(c, "commit", containerID, "busybox-one")
out, _, err = runCommandWithOutput(runCmd)
if err != nil { if err != nil {
c.Fatalf("failed to commit a new busybox-one:%s, %v", out, err) 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) c.Assert(err, check.IsNil)
// run a container with the image // 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 { if err != nil {
c.Fatalf("failed to create a container:%s, %v", out, err) c.Fatalf("failed to create a container:%s, %v", out, err)
} }
containerID = strings.TrimSpace(out) containerID = strings.TrimSpace(out)
// first checkout without force it fails // 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]) 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) { 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) 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]) { if strings.Contains(imagesAfter, imgID[:12]) {
c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter) c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter)
} }
} }
func (s *DockerSuite) TestRmiImgIDForce(c *check.C) { func (s *DockerSuite) TestRmiImgIDForce(c *check.C) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'") out, _, err := dockerCmdWithError(c, "run", "-d", "busybox", "/bin/sh", "-c", "mkdir '/busybox-test'")
out, _, err := runCommandWithOutput(runCmd)
if err != nil { if err != nil {
c.Fatalf("failed to create a container:%s, %v", out, err) c.Fatalf("failed to create a container:%s, %v", out, err)
} }
containerID := strings.TrimSpace(out) containerID := strings.TrimSpace(out)
runCmd = exec.Command(dockerBinary, "commit", containerID, "busybox-test") out, _, err = dockerCmdWithError(c, "commit", containerID, "busybox-test")
out, _, err = runCommandWithOutput(runCmd)
if err != nil { if err != nil {
c.Fatalf("failed to commit a new busybox-test:%s, %v", out, err) 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) c.Assert(err, check.IsNil)
// first checkout without force it fails // first checkout without force it fails
runCmd = exec.Command(dockerBinary, "rmi", imgID) out, _, err = dockerCmdWithError(c, "rmi", imgID)
out, _, err = runCommandWithOutput(runCmd)
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)) { 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) 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]) { if strings.Contains(imagesAfter, imgID[:12]) {
c.Fatalf("rmi -f %s failed, image still exists: %q\n\n", imgID, imagesAfter) 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" container := "test-delete-tag"
newtag := "busybox:newtag" newtag := "busybox:newtag"
bb := "busybox:latest" 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) 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) 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 { if err != nil {
c.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out) c.Fatalf("Could not remove tag %s: %v: %s", newtag, err, out)
} }
if d := strings.Count(out, "Untagged: "); d != 1 { if d := strings.Count(out, "Untagged: "); d != 1 {
c.Fatalf("Expected 1 untagged entry got %d: %q", d, out) c.Fatalf("Expected 1 untagged entry got %d: %q", d, out)
} }
} }
func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) { func (s *DockerSuite) TestRmiForceWithExistingContainers(c *check.C) {
image := "busybox-clone" image := "busybox-clone"
cmd := exec.Command(dockerBinary, "build", "--no-cache", "-t", image, "-") 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) 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) c.Fatalf("Could not run container: %s, %v", out, err)
} }
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rmi", "-f", image)) if out, _, err := dockerCmdWithError(c, "rmi", "-f", image); err != nil {
if err != nil {
c.Fatalf("Could not remove image %s: %s, %v", image, out, err) c.Fatalf("Could not remove image %s: %s, %v", image, out, err)
} }
} }
func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) { func (s *DockerSuite) TestRmiWithMultipleRepositories(c *check.C) {
newRepo := "127.0.0.1:5000/busybox" newRepo := "127.0.0.1:5000/busybox"
oldRepo := "busybox" oldRepo := "busybox"
newTag := "busybox:test" newTag := "busybox:test"
cmd := exec.Command(dockerBinary, "tag", oldRepo, newRepo) out, _, err := dockerCmdWithError(c, "tag", oldRepo, newRepo)
out, _, err := runCommandWithOutput(cmd)
if err != nil { if err != nil {
c.Fatalf("Could not tag busybox: %v: %s", err, out) 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 { if err != nil {
c.Fatalf("failed to run container: %v, output: %s", err, out) 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 { if err != nil {
c.Fatalf("failed to commit container: %v, output: %s", err, out) 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 { if err != nil {
c.Fatalf("failed to remove image: %v, output: %s", err, out) c.Fatalf("failed to remove image: %v, output: %s", err, out)
} }
if !strings.Contains(out, "Untagged: "+newTag) { if !strings.Contains(out, "Untagged: "+newTag) {
c.Fatalf("Could not remove image %s: %s, %v", newTag, out, err) c.Fatalf("Could not remove image %s: %s, %v", newTag, out, err)
} }
} }
func (s *DockerSuite) TestRmiBlank(c *check.C) { func (s *DockerSuite) TestRmiBlank(c *check.C) {
// try to delete a blank image name // try to delete a blank image name
runCmd := exec.Command(dockerBinary, "rmi", "") out, _, err := dockerCmdWithError(c, "rmi", "")
out, _, err := runCommandWithOutput(runCmd)
if err == nil { if err == nil {
c.Fatal("Should have failed to delete '' image") c.Fatal("Should have failed to delete '' image")
} }
if strings.Contains(out, "No such image") { if strings.Contains(out, "No such image") {
c.Fatalf("Wrong error message generated: %s", out) c.Fatalf("Wrong error message generated: %s", out)
} }
if !strings.Contains(out, "Image name can not be blank") { if !strings.Contains(out, "Image name can not be blank") {
c.Fatalf("Expected error message not generated: %s", out) c.Fatalf("Expected error message not generated: %s", out)
} }
runCmd = exec.Command(dockerBinary, "rmi", " ") out, _, err = dockerCmdWithError(c, "rmi", " ")
out, _, err = runCommandWithOutput(runCmd)
if err == nil { if err == nil {
c.Fatal("Should have failed to delete '' image") c.Fatal("Should have failed to delete '' image")
} }
if !strings.Contains(out, "No such image") { if !strings.Contains(out, "No such image") {
c.Fatalf("Expected error message not generated: %s", out) c.Fatalf("Expected error message not generated: %s", out)
} }

View File

@ -88,11 +88,8 @@ func (s *DockerSuite) TestRunWithVolumesIsRecursive(c *check.C) {
func (s *DockerSuite) TestRunWithUlimits(c *check.C) { func (s *DockerSuite) TestRunWithUlimits(c *check.C) {
testRequires(c, NativeExecDriver) 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) ul := strings.TrimSpace(out)
if ul != "42" { if ul != "42" {
c.Fatalf("expected `ulimit -n` to be 42, got %s", ul) 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) 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 { if err != nil {
c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err) 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) testRequires(c, NativeExecDriver)
cgroupParent := "/cgroup-parent/test" cgroupParent := "/cgroup-parent/test"
out, _, err := dockerCmdWithError(c, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup")
out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "--cgroup-parent", cgroupParent, "--rm", "busybox", "cat", "/proc/self/cgroup"))
if err != nil { if err != nil {
c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err) 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) testRequires(c, NativeExecDriver)
filename := "/sys/fs/cgroup/devices/test123" filename := "/sys/fs/cgroup/devices/test123"
cmd := exec.Command(dockerBinary, "run", "busybox", "touch", filename) out, _, err := dockerCmdWithError(c, "run", "busybox", "touch", filename)
out, _, err := runCommandWithOutput(cmd)
if err == nil { if err == nil {
c.Fatal("expected cgroup mount point to be read-only, touch file should fail") 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) { func (s *DockerSuite) TestRunDeviceDirectory(c *check.C) {
testRequires(c, NativeExecDriver) 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") { if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "timer") {
c.Fatalf("expected output /dev/snd/timer, received %s", actual) 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, _ = dockerCmd(c, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/")
out, _, err = runCommandWithOutput(cmd)
if err != nil {
c.Fatal(err, out)
}
if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "seq") { if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "seq") {
c.Fatalf("expected output /dev/othersnd/seq, received %s", actual) 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 // "test" should be printed
func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) { func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
testRequires(c, CpuCfsQuota) 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 { if err != nil {
c.Fatalf("failed to run container: %v, output: %q", err, out) 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) { func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
testRequires(c, CpuCfsPeriod) 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) c.Fatalf("failed to run container: %v", err)
} }
@ -306,8 +290,7 @@ func (s *DockerSuite) TestRunOOMExitCode(c *check.C) {
errChan := make(chan error) errChan := make(chan error)
go func() { go func() {
defer close(errChan) 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, _ := dockerCmdWithError(c, "run", "-m", "4MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done")
out, exitCode, _ := runCommandWithOutput(runCmd)
if expected := 137; exitCode != expected { if expected := 137; exitCode != expected {
errChan <- fmt.Errorf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out) 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) { func (s *DockerSuite) TestContainerNetworkModeToSelf(c *check.C) {
cmd := exec.Command(dockerBinary, "run", "--name=me", "--net=container:me", "busybox", "true") out, _, err := dockerCmdWithError(c, "run", "--name=me", "--net=container:me", "busybox", "true")
out, _, err := runCommandWithOutput(cmd)
if err == nil || !strings.Contains(out, "cannot join own network") { if err == nil || !strings.Contains(out, "cannot join own network") {
c.Fatalf("using container net mode to self should result in an error") c.Fatalf("using container net mode to self should result in an error")
} }
} }
func (s *DockerSuite) TestRunContainerNetModeWithDnsMacHosts(c *check.C) { func (s *DockerSuite) TestRunContainerNetModeWithDnsMacHosts(c *check.C) {
cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top") out, _, err := dockerCmdWithError(c, "run", "-d", "--name", "parent", "busybox", "top")
out, _, err := runCommandWithOutput(cmd)
if err != nil { if err != nil {
c.Fatalf("failed to run container: %v, output: %q", err, out) 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 = dockerCmdWithError(c, "run", "--dns", "1.2.3.4", "--net=container:parent", "busybox")
out, _, err = runCommandWithOutput(cmd)
if err == nil || !strings.Contains(out, "Conflicting options: --dns and the network mode") { if err == nil || !strings.Contains(out, "Conflicting options: --dns and the network mode") {
c.Fatalf("run --net=container with --dns should error out") 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 = dockerCmdWithError(c, "run", "--mac-address", "92:d0:c6:0a:29:33", "--net=container:parent", "busybox")
out, _, err = runCommandWithOutput(cmd)
if err == nil || !strings.Contains(out, "--mac-address and the network mode") { if err == nil || !strings.Contains(out, "--mac-address and the network mode") {
c.Fatalf("run --net=container with --mac-address should error out") 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 = dockerCmdWithError(c, "run", "--add-host", "test:192.168.2.109", "--net=container:parent", "busybox")
out, _, err = runCommandWithOutput(cmd)
if err == nil || !strings.Contains(out, "--add-host and the network mode") { if err == nil || !strings.Contains(out, "--add-host and the network mode") {
c.Fatalf("run --net=container with --add-host should error out") c.Fatalf("run --net=container with --add-host should error out")
} }
} }
func (s *DockerSuite) TestRunContainerNetModeWithExposePort(c *check.C) { func (s *DockerSuite) TestRunContainerNetModeWithExposePort(c *check.C) {
cmd := exec.Command(dockerBinary, "run", "-d", "--name", "parent", "busybox", "top") dockerCmd(c, "run", "-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, "run", "-p", "5000:5000", "--net=container:parent", "busybox") out, _, err := dockerCmdWithError(c, "run", "-p", "5000:5000", "--net=container:parent", "busybox")
out, _, err = runCommandWithOutput(cmd)
if err == nil || !strings.Contains(out, "Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)") { 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") c.Fatalf("run --net=container with -p should error out")
} }
cmd = exec.Command(dockerBinary, "run", "-P", "--net=container:parent", "busybox") out, _, err = dockerCmdWithError(c, "run", "-P", "--net=container:parent", "busybox")
out, _, err = runCommandWithOutput(cmd)
if err == nil || !strings.Contains(out, "Conflicting options: -p, -P, --publish-all, --publish and the network mode (--net)") { 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") c.Fatalf("run --net=container with -P should error out")
} }
cmd = exec.Command(dockerBinary, "run", "--expose", "5000", "--net=container:parent", "busybox") out, _, err = dockerCmdWithError(c, "run", "--expose", "5000", "--net=container:parent", "busybox")
out, _, err = runCommandWithOutput(cmd)
if err == nil || !strings.Contains(out, "Conflicting options: --expose and the network mode (--expose)") { if err == nil || !strings.Contains(out, "Conflicting options: --expose and the network mode (--expose)") {
c.Fatalf("run --net=container with --expose should error out") c.Fatalf("run --net=container with --expose should error out")
} }
} }
func (s *DockerSuite) TestRunLinkToContainerNetMode(c *check.C) { func (s *DockerSuite) TestRunLinkToContainerNetMode(c *check.C) {
cmd := exec.Command(dockerBinary, "run", "--name", "test", "-d", "busybox", "top") dockerCmd(c, "run", "--name", "test", "-d", "busybox", "top")
out, _, err := runCommandWithOutput(cmd) dockerCmd(c, "run", "--name", "parent", "-d", "--net=container:test", "busybox", "top")
if err != nil { dockerCmd(c, "run", "-d", "--link=parent:parent", "busybox", "top")
c.Fatalf("failed to run container: %v, output: %q", err, out) dockerCmd(c, "run", "--name", "child", "-d", "--net=container:parent", "busybox", "top")
} dockerCmd(c, "run", "-d", "--link=child:child", "busybox", "top")
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)
}
} }
func (s *DockerSuite) TestRunLoopbackOnlyExistsWhenNetworkingDisabled(c *check.C) { func (s *DockerSuite) TestRunLoopbackOnlyExistsWhenNetworkingDisabled(c *check.C) {
cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up") out, _ := dockerCmd(c, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
out, _, err := runCommandWithOutput(cmd)
if err != nil {
c.Fatal(err, out)
}
var ( var (
count = 0 count = 0
@ -441,41 +385,23 @@ func (s *DockerSuite) TestRunLoopbackOnlyExistsWhenNetworkingDisabled(c *check.C
// Issue #4681 // Issue #4681
func (s *DockerSuite) TestRunLoopbackWhenNetworkDisabled(c *check.C) { func (s *DockerSuite) TestRunLoopbackWhenNetworkDisabled(c *check.C) {
cmd := exec.Command(dockerBinary, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1") dockerCmd(c, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
if _, err := runCommand(cmd); err != nil {
c.Fatal(err)
}
} }
func (s *DockerSuite) TestRunModeNetContainerHostname(c *check.C) { func (s *DockerSuite) TestRunModeNetContainerHostname(c *check.C) {
testRequires(c, ExecSupport) 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") dockerCmd(c, "run", "-i", "-d", "--name", "parent", "busybox", "top")
out1, _, err := runCommandWithOutput(cmd) out, _ := dockerCmd(c, "exec", "parent", "cat", "/etc/hostname")
if err != nil { out1, _ := dockerCmd(c, "run", "--net=container:parent", "busybox", "cat", "/etc/hostname")
c.Fatalf("failed to run container: %v, output: %q", err, out1)
}
if out1 != out { if out1 != out {
c.Fatal("containers with shared net namespace should have same hostname") c.Fatal("containers with shared net namespace should have same hostname")
} }
} }
func (s *DockerSuite) TestRunNetworkNotInitializedNoneMode(c *check.C) { func (s *DockerSuite) TestRunNetworkNotInitializedNoneMode(c *check.C) {
cmd := exec.Command(dockerBinary, "run", "-d", "--net=none", "busybox", "top") out, _, err := dockerCmdWithError(c, "run", "-d", "--net=none", "busybox", "top")
out, _, err := runCommandWithOutput(cmd)
if err != nil {
c.Fatal(err)
}
id := strings.TrimSpace(out) id := strings.TrimSpace(out)
res, err := inspectField(id, "NetworkSettings.IPAddress") res, err := inspectField(id, "NetworkSettings.IPAddress")
c.Assert(err, check.IsNil) c.Assert(err, check.IsNil)