github.com/hustcat/docker@v1.3.3-0.20160314103604-901c67a8eeab/integration-cli/docker_cli_netmode_test.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/docker/docker/pkg/integration/checker"
     5  	"github.com/docker/docker/runconfig"
     6  	"github.com/go-check/check"
     7  )
     8  
     9  // GH14530. Validates combinations of --net= with other options
    10  
    11  // stringCheckPS is how the output of PS starts in order to validate that
    12  // the command executed in a container did really run PS correctly.
    13  const stringCheckPS = "PID   USER"
    14  
    15  // DockerCmdWithFail executes a docker command that is supposed to fail and returns
    16  // the output, the exit code. If the command returns an Nil error, it will fail and
    17  // stop the tests.
    18  func dockerCmdWithFail(c *check.C, args ...string) (string, int) {
    19  	out, status, err := dockerCmdWithError(args...)
    20  	c.Assert(err, check.NotNil, check.Commentf("%v", out))
    21  	return out, status
    22  }
    23  
    24  func (s *DockerSuite) TestNetHostname(c *check.C) {
    25  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    26  
    27  	out, _ := dockerCmd(c, "run", "-h=name", "busybox", "ps")
    28  	c.Assert(out, checker.Contains, stringCheckPS)
    29  
    30  	out, _ = dockerCmd(c, "run", "--net=host", "busybox", "ps")
    31  	c.Assert(out, checker.Contains, stringCheckPS)
    32  
    33  	out, _ = dockerCmd(c, "run", "-h=name", "--net=bridge", "busybox", "ps")
    34  	c.Assert(out, checker.Contains, stringCheckPS)
    35  
    36  	out, _ = dockerCmd(c, "run", "-h=name", "--net=none", "busybox", "ps")
    37  	c.Assert(out, checker.Contains, stringCheckPS)
    38  
    39  	out, _ = dockerCmdWithFail(c, "run", "-h=name", "--net=host", "busybox", "ps")
    40  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkHostname.Error())
    41  
    42  	out, _ = dockerCmdWithFail(c, "run", "-h=name", "--net=container:other", "busybox", "ps")
    43  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkHostname.Error())
    44  
    45  	out, _ = dockerCmdWithFail(c, "run", "--net=container", "busybox", "ps")
    46  	c.Assert(out, checker.Contains, "--net: invalid net mode: invalid container format container:<name|id>")
    47  
    48  	out, _ = dockerCmdWithFail(c, "run", "--net=weird", "busybox", "ps")
    49  	c.Assert(out, checker.Contains, "network weird not found")
    50  }
    51  
    52  func (s *DockerSuite) TestConflictContainerNetworkAndLinks(c *check.C) {
    53  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    54  
    55  	out, _ := dockerCmdWithFail(c, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps")
    56  	c.Assert(out, checker.Contains, runconfig.ErrConflictContainerNetworkAndLinks.Error())
    57  
    58  	out, _ = dockerCmdWithFail(c, "run", "--net=host", "--link=zip:zap", "busybox", "ps")
    59  	c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error())
    60  }
    61  
    62  func (s *DockerSuite) TestConflictNetworkModeAndOptions(c *check.C) {
    63  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    64  
    65  	out, _ := dockerCmdWithFail(c, "run", "--net=host", "--dns=8.8.8.8", "busybox", "ps")
    66  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkAndDNS.Error())
    67  
    68  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps")
    69  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkAndDNS.Error())
    70  
    71  	out, _ = dockerCmdWithFail(c, "run", "--net=host", "--add-host=name:8.8.8.8", "busybox", "ps")
    72  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkHosts.Error())
    73  
    74  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--add-host=name:8.8.8.8", "busybox", "ps")
    75  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkHosts.Error())
    76  
    77  	out, _ = dockerCmdWithFail(c, "run", "--net=host", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
    78  	c.Assert(out, checker.Contains, runconfig.ErrConflictContainerNetworkAndMac.Error())
    79  
    80  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
    81  	c.Assert(out, checker.Contains, runconfig.ErrConflictContainerNetworkAndMac.Error())
    82  
    83  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "-P", "busybox", "ps")
    84  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkPublishPorts.Error())
    85  
    86  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "-p", "8080", "busybox", "ps")
    87  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkPublishPorts.Error())
    88  
    89  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--expose", "8000-9000", "busybox", "ps")
    90  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkExposePorts.Error())
    91  }