docker_cli_links_test.go 7.2 KB

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