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