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