docker_cli_links_test.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "reflect"
  8. "regexp"
  9. "strings"
  10. "time"
  11. "github.com/docker/docker/pkg/iptables"
  12. "github.com/go-check/check"
  13. )
  14. func (s *DockerSuite) TestLinksEtcHostsRegularFile(c *check.C) {
  15. runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
  16. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  17. if err != nil {
  18. c.Fatal(out, err)
  19. }
  20. if !strings.HasPrefix(out, "-") {
  21. c.Errorf("/etc/hosts should be a regular file")
  22. }
  23. }
  24. func (s *DockerSuite) TestLinksEtcHostsContentMatch(c *check.C) {
  25. testRequires(c, SameHostDaemon)
  26. runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hosts")
  27. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  28. if err != nil {
  29. c.Fatal(out, err)
  30. }
  31. hosts, err := ioutil.ReadFile("/etc/hosts")
  32. if os.IsNotExist(err) {
  33. c.Skip("/etc/hosts does not exist, skip this test")
  34. }
  35. if out != string(hosts) {
  36. c.Errorf("container")
  37. }
  38. }
  39. func (s *DockerSuite) TestLinksPingUnlinkedContainers(c *check.C) {
  40. runCmd := exec.Command(dockerBinary, "run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
  41. exitCode, err := runCommand(runCmd)
  42. if exitCode == 0 {
  43. c.Fatal("run ping did not fail")
  44. } else if exitCode != 1 {
  45. c.Fatalf("run ping failed with errors: %v", err)
  46. }
  47. }
  48. // Test for appropriate error when calling --link with an invalid target container
  49. func (s *DockerSuite) TestLinksInvalidContainerTarget(c *check.C) {
  50. runCmd := exec.Command(dockerBinary, "run", "--link", "bogus:alias", "busybox", "true")
  51. out, _, err := runCommandWithOutput(runCmd)
  52. if err == nil {
  53. c.Fatal("an invalid container target should produce an error")
  54. }
  55. if !strings.Contains(out, "Could not get container") {
  56. c.Fatalf("error output expected 'Could not get container', but got %q instead; err: %v", out, err)
  57. }
  58. }
  59. func (s *DockerSuite) TestLinksPingLinkedContainers(c *check.C) {
  60. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
  61. if _, err := runCommand(runCmd); err != nil {
  62. c.Fatal(err)
  63. }
  64. runCmd = exec.Command(dockerBinary, "run", "-d", "--name", "container2", "--hostname", "wilma", "busybox", "top")
  65. if _, err := runCommand(runCmd); err != nil {
  66. c.Fatal(err)
  67. }
  68. runArgs := []string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c"}
  69. pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
  70. // test ping by alias, ping by name, and ping by hostname
  71. // 1. Ping by alias
  72. dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
  73. // 2. Ping by container name
  74. dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
  75. // 3. Ping by hostname
  76. dockerCmd(c, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
  77. }
  78. func (s *DockerSuite) TestLinksPingLinkedContainersAfterRename(c *check.C) {
  79. out, _ := dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  80. idA := strings.TrimSpace(out)
  81. out, _ = dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
  82. idB := strings.TrimSpace(out)
  83. dockerCmd(c, "rename", "container1", "container_new")
  84. 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")
  85. dockerCmd(c, "kill", idA)
  86. dockerCmd(c, "kill", idB)
  87. }
  88. func (s *DockerSuite) TestLinksIpTablesRulesWhenLinkAndUnlink(c *check.C) {
  89. testRequires(c, SameHostDaemon)
  90. dockerCmd(c, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "top")
  91. dockerCmd(c, "run", "-d", "--name", "parent", "--link", "child:http", "busybox", "top")
  92. childIP := findContainerIP(c, "child")
  93. parentIP := findContainerIP(c, "parent")
  94. sourceRule := []string{"-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
  95. destinationRule := []string{"-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
  96. if !iptables.Exists("filter", "DOCKER", sourceRule...) || !iptables.Exists("filter", "DOCKER", destinationRule...) {
  97. c.Fatal("Iptables rules not found")
  98. }
  99. dockerCmd(c, "rm", "--link", "parent/http")
  100. if iptables.Exists("filter", "DOCKER", sourceRule...) || iptables.Exists("filter", "DOCKER", destinationRule...) {
  101. c.Fatal("Iptables rules should be removed when unlink")
  102. }
  103. dockerCmd(c, "kill", "child")
  104. dockerCmd(c, "kill", "parent")
  105. }
  106. func (s *DockerSuite) TestLinksInspectLinksStarted(c *check.C) {
  107. var (
  108. expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
  109. result []string
  110. )
  111. dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  112. dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
  113. dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "top")
  114. links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
  115. if err != nil {
  116. c.Fatal(err)
  117. }
  118. err = unmarshalJSON([]byte(links), &result)
  119. if err != nil {
  120. c.Fatal(err)
  121. }
  122. output := convertSliceOfStringsToMap(result)
  123. equal := reflect.DeepEqual(output, expected)
  124. if !equal {
  125. c.Fatalf("Links %s, expected %s", result, expected)
  126. }
  127. }
  128. func (s *DockerSuite) TestLinksInspectLinksStopped(c *check.C) {
  129. var (
  130. expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
  131. result []string
  132. )
  133. dockerCmd(c, "run", "-d", "--name", "container1", "busybox", "top")
  134. dockerCmd(c, "run", "-d", "--name", "container2", "busybox", "top")
  135. dockerCmd(c, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
  136. links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
  137. if err != nil {
  138. c.Fatal(err)
  139. }
  140. err = unmarshalJSON([]byte(links), &result)
  141. if err != nil {
  142. c.Fatal(err)
  143. }
  144. output := convertSliceOfStringsToMap(result)
  145. equal := reflect.DeepEqual(output, expected)
  146. if !equal {
  147. c.Fatalf("Links %s, but expected %s", result, expected)
  148. }
  149. }
  150. func (s *DockerSuite) TestLinksNotStartedParentNotFail(c *check.C) {
  151. runCmd := exec.Command(dockerBinary, "create", "--name=first", "busybox", "top")
  152. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  153. if err != nil {
  154. c.Fatal(out, err)
  155. }
  156. runCmd = exec.Command(dockerBinary, "create", "--name=second", "--link=first:first", "busybox", "top")
  157. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  158. if err != nil {
  159. c.Fatal(out, err)
  160. }
  161. runCmd = exec.Command(dockerBinary, "start", "first")
  162. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  163. if err != nil {
  164. c.Fatal(out, err)
  165. }
  166. }
  167. func (s *DockerSuite) TestLinksHostsFilesInject(c *check.C) {
  168. testRequires(c, SameHostDaemon, ExecSupport)
  169. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "one", "busybox", "top"))
  170. if err != nil {
  171. c.Fatal(err, out)
  172. }
  173. idOne := strings.TrimSpace(out)
  174. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top"))
  175. if err != nil {
  176. c.Fatal(err, out)
  177. }
  178. idTwo := strings.TrimSpace(out)
  179. time.Sleep(1 * time.Second)
  180. contentOne, err := readContainerFileWithExec(idOne, "/etc/hosts")
  181. if err != nil {
  182. c.Fatal(err, string(contentOne))
  183. }
  184. contentTwo, err := readContainerFileWithExec(idTwo, "/etc/hosts")
  185. if err != nil {
  186. c.Fatal(err, string(contentTwo))
  187. }
  188. if !strings.Contains(string(contentTwo), "onetwo") {
  189. c.Fatal("Host is not present in updated hosts file", string(contentTwo))
  190. }
  191. }
  192. func (s *DockerSuite) TestLinksNetworkHostContainer(c *check.C) {
  193. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top"))
  194. if err != nil {
  195. c.Fatal(err, out)
  196. }
  197. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true"))
  198. if err == nil || !strings.Contains(out, "--net=host can't be used with links. This would result in undefined behavior.") {
  199. c.Fatalf("Running container linking to a container with --net host should have failed: %s", out)
  200. }
  201. }
  202. func (s *DockerSuite) TestLinksUpdateOnRestart(c *check.C) {
  203. testRequires(c, SameHostDaemon, ExecSupport)
  204. if out, err := exec.Command(dockerBinary, "run", "-d", "--name", "one", "busybox", "top").CombinedOutput(); err != nil {
  205. c.Fatal(err, string(out))
  206. }
  207. out, err := exec.Command(dockerBinary, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top").CombinedOutput()
  208. if err != nil {
  209. c.Fatal(err, string(out))
  210. }
  211. id := strings.TrimSpace(string(out))
  212. realIP, err := inspectField("one", "NetworkSettings.IPAddress")
  213. if err != nil {
  214. c.Fatal(err)
  215. }
  216. content, err := readContainerFileWithExec(id, "/etc/hosts")
  217. if err != nil {
  218. c.Fatal(err, string(content))
  219. }
  220. getIP := func(hosts []byte, hostname string) string {
  221. re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
  222. matches := re.FindSubmatch(hosts)
  223. if matches == nil {
  224. c.Fatalf("Hostname %s have no matches in hosts", hostname)
  225. }
  226. return string(matches[1])
  227. }
  228. if ip := getIP(content, "one"); ip != realIP {
  229. c.Fatalf("For 'one' alias expected IP: %s, got: %s", realIP, ip)
  230. }
  231. if ip := getIP(content, "onetwo"); ip != realIP {
  232. c.Fatalf("For 'onetwo' alias expected IP: %s, got: %s", realIP, ip)
  233. }
  234. if out, err := exec.Command(dockerBinary, "restart", "one").CombinedOutput(); err != nil {
  235. c.Fatal(err, string(out))
  236. }
  237. realIP, err = inspectField("one", "NetworkSettings.IPAddress")
  238. if err != nil {
  239. c.Fatal(err)
  240. }
  241. content, err = readContainerFileWithExec(id, "/etc/hosts")
  242. if err != nil {
  243. c.Fatal(err, string(content))
  244. }
  245. if ip := getIP(content, "one"); ip != realIP {
  246. c.Fatalf("For 'one' alias expected IP: %s, got: %s", realIP, ip)
  247. }
  248. if ip := getIP(content, "onetwo"); ip != realIP {
  249. c.Fatalf("For 'onetwo' alias expected IP: %s, got: %s", realIP, ip)
  250. }
  251. }