docker_cli_links_test.go 8.9 KB

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