docker_cli_links_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "regexp"
  6. "sort"
  7. "strings"
  8. "testing"
  9. "github.com/docker/docker/integration-cli/checker"
  10. "github.com/docker/docker/runconfig"
  11. "github.com/go-check/check"
  12. "gotest.tools/assert"
  13. "gotest.tools/assert/cmp"
  14. )
  15. func (s *DockerSuite) TestLinksPingUnlinkedContainers(c *testing.T) {
  16. testRequires(c, DaemonIsLinux)
  17. _, exitCode, err := dockerCmdWithError("run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
  18. // run ping failed with error
  19. assert.Equal(c, exitCode, 1, check.Commentf("error: %v", err))
  20. }
  21. // Test for appropriate error when calling --link with an invalid target container
  22. func (s *DockerSuite) TestLinksInvalidContainerTarget(c *testing.T) {
  23. testRequires(c, DaemonIsLinux)
  24. out, _, err := dockerCmdWithError("run", "--link", "bogus:alias", "busybox", "true")
  25. // an invalid container target should produce an error
  26. assert.Assert(c, err != nil, check.Commentf("out: %s", out))
  27. // an invalid container target should produce an error
  28. // note: convert the output to lowercase first as the error string
  29. // capitalization was changed after API version 1.32
  30. assert.Assert(c, strings.ToLower(out), checker.Contains, "could not get container")
  31. }
  32. func (s *DockerSuite) TestLinksPingLinkedContainers(c *testing.T) {
  33. testRequires(c, DaemonIsLinux)
  34. // Test with the three different ways of specifying the default network on Linux
  35. testLinkPingOnNetwork(c, "")
  36. testLinkPingOnNetwork(c, "default")
  37. testLinkPingOnNetwork(c, "bridge")
  38. }
  39. func testLinkPingOnNetwork(c *testing.T, network string) {
  40. var postArgs []string
  41. if network != "" {
  42. postArgs = append(postArgs, []string{"--net", network}...)
  43. }
  44. postArgs = append(postArgs, []string{"busybox", "top"}...)
  45. runArgs1 := append([]string{"run", "-d", "--name", "container1", "--hostname", "fred"}, postArgs...)
  46. runArgs2 := append([]string{"run", "-d", "--name", "container2", "--hostname", "wilma"}, postArgs...)
  47. // Run the two named containers
  48. dockerCmd(c, runArgs1...)
  49. dockerCmd(c, runArgs2...)
  50. postArgs = []string{}
  51. if network != "" {
  52. postArgs = append(postArgs, []string{"--net", network}...)
  53. }
  54. postArgs = append(postArgs, []string{"busybox", "sh", "-c"}...)
  55. // Format a run for a container which links to the other two
  56. runArgs := append([]string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2"}, postArgs...)
  57. pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
  58. // test ping by alias, ping by name, and ping by hostname
  59. // 1. Ping by alias
  60. dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
  61. // 2. Ping by container name
  62. dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
  63. // 3. Ping by hostname
  64. dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
  65. // Clean for next round
  66. dockerCmd(c, "rm", "-f", "container1")
  67. dockerCmd(c, "rm", "-f", "container2")
  68. }
  69. func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *testing.T) {
  70. testRequires(c, DaemonIsLinux)
  71. out, _ := dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  72. idA := strings.TrimSpace(out)
  73. out, _ = dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
  74. idB := strings.TrimSpace(out)
  75. dockerCmd(c, "rename", "container1", "container_new")
  76. 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")
  77. dockerCmd(c, "kill", idA)
  78. dockerCmd(c, "kill", idB)
  79. }
  80. func (s *DockerSuite) TestLinksInspectLinksStarted(c *testing.T) {
  81. testRequires(c, DaemonIsLinux)
  82. dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  83. dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
  84. dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "top")
  85. links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links")
  86. var result []string
  87. err := json.Unmarshal([]byte(links), &result)
  88. assert.NilError(c, err)
  89. var expected = []string{
  90. "/container1:/testinspectlink/alias1",
  91. "/container2:/testinspectlink/alias2",
  92. }
  93. sort.Strings(result)
  94. assert.DeepEqual(c, result, expected)
  95. }
  96. func (s *DockerSuite) TestLinksInspectLinksStopped(c *testing.T) {
  97. testRequires(c, DaemonIsLinux)
  98. dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  99. dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
  100. dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
  101. links := inspectFieldJSON(c, "testinspectlink", "HostConfig.Links")
  102. var result []string
  103. err := json.Unmarshal([]byte(links), &result)
  104. assert.NilError(c, err)
  105. var expected = []string{
  106. "/container1:/testinspectlink/alias1",
  107. "/container2:/testinspectlink/alias2",
  108. }
  109. sort.Strings(result)
  110. assert.DeepEqual(c, result, expected)
  111. }
  112. func (s *DockerSuite) TestLinksNotStartedParentNotFail(c *testing.T) {
  113. testRequires(c, DaemonIsLinux)
  114. dockerCmd(c, "create", "--name=first", "busybox", "top")
  115. dockerCmd(c, "create", "--name=second", "--link=first:first", "busybox", "top")
  116. dockerCmd(c, "start", "first")
  117. }
  118. func (s *DockerSuite) TestLinksHostsFilesInject(c *testing.T) {
  119. testRequires(c, DaemonIsLinux)
  120. testRequires(c, testEnv.IsLocalDaemon)
  121. out, _ := dockerCmd(c, "run", "-itd", "--name", "one", "busybox", "top")
  122. idOne := strings.TrimSpace(out)
  123. out, _ = dockerCmd(c, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top")
  124. idTwo := strings.TrimSpace(out)
  125. assert.Assert(c, waitRun(idTwo) == nil)
  126. readContainerFileWithExec(c, idOne, "/etc/hosts")
  127. contentTwo := readContainerFileWithExec(c, idTwo, "/etc/hosts")
  128. // Host is not present in updated hosts file
  129. assert.Assert(c, string(contentTwo), checker.Contains, "onetwo")
  130. }
  131. func (s *DockerSuite) TestLinksUpdateOnRestart(c *testing.T) {
  132. testRequires(c, DaemonIsLinux)
  133. testRequires(c, testEnv.IsLocalDaemon)
  134. dockerCmd(c, "run", "-d", "--name", "one", "busybox", "top")
  135. out, _ := dockerCmd(c, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top")
  136. id := strings.TrimSpace(string(out))
  137. realIP := inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress")
  138. content := readContainerFileWithExec(c, id, "/etc/hosts")
  139. getIP := func(hosts []byte, hostname string) string {
  140. re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
  141. matches := re.FindSubmatch(hosts)
  142. assert.Assert(c, matches != nil, check.Commentf("Hostname %s have no matches in hosts", hostname))
  143. return string(matches[1])
  144. }
  145. ip := getIP(content, "one")
  146. assert.Equal(c, ip, realIP)
  147. ip = getIP(content, "onetwo")
  148. assert.Equal(c, ip, realIP)
  149. dockerCmd(c, "restart", "one")
  150. realIP = inspectField(c, "one", "NetworkSettings.Networks.bridge.IPAddress")
  151. content = readContainerFileWithExec(c, id, "/etc/hosts")
  152. ip = getIP(content, "one")
  153. assert.Equal(c, ip, realIP)
  154. ip = getIP(content, "onetwo")
  155. assert.Equal(c, ip, realIP)
  156. }
  157. func (s *DockerSuite) TestLinksEnvs(c *testing.T) {
  158. testRequires(c, DaemonIsLinux)
  159. dockerCmd(c, "run", "-d", "-e", "e1=", "-e", "e2=v2", "-e", "e3=v3=v3", "--name=first", "busybox", "top")
  160. out, _ := dockerCmd(c, "run", "--name=second", "--link=first:first", "busybox", "env")
  161. assert.Assert(c, out, checker.Contains, "FIRST_ENV_e1=\n")
  162. assert.Assert(c, out, checker.Contains, "FIRST_ENV_e2=v2")
  163. assert.Assert(c, out, checker.Contains, "FIRST_ENV_e3=v3=v3")
  164. }
  165. func (s *DockerSuite) TestLinkShortDefinition(c *testing.T) {
  166. testRequires(c, DaemonIsLinux)
  167. out, _ := dockerCmd(c, "run", "-d", "--name", "shortlinkdef", "busybox", "top")
  168. cid := strings.TrimSpace(out)
  169. assert.Assert(c, waitRun(cid) == nil)
  170. out, _ = dockerCmd(c, "run", "-d", "--name", "link2", "--link", "shortlinkdef", "busybox", "top")
  171. cid2 := strings.TrimSpace(out)
  172. assert.Assert(c, waitRun(cid2) == nil)
  173. links := inspectFieldJSON(c, cid2, "HostConfig.Links")
  174. assert.Equal(c, links, "[\"/shortlinkdef:/link2/shortlinkdef\"]")
  175. }
  176. func (s *DockerSuite) TestLinksNetworkHostContainer(c *testing.T) {
  177. testRequires(c, DaemonIsLinux, NotUserNamespace)
  178. dockerCmd(c, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top")
  179. out, _, err := dockerCmdWithError("run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true")
  180. // Running container linking to a container with --net host should have failed
  181. assert.Assert(c, err != nil, check.Commentf("out: %s", out))
  182. // Running container linking to a container with --net host should have failed
  183. assert.Assert(c, out, checker.Contains, runconfig.ErrConflictHostNetworkAndLinks.Error())
  184. }
  185. func (s *DockerSuite) TestLinksEtcHostsRegularFile(c *testing.T) {
  186. testRequires(c, DaemonIsLinux, NotUserNamespace)
  187. out, _ := dockerCmd(c, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
  188. // /etc/hosts should be a regular file
  189. assert.Assert(c, cmp.Regexp("^"+
  190. "^-.+\n"+
  191. "$",
  192. out))
  193. }
  194. func (s *DockerSuite) TestLinksMultipleWithSameName(c *testing.T) {
  195. testRequires(c, DaemonIsLinux)
  196. dockerCmd(c, "run", "-d", "--name=upstream-a", "busybox", "top")
  197. dockerCmd(c, "run", "-d", "--name=upstream-b", "busybox", "top")
  198. dockerCmd(c, "run", "--link", "upstream-a:upstream", "--link", "upstream-b:upstream", "busybox", "sh", "-c", "ping -c 1 upstream")
  199. }