docker_cli_links_test.go 9.1 KB

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