github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/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 // note: convert the output to lowercase first as the error string 32 // capitalization was changed after API version 1.32 33 c.Assert(strings.ToLower(out), checker.Contains, "could not get container") 34 } 35 36 func (s *DockerSuite) TestLinksPingLinkedContainers(c *check.C) { 37 testRequires(c, DaemonIsLinux) 38 // Test with the three different ways of specifying the default network on Linux 39 testLinkPingOnNetwork(c, "") 40 testLinkPingOnNetwork(c, "default") 41 testLinkPingOnNetwork(c, "bridge") 42 } 43 44 func testLinkPingOnNetwork(c *check.C, network string) { 45 var postArgs []string 46 if network != "" { 47 postArgs = append(postArgs, []string{"--net", network}...) 48 } 49 postArgs = append(postArgs, []string{"busybox", "top"}...) 50 runArgs1 := append([]string{"run", "-d", "--name", "container1", "--hostname", "fred"}, postArgs...) 51 runArgs2 := append([]string{"run", "-d", "--name", "container2", "--hostname", "wilma"}, postArgs...) 52 53 // Run the two named containers 54 dockerCmd(c, runArgs1...) 55 dockerCmd(c, runArgs2...) 56 57 postArgs = []string{} 58 if network != "" { 59 postArgs = append(postArgs, []string{"--net", network}...) 60 } 61 postArgs = append(postArgs, []string{"busybox", "sh", "-c"}...) 62 63 // Format a run for a container which links to the other two 64 runArgs := append([]string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2"}, postArgs...) 65 pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1" 66 67 // test ping by alias, ping by name, and ping by hostname 68 // 1. Ping by alias 69 dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...) 70 // 2. Ping by container name 71 dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...) 72 // 3. Ping by hostname 73 dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...) 74 75 // Clean for next round 76 dockerCmd(c, "rm", "-f", "container1") 77 dockerCmd(c, "rm", "-f", "container2") 78 } 79 80 func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *check.C) { 81 testRequires(c, DaemonIsLinux) 82 out, _ := dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top") 83 idA := strings.TrimSpace(out) 84 out, _ = dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top") 85 idB := strings.TrimSpace(out) 86 dockerCmd(c, "rename", "container1", "container_new") 87 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") 88 dockerCmd(c, "kill", idA) 89 dockerCmd(c, "kill", idB) 90 91 } 92 93 func (s *DockerSuite) TestLinksInspectLinksStarted(c *check.C) { 94 testRequires(c, DaemonIsLinux) 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 var result []string 101 err := json.Unmarshal([]byte(links), &result) 102 c.Assert(err, checker.IsNil) 103 104 var expected = []string{ 105 "/container1:/testinspectlink/alias1", 106 "/container2:/testinspectlink/alias2", 107 } 108 sort.Strings(result) 109 c.Assert(result, checker.DeepEquals, expected) 110 } 111 112 func (s *DockerSuite) TestLinksInspectLinksStopped(c *check.C) { 113 testRequires(c, DaemonIsLinux) 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 var result []string 121 err := json.Unmarshal([]byte(links), &result) 122 c.Assert(err, checker.IsNil) 123 124 var expected = []string{ 125 "/container1:/testinspectlink/alias1", 126 "/container2:/testinspectlink/alias2", 127 } 128 sort.Strings(result) 129 c.Assert(result, checker.DeepEquals, expected) 130 } 131 132 func (s *DockerSuite) TestLinksNotStartedParentNotFail(c *check.C) { 133 testRequires(c, DaemonIsLinux) 134 dockerCmd(c, "create", "--name=first", "busybox", "top") 135 dockerCmd(c, "create", "--name=second", "--link=first:first", "busybox", "top") 136 dockerCmd(c, "start", "first") 137 138 } 139 140 func (s *DockerSuite) TestLinksHostsFilesInject(c *check.C) { 141 testRequires(c, DaemonIsLinux) 142 testRequires(c, testEnv.IsLocalDaemon, ExecSupport) 143 144 out, _ := dockerCmd(c, "run", "-itd", "--name", "one", "busybox", "top") 145 idOne := strings.TrimSpace(out) 146 147 out, _ = dockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top") 148 idTwo := strings.TrimSpace(out) 149 150 c.Assert(waitRun(idTwo), checker.IsNil) 151 152 readContainerFileWithExec(c, idOne, "/etc/hosts") 153 contentTwo := readContainerFileWithExec(c, idTwo, "/etc/hosts") 154 // Host is not present in updated hosts file 155 c.Assert(string(contentTwo), checker.Contains, "onetwo") 156 } 157 158 func (s *DockerSuite) TestLinksUpdateOnRestart(c *check.C) { 159 testRequires(c, DaemonIsLinux) 160 testRequires(c, testEnv.IsLocalDaemon, ExecSupport) 161 dockerCmd(c, "run", "-d", "--name", "one", "busybox", "top") 162 out, _ := dockerCmd(c, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top") 163 id := strings.TrimSpace(string(out)) 164 165 realIP := inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress") 166 content := readContainerFileWithExec(c, id, "/etc/hosts") 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 = readContainerFileWithExec(c, id, "/etc/hosts") 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 }