github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/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 contentOne, err := readContainerFileWithExec(idOne, "/etc/hosts") 150 c.Assert(err, checker.IsNil, check.Commentf("contentOne: %s", string(contentOne))) 151 152 contentTwo, err := readContainerFileWithExec(idTwo, "/etc/hosts") 153 c.Assert(err, checker.IsNil, check.Commentf("contentTwo: %s", string(contentTwo))) 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, SameHostDaemon, 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, err := readContainerFileWithExec(id, "/etc/hosts") 167 c.Assert(err, checker.IsNil) 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, err = readContainerFileWithExec(id, "/etc/hosts") 185 c.Assert(err, checker.IsNil, check.Commentf("content: %s", string(content))) 186 ip = getIP(content, "one") 187 c.Assert(ip, checker.Equals, realIP) 188 189 ip = getIP(content, "onetwo") 190 c.Assert(ip, checker.Equals, realIP) 191 } 192 193 func (s *DockerSuite) TestLinksEnvs(c *check.C) { 194 testRequires(c, DaemonIsLinux) 195 dockerCmd(c, "run", "-d", "-e", "e1=", "-e", "e2=v2", "-e", "e3=v3=v3", "--name=first", "busybox", "top") 196 out, _ := dockerCmd(c, "run", "--name=second", "--link=first:first", "busybox", "env") 197 c.Assert(out, checker.Contains, "FIRST_ENV_e1=\n") 198 c.Assert(out, checker.Contains, "FIRST_ENV_e2=v2") 199 c.Assert(out, checker.Contains, "FIRST_ENV_e3=v3=v3") 200 } 201 202 func (s *DockerSuite) TestLinkShortDefinition(c *check.C) { 203 testRequires(c, DaemonIsLinux) 204 out, _ := dockerCmd(c, "run", "-d", "--name", "shortlinkdef", "busybox", "top") 205 206 cid := strings.TrimSpace(out) 207 c.Assert(waitRun(cid), checker.IsNil) 208 209 out, _ = dockerCmd(c, "run", "-d", "--name", "link2", "--link", "shortlinkdef", "busybox", "top") 210 211 cid2 := strings.TrimSpace(out) 212 c.Assert(waitRun(cid2), checker.IsNil) 213 214 links := inspectFieldJSON(c, cid2, "HostConfig.Links") 215 c.Assert(links, checker.Equals, "[\"/shortlinkdef:/link2/shortlinkdef\"]") 216 } 217 218 func (s *DockerSuite) TestLinksNetworkHostContainer(c *check.C) { 219 testRequires(c, DaemonIsLinux, NotUserNamespace) 220 dockerCmd(c, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top") 221 out, _, err := dockerCmdWithError("run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true") 222 223 // Running container linking to a container with --net host should have failed 224 c.Assert(err, checker.NotNil, check.Commentf("out: %s", out)) 225 // Running container linking to a container with --net host should have failed 226 c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error()) 227 } 228 229 func (s *DockerSuite) TestLinksEtcHostsRegularFile(c *check.C) { 230 testRequires(c, DaemonIsLinux, NotUserNamespace) 231 out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts") 232 // /etc/hosts should be a regular file 233 c.Assert(out, checker.Matches, "^-.+\n") 234 } 235 236 func (s *DockerSuite) TestLinksMultipleWithSameName(c *check.C) { 237 testRequires(c, DaemonIsLinux) 238 dockerCmd(c, "run", "-d", "--name=upstream-a", "busybox", "top") 239 dockerCmd(c, "run", "-d", "--name=upstream-b", "busybox", "top") 240 dockerCmd(c, "run", "--link", "upstream-a:upstream", "--link", "upstream-b:upstream", "busybox", "sh", "-c", "ping -c 1 upstream") 241 }