docker_cli_links_test.go 9.4 KB

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