github.com/portworx/docker@v1.12.1/integration-cli/docker_cli_links_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  
     8  	"github.com/docker/docker/pkg/integration/checker"
     9  	"github.com/docker/docker/runconfig"
    10  	"github.com/go-check/check"
    11  )
    12  
    13  func (s *DockerSuite) TestLinksPingUnlinkedContainers(c *check.C) {
    14  	testRequires(c, DaemonIsLinux)
    15  	_, exitCode, err := dockerCmdWithError("run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
    16  
    17  	// run ping failed with error
    18  	c.Assert(exitCode, checker.Equals, 1, check.Commentf("error: %v", err))
    19  }
    20  
    21  // Test for appropriate error when calling --link with an invalid target container
    22  func (s *DockerSuite) TestLinksInvalidContainerTarget(c *check.C) {
    23  	testRequires(c, DaemonIsLinux)
    24  	out, _, err := dockerCmdWithError("run", "--link", "bogus:alias", "busybox", "true")
    25  
    26  	// an invalid container target should produce an error
    27  	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
    28  	// an invalid container target should produce an error
    29  	c.Assert(out, checker.Contains, "Could not get container")
    30  }
    31  
    32  func (s *DockerSuite) TestLinksPingLinkedContainers(c *check.C) {
    33  	testRequires(c, DaemonIsLinux)
    34  	// Test with the three different ways of specifying the default network on Linux
    35  	testLinkPingOnNetwork(c, "")
    36  	testLinkPingOnNetwork(c, "default")
    37  	testLinkPingOnNetwork(c, "bridge")
    38  }
    39  
    40  func testLinkPingOnNetwork(c *check.C, network string) {
    41  	var postArgs []string
    42  	if network != "" {
    43  		postArgs = append(postArgs, []string{"--net", network}...)
    44  	}
    45  	postArgs = append(postArgs, []string{"busybox", "top"}...)
    46  	runArgs1 := append([]string{"run", "-d", "--name", "container1", "--hostname", "fred"}, postArgs...)
    47  	runArgs2 := append([]string{"run", "-d", "--name", "container2", "--hostname", "wilma"}, postArgs...)
    48  
    49  	// Run the two named containers
    50  	dockerCmd(c, runArgs1...)
    51  	dockerCmd(c, runArgs2...)
    52  
    53  	postArgs = []string{}
    54  	if network != "" {
    55  		postArgs = append(postArgs, []string{"--net", network}...)
    56  	}
    57  	postArgs = append(postArgs, []string{"busybox", "sh", "-c"}...)
    58  
    59  	// Format a run for a container which links to the other two
    60  	runArgs := append([]string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2"}, postArgs...)
    61  	pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
    62  
    63  	// test ping by alias, ping by name, and ping by hostname
    64  	// 1. Ping by alias
    65  	dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
    66  	// 2. Ping by container name
    67  	dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
    68  	// 3. Ping by hostname
    69  	dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
    70  
    71  	// Clean for next round
    72  	dockerCmd(c, "rm", "-f", "container1")
    73  	dockerCmd(c, "rm", "-f", "container2")
    74  }
    75  
    76  func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *check.C) {
    77  	testRequires(c, DaemonIsLinux)
    78  	out, _ := dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
    79  	idA := strings.TrimSpace(out)
    80  	out, _ = dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
    81  	idB := strings.TrimSpace(out)
    82  	dockerCmd(c, "rename", "container1", "container_new")
    83  	dockerCmd(c, "run", "--rm", "--link", "container_new:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
    84  	dockerCmd(c, "kill", idA)
    85  	dockerCmd(c, "kill", idB)
    86  
    87  }
    88  
    89  func (s *DockerSuite) TestLinksInspectLinksStarted(c *check.C) {
    90  	testRequires(c, DaemonIsLinux)
    91  	var (
    92  		expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
    93  		result   []string
    94  	)
    95  	dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
    96  	dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
    97  	dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "top")
    98  	links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links")
    99  
   100  	err := unmarshalJSON([]byte(links), &result)
   101  	c.Assert(err, checker.IsNil)
   102  
   103  	output := convertSliceOfStringsToMap(result)
   104  
   105  	c.Assert(output, checker.DeepEquals, expected)
   106  }
   107  
   108  func (s *DockerSuite) TestLinksInspectLinksStopped(c *check.C) {
   109  	testRequires(c, DaemonIsLinux)
   110  	var (
   111  		expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
   112  		result   []string
   113  	)
   114  	dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
   115  	dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
   116  	dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
   117  	links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links")
   118  
   119  	err := unmarshalJSON([]byte(links), &result)
   120  	c.Assert(err, checker.IsNil)
   121  
   122  	output := convertSliceOfStringsToMap(result)
   123  
   124  	c.Assert(output, checker.DeepEquals, expected)
   125  }
   126  
   127  func (s *DockerSuite) TestLinksNotStartedParentNotFail(c *check.C) {
   128  	testRequires(c, DaemonIsLinux)
   129  	dockerCmd(c, "create", "--name=first", "busybox", "top")
   130  	dockerCmd(c, "create", "--name=second", "--link=first:first", "busybox", "top")
   131  	dockerCmd(c, "start", "first")
   132  
   133  }
   134  
   135  func (s *DockerSuite) TestLinksHostsFilesInject(c *check.C) {
   136  	testRequires(c, DaemonIsLinux)
   137  	testRequires(c, SameHostDaemon, ExecSupport)
   138  
   139  	out, _ := dockerCmd(c, "run", "-itd", "--name", "one", "busybox", "top")
   140  	idOne := strings.TrimSpace(out)
   141  
   142  	out, _ = dockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top")
   143  	idTwo := strings.TrimSpace(out)
   144  
   145  	c.Assert(waitRun(idTwo), checker.IsNil)
   146  
   147  	contentOne, err := readContainerFileWithExec(idOne, "/etc/hosts")
   148  	c.Assert(err, checker.IsNil, check.Commentf("contentOne: %s", string(contentOne)))
   149  
   150  	contentTwo, err := readContainerFileWithExec(idTwo, "/etc/hosts")
   151  	c.Assert(err, checker.IsNil, check.Commentf("contentTwo: %s", string(contentTwo)))
   152  	// Host is not present in updated hosts file
   153  	c.Assert(string(contentTwo), checker.Contains, "onetwo")
   154  }
   155  
   156  func (s *DockerSuite) TestLinksUpdateOnRestart(c *check.C) {
   157  	testRequires(c, DaemonIsLinux)
   158  	testRequires(c, SameHostDaemon, ExecSupport)
   159  	dockerCmd(c, "run", "-d", "--name", "one", "busybox", "top")
   160  	out, _ := dockerCmd(c, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top")
   161  	id := strings.TrimSpace(string(out))
   162  
   163  	realIP := inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress")
   164  	content, err := readContainerFileWithExec(id, "/etc/hosts")
   165  	c.Assert(err, checker.IsNil)
   166  
   167  	getIP := func(hosts []byte, hostname string) string {
   168  		re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
   169  		matches := re.FindSubmatch(hosts)
   170  		c.Assert(matches, checker.NotNil, check.Commentf("Hostname %s have no matches in hosts", hostname))
   171  		return string(matches[1])
   172  	}
   173  	ip := getIP(content, "one")
   174  	c.Assert(ip, checker.Equals, realIP)
   175  
   176  	ip = getIP(content, "onetwo")
   177  	c.Assert(ip, checker.Equals, realIP)
   178  
   179  	dockerCmd(c, "restart", "one")
   180  	realIP = inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress")
   181  
   182  	content, err = readContainerFileWithExec(id, "/etc/hosts")
   183  	c.Assert(err, checker.IsNil, check.Commentf("content: %s", string(content)))
   184  	ip = getIP(content, "one")
   185  	c.Assert(ip, checker.Equals, realIP)
   186  
   187  	ip = getIP(content, "onetwo")
   188  	c.Assert(ip, checker.Equals, realIP)
   189  }
   190  
   191  func (s *DockerSuite) TestLinksEnvs(c *check.C) {
   192  	testRequires(c, DaemonIsLinux)
   193  	dockerCmd(c, "run", "-d", "-e", "e1=", "-e", "e2=v2", "-e", "e3=v3=v3", "--name=first", "busybox", "top")
   194  	out, _ := dockerCmd(c, "run", "--name=second", "--link=first:first", "busybox", "env")
   195  	c.Assert(out, checker.Contains, "FIRST_ENV_e1=\n")
   196  	c.Assert(out, checker.Contains, "FIRST_ENV_e2=v2")
   197  	c.Assert(out, checker.Contains, "FIRST_ENV_e3=v3=v3")
   198  }
   199  
   200  func (s *DockerSuite) TestLinkShortDefinition(c *check.C) {
   201  	testRequires(c, DaemonIsLinux)
   202  	out, _ := dockerCmd(c, "run", "-d", "--name", "shortlinkdef", "busybox", "top")
   203  
   204  	cid := strings.TrimSpace(out)
   205  	c.Assert(waitRun(cid), checker.IsNil)
   206  
   207  	out, _ = dockerCmd(c, "run", "-d", "--name", "link2", "--link", "shortlinkdef", "busybox", "top")
   208  
   209  	cid2 := strings.TrimSpace(out)
   210  	c.Assert(waitRun(cid2), checker.IsNil)
   211  
   212  	links := inspectFieldJSON(c, cid2, "HostConfig.Links")
   213  	c.Assert(links, checker.Equals, "[\"/shortlinkdef:/link2/shortlinkdef\"]")
   214  }
   215  
   216  func (s *DockerSuite) TestLinksNetworkHostContainer(c *check.C) {
   217  	testRequires(c, DaemonIsLinux, NotUserNamespace)
   218  	dockerCmd(c, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top")
   219  	out, _, err := dockerCmdWithError("run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true")
   220  
   221  	// Running container linking to a container with --net host should have failed
   222  	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
   223  	// Running container linking to a container with --net host should have failed
   224  	c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error())
   225  }
   226  
   227  func (s *DockerSuite) TestLinksEtcHostsRegularFile(c *check.C) {
   228  	testRequires(c, DaemonIsLinux, NotUserNamespace)
   229  	out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
   230  	// /etc/hosts should be a regular file
   231  	c.Assert(out, checker.Matches, "^-.+\n")
   232  }
   233  
   234  func (s *DockerSuite) TestLinksMultipleWithSameName(c *check.C) {
   235  	testRequires(c, DaemonIsLinux)
   236  	dockerCmd(c, "run", "-d", "--name=upstream-a", "busybox", "top")
   237  	dockerCmd(c, "run", "-d", "--name=upstream-b", "busybox", "top")
   238  	dockerCmd(c, "run", "--link", "upstream-a:upstream", "--link", "upstream-b:upstream", "busybox", "sh", "-c", "ping -c 1 upstream")
   239  }