github.com/damirazo/docker@v1.9.0/integration-cli/docker_cli_netmode_test.go (about)

     1  package main
     2  
     3  import (
     4  	"os/exec"
     5  	"strings"
     6  
     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  // checkContains is a helper function that validates a command output did
    18  // contain what was expected.
    19  func checkContains(expected string, out string, c *check.C) {
    20  	if !strings.Contains(out, expected) {
    21  		c.Fatalf("Expected '%s', got '%s'", expected, out)
    22  	}
    23  }
    24  
    25  func (s *DockerSuite) TestNetHostname(c *check.C) {
    26  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    27  
    28  	var (
    29  		out    string
    30  		err    error
    31  		runCmd *exec.Cmd
    32  	)
    33  
    34  	runCmd = exec.Command(dockerBinary, "run", "-h=name", "busybox", "ps")
    35  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    36  		c.Fatalf(out, err)
    37  	}
    38  	checkContains(stringCheckPS, out, c)
    39  
    40  	runCmd = exec.Command(dockerBinary, "run", "--net=host", "busybox", "ps")
    41  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    42  		c.Fatalf(out, err)
    43  	}
    44  	checkContains(stringCheckPS, out, c)
    45  
    46  	runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=bridge", "busybox", "ps")
    47  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    48  		c.Fatalf(out, err)
    49  	}
    50  	checkContains(stringCheckPS, out, c)
    51  
    52  	runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=none", "busybox", "ps")
    53  	if out, _, err = runCommandWithOutput(runCmd); err != nil {
    54  		c.Fatalf(out, err)
    55  	}
    56  	checkContains(stringCheckPS, out, c)
    57  
    58  	runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=host", "busybox", "ps")
    59  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
    60  		c.Fatalf(out, err)
    61  	}
    62  	checkContains(runconfig.ErrConflictNetworkHostname.Error(), out, c)
    63  
    64  	runCmd = exec.Command(dockerBinary, "run", "-h=name", "--net=container:other", "busybox", "ps")
    65  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
    66  		c.Fatalf(out, err, c)
    67  	}
    68  	checkContains(runconfig.ErrConflictNetworkHostname.Error(), out, c)
    69  
    70  	runCmd = exec.Command(dockerBinary, "run", "--net=container", "busybox", "ps")
    71  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
    72  		c.Fatalf(out, err, c)
    73  	}
    74  	checkContains("--net: invalid net mode: invalid container format container:<name|id>", out, c)
    75  
    76  	runCmd = exec.Command(dockerBinary, "run", "--net=weird", "busybox", "ps")
    77  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
    78  		c.Fatalf(out, err)
    79  	}
    80  	checkContains("network weird not found", out, c)
    81  }
    82  
    83  func (s *DockerSuite) TestConflictContainerNetworkAndLinks(c *check.C) {
    84  	testRequires(c, DaemonIsLinux, NotUserNamespace)
    85  	var (
    86  		out    string
    87  		err    error
    88  		runCmd *exec.Cmd
    89  	)
    90  
    91  	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--link=zip:zap", "busybox", "ps")
    92  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
    93  		c.Fatalf(out, err)
    94  	}
    95  	checkContains(runconfig.ErrConflictContainerNetworkAndLinks.Error(), out, c)
    96  
    97  	runCmd = exec.Command(dockerBinary, "run", "--net=host", "--link=zip:zap", "busybox", "ps")
    98  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
    99  		c.Fatalf(out, err)
   100  	}
   101  	checkContains(runconfig.ErrConflictHostNetworkAndLinks.Error(), out, c)
   102  }
   103  
   104  func (s *DockerSuite) TestConflictNetworkModeAndOptions(c *check.C) {
   105  	testRequires(c, DaemonIsLinux, NotUserNamespace)
   106  	var (
   107  		out    string
   108  		err    error
   109  		runCmd *exec.Cmd
   110  	)
   111  
   112  	runCmd = exec.Command(dockerBinary, "run", "--net=host", "--dns=8.8.8.8", "busybox", "ps")
   113  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
   114  		c.Fatalf(out, err)
   115  	}
   116  	checkContains(runconfig.ErrConflictNetworkAndDNS.Error(), out, c)
   117  
   118  	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--dns=8.8.8.8", "busybox", "ps")
   119  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
   120  		c.Fatalf(out, err)
   121  	}
   122  	checkContains(runconfig.ErrConflictNetworkAndDNS.Error(), out, c)
   123  
   124  	runCmd = exec.Command(dockerBinary, "run", "--net=host", "--add-host=name:8.8.8.8", "busybox", "ps")
   125  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
   126  		c.Fatalf(out, err)
   127  	}
   128  	checkContains(runconfig.ErrConflictNetworkHosts.Error(), out, c)
   129  
   130  	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--add-host=name:8.8.8.8", "busybox", "ps")
   131  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
   132  		c.Fatalf(out, err)
   133  	}
   134  	checkContains(runconfig.ErrConflictNetworkHosts.Error(), out, c)
   135  
   136  	runCmd = exec.Command(dockerBinary, "run", "--net=host", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
   137  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
   138  		c.Fatalf(out, err)
   139  	}
   140  	checkContains(runconfig.ErrConflictContainerNetworkAndMac.Error(), out, c)
   141  
   142  	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--mac-address=92:d0:c6:0a:29:33", "busybox", "ps")
   143  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
   144  		c.Fatalf(out, err)
   145  	}
   146  	checkContains(runconfig.ErrConflictContainerNetworkAndMac.Error(), out, c)
   147  
   148  	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "-P", "busybox", "ps")
   149  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
   150  		c.Fatalf(out, err)
   151  	}
   152  	checkContains(runconfig.ErrConflictNetworkPublishPorts.Error(), out, c)
   153  
   154  	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "-p", "8080", "busybox", "ps")
   155  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
   156  		c.Fatalf(out, err)
   157  	}
   158  	checkContains(runconfig.ErrConflictNetworkPublishPorts.Error(), out, c)
   159  
   160  	runCmd = exec.Command(dockerBinary, "run", "--net=container:other", "--expose", "8000-9000", "busybox", "ps")
   161  	if out, _, err = runCommandWithOutput(runCmd); err == nil {
   162  		c.Fatalf(out, err)
   163  	}
   164  	checkContains(runconfig.ErrConflictNetworkExposePorts.Error(), out, c)
   165  }