docker_cli_links_test.go 8.5 KB

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