docker_cli_links_test.go 5.4 KB

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