github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/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  	dockerCmd(c, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
    35  	dockerCmd(c, "run", "-d", "--name", "container2", "--hostname", "wilma", "busybox", "top")
    36  
    37  	runArgs := []string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c"}
    38  	pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
    39  
    40  	// test ping by alias, ping by name, and ping by hostname
    41  	// 1. Ping by alias
    42  	dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
    43  	// 2. Ping by container name
    44  	dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
    45  	// 3. Ping by hostname
    46  	dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
    47  
    48  }
    49  
    50  func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *check.C) {
    51  	testRequires(c, DaemonIsLinux)
    52  	out, _ := dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
    53  	idA := strings.TrimSpace(out)
    54  	out, _ = dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
    55  	idB := strings.TrimSpace(out)
    56  	dockerCmd(c, "rename", "container1", "container_new")
    57  	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")
    58  	dockerCmd(c, "kill", idA)
    59  	dockerCmd(c, "kill", idB)
    60  
    61  }
    62  
    63  func (s *DockerSuite) TestLinksInspectLinksStarted(c *check.C) {
    64  	testRequires(c, DaemonIsLinux)
    65  	var (
    66  		expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
    67  		result   []string
    68  	)
    69  	dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
    70  	dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
    71  	dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "top")
    72  	links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links")
    73  
    74  	err := unmarshalJSON([]byte(links), &result)
    75  	c.Assert(err, checker.IsNil)
    76  
    77  	output := convertSliceOfStringsToMap(result)
    78  
    79  	c.Assert(output, checker.DeepEquals, expected)
    80  }
    81  
    82  func (s *DockerSuite) TestLinksInspectLinksStopped(c *check.C) {
    83  	testRequires(c, DaemonIsLinux)
    84  	var (
    85  		expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
    86  		result   []string
    87  	)
    88  	dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
    89  	dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
    90  	dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
    91  	links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links")
    92  
    93  	err := unmarshalJSON([]byte(links), &result)
    94  	c.Assert(err, checker.IsNil)
    95  
    96  	output := convertSliceOfStringsToMap(result)
    97  
    98  	c.Assert(output, checker.DeepEquals, expected)
    99  }
   100  
   101  func (s *DockerSuite) TestLinksNotStartedParentNotFail(c *check.C) {
   102  	testRequires(c, DaemonIsLinux)
   103  	dockerCmd(c, "create", "--name=first", "busybox", "top")
   104  	dockerCmd(c, "create", "--name=second", "--link=first:first", "busybox", "top")
   105  	dockerCmd(c, "start", "first")
   106  
   107  }
   108  
   109  func (s *DockerSuite) TestLinksHostsFilesInject(c *check.C) {
   110  	testRequires(c, DaemonIsLinux)
   111  	testRequires(c, SameHostDaemon, ExecSupport)
   112  
   113  	out, _ := dockerCmd(c, "run", "-itd", "--name", "one", "busybox", "top")
   114  	idOne := strings.TrimSpace(out)
   115  
   116  	out, _ = dockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top")
   117  	idTwo := strings.TrimSpace(out)
   118  
   119  	c.Assert(waitRun(idTwo), checker.IsNil)
   120  
   121  	contentOne, err := readContainerFileWithExec(idOne, "/etc/hosts")
   122  	c.Assert(err, checker.IsNil, check.Commentf("contentOne: %s", string(contentOne)))
   123  
   124  	contentTwo, err := readContainerFileWithExec(idTwo, "/etc/hosts")
   125  	c.Assert(err, checker.IsNil, check.Commentf("contentTwo: %s", string(contentTwo)))
   126  	// Host is not present in updated hosts file
   127  	c.Assert(string(contentTwo), checker.Contains, "onetwo")
   128  }
   129  
   130  func (s *DockerSuite) TestLinksUpdateOnRestart(c *check.C) {
   131  	testRequires(c, DaemonIsLinux)
   132  	testRequires(c, SameHostDaemon, ExecSupport)
   133  	dockerCmd(c, "run", "-d", "--name", "one", "busybox", "top")
   134  	out, _ := dockerCmd(c, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top")
   135  	id := strings.TrimSpace(string(out))
   136  
   137  	realIP := inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress")
   138  	content, err := readContainerFileWithExec(id, "/etc/hosts")
   139  	c.Assert(err, checker.IsNil)
   140  
   141  	getIP := func(hosts []byte, hostname string) string {
   142  		re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
   143  		matches := re.FindSubmatch(hosts)
   144  		c.Assert(matches, checker.NotNil, check.Commentf("Hostname %s have no matches in hosts", hostname))
   145  		return string(matches[1])
   146  	}
   147  	ip := getIP(content, "one")
   148  	c.Assert(ip, checker.Equals, realIP)
   149  
   150  	ip = getIP(content, "onetwo")
   151  	c.Assert(ip, checker.Equals, realIP)
   152  
   153  	dockerCmd(c, "restart", "one")
   154  	realIP = inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress")
   155  
   156  	content, err = readContainerFileWithExec(id, "/etc/hosts")
   157  	c.Assert(err, checker.IsNil, check.Commentf("content: %s", string(content)))
   158  	ip = getIP(content, "one")
   159  	c.Assert(ip, checker.Equals, realIP)
   160  
   161  	ip = getIP(content, "onetwo")
   162  	c.Assert(ip, checker.Equals, realIP)
   163  }
   164  
   165  func (s *DockerSuite) TestLinksEnvs(c *check.C) {
   166  	testRequires(c, DaemonIsLinux)
   167  	dockerCmd(c, "run", "-d", "-e", "e1=", "-e", "e2=v2", "-e", "e3=v3=v3", "--name=first", "busybox", "top")
   168  	out, _ := dockerCmd(c, "run", "--name=second", "--link=first:first", "busybox", "env")
   169  	c.Assert(out, checker.Contains, "FIRST_ENV_e1=\n")
   170  	c.Assert(out, checker.Contains, "FIRST_ENV_e2=v2")
   171  	c.Assert(out, checker.Contains, "FIRST_ENV_e3=v3=v3")
   172  }
   173  
   174  func (s *DockerSuite) TestLinkShortDefinition(c *check.C) {
   175  	testRequires(c, DaemonIsLinux)
   176  	out, _ := dockerCmd(c, "run", "-d", "--name", "shortlinkdef", "busybox", "top")
   177  
   178  	cid := strings.TrimSpace(out)
   179  	c.Assert(waitRun(cid), checker.IsNil)
   180  
   181  	out, _ = dockerCmd(c, "run", "-d", "--name", "link2", "--link", "shortlinkdef", "busybox", "top")
   182  
   183  	cid2 := strings.TrimSpace(out)
   184  	c.Assert(waitRun(cid2), checker.IsNil)
   185  
   186  	links := inspectFieldJSON(c, cid2, "HostConfig.Links")
   187  	c.Assert(links, checker.Equals, "[\"/shortlinkdef:/link2/shortlinkdef\"]")
   188  }
   189  
   190  func (s *DockerSuite) TestLinksNetworkHostContainer(c *check.C) {
   191  	testRequires(c, DaemonIsLinux, NotUserNamespace)
   192  	dockerCmd(c, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top")
   193  	out, _, err := dockerCmdWithError("run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true")
   194  
   195  	// Running container linking to a container with --net host should have failed
   196  	c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
   197  	// Running container linking to a container with --net host should have failed
   198  	c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error())
   199  }
   200  
   201  func (s *DockerSuite) TestLinksEtcHostsRegularFile(c *check.C) {
   202  	testRequires(c, DaemonIsLinux, NotUserNamespace)
   203  	out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
   204  	// /etc/hosts should be a regular file
   205  	c.Assert(out, checker.Matches, "^-.+\n")
   206  }
   207  
   208  func (s *DockerSuite) TestLinksMultipleWithSameName(c *check.C) {
   209  	testRequires(c, DaemonIsLinux)
   210  	dockerCmd(c, "run", "-d", "--name=upstream-a", "busybox", "top")
   211  	dockerCmd(c, "run", "-d", "--name=upstream-b", "busybox", "top")
   212  	dockerCmd(c, "run", "--link", "upstream-a:upstream", "--link", "upstream-b:upstream", "busybox", "sh", "-c", "ping -c 1 upstream")
   213  }