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