github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/integration-cli/docker_cli_netmode_test.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/Prakhar-Agarwal-byte/moby/integration-cli/cli"
     9  	"github.com/Prakhar-Agarwal-byte/moby/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  	out, _, err := dockerCmdWithError(args...)
    35  	assert.Assert(c, err != nil, "%v", out)
    36  	return out
    37  }
    38  
    39  func (s *DockerCLINetmodeSuite) TestNetHostnameWithNetHost(c *testing.T) {
    40  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    41  
    42  	out := cli.DockerCmd(c, "run", "--net=host", "busybox", "ps").Stdout()
    43  	assert.Assert(c, strings.Contains(out, stringCheckPS))
    44  }
    45  
    46  func (s *DockerCLINetmodeSuite) TestNetHostname(c *testing.T) {
    47  	testRequires(c, DaemonIsLinux)
    48  
    49  	out := cli.DockerCmd(c, "run", "-h=name", "busybox", "ps").Stdout()
    50  	assert.Assert(c, strings.Contains(out, stringCheckPS))
    51  	out = cli.DockerCmd(c, "run", "-h=name", "--net=bridge", "busybox", "ps").Stdout()
    52  	assert.Assert(c, strings.Contains(out, stringCheckPS))
    53  	out = cli.DockerCmd(c, "run", "-h=name", "--net=none", "busybox", "ps").Stdout()
    54  	assert.Assert(c, strings.Contains(out, stringCheckPS))
    55  	out = dockerCmdWithFail(c, "run", "-h=name", "--net=container:other", "busybox", "ps")
    56  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkHostname.Error()))
    57  	out = dockerCmdWithFail(c, "run", "--net=container", "busybox", "ps")
    58  	assert.Assert(c, strings.Contains(out, "invalid container format container:<name|id>"))
    59  	out = dockerCmdWithFail(c, "run", "--net=weird", "busybox", "ps")
    60  	assert.Assert(c, strings.Contains(strings.ToLower(out), "not found"))
    61  }
    62  
    63  func (s *DockerCLINetmodeSuite) TestConflictContainerNetworkAndLinks(c *testing.T) {
    64  	testRequires(c, DaemonIsLinux)
    65  
    66  	out := dockerCmdWithFail(c, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps")
    67  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictContainerNetworkAndLinks.Error()))
    68  }
    69  
    70  func (s *DockerCLINetmodeSuite) TestConflictContainerNetworkHostAndLinks(c *testing.T) {
    71  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    72  
    73  	out := dockerCmdWithFail(c, "run", "--net=host", "--link=zip:zap", "busybox", "ps")
    74  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictHostNetworkAndLinks.Error()))
    75  }
    76  
    77  func (s *DockerCLINetmodeSuite) TestConflictNetworkModeNetHostAndOptions(c *testing.T) {
    78  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    79  
    80  	out := dockerCmdWithFail(c, "run", "--net=host", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
    81  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictContainerNetworkAndMac.Error()))
    82  }
    83  
    84  func (s *DockerCLINetmodeSuite) TestConflictNetworkModeAndOptions(c *testing.T) {
    85  	testRequires(c, DaemonIsLinux)
    86  
    87  	out := dockerCmdWithFail(c, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps")
    88  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkAndDNS.Error()))
    89  	out = dockerCmdWithFail(c, "run", "--net=container:other", "--add-host=name:8.8.8.8", "busybox", "ps")
    90  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkHosts.Error()))
    91  	out = dockerCmdWithFail(c, "run", "--net=container:other", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
    92  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictContainerNetworkAndMac.Error()))
    93  	out = dockerCmdWithFail(c, "run", "--net=container:other", "-P", "busybox", "ps")
    94  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkPublishPorts.Error()))
    95  	out = dockerCmdWithFail(c, "run", "--net=container:other", "-p", "8080", "busybox", "ps")
    96  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkPublishPorts.Error()))
    97  	out = dockerCmdWithFail(c, "run", "--net=container:other", "--expose", "8000-9000", "busybox", "ps")
    98  	assert.Assert(c, strings.Contains(out, runconfig.ErrConflictNetworkExposePorts.Error()))
    99  }