4 "github.com/docker/docker/integration-cli/checker"
5 "github.com/docker/docker/runconfig"
6 "github.com/go-check/check"
9 // GH14530. Validates combinations of --net= with other options
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"
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
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))
24 func (s *DockerSuite) TestNetHostnameWithNetHost(c *check.C) {
25 testRequires(c, DaemonIsLinux, NotUserNamespace)
27 out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ps")
28 c.Assert(out, checker.Contains, stringCheckPS)
31 func (s *DockerSuite) TestNetHostname(c *check.C) {
32 testRequires(c, DaemonIsLinux)
34 out, _ := dockerCmd(c, "run", "-h=name", "busybox", "ps")
35 c.Assert(out, checker.Contains, stringCheckPS)
37 out, _ = dockerCmd(c, "run", "-h=name", "--net=bridge", "busybox", "ps")
38 c.Assert(out, checker.Contains, stringCheckPS)
40 out, _ = dockerCmd(c, "run", "-h=name", "--net=none", "busybox", "ps")
41 c.Assert(out, checker.Contains, stringCheckPS)
43 out, _ = dockerCmdWithFail(c, "run", "-h=name", "--net=container:other", "busybox", "ps")
44 c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkHostname.Error())
46 out, _ = dockerCmdWithFail(c, "run", "--net=container", "busybox", "ps")
47 c.Assert(out, checker.Contains, "Invalid network mode: invalid container format container:<name|id>")
49 out, _ = dockerCmdWithFail(c, "run", "--net=weird", "busybox", "ps")
50 c.Assert(out, checker.Contains, "network weird not found")
53 func (s *DockerSuite) TestConflictContainerNetworkAndLinks(c *check.C) {
54 testRequires(c, DaemonIsLinux)
56 out, _ := dockerCmdWithFail(c, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps")
57 c.Assert(out, checker.Contains, runconfig.ErrConflictContainerNetworkAndLinks.Error())
60 func (s *DockerSuite) TestConflictContainerNetworkHostAndLinks(c *check.C) {
61 testRequires(c, DaemonIsLinux, NotUserNamespace)
63 out, _ := dockerCmdWithFail(c, "run", "--net=host", "--link=zip:zap", "busybox", "ps")
64 c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error())
67 func (s *DockerSuite) TestConflictNetworkModeNetHostAndOptions(c *check.C) {
68 testRequires(c, DaemonIsLinux, NotUserNamespace)
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())
74 func (s *DockerSuite) TestConflictNetworkModeAndOptions(c *check.C) {
75 testRequires(c, DaemonIsLinux)
77 out, _ := dockerCmdWithFail(c, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps")
78 c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkAndDNS.Error())
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())
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())
86 out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "-P", "busybox", "ps")
87 c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkPublishPorts.Error())
89 out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "-p", "8080", "busybox", "ps")
90 c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkPublishPorts.Error())
92 out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--expose", "8000-9000", "busybox", "ps")
93 c.Assert(out, checker.Contains, runconfig.ErrConflictNetworkExposePorts.Error())