docker_cli_links_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. defer deleteAllContainers()
  16. runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
  17. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  18. if err != nil {
  19. t.Fatal(out, err)
  20. }
  21. if !strings.HasPrefix(out, "-") {
  22. t.Errorf("/etc/hosts should be a regular file")
  23. }
  24. logDone("link - /etc/hosts is a regular file")
  25. }
  26. func TestLinksEtcHostsContentMatch(t *testing.T) {
  27. testRequires(t, SameHostDaemon)
  28. defer deleteAllContainers()
  29. runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hosts")
  30. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  31. if err != nil {
  32. t.Fatal(out, err)
  33. }
  34. hosts, err := ioutil.ReadFile("/etc/hosts")
  35. if os.IsNotExist(err) {
  36. t.Skip("/etc/hosts does not exist, skip this test")
  37. }
  38. if out != string(hosts) {
  39. t.Errorf("container")
  40. }
  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. // Test for appropriate error when calling --link with an invalid target container
  54. func TestLinksInvalidContainerTarget(t *testing.T) {
  55. defer deleteAllContainers()
  56. runCmd := exec.Command(dockerBinary, "run", "--link", "bogus:alias", "busybox", "true")
  57. out, _, err := runCommandWithOutput(runCmd)
  58. if err == nil {
  59. t.Fatal("an invalid container target should produce an error")
  60. }
  61. if !strings.Contains(out, "Could not get container") {
  62. t.Fatalf("error output expected 'Could not get container', but got %q instead; err: %v", out, err)
  63. }
  64. logDone("links - linking to non-existent container should not work")
  65. }
  66. func TestLinksPingLinkedContainers(t *testing.T) {
  67. defer deleteAllContainers()
  68. runCmd := exec.Command(dockerBinary, "run", "-d", "--name", "container1", "--hostname", "fred", "busybox", "top")
  69. if _, err := runCommand(runCmd); err != nil {
  70. t.Fatal(err)
  71. }
  72. runCmd = exec.Command(dockerBinary, "run", "-d", "--name", "container2", "--hostname", "wilma", "busybox", "top")
  73. if _, err := runCommand(runCmd); err != nil {
  74. t.Fatal(err)
  75. }
  76. runArgs := []string{"run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c"}
  77. pingCmd := "ping -c 1 %s -W 1 && ping -c 1 %s -W 1"
  78. // test ping by alias, ping by name, and ping by hostname
  79. // 1. Ping by alias
  80. dockerCmd(t, append(runArgs, fmt.Sprintf(pingCmd, "alias1", "alias2"))...)
  81. // 2. Ping by container name
  82. dockerCmd(t, append(runArgs, fmt.Sprintf(pingCmd, "container1", "container2"))...)
  83. // 3. Ping by hostname
  84. dockerCmd(t, append(runArgs, fmt.Sprintf(pingCmd, "fred", "wilma"))...)
  85. logDone("links - ping linked container")
  86. }
  87. func TestLinksPingLinkedContainersAfterRename(t *testing.T) {
  88. defer deleteAllContainers()
  89. out, _, _ := dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  90. idA := stripTrailingCharacters(out)
  91. out, _, _ = dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
  92. idB := stripTrailingCharacters(out)
  93. dockerCmd(t, "rename", "container1", "container_new")
  94. 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")
  95. dockerCmd(t, "kill", idA)
  96. dockerCmd(t, "kill", idB)
  97. logDone("links - ping linked container after rename")
  98. }
  99. func TestLinksIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
  100. testRequires(t, SameHostDaemon)
  101. defer deleteAllContainers()
  102. dockerCmd(t, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "sleep", "10")
  103. dockerCmd(t, "run", "-d", "--name", "parent", "--link", "child:http", "busybox", "sleep", "10")
  104. childIP := findContainerIP(t, "child")
  105. parentIP := findContainerIP(t, "parent")
  106. sourceRule := []string{"-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
  107. destinationRule := []string{"-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
  108. if !iptables.Exists("filter", "DOCKER", sourceRule...) || !iptables.Exists("filter", "DOCKER", destinationRule...) {
  109. t.Fatal("Iptables rules not found")
  110. }
  111. dockerCmd(t, "rm", "--link", "parent/http")
  112. if iptables.Exists("filter", "DOCKER", sourceRule...) || iptables.Exists("filter", "DOCKER", destinationRule...) {
  113. t.Fatal("Iptables rules should be removed when unlink")
  114. }
  115. dockerCmd(t, "kill", "child")
  116. dockerCmd(t, "kill", "parent")
  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 successfully updating stopped parent links")
  185. }
  186. func TestLinksHostsFilesInject(t *testing.T) {
  187. testRequires(t, SameHostDaemon, ExecSupport)
  188. defer deleteAllContainers()
  189. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "one", "busybox", "top"))
  190. if err != nil {
  191. t.Fatal(err, out)
  192. }
  193. idOne := strings.TrimSpace(out)
  194. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top"))
  195. if err != nil {
  196. t.Fatal(err, out)
  197. }
  198. idTwo := strings.TrimSpace(out)
  199. time.Sleep(1 * time.Second)
  200. contentOne, err := readContainerFileWithExec(idOne, "/etc/hosts")
  201. if err != nil {
  202. t.Fatal(err, string(contentOne))
  203. }
  204. contentTwo, err := readContainerFileWithExec(idTwo, "/etc/hosts")
  205. if err != nil {
  206. t.Fatal(err, string(contentTwo))
  207. }
  208. if !strings.Contains(string(contentTwo), "onetwo") {
  209. t.Fatal("Host is not present in updated hosts file", string(contentTwo))
  210. }
  211. logDone("link - ensure containers hosts files are updated with the link alias.")
  212. }
  213. func TestLinksNetworkHostContainer(t *testing.T) {
  214. defer deleteAllContainers()
  215. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top"))
  216. if err != nil {
  217. t.Fatal(err, out)
  218. }
  219. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true"))
  220. if err == nil || !strings.Contains(out, "--net=host can't be used with links. This would result in undefined behavior.") {
  221. t.Fatalf("Running container linking to a container with --net host should have failed: %s", out)
  222. }
  223. logDone("link - error thrown when linking to container with --net host")
  224. }
  225. func TestLinksUpdateOnRestart(t *testing.T) {
  226. testRequires(t, SameHostDaemon, ExecSupport)
  227. defer deleteAllContainers()
  228. if out, err := exec.Command(dockerBinary, "run", "-d", "--name", "one", "busybox", "top").CombinedOutput(); err != nil {
  229. t.Fatal(err, string(out))
  230. }
  231. out, err := exec.Command(dockerBinary, "run", "-d", "--name", "two", "--link", "one:onetwo", "--link", "one:one", "busybox", "top").CombinedOutput()
  232. if err != nil {
  233. t.Fatal(err, string(out))
  234. }
  235. id := strings.TrimSpace(string(out))
  236. realIP, err := inspectField("one", "NetworkSettings.IPAddress")
  237. if err != nil {
  238. t.Fatal(err)
  239. }
  240. content, err := readContainerFileWithExec(id, "/etc/hosts")
  241. if err != nil {
  242. t.Fatal(err, string(content))
  243. }
  244. getIP := func(hosts []byte, hostname string) string {
  245. re := regexp.MustCompile(fmt.Sprintf(`(\S*)\t%s`, regexp.QuoteMeta(hostname)))
  246. matches := re.FindSubmatch(hosts)
  247. if matches == nil {
  248. t.Fatalf("Hostname %s have no matches in hosts", hostname)
  249. }
  250. return string(matches[1])
  251. }
  252. if ip := getIP(content, "one"); ip != realIP {
  253. t.Fatalf("For 'one' alias expected IP: %s, got: %s", realIP, ip)
  254. }
  255. if ip := getIP(content, "onetwo"); ip != realIP {
  256. t.Fatalf("For 'onetwo' alias expected IP: %s, got: %s", realIP, ip)
  257. }
  258. if out, err := exec.Command(dockerBinary, "restart", "one").CombinedOutput(); err != nil {
  259. t.Fatal(err, string(out))
  260. }
  261. realIP, err = inspectField("one", "NetworkSettings.IPAddress")
  262. if err != nil {
  263. t.Fatal(err)
  264. }
  265. content, err = readContainerFileWithExec(id, "/etc/hosts")
  266. if err != nil {
  267. t.Fatal(err, string(content))
  268. }
  269. if ip := getIP(content, "one"); ip != realIP {
  270. t.Fatalf("For 'one' alias expected IP: %s, got: %s", realIP, ip)
  271. }
  272. if ip := getIP(content, "onetwo"); ip != realIP {
  273. t.Fatalf("For 'onetwo' alias expected IP: %s, got: %s", realIP, ip)
  274. }
  275. logDone("link - ensure containers hosts files are updated on restart")
  276. }