docker_cli_links_test.go 7.4 KB

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