docker_cli_links_test.go 8.5 KB

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