docker_cli_links_test.go 9.6 KB

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