github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/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 "gotest.tools/assert" 14 ) 15 16 func (s *DockerSuite) TestLinksPingUnlinkedContainers(c *check.C) { 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 c.Assert(exitCode, checker.Equals, 1, check.Commentf("error: %v", err)) 22 } 23 24 // Test for appropriate error when calling --link with an invalid target container 25 func (s *DockerSuite) TestLinksInvalidContainerTarget(c *check.C) { 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 c.Assert(err, checker.NotNil, check.Commentf("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 c.Assert(strings.ToLower(out), checker.Contains, "could not get container") 35 } 36 37 func (s *DockerSuite) TestLinksPingLinkedContainers(c *check.C) { 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 *check.C, 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 *check.C) { 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 *check.C) { 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 *check.C) { 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 *check.C) { 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 *check.C) { 142 testRequires(c, DaemonIsLinux) 143 testRequires(c, testEnv.IsLocalDaemon, ExecSupport) 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 c.Assert(waitRun(idTwo), checker.IsNil) 152 153 readContainerFileWithExec(c, idOne, "/etc/hosts") 154 contentTwo := readContainerFileWithExec(c, idTwo, "/etc/hosts") 155 // Host is not present in updated hosts file 156 c.Assert(string(contentTwo), checker.Contains, "onetwo") 157 } 158 159 func (s *DockerSuite) TestLinksUpdateOnRestart(c *check.C) { 160 testRequires(c, DaemonIsLinux) 161 testRequires(c, testEnv.IsLocalDaemon, ExecSupport) 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(string(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 c.Assert(matches, checker.NotNil, check.Commentf("Hostname %s have no matches in hosts", hostname)) 173 return string(matches[1]) 174 } 175 ip := getIP(content, "one") 176 c.Assert(ip, checker.Equals, realIP) 177 178 ip = getIP(content, "onetwo") 179 c.Assert(ip, checker.Equals, 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 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 }