github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/integration-cli/docker_cli_netmode_test.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/runconfig"
     8  	"gotest.tools/v3/assert"
     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  type DockerCLINetmodeSuite struct {
    18  	ds *DockerSuite
    19  }
    20  
    21  func (s *DockerCLINetmodeSuite) TearDownTest(c *testing.T) {
    22  	s.ds.TearDownTest(c)
    23  }
    24  
    25  func (s *DockerCLINetmodeSuite) OnTimeout(c *testing.T) {
    26  	s.ds.OnTimeout(c)
    27  }
    28  
    29  // DockerCmdWithFail executes a docker command that is supposed to fail and returns
    30  // the output, the exit code. If the command returns a Nil error, it will fail and
    31  // stop the tests.
    32  func dockerCmdWithFail(c *testing.T, args ...string) (string, int) {
    33  	out, status, err := dockerCmdWithError(args...)
    34  	assert.Assert(c, err != nil, "%v", out)
    35  	return out, status
    36  }
    37  
    38  func (s *DockerCLINetmodeSuite) TestNetHostnameWithNetHost(c *testing.T) {
    39  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    40  
    41  	out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ps")
    42  	assert.Assert(c, strings.Contains(out, stringCheckPS))
    43  }
    44  
    45  func (s *DockerCLINetmodeSuite) TestNetHostname(c *testing.T) {
    46  	testRequires(c, DaemonIsLinux)
    47  
    48  	out, _ := dockerCmd(c, "run", "-h=name", "busybox", "ps")
    49  	assert.Assert(c, strings.Contains(out, stringCheckPS))
    50  	out, _ = dockerCmd(c, "run", "-h=name", "--net=bridge", "busybox", "ps")
    51  	assert.Assert(c, strings.Contains(out, stringCheckPS))
    52  	out, _ = dockerCmd(c, "run", "-h=name", "--net=none", "busybox", "ps")
    53  	assert.Assert(c, strings.Contains(out, stringCheckPS))
    54  	out, _ = dockerCmdWithFail(c, "run", "-h=name", "--net=container:other", "busybox", "ps")
    55  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkHostname.Error()))
    56  	out, _ = dockerCmdWithFail(c, "run", "--net=container", "busybox", "ps")
    57  	assert.Assert(c, strings.Contains(out, "invalid container format container:<name|id>"))
    58  	out, _ = dockerCmdWithFail(c, "run", "--net=weird", "busybox", "ps")
    59  	assert.Assert(c, strings.Contains(strings.ToLower(out), "not found"))
    60  }
    61  
    62  func (s *DockerCLINetmodeSuite) TestConflictContainerNetworkAndLinks(c *testing.T) {
    63  	testRequires(c, DaemonIsLinux)
    64  
    65  	out, _ := dockerCmdWithFail(c, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps")
    66  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictContainerNetworkAndLinks.Error()))
    67  }
    68  
    69  func (s *DockerCLINetmodeSuite) TestConflictContainerNetworkHostAndLinks(c *testing.T) {
    70  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    71  
    72  	out, _ := dockerCmdWithFail(c, "run", "--net=host", "--link=zip:zap", "busybox", "ps")
    73  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictHostNetworkAndLinks.Error()))
    74  }
    75  
    76  func (s *DockerCLINetmodeSuite) TestConflictNetworkModeNetHostAndOptions(c *testing.T) {
    77  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    78  
    79  	out, _ := dockerCmdWithFail(c, "run", "--net=host", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
    80  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictContainerNetworkAndMac.Error()))
    81  }
    82  
    83  func (s *DockerCLINetmodeSuite) TestConflictNetworkModeAndOptions(c *testing.T) {
    84  	testRequires(c, DaemonIsLinux)
    85  
    86  	out, _ := dockerCmdWithFail(c, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps")
    87  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkAndDNS.Error()))
    88  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--add-host=name:8.8.8.8", "busybox", "ps")
    89  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkHosts.Error()))
    90  	out, _ = dockerCmdWithFail(c, "run", "--net=container:other", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
    91  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictContainerNetworkAndMac.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  }