docker_cli_links_test.go 9.8 KB

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