2014-11-20 05:19:16 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-11 03:16:42 +00:00
|
|
|
"bufio"
|
2014-11-20 05:19:16 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2015-03-16 23:41:38 +00:00
|
|
|
"path"
|
2014-11-20 05:19:16 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/mount"
|
2015-04-18 16:46:47 +00:00
|
|
|
"github.com/go-check/check"
|
2014-11-20 05:19:16 +00:00
|
|
|
"github.com/kr/pty"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #6509
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRunRedirectStdout(c *check.C) {
|
2014-11-20 05:19:16 +00:00
|
|
|
checkRedirect := func(command string) {
|
|
|
|
_, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Could not open pty: %v", err)
|
2014-11-20 05:19:16 +00:00
|
|
|
}
|
|
|
|
cmd := exec.Command("sh", "-c", command)
|
|
|
|
cmd.Stdin = tty
|
|
|
|
cmd.Stdout = tty
|
|
|
|
cmd.Stderr = tty
|
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("start err: %v", err)
|
2014-11-20 05:19:16 +00:00
|
|
|
}
|
2015-04-27 17:29:48 +00:00
|
|
|
ch := make(chan error)
|
2014-11-20 05:19:16 +00:00
|
|
|
go func() {
|
2015-04-27 17:29:48 +00:00
|
|
|
ch <- cmd.Wait()
|
2014-11-20 05:19:16 +00:00
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("command timeout")
|
2015-04-27 17:29:48 +00:00
|
|
|
case err := <-ch:
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("wait err=%v", err)
|
|
|
|
}
|
2014-11-20 05:19:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkRedirect(dockerBinary + " run -i busybox cat /etc/passwd | grep -q root")
|
|
|
|
checkRedirect(dockerBinary + " run busybox cat /etc/passwd | grep -q root")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test recursive bind mount works by default
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRunWithVolumesIsRecursive(c *check.C) {
|
2014-11-20 05:19:16 +00:00
|
|
|
tmpDir, err := ioutil.TempDir("", "docker_recursive_mount_test")
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-11-20 05:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
// Create a temporary tmpfs mount.
|
|
|
|
tmpfsDir := filepath.Join(tmpDir, "tmpfs")
|
|
|
|
if err := os.MkdirAll(tmpfsDir, 0777); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("failed to mkdir at %s - %s", tmpfsDir, err)
|
2014-11-20 05:19:16 +00:00
|
|
|
}
|
|
|
|
if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
|
2014-11-20 05:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := ioutil.TempFile(tmpfsDir, "touch-me")
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2014-11-20 05:19:16 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "--name", "test-data", "--volume", fmt.Sprintf("%s:/tmp:ro", tmpDir), "busybox:latest", "ls", "/tmp/tmpfs")
|
|
|
|
out, stderr, exitCode, err := runCommandWithStdoutStderr(runCmd)
|
|
|
|
if err != nil && exitCode != 0 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(out, stderr, err)
|
2014-11-20 05:19:16 +00:00
|
|
|
}
|
|
|
|
if !strings.Contains(out, filepath.Base(f.Name())) {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("Recursive bind mount test failed. Expected file not found")
|
2014-11-20 05:19:16 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-11 19:21:38 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRunWithUlimits(c *check.C) {
|
|
|
|
testRequires(c, NativeExecDriver)
|
2015-02-11 19:21:38 +00:00
|
|
|
|
2015-07-14 20:15:00 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "--name=testulimits", "--ulimit", "nofile=42", "busybox", "/bin/sh", "-c", "ulimit -n")
|
2015-02-11 19:21:38 +00:00
|
|
|
ul := strings.TrimSpace(out)
|
|
|
|
if ul != "42" {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("expected `ulimit -n` to be 42, got %s", ul)
|
2015-02-11 19:21:38 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-16 23:41:38 +00:00
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRunContainerWithCgroupParent(c *check.C) {
|
|
|
|
testRequires(c, NativeExecDriver)
|
2015-03-16 23:41:38 +00:00
|
|
|
|
|
|
|
cgroupParent := "test"
|
2015-07-21 15:59:12 +00:00
|
|
|
name := "cgroup-test"
|
2015-03-16 23:41:38 +00:00
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "--cgroup-parent", cgroupParent, "--name", name, "busybox", "cat", "/proc/self/cgroup")
|
2015-03-16 23:41:38 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
|
2015-03-16 23:41:38 +00:00
|
|
|
}
|
2015-03-20 22:32:40 +00:00
|
|
|
cgroupPaths := parseCgroupPaths(string(out))
|
2015-03-16 23:41:38 +00:00
|
|
|
if len(cgroupPaths) == 0 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("unexpected output - %q", string(out))
|
2015-03-16 23:41:38 +00:00
|
|
|
}
|
2015-07-21 15:59:12 +00:00
|
|
|
id, err := getIDByName(name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
expectedCgroup := path.Join(cgroupParent, id)
|
|
|
|
found := false
|
2015-03-16 23:41:38 +00:00
|
|
|
for _, path := range cgroupPaths {
|
2015-07-21 15:59:12 +00:00
|
|
|
if strings.HasSuffix(path, expectedCgroup) {
|
2015-03-16 23:41:38 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2015-07-21 15:59:12 +00:00
|
|
|
c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have suffix %q. Cgroup Paths: %v", expectedCgroup, cgroupPaths)
|
2015-03-16 23:41:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRunContainerWithCgroupParentAbsPath(c *check.C) {
|
|
|
|
testRequires(c, NativeExecDriver)
|
2015-03-16 23:41:38 +00:00
|
|
|
|
|
|
|
cgroupParent := "/cgroup-parent/test"
|
2015-07-21 15:59:12 +00:00
|
|
|
name := "cgroup-test"
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "--cgroup-parent", cgroupParent, "--name", name, "busybox", "cat", "/proc/self/cgroup")
|
2015-03-16 23:41:38 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("unexpected failure when running container with --cgroup-parent option - %s\n%v", string(out), err)
|
2015-03-16 23:41:38 +00:00
|
|
|
}
|
2015-03-20 22:32:40 +00:00
|
|
|
cgroupPaths := parseCgroupPaths(string(out))
|
2015-03-16 23:41:38 +00:00
|
|
|
if len(cgroupPaths) == 0 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("unexpected output - %q", string(out))
|
2015-03-16 23:41:38 +00:00
|
|
|
}
|
2015-07-21 15:59:12 +00:00
|
|
|
id, err := getIDByName(name)
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
expectedCgroup := path.Join(cgroupParent, id)
|
2015-03-16 23:41:38 +00:00
|
|
|
found := false
|
|
|
|
for _, path := range cgroupPaths {
|
2015-07-21 15:59:12 +00:00
|
|
|
if strings.HasSuffix(path, expectedCgroup) {
|
2015-03-16 23:41:38 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2015-07-21 15:59:12 +00:00
|
|
|
c.Fatalf("unexpected cgroup paths. Expected at least one cgroup path to have suffix %q. Cgroup Paths: %v", expectedCgroup, cgroupPaths)
|
2015-03-16 23:41:38 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-12 18:51:51 +00:00
|
|
|
|
2015-07-10 05:12:09 +00:00
|
|
|
func (s *DockerSuite) TestRunContainerWithCgroupMountRO(c *check.C) {
|
|
|
|
testRequires(c, NativeExecDriver)
|
|
|
|
|
|
|
|
filename := "/sys/fs/cgroup/devices/test123"
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "busybox", "touch", filename)
|
2015-07-10 05:12:09 +00:00
|
|
|
if err == nil {
|
|
|
|
c.Fatal("expected cgroup mount point to be read-only, touch file should fail")
|
|
|
|
}
|
|
|
|
expected := "Read-only file system"
|
|
|
|
if !strings.Contains(out, expected) {
|
|
|
|
c.Fatalf("expected output from failure to contain %s but contains %s", expected, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRunDeviceDirectory(c *check.C) {
|
|
|
|
testRequires(c, NativeExecDriver)
|
2015-02-12 18:51:51 +00:00
|
|
|
|
2015-07-14 20:15:00 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "--device", "/dev/snd:/dev/snd", "busybox", "sh", "-c", "ls /dev/snd/")
|
2015-02-12 18:51:51 +00:00
|
|
|
if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "timer") {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("expected output /dev/snd/timer, received %s", actual)
|
2015-02-12 18:51:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 20:15:00 +00:00
|
|
|
out, _ = dockerCmd(c, "run", "--device", "/dev/snd:/dev/othersnd", "busybox", "sh", "-c", "ls /dev/othersnd/")
|
2015-02-12 18:51:51 +00:00
|
|
|
if actual := strings.Trim(out, "\r\n"); !strings.Contains(out, "seq") {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("expected output /dev/othersnd/seq, received %s", actual)
|
2015-02-12 18:51:51 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-11 03:16:42 +00:00
|
|
|
|
|
|
|
// TestRunDetach checks attaching and detaching with the escape sequence.
|
2015-04-18 16:46:47 +00:00
|
|
|
func (s *DockerSuite) TestRunAttachDetach(c *check.C) {
|
2015-04-11 03:16:42 +00:00
|
|
|
name := "attach-detach"
|
|
|
|
cmd := exec.Command(dockerBinary, "run", "--name", name, "-it", "busybox", "cat")
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:16:42 +00:00
|
|
|
}
|
|
|
|
cpty, tty, err := pty.Open()
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:16:42 +00:00
|
|
|
}
|
|
|
|
defer cpty.Close()
|
|
|
|
cmd.Stdin = tty
|
|
|
|
if err := cmd.Start(); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:16:42 +00:00
|
|
|
}
|
|
|
|
if err := waitRun(name); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := cpty.Write([]byte("hello\n")); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out, err := bufio.NewReader(stdout).ReadString('\n')
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:16:42 +00:00
|
|
|
}
|
|
|
|
if strings.TrimSpace(out) != "hello" {
|
2015-04-27 20:33:30 +00:00
|
|
|
c.Fatalf("expected 'hello', got %q", out)
|
2015-04-11 03:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// escape sequence
|
|
|
|
if _, err := cpty.Write([]byte{16}); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:16:42 +00:00
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
if _, err := cpty.Write([]byte{17}); err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
cmd.Wait()
|
|
|
|
ch <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
running, err := inspectField(name, "State.Running")
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(err)
|
2015-04-11 03:16:42 +00:00
|
|
|
}
|
|
|
|
if running != "true" {
|
2015-04-27 20:33:30 +00:00
|
|
|
c.Fatal("expected container to still be running")
|
2015-04-11 03:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2015-04-18 16:46:47 +00:00
|
|
|
exec.Command(dockerBinary, "kill", name).Run()
|
2015-04-11 03:16:42 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal("timed out waiting for container to exit")
|
2015-04-11 03:16:42 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-29 18:22:21 +00:00
|
|
|
|
|
|
|
// "test" should be printed
|
|
|
|
func (s *DockerSuite) TestRunEchoStdoutWithCPUQuota(c *check.C) {
|
2015-07-22 12:59:24 +00:00
|
|
|
testRequires(c, cpuCfsQuota)
|
2015-07-14 20:15:00 +00:00
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "--cpu-quota", "8000", "--name", "test", "busybox", "echo", "test")
|
2015-05-29 18:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("failed to run container: %v, output: %q", err, out)
|
|
|
|
}
|
|
|
|
out = strings.TrimSpace(out)
|
|
|
|
if out != "test" {
|
|
|
|
c.Errorf("container should've printed 'test'")
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err = inspectField("test", "HostConfig.CpuQuota")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
|
|
|
if out != "8000" {
|
|
|
|
c.Fatalf("setting the CPU CFS quota failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestRunWithCpuPeriod(c *check.C) {
|
2015-07-22 12:59:24 +00:00
|
|
|
testRequires(c, cpuCfsPeriod)
|
2015-07-14 20:15:00 +00:00
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
if _, _, err := dockerCmdWithError("run", "--cpu-period", "50000", "--name", "test", "busybox", "true"); err != nil {
|
2015-05-29 18:22:21 +00:00
|
|
|
c.Fatalf("failed to run container: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := inspectField("test", "HostConfig.CpuPeriod")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
if out != "50000" {
|
|
|
|
c.Fatalf("setting the CPU CFS period failed")
|
|
|
|
}
|
|
|
|
}
|
2015-07-08 20:30:03 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestRunOOMExitCode(c *check.C) {
|
2015-07-22 12:59:24 +00:00
|
|
|
testRequires(c, oomControl)
|
2015-07-08 20:30:03 +00:00
|
|
|
errChan := make(chan error)
|
|
|
|
go func() {
|
|
|
|
defer close(errChan)
|
2015-07-27 18:13:25 +00:00
|
|
|
out, exitCode, _ := dockerCmdWithError("run", "-m", "4MB", "busybox", "sh", "-c", "x=a; while true; do x=$x$x$x$x; done")
|
2015-07-08 20:30:03 +00:00
|
|
|
if expected := 137; exitCode != expected {
|
|
|
|
errChan <- fmt.Errorf("wrong exit code for OOM container: expected %d, got %d (output: %q)", expected, exitCode, out)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
case <-time.After(30 * time.Second):
|
|
|
|
c.Fatal("Timeout waiting for container to die on OOM")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestContainerNetworkModeToSelf(c *check.C) {
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "--name=me", "--net=container:me", "busybox", "true")
|
2015-07-08 20:30:03 +00:00
|
|
|
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) {
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "-d", "--name", "parent", "busybox", "top")
|
2015-07-08 20:30:03 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("failed to run container: %v, output: %q", err, out)
|
|
|
|
}
|
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err = dockerCmdWithError("run", "--dns", "1.2.3.4", "--net=container:parent", "busybox")
|
2015-07-08 20:30:03 +00:00
|
|
|
if err == nil || !strings.Contains(out, "Conflicting options: --dns and the network mode") {
|
|
|
|
c.Fatalf("run --net=container with --dns should error out")
|
|
|
|
}
|
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err = dockerCmdWithError("run", "--mac-address", "92:d0:c6:0a:29:33", "--net=container:parent", "busybox")
|
2015-07-08 20:30:03 +00:00
|
|
|
if err == nil || !strings.Contains(out, "--mac-address and the network mode") {
|
|
|
|
c.Fatalf("run --net=container with --mac-address should error out")
|
|
|
|
}
|
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err = dockerCmdWithError("run", "--add-host", "test:192.168.2.109", "--net=container:parent", "busybox")
|
2015-07-08 20:30:03 +00:00
|
|
|
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) {
|
2015-07-14 20:15:00 +00:00
|
|
|
dockerCmd(c, "run", "-d", "--name", "parent", "busybox", "top")
|
2015-07-08 20:30:03 +00:00
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "-p", "5000:5000", "--net=container:parent", "busybox")
|
2015-07-08 20:30:03 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err = dockerCmdWithError("run", "-P", "--net=container:parent", "busybox")
|
2015-07-08 20:30:03 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err = dockerCmdWithError("run", "--expose", "5000", "--net=container:parent", "busybox")
|
2015-07-08 20:30:03 +00:00
|
|
|
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) {
|
2015-07-14 20:15:00 +00:00
|
|
|
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")
|
2015-07-08 20:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestRunLoopbackOnlyExistsWhenNetworkingDisabled(c *check.C) {
|
2015-07-14 20:15:00 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "--net=none", "busybox", "ip", "-o", "-4", "a", "show", "up")
|
2015-07-08 20:30:03 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
count = 0
|
|
|
|
parts = strings.Split(out, "\n")
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, l := range parts {
|
|
|
|
if l != "" {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if count != 1 {
|
|
|
|
c.Fatalf("Wrong interface count in container %d", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(out, "1: lo") {
|
|
|
|
c.Fatalf("Wrong interface in test container: expected [1: lo], got %s", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue #4681
|
|
|
|
func (s *DockerSuite) TestRunLoopbackWhenNetworkDisabled(c *check.C) {
|
2015-07-14 20:15:00 +00:00
|
|
|
dockerCmd(c, "run", "--net=none", "busybox", "ping", "-c", "1", "127.0.0.1")
|
2015-07-08 20:30:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestRunModeNetContainerHostname(c *check.C) {
|
|
|
|
testRequires(c, ExecSupport)
|
|
|
|
|
2015-07-14 20:15:00 +00:00
|
|
|
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")
|
|
|
|
|
2015-07-08 20:30:03 +00:00
|
|
|
if out1 != out {
|
|
|
|
c.Fatal("containers with shared net namespace should have same hostname")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestRunNetworkNotInitializedNoneMode(c *check.C) {
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("run", "-d", "--net=none", "busybox", "top")
|
2015-07-08 20:30:03 +00:00
|
|
|
id := strings.TrimSpace(out)
|
|
|
|
res, err := inspectField(id, "NetworkSettings.IPAddress")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
if res != "" {
|
|
|
|
c.Fatalf("For 'none' mode network must not be initialized, but container got IP: %s", res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestTwoContainersInNetHost(c *check.C) {
|
|
|
|
dockerCmd(c, "run", "-d", "--net=host", "--name=first", "busybox", "top")
|
|
|
|
dockerCmd(c, "run", "-d", "--net=host", "--name=second", "busybox", "top")
|
|
|
|
dockerCmd(c, "stop", "first")
|
|
|
|
dockerCmd(c, "stop", "second")
|
|
|
|
}
|