docker_cli_links_test.go 9.1 KB

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