docker_cli_links_test.go 10 KB

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