docker_cli_links_test.go 10 KB

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