docker_cli_links_test.go 9.2 KB

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