docker_cli_links_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 TestLinksPingLinkedContainersOnRename(t *testing.T) {
  77. var out string
  78. out, _, _ = dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  79. idA := stripTrailingCharacters(out)
  80. if idA == "" {
  81. t.Fatal(out, "id should not be nil")
  82. }
  83. out, _, _ = dockerCmd(t, "run", "-d", "--link", "container1:alias1", "--name", "container2", "busybox", "sleep", "10")
  84. idB := stripTrailingCharacters(out)
  85. if idB == "" {
  86. t.Fatal(out, "id should not be nil")
  87. }
  88. execCmd := exec.Command(dockerBinary, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  89. out, _, err := runCommandWithOutput(execCmd)
  90. if err != nil {
  91. t.Fatal(out, err)
  92. }
  93. dockerCmd(t, "rename", "container1", "container_new")
  94. execCmd = exec.Command(dockerBinary, "exec", "container2", "ping", "-c", "1", "alias1", "-W", "1")
  95. out, _, err = runCommandWithOutput(execCmd)
  96. if err != nil {
  97. t.Fatal(out, err)
  98. }
  99. deleteAllContainers()
  100. logDone("links - ping linked container upon rename")
  101. }
  102. func TestLinksIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
  103. dockerCmd(t, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "sleep", "10")
  104. dockerCmd(t, "run", "-d", "--name", "parent", "--link", "child:http", "busybox", "sleep", "10")
  105. childIP := findContainerIP(t, "child")
  106. parentIP := findContainerIP(t, "parent")
  107. sourceRule := []string{"DOCKER", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
  108. destinationRule := []string{"DOCKER", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
  109. if !iptables.Exists(sourceRule...) || !iptables.Exists(destinationRule...) {
  110. t.Fatal("Iptables rules not found")
  111. }
  112. dockerCmd(t, "rm", "--link", "parent/http")
  113. if iptables.Exists(sourceRule...) || iptables.Exists(destinationRule...) {
  114. t.Fatal("Iptables rules should be removed when unlink")
  115. }
  116. dockerCmd(t, "kill", "child")
  117. dockerCmd(t, "kill", "parent")
  118. deleteAllContainers()
  119. logDone("link - verify iptables when link and unlink")
  120. }
  121. func TestLinksInspectLinksStarted(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", "sleep", "10")
  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, expected %s", result, expected)
  142. }
  143. logDone("link - links in started container inspect")
  144. }
  145. func TestLinksInspectLinksStopped(t *testing.T) {
  146. var (
  147. expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
  148. result []string
  149. )
  150. defer deleteAllContainers()
  151. dockerCmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  152. dockerCmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
  153. dockerCmd(t, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
  154. links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. err = unmarshalJSON([]byte(links), &result)
  159. if err != nil {
  160. t.Fatal(err)
  161. }
  162. output := convertSliceOfStringsToMap(result)
  163. equal := reflect.DeepEqual(output, expected)
  164. if !equal {
  165. t.Fatalf("Links %s, but expected %s", result, expected)
  166. }
  167. logDone("link - links in stopped container inspect")
  168. }
  169. func TestLinksNotStartedParentNotFail(t *testing.T) {
  170. defer deleteAllContainers()
  171. runCmd := exec.Command(dockerBinary, "create", "--name=first", "busybox", "top")
  172. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  173. if err != nil {
  174. t.Fatal(out, err)
  175. }
  176. runCmd = exec.Command(dockerBinary, "create", "--name=second", "--link=first:first", "busybox", "top")
  177. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  178. if err != nil {
  179. t.Fatal(out, err)
  180. }
  181. runCmd = exec.Command(dockerBinary, "start", "first")
  182. out, _, _, err = runCommandWithStdoutStderr(runCmd)
  183. if err != nil {
  184. t.Fatal(out, err)
  185. }
  186. logDone("link - container start not failing on updating stopped parent links")
  187. }
  188. func TestLinksHostsFilesInject(t *testing.T) {
  189. defer deleteAllContainers()
  190. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "one", "busybox", "top"))
  191. if err != nil {
  192. t.Fatal(err, out)
  193. }
  194. idOne := strings.TrimSpace(out)
  195. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "-itd", "--name", "two", "--link", "one:onetwo", "busybox", "top"))
  196. if err != nil {
  197. t.Fatal(err, out)
  198. }
  199. idTwo := strings.TrimSpace(out)
  200. time.Sleep(1 * time.Second)
  201. contentOne, err := readContainerFile(idOne, "hosts")
  202. if err != nil {
  203. t.Fatal(err, string(contentOne))
  204. }
  205. contentTwo, err := readContainerFile(idTwo, "hosts")
  206. if err != nil {
  207. t.Fatal(err, string(contentTwo))
  208. }
  209. if !strings.Contains(string(contentTwo), "onetwo") {
  210. t.Fatal("Host is not present in updated hosts file", string(contentTwo))
  211. }
  212. logDone("link - ensure containers hosts files are updated with the link alias.")
  213. }
  214. func TestLinksNetworkHostContainer(t *testing.T) {
  215. defer deleteAllContainers()
  216. out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "run", "-d", "--net", "host", "--name", "host_container", "busybox", "top"))
  217. if err != nil {
  218. t.Fatal(err, out)
  219. }
  220. out, _, err = runCommandWithOutput(exec.Command(dockerBinary, "run", "--name", "should_fail", "--link", "host_container:tester", "busybox", "true"))
  221. if err == nil || !strings.Contains(out, "--net=host can't be used with links. This would result in undefined behavior.") {
  222. t.Fatalf("Running container linking to a container with --net host should have failed: %s", out)
  223. }
  224. logDone("link - error thrown when linking to container with --net host")
  225. }
  226. func TestLinksUpdateOnRestart(t *testing.T) {
  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 := readContainerFile(id, "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 = readContainerFile(id, "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. }