docker_cli_links_test.go 10 KB

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