github.com/hms58/moby@v1.13.1/integration-cli/docker_cli_links_test.go (about)

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