docker_cli_links_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. dockerCmd(c, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
  28. dockerCmd(c, "run", "-d", "--name", "container2", "--hostname", "wilma", "busybox", "top")
  29. runArgs := []string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c"}
  30. pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
  31. // test ping by alias, ping by name, and ping by hostname
  32. // 1. Ping by alias
  33. dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
  34. // 2. Ping by container name
  35. dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
  36. // 3. Ping by hostname
  37. dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
  38. }
  39. func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *check.C) {
  40. testRequires(c, DaemonIsLinux)
  41. out, _ := dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  42. idA := strings.TrimSpace(out)
  43. out, _ = dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
  44. idB := strings.TrimSpace(out)
  45. dockerCmd(c, "rename", "container1", "container_new")
  46. 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")
  47. dockerCmd(c, "kill", idA)
  48. dockerCmd(c, "kill", idB)
  49. }
  50. func (s *DockerSuite) TestLinksInspectLinksStarted(c *check.C) {
  51. testRequires(c, DaemonIsLinux)
  52. var (
  53. expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
  54. result []string
  55. )
  56. dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  57. dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
  58. dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "top")
  59. links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
  60. c.Assert(err, checker.IsNil)
  61. err = unmarshalJSON([]byte(links), &result)
  62. c.Assert(err, checker.IsNil)
  63. output := convertSliceOfStringsToMap(result)
  64. c.Assert(output, checker.DeepEquals, expected)
  65. }
  66. func (s *DockerSuite) TestLinksInspectLinksStopped(c *check.C) {
  67. testRequires(c, DaemonIsLinux)
  68. var (
  69. expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
  70. result []string
  71. )
  72. dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  73. dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
  74. dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
  75. links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
  76. c.Assert(err, checker.IsNil)
  77. err = unmarshalJSON([]byte(links), &result)
  78. c.Assert(err, checker.IsNil)
  79. output := convertSliceOfStringsToMap(result)
  80. c.Assert(output, checker.DeepEquals, expected)
  81. }
  82. func (s *DockerSuite) TestLinksNotStartedParentNotFail(c *check.C) {
  83. testRequires(c, DaemonIsLinux)
  84. dockerCmd(c, "create", "--name=first", "busybox", "top")
  85. dockerCmd(c, "create", "--name=second", "--link=first:first", "busybox", "top")
  86. dockerCmd(c, "start", "first")
  87. }
  88. func (s *DockerSuite) TestLinksHostsFilesInject(c *check.C) {
  89. testRequires(c, DaemonIsLinux)
  90. testRequires(c, SameHostDaemon, ExecSupport)
  91. out, _ := dockerCmd(c, "run", "-itd", "--name", "one", "busybox", "top")
  92. idOne := strings.TrimSpace(out)
  93. out, _ = dockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top")
  94. idTwo := strings.TrimSpace(out)
  95. c.Assert(waitRun(idTwo), checker.IsNil)
  96. contentOne, err := readContainerFileWithExec(idOne, "/etc/hosts")
  97. c.Assert(err, checker.IsNil, check.Commentf("contentOne: %s", string(contentOne)))
  98. contentTwo, err := readContainerFileWithExec(idTwo, "/etc/hosts")
  99. c.Assert(err, checker.IsNil, check.Commentf("contentTwo: %s", string(contentTwo)))
  100. // Host is not present in updated hosts file
  101. c.Assert(string(contentTwo), checker.Contains, "onetwo")
  102. }
  103. func (s *DockerSuite) TestLinksUpdateOnRestart(c *check.C) {
  104. testRequires(c, DaemonIsLinux)
  105. testRequires(c, SameHostDaemon, ExecSupport)
  106. dockerCmd(c, "run", "-d", "--name", "one", "busybox", "top")
  107. out, _ := dockerCmd(c, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top")
  108. id := strings.TrimSpace(string(out))
  109. realIP, err := inspectField("one", "NetworkSettings.Networks.bridge.IPAddress")
  110. if err != nil {
  111. c.Fatal(err)
  112. }
  113. c.Assert(err, checker.IsNil)
  114. content, err := readContainerFileWithExec(id, "/etc/hosts")
  115. c.Assert(err, checker.IsNil)
  116. getIP := func(hosts []byte, hostname string) string {
  117. re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
  118. matches := re.FindSubmatch(hosts)
  119. c.Assert(matches, checker.NotNil, check.Commentf("Hostname %s have no matches in hosts", hostname))
  120. return string(matches[1])
  121. }
  122. ip := getIP(content, "one")
  123. c.Assert(ip, checker.Equals, realIP)
  124. ip = getIP(content, "onetwo")
  125. c.Assert(ip, checker.Equals, realIP)
  126. dockerCmd(c, "restart", "one")
  127. realIP, err = inspectField("one", "NetworkSettings.Networks.bridge.IPAddress")
  128. c.Assert(err, checker.IsNil)
  129. content, err = readContainerFileWithExec(id, "/etc/hosts")
  130. c.Assert(err, checker.IsNil, check.Commentf("content: %s", string(content)))
  131. ip = getIP(content, "one")
  132. c.Assert(ip, checker.Equals, realIP)
  133. ip = getIP(content, "onetwo")
  134. c.Assert(ip, checker.Equals, realIP)
  135. }
  136. func (s *DockerSuite) TestLinksEnvs(c *check.C) {
  137. testRequires(c, DaemonIsLinux)
  138. dockerCmd(c, "run", "-d", "-e", "e1=", "-e", "e2=v2", "-e", "e3=v3=v3", "--name=first", "busybox", "top")
  139. out, _ := dockerCmd(c, "run", "--name=second", "--link=first:first", "busybox", "env")
  140. c.Assert(out, checker.Contains, "FIRST_ENV_e1=\n")
  141. c.Assert(out, checker.Contains, "FIRST_ENV_e2=v2")
  142. c.Assert(out, checker.Contains, "FIRST_ENV_e3=v3=v3")
  143. }
  144. func (s *DockerSuite) TestLinkShortDefinition(c *check.C) {
  145. testRequires(c, DaemonIsLinux)
  146. out, _ := dockerCmd(c, "run", "-d", "--name", "shortlinkdef", "busybox", "top")
  147. cid := strings.TrimSpace(out)
  148. c.Assert(waitRun(cid), checker.IsNil)
  149. out, _ = dockerCmd(c, "run", "-d", "--name", "link2", "--link", "shortlinkdef", "busybox", "top")
  150. cid2 := strings.TrimSpace(out)
  151. c.Assert(waitRun(cid2), checker.IsNil)
  152. links, err := inspectFieldJSON(cid2, "HostConfig.Links")
  153. c.Assert(err, checker.IsNil)
  154. c.Assert(links, checker.Equals, "[\"/shortlinkdef:/link2/shortlinkdef\"]")
  155. }
  156. func (s *DockerSuite) TestLinksNetworkHostContainer(c *check.C) {
  157. testRequires(c, DaemonIsLinux, NotUserNamespace)
  158. dockerCmd(c, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top")
  159. out, _, err := dockerCmdWithError("run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true")
  160. // Running container linking to a container with --net host should have failed
  161. c.Assert(err, checker.NotNil, check.Commentf("out: %s", out))
  162. // Running container linking to a container with --net host should have failed
  163. c.Assert(out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error())
  164. }
  165. func (s *DockerSuite) TestLinksEtcHostsRegularFile(c *check.C) {
  166. testRequires(c, DaemonIsLinux, NotUserNamespace)
  167. out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
  168. // /etc/hosts should be a regular file
  169. c.Assert(out, checker.Matches, "^-.+\n")
  170. }
  171. func (s *DockerSuite) TestLinksMultipleWithSameName(c *check.C) {
  172. dockerCmd(c, "run", "-d", "--name=upstream-a", "busybox", "top")
  173. dockerCmd(c, "run", "-d", "--name=upstream-b", "busybox", "top")
  174. dockerCmd(c, "run", "--link", "upstream-a:upstream", "--link", "upstream-b:upstream", "busybox", "sh", "-c", "ping -c 1 upstream")
  175. }