docker_cli_links_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "os/exec"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/docker/docker/pkg/iptables"
  10. )
  11. func TestLinksEtcHostsRegularFile(t *testing.T) {
  12. runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
  13. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  14. if err != nil {
  15. t.Fatal(out, err)
  16. }
  17. if !strings.HasPrefix(out, "-") {
  18. t.Errorf("/etc/hosts should be a regular file")
  19. }
  20. deleteAllContainers()
  21. logDone("link - /etc/hosts is a regular file")
  22. }
  23. func TestLinksEtcHostsContentMatch(t *testing.T) {
  24. runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hosts")
  25. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  26. if err != nil {
  27. t.Fatal(out, err)
  28. }
  29. hosts, err := ioutil.ReadFile("/etc/hosts")
  30. if os.IsNotExist(err) {
  31. t.Skip("/etc/hosts does not exist, skip this test")
  32. }
  33. if out != string(hosts) {
  34. t.Errorf("container")
  35. }
  36. deleteAllContainers()
  37. logDone("link - /etc/hosts matches hosts copy")
  38. }
  39. func TestLinksPingUnlinkedContainers(t *testing.T) {
  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. t.Fatal("run ping did not fail")
  44. } else if exitCode != 1 {
  45. t.Fatalf("run ping failed with errors: %v", err)
  46. }
  47. logDone("links - ping unlinked container")
  48. }
  49. func TestLinksPingLinkedContainers(t *testing.T) {
  50. var out string
  51. out, _, _ = dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  52. idA := stripTrailingCharacters(out)
  53. out, _, _ = dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
  54. idB := stripTrailingCharacters(out)
  55. dockerCmd(t, "run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
  56. dockerCmd(t, "kill", idA)
  57. dockerCmd(t, "kill", idB)
  58. deleteAllContainers()
  59. logDone("links - ping linked container")
  60. }
  61. func TestLinksIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
  62. dockerCmd(t, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "sleep", "10")
  63. dockerCmd(t, "run", "-d", "--name", "parent", "--link", "child:http", "busybox", "sleep", "10")
  64. childIP := findContainerIP(t, "child")
  65. parentIP := findContainerIP(t, "parent")
  66. sourceRule := []string{"FORWARD", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
  67. destinationRule := []string{"FORWARD", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
  68. if !iptables.Exists(sourceRule...) || !iptables.Exists(destinationRule...) {
  69. t.Fatal("Iptables rules not found")
  70. }
  71. dockerCmd(t, "rm", "--link", "parent/http")
  72. if iptables.Exists(sourceRule...) || iptables.Exists(destinationRule...) {
  73. t.Fatal("Iptables rules should be removed when unlink")
  74. }
  75. dockerCmd(t, "kill", "child")
  76. dockerCmd(t, "kill", "parent")
  77. deleteAllContainers()
  78. logDone("link - verify iptables when link and unlink")
  79. }
  80. func TestLinksInspectLinksStarted(t *testing.T) {
  81. var (
  82. expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
  83. result []string
  84. )
  85. defer deleteAllContainers()
  86. dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  87. dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
  88. dockerCmd(t, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sleep", "10")
  89. links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. err = unmarshalJSON([]byte(links), &result)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. output := convertSliceOfStringsToMap(result)
  98. equal := deepEqual(expected, output)
  99. if !equal {
  100. t.Fatalf("Links %s, expected %s", result, expected)
  101. }
  102. logDone("link - links in started container inspect")
  103. }
  104. func TestLinksInspectLinksStopped(t *testing.T) {
  105. var (
  106. expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
  107. result []string
  108. )
  109. defer deleteAllContainers()
  110. dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  111. dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
  112. dockerCmd(t, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
  113. links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. err = unmarshalJSON([]byte(links), &result)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. output := convertSliceOfStringsToMap(result)
  122. equal := deepEqual(expected, output)
  123. if !equal {
  124. t.Fatalf("Links %s, but expected %s", result, expected)
  125. }
  126. logDone("link - links in stopped container inspect")
  127. }
  128. func TestLinksNotStartedParentNotFail(t *testing.T) {
  129. defer deleteAllContainers()
  130. runCmd := exec.Command(dockerBinary, "create", "--name=first", "busybox", "top")
  131. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  132. if err != nil {
  133. t.Fatal(out, err)
  134. }
  135. runCmd = exec.Command(dockerBinary, "create", "--name=second", "--link=first:first", "busybox", "top")
  136. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  137. if err != nil {
  138. t.Fatal(out, err)
  139. }
  140. runCmd = exec.Command(dockerBinary, "start", "first")
  141. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  142. if err != nil {
  143. t.Fatal(out, err)
  144. }
  145. logDone("link - container start not failing on updating stopped parent links")
  146. }
  147. func TestLinksHostsFilesInject(t *testing.T) {
  148. defer deleteAllContainers()
  149. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "one", "busybox", "top"))
  150. if err != nil {
  151. t.Fatal(err, out)
  152. }
  153. idOne := strings.TrimSpace(out)
  154. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top"))
  155. if err != nil {
  156. t.Fatal(err, out)
  157. }
  158. idTwo := strings.TrimSpace(out)
  159. time.Sleep(1 * time.Second)
  160. contentOne, err := readContainerFile(idOne, "hosts")
  161. if err != nil {
  162. t.Fatal(err, string(contentOne))
  163. }
  164. contentTwo, err := readContainerFile(idTwo, "hosts")
  165. if err != nil {
  166. t.Fatal(err, string(contentTwo))
  167. }
  168. if !strings.Contains(string(contentTwo), "onetwo") {
  169. t.Fatal("Host is not present in updated hosts file", string(contentTwo))
  170. }
  171. logDone("link - ensure containers hosts files are updated with the link alias.")
  172. }