github.com/jiasir/docker@v1.3.3-0.20170609024000-252e610103e7/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/integration-cli/checker" 10 "github.com/docker/docker/pkg/testutil" 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 var ( 94 expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}} 95 result []string 96 ) 97 dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top") 98 dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top") 99 dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "top") 100 links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links") 101 102 err := json.Unmarshal([]byte(links), &result) 103 c.Assert(err, checker.IsNil) 104 105 output := testutil.ConvertSliceOfStringsToMap(result) 106 107 c.Assert(output, checker.DeepEquals, expected) 108 } 109 110 func (s *DockerSuite) TestLinksInspectLinksStopped(c *check.C) { 111 testRequires(c, DaemonIsLinux) 112 var ( 113 expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}} 114 result []string 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 err := json.Unmarshal([]byte(links), &result) 122 c.Assert(err, checker.IsNil) 123 124 output := testutil.ConvertSliceOfStringsToMap(result) 125 126 c.Assert(output, checker.DeepEquals, expected) 127 } 128 129 func (s *DockerSuite) TestLinksNotStartedParentNotFail(c *check.C) { 130 testRequires(c, DaemonIsLinux) 131 dockerCmd(c, "create", "--name=first", "busybox", "top") 132 dockerCmd(c, "create", "--name=second", "--link=first:first", "busybox", "top") 133 dockerCmd(c, "start", "first") 134 135 } 136 137 func (s *DockerSuite) TestLinksHostsFilesInject(c *check.C) { 138 testRequires(c, DaemonIsLinux) 139 testRequires(c, SameHostDaemon, ExecSupport) 140 141 out, _ := dockerCmd(c, "run", "-itd", "--name", "one", "busybox", "top") 142 idOne := strings.TrimSpace(out) 143 144 out, _ = dockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top") 145 idTwo := strings.TrimSpace(out) 146 147 c.Assert(waitRun(idTwo), checker.IsNil) 148 149 readContainerFileWithExec(c, idOne, "/etc/hosts") 150 contentTwo := readContainerFileWithExec(c, idTwo, "/etc/hosts") 151 // Host is not present in updated hosts file 152 c.Assert(string(contentTwo), checker.Contains, "onetwo") 153 } 154 155 func (s *DockerSuite) TestLinksUpdateOnRestart(c *check.C) { 156 testRequires(c, DaemonIsLinux) 157 testRequires(c, SameHostDaemon, ExecSupport) 158 dockerCmd(c, "run", "-d", "--name", "one", "busybox", "top") 159 out, _ := dockerCmd(c, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top") 160 id := strings.TrimSpace(string(out)) 161 162 realIP := inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress") 163 content := readContainerFileWithExec(c, id, "/etc/hosts") 164 165 getIP := func(hosts []byte, hostname string) string { 166 re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname))) 167 matches := re.FindSubmatch(hosts) 168 c.Assert(matches, checker.NotNil, check.Commentf("Hostname %s have no matches in hosts", hostname)) 169 return string(matches[1]) 170 } 171 ip := getIP(content, "one") 172 c.Assert(ip, checker.Equals, realIP) 173 174 ip = getIP(content, "onetwo") 175 c.Assert(ip, checker.Equals, realIP) 176 177 dockerCmd(c, "restart", "one") 178 realIP = inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress") 179 180 content = readContainerFileWithExec(c, id, "/etc/hosts") 181 ip = getIP(content, "one") 182 c.Assert(ip, checker.Equals, realIP) 183 184 ip = getIP(content, "onetwo") 185 c.Assert(ip, checker.Equals, realIP) 186 } 187 188 func (s *DockerSuite) TestLinksEnvs(c *check.C) { 189 testRequires(c, DaemonIsLinux) 190 dockerCmd(c, "run", "-d", "-e", "e1=", "-e", "e2=v2", "-e", "e3=v3=v3", "--name=first", "busybox", "top") 191 out, _ := dockerCmd(c, "run", "--name=second", "--link=first:first", "busybox", "env") 192 c.Assert(out, checker.Contains, "FIRST_ENV_e1=\n") 193 c.Assert(out, checker.Contains, "FIRST_ENV_e2=v2") 194 c.Assert(out, checker.Contains, "FIRST_ENV_e3=v3=v3") 195 } 196 197 func (s *DockerSuite) TestLinkShortDefinition(c *check.C) { 198 testRequires(c, DaemonIsLinux) 199 out, _ := dockerCmd(c, "run", "-d", "--name", "shortlinkdef", "busybox", "top") 200 201 cid := strings.TrimSpace(out) 202 c.Assert(waitRun(cid), checker.IsNil) 203 204 out, _ = dockerCmd(c, "run", "-d", "--name", "link2", "--link", "shortlinkdef", "busybox", "top") 205 206 cid2 := strings.TrimSpace(out) 207 c.Assert(waitRun(cid2), checker.IsNil) 208 209 links := inspectFieldJSON(c, cid2, "HostConfig.Links") 210 c.Assert(links, checker.Equals, "[\"/shortlinkdef:/link2/shortlinkdef\"]") 211 } 212 213 func (s *DockerSuite) TestLinksNetworkHostContainer(c *check.C) { 214 testRequires(c, DaemonIsLinux, NotUserNamespace) 215 dockerCmd(c, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top") 216 out, _, err := dockerCmdWithError("run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true") 217 218 // Running container linking to a container with --net host should have failed 219 c.Assert(err, checker.NotNil, check.Commentf("out: %s", out)) 220 // Running container linking to a container with --net host should have failed 221 c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error()) 222 } 223 224 func (s *DockerSuite) TestLinksEtcHostsRegularFile(c *check.C) { 225 testRequires(c, DaemonIsLinux, NotUserNamespace) 226 out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts") 227 // /etc/hosts should be a regular file 228 c.Assert(out, checker.Matches, "^-.+\n") 229 } 230 231 func (s *DockerSuite) TestLinksMultipleWithSameName(c *check.C) { 232 testRequires(c, DaemonIsLinux) 233 dockerCmd(c, "run", "-d", "--name=upstream-a", "busybox", "top") 234 dockerCmd(c, "run", "-d", "--name=upstream-b", "busybox", "top") 235 dockerCmd(c, "run", "--link", "upstream-a:upstream", "--link", "upstream-b:upstream", "busybox", "sh", "-c", "ping -c 1 upstream") 236 }