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