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