docker_cli_links_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package main
  2. import (
  3. "github.com/docker/docker/pkg/iptables"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "reflect"
  8. "strings"
  9. "testing"
  10. "time"
  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 TestLinksPingLinkedContainersAfterRename(t *testing.T) {
  63. out, _, _ := dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  64. idA := stripTrailingCharacters(out)
  65. out, _, _ = dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
  66. idB := stripTrailingCharacters(out)
  67. dockerCmd(t, "rename", "container1", "container_new")
  68. dockerCmd(t, "run", "--rm", "--link", "container_new:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
  69. dockerCmd(t, "kill", idA)
  70. dockerCmd(t, "kill", idB)
  71. deleteAllContainers()
  72. logDone("links - ping linked container after rename")
  73. }
  74. func TestLinksPingLinkedContainersOnRename(t *testing.T) {
  75. var out string
  76. out, _, _ = dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  77. idA := stripTrailingCharacters(out)
  78. if idA == "" {
  79. t.Fatal(out, "id should not be nil")
  80. }
  81. out, _, _ = dockerCmd(t, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "sleep", "10")
  82. idB := stripTrailingCharacters(out)
  83. if idB == "" {
  84. t.Fatal(out, "id should not be nil")
  85. }
  86. execCmd := exec.Command(dockerBinary, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  87. out, _, err := runCommandWithOutput(execCmd)
  88. if err != nil {
  89. t.Fatal(out, err)
  90. }
  91. dockerCmd(t, "rename", "container1", "container_new")
  92. execCmd = exec.Command(dockerBinary, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  93. out, _, err = runCommandWithOutput(execCmd)
  94. if err != nil {
  95. t.Fatal(out, err)
  96. }
  97. deleteAllContainers()
  98. logDone("links - ping linked container upon rename")
  99. }
  100. func TestLinksIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
  101. dockerCmd(t, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "sleep", "10")
  102. dockerCmd(t, "run", "-d", "--name", "parent", "--link", "child:http", "busybox", "sleep", "10")
  103. childIP := findContainerIP(t, "child")
  104. parentIP := findContainerIP(t, "parent")
  105. sourceRule := []string{"DOCKER", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
  106. destinationRule := []string{"DOCKER", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
  107. if !iptables.Exists(sourceRule...) || !iptables.Exists(destinationRule...) {
  108. t.Fatal("Iptables rules not found")
  109. }
  110. dockerCmd(t, "rm", "--link", "parent/http")
  111. if iptables.Exists(sourceRule...) || iptables.Exists(destinationRule...) {
  112. t.Fatal("Iptables rules should be removed when unlink")
  113. }
  114. dockerCmd(t, "kill", "child")
  115. dockerCmd(t, "kill", "parent")
  116. deleteAllContainers()
  117. logDone("link - verify iptables when link and unlink")
  118. }
  119. func TestLinksInspectLinksStarted(t *testing.T) {
  120. var (
  121. expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
  122. result []string
  123. )
  124. defer deleteAllContainers()
  125. dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  126. dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
  127. dockerCmd(t, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sleep", "10")
  128. links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. err = unmarshalJSON([]byte(links), &result)
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. output := convertSliceOfStringsToMap(result)
  137. equal := reflect.DeepEqual(output, expected)
  138. if !equal {
  139. t.Fatalf("Links %s, expected %s", result, expected)
  140. }
  141. logDone("link - links in started container inspect")
  142. }
  143. func TestLinksInspectLinksStopped(t *testing.T) {
  144. var (
  145. expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
  146. result []string
  147. )
  148. defer deleteAllContainers()
  149. dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  150. dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
  151. dockerCmd(t, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
  152. links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. err = unmarshalJSON([]byte(links), &result)
  157. if err != nil {
  158. t.Fatal(err)
  159. }
  160. output := convertSliceOfStringsToMap(result)
  161. equal := reflect.DeepEqual(output, expected)
  162. if !equal {
  163. t.Fatalf("Links %s, but expected %s", result, expected)
  164. }
  165. logDone("link - links in stopped container inspect")
  166. }
  167. func TestLinksNotStartedParentNotFail(t *testing.T) {
  168. defer deleteAllContainers()
  169. runCmd := exec.Command(dockerBinary, "create", "--name=first", "busybox", "top")
  170. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  171. if err != nil {
  172. t.Fatal(out, err)
  173. }
  174. runCmd = exec.Command(dockerBinary, "create", "--name=second", "--link=first:first", "busybox", "top")
  175. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  176. if err != nil {
  177. t.Fatal(out, err)
  178. }
  179. runCmd = exec.Command(dockerBinary, "start", "first")
  180. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  181. if err != nil {
  182. t.Fatal(out, err)
  183. }
  184. logDone("link - container start not failing on updating stopped parent links")
  185. }
  186. func TestLinksHostsFilesInject(t *testing.T) {
  187. defer deleteAllContainers()
  188. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "one", "busybox", "top"))
  189. if err != nil {
  190. t.Fatal(err, out)
  191. }
  192. idOne := strings.TrimSpace(out)
  193. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top"))
  194. if err != nil {
  195. t.Fatal(err, out)
  196. }
  197. idTwo := strings.TrimSpace(out)
  198. time.Sleep(1 * time.Second)
  199. contentOne, err := readContainerFile(idOne, "hosts")
  200. if err != nil {
  201. t.Fatal(err, string(contentOne))
  202. }
  203. contentTwo, err := readContainerFile(idTwo, "hosts")
  204. if err != nil {
  205. t.Fatal(err, string(contentTwo))
  206. }
  207. if !strings.Contains(string(contentTwo), "onetwo") {
  208. t.Fatal("Host is not present in updated hosts file", string(contentTwo))
  209. }
  210. logDone("link - ensure containers hosts files are updated with the link alias.")
  211. }
  212. func TestLinksNetworkHostContainer(t *testing.T) {
  213. defer deleteAllContainers()
  214. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top"))
  215. if err != nil {
  216. t.Fatal(err, out)
  217. }
  218. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true"))
  219. if err == nil || !strings.Contains(out, "--net=host can't be used with links. This would result in undefined behavior.") {
  220. t.Fatalf("Running container linking to a container with --net host should have failed: %s", out)
  221. }
  222. logDone("link - error thrown when linking to container with --net host")
  223. }