docker_cli_links_test.go 9.2 KB

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