docker_cli_links_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/go-check/check"
  5. "reflect"
  6. "regexp"
  7. "strings"
  8. )
  9. func (s *DockerSuite) TestLinksPingUnlinkedContainers(c *check.C) {
  10. _, exitCode, err := dockerCmdWithError("run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
  11. if exitCode == 0 {
  12. c.Fatal("run ping did not fail")
  13. } else if exitCode != 1 {
  14. c.Fatalf("run ping failed with errors: %v", err)
  15. }
  16. }
  17. // Test for appropriate error when calling --link with an invalid target container
  18. func (s *DockerSuite) TestLinksInvalidContainerTarget(c *check.C) {
  19. out, _, err := dockerCmdWithError("run", "--link", "bogus:alias", "busybox", "true")
  20. if err == nil {
  21. c.Fatal("an invalid container target should produce an error")
  22. }
  23. if !strings.Contains(out, "Could not get container") {
  24. c.Fatalf("error output expected 'Could not get container', but got %q instead; err: %v", out, err)
  25. }
  26. }
  27. func (s *DockerSuite) TestLinksPingLinkedContainers(c *check.C) {
  28. dockerCmd(c, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
  29. dockerCmd(c, "run", "-d", "--name", "container2", "--hostname", "wilma", "busybox", "top")
  30. runArgs := []string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c"}
  31. pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
  32. // test ping by alias, ping by name, and ping by hostname
  33. // 1. Ping by alias
  34. dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
  35. // 2. Ping by container name
  36. dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
  37. // 3. Ping by hostname
  38. dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
  39. }
  40. func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *check.C) {
  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. 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. if err != nil {
  60. c.Fatal(err)
  61. }
  62. err = unmarshalJSON([]byte(links), &result)
  63. if err != nil {
  64. c.Fatal(err)
  65. }
  66. output := convertSliceOfStringsToMap(result)
  67. equal := reflect.DeepEqual(output, expected)
  68. if !equal {
  69. c.Fatalf("Links %s, expected %s", result, expected)
  70. }
  71. }
  72. func (s *DockerSuite) TestLinksInspectLinksStopped(c *check.C) {
  73. var (
  74. expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
  75. result []string
  76. )
  77. dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  78. dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
  79. dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
  80. links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
  81. if err != nil {
  82. c.Fatal(err)
  83. }
  84. err = unmarshalJSON([]byte(links), &result)
  85. if err != nil {
  86. c.Fatal(err)
  87. }
  88. output := convertSliceOfStringsToMap(result)
  89. equal := reflect.DeepEqual(output, expected)
  90. if !equal {
  91. c.Fatalf("Links %s, but expected %s", result, expected)
  92. }
  93. }
  94. func (s *DockerSuite) TestLinksNotStartedParentNotFail(c *check.C) {
  95. dockerCmd(c, "create", "--name=first", "busybox", "top")
  96. dockerCmd(c, "create", "--name=second", "--link=first:first", "busybox", "top")
  97. dockerCmd(c, "start", "first")
  98. }
  99. func (s *DockerSuite) TestLinksHostsFilesInject(c *check.C) {
  100. testRequires(c, SameHostDaemon, ExecSupport)
  101. out, _ := dockerCmd(c, "run", "-itd", "--name", "one", "busybox", "top")
  102. idOne := strings.TrimSpace(out)
  103. out, _ = dockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top")
  104. idTwo := strings.TrimSpace(out)
  105. c.Assert(waitRun(idTwo), check.IsNil)
  106. contentOne, err := readContainerFileWithExec(idOne, "/etc/hosts")
  107. if err != nil {
  108. c.Fatal(err, string(contentOne))
  109. }
  110. contentTwo, err := readContainerFileWithExec(idTwo, "/etc/hosts")
  111. if err != nil {
  112. c.Fatal(err, string(contentTwo))
  113. }
  114. if !strings.Contains(string(contentTwo), "onetwo") {
  115. c.Fatal("Host is not present in updated hosts file", string(contentTwo))
  116. }
  117. }
  118. func (s *DockerSuite) TestLinksUpdateOnRestart(c *check.C) {
  119. testRequires(c, SameHostDaemon, ExecSupport)
  120. dockerCmd(c, "run", "-d", "--name", "one", "busybox", "top")
  121. out, _ := dockerCmd(c, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top")
  122. id := strings.TrimSpace(string(out))
  123. realIP, err := inspectField("one", "NetworkSettings.IPAddress")
  124. if err != nil {
  125. c.Fatal(err)
  126. }
  127. content, err := readContainerFileWithExec(id, "/etc/hosts")
  128. if err != nil {
  129. c.Fatal(err, string(content))
  130. }
  131. getIP := func(hosts []byte, hostname string) string {
  132. re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
  133. matches := re.FindSubmatch(hosts)
  134. if matches == nil {
  135. c.Fatalf("Hostname %s have no matches in hosts", hostname)
  136. }
  137. return string(matches[1])
  138. }
  139. if ip := getIP(content, "one"); ip != realIP {
  140. c.Fatalf("For 'one' alias expected IP: %s, got: %s", realIP, ip)
  141. }
  142. if ip := getIP(content, "onetwo"); ip != realIP {
  143. c.Fatalf("For 'onetwo' alias expected IP: %s, got: %s", realIP, ip)
  144. }
  145. dockerCmd(c, "restart", "one")
  146. realIP, err = inspectField("one", "NetworkSettings.IPAddress")
  147. if err != nil {
  148. c.Fatal(err)
  149. }
  150. content, err = readContainerFileWithExec(id, "/etc/hosts")
  151. if err != nil {
  152. c.Fatal(err, string(content))
  153. }
  154. if ip := getIP(content, "one"); ip != realIP {
  155. c.Fatalf("For 'one' alias expected IP: %s, got: %s", realIP, ip)
  156. }
  157. if ip := getIP(content, "onetwo"); ip != realIP {
  158. c.Fatalf("For 'onetwo' alias expected IP: %s, got: %s", realIP, ip)
  159. }
  160. }
  161. func (s *DockerSuite) TestLinksEnvs(c *check.C) {
  162. dockerCmd(c, "run", "-d", "-e", "e1=", "-e", "e2=v2", "-e", "e3=v3=v3", "--name=first", "busybox", "top")
  163. out, _ := dockerCmd(c, "run", "--name=second", "--link=first:first", "busybox", "env")
  164. if !strings.Contains(out, "FIRST_ENV_e1=\n") ||
  165. !strings.Contains(out, "FIRST_ENV_e2=v2") ||
  166. !strings.Contains(out, "FIRST_ENV_e3=v3=v3") {
  167. c.Fatalf("Incorrect output: %s", out)
  168. }
  169. }
  170. func (s *DockerSuite) TestLinkShortDefinition(c *check.C) {
  171. out, _ := dockerCmd(c, "run", "-d", "--name", "shortlinkdef", "busybox", "top")
  172. cid := strings.TrimSpace(out)
  173. c.Assert(waitRun(cid), check.IsNil)
  174. out, _ = dockerCmd(c, "run", "-d", "--name", "link2", "--link", "shortlinkdef", "busybox", "top")
  175. cid2 := strings.TrimSpace(out)
  176. c.Assert(waitRun(cid2), check.IsNil)
  177. links, err := inspectFieldJSON(cid2, "HostConfig.Links")
  178. c.Assert(err, check.IsNil)
  179. c.Assert(links, check.Equals, "[\"/shortlinkdef:/link2/shortlinkdef\"]")
  180. }
  181. func (s *DockerSuite) TestLinksNetworkHostContainer(c *check.C) {
  182. dockerCmd(c, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top")
  183. out, _, err := dockerCmdWithError("run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true")
  184. if err == nil || !strings.Contains(out, "--net=host can't be used with links. This would result in undefined behavior") {
  185. c.Fatalf("Running container linking to a container with --net host should have failed: %s", out)
  186. }
  187. }
  188. func (s *DockerSuite) TestLinksEtcHostsRegularFile(c *check.C) {
  189. out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
  190. if !strings.HasPrefix(out, "-") {
  191. c.Errorf("/etc/hosts should be a regular file")
  192. }
  193. }