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