docker_cli_links_test.go 9.4 KB

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