docker_cli_links_test.go 8.3 KB

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