github.com/MaximeAubanel/moby@v1.13.1/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 a 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) TestNetHostnameWithNetHost(c *check.C) {
    25  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    26  
    27  	out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ps")
    28  	c.Assert(out, checker.Contains, stringCheckPS)
    29  }
    30  
    31  func (s *DockerSuite) TestNetHostname(c *check.C) {
    32  	testRequires(c, DaemonIsLinux)
    33  
    34  	out, _ := dockerCmd(c, "run", "-h=name", "busybox", "ps")
    35  	c.Assert(out, checker.Contains, stringCheckPS)
    36  
    37  	out, _ = dockerCmd(c, "run", "-h=name", "--net=bridge", "busybox", "ps")
    38  	c.Assert(out, checker.Contains, stringCheckPS)
    39  
    40  	out, _ = dockerCmd(c, "run", "-h=name", "--net=none", "busybox", "ps")
    41  	c.Assert(out, checker.Contains, stringCheckPS)
    42  
    43  	out, _ = dockerCmdWithFail(c, "run", "-h=name", "--net=container:other", "busybox", "ps")
    44  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkHostname.Error())
    45  
    46  	out, _ = dockerCmdWithFail(c, "run", "--net=container", "busybox", "ps")
    47  	c.Assert(out, checker.Contains, "--net: invalid net mode: invalid container format container:<name|id>")
    48  
    49  	out, _ = dockerCmdWithFail(c, "run", "--net=weird", "busybox", "ps")
    50  	c.Assert(out, checker.Contains, "network weird not found")
    51  }
    52  
    53  func (s *DockerSuite) TestConflictContainerNetworkAndLinks(c *check.C) {
    54  	testRequires(c, DaemonIsLinux)
    55  
    56  	out, _ := dockerCmdWithFail(c, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps")
    57  	c.Assert(out, checker.Contains, runconfig.ErrConflictContainerNetworkAndLinks.Error())
    58  }
    59  
    60  func (s *DockerSuite) TestConflictContainerNetworkHostAndLinks(c *check.C) {
    61  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    62  
    63  	out, _ := dockerCmdWithFail(c, "run", "--net=host", "--link=zip:zap", "busybox", "ps")
    64  	c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error())
    65  }
    66  
    67  func (s *DockerSuite) TestConflictNetworkModeNetHostAndOptions(c *check.C) {
    68  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    69  
    70  	out, _ := dockerCmdWithFail(c, "run", "--net=host", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
    71  	c.Assert(out, checker.Contains, runconfig.ErrConflictContainerNetworkAndMac.Error())
    72  }
    73  
    74  func (s *DockerSuite) TestConflictNetworkModeAndOptions(c *check.C) {
    75  	testRequires(c, DaemonIsLinux)
    76  
    77  	out, _ := dockerCmdWithFail(c, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps")
    78  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkAndDNS.Error())
    79  
    80  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--add-host=name:8.8.8.8", "busybox", "ps")
    81  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkHosts.Error())
    82  
    83  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
    84  	c.Assert(out, checker.Contains, runconfig.ErrConflictContainerNetworkAndMac.Error())
    85  
    86  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "-P", "busybox", "ps")
    87  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkPublishPorts.Error())
    88  
    89  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "-p", "8080", "busybox", "ps")
    90  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkPublishPorts.Error())
    91  
    92  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--expose", "8000-9000", "busybox", "ps")
    93  	c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkExposePorts.Error())
    94  }