docker_cli_links_test.go 9.4 KB

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