docker_cli_links_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. "testing"
  9. "github.com/dotcloud/docker/pkg/iptables"
  10. )
  11. func TestEtcHostsRegularFile(t *testing.T) {
  12. runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "ls", "-la", "/etc/hosts")
  13. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  14. errorOut(err, t, out)
  15. if !strings.HasPrefix(out, "-") {
  16. t.Errorf("/etc/hosts should be a regular file")
  17. }
  18. deleteAllContainers()
  19. logDone("link - /etc/hosts is a regular file")
  20. }
  21. func TestEtcHostsContentMatch(t *testing.T) {
  22. runCmd := exec.Command(dockerBinary, "run", "--net=host", "busybox", "cat", "/etc/hosts")
  23. out, _, _, err := runCommandWithStdoutStderr(runCmd)
  24. errorOut(err, t, out)
  25. hosts, err := ioutil.ReadFile("/etc/hosts")
  26. if os.IsNotExist(err) {
  27. t.Skip("/etc/hosts does not exist, skip this test")
  28. }
  29. if out != string(hosts) {
  30. t.Errorf("container")
  31. }
  32. deleteAllContainers()
  33. logDone("link - /etc/hosts matches hosts copy")
  34. }
  35. func TestPingUnlinkedContainers(t *testing.T) {
  36. runCmd := exec.Command(dockerBinary, "run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
  37. exitCode, err := runCommand(runCmd)
  38. if exitCode == 0 {
  39. t.Fatal("run ping did not fail")
  40. } else if exitCode != 1 {
  41. errorOut(err, t, fmt.Sprintf("run ping failed with errors: %v", err))
  42. }
  43. }
  44. func TestPingLinkedContainers(t *testing.T) {
  45. var out string
  46. out, _, _ = cmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  47. idA := stripTrailingCharacters(out)
  48. out, _, _ = cmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
  49. idB := stripTrailingCharacters(out)
  50. cmd(t, "run", "--rm", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
  51. cmd(t, "kill", idA)
  52. cmd(t, "kill", idB)
  53. deleteAllContainers()
  54. }
  55. func TestIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
  56. cmd(t, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "sleep", "10")
  57. cmd(t, "run", "-d", "--name", "parent", "--link", "child:http", "busybox", "sleep", "10")
  58. childIp := findContainerIp(t, "child")
  59. parentIp := findContainerIp(t, "parent")
  60. sourceRule := []string{"FORWARD", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", childIp, "--sport", "80", "-d", parentIp, "-j", "ACCEPT"}
  61. destinationRule := []string{"FORWARD", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", parentIp, "--dport", "80", "-d", childIp, "-j", "ACCEPT"}
  62. if !iptables.Exists(sourceRule...) || !iptables.Exists(destinationRule...) {
  63. t.Fatal("Iptables rules not found")
  64. }
  65. cmd(t, "rm", "--link", "parent/http")
  66. if iptables.Exists(sourceRule...) || iptables.Exists(destinationRule...) {
  67. t.Fatal("Iptables rules should be removed when unlink")
  68. }
  69. cmd(t, "kill", "child")
  70. cmd(t, "kill", "parent")
  71. deleteAllContainers()
  72. logDone("link - verify iptables when link and unlink")
  73. }
  74. func TestInspectLinksStarted(t *testing.T) {
  75. var (
  76. expected = map[string]struct{}{"/container1:/testinspectlink/alias1": {}, "/container2:/testinspectlink/alias2": {}}
  77. result []string
  78. )
  79. defer deleteAllContainers()
  80. cmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  81. cmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
  82. cmd(t, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "sleep", "10")
  83. links, err := inspectFieldJSON("testinspectlink", "HostConfig.Links")
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. err = unmarshalJSON([]byte(links), &result)
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. output := convertSliceOfStringsToMap(result)
  92. equal := deepEqual(expected, output)
  93. if !equal {
  94. t.Fatalf("Links %s, expected %s", result, expected)
  95. }
  96. logDone("link - links in started container inspect")
  97. }
  98. func TestInspectLinksStopped(t *testing.T) {
  99. defer deleteAllContainers()
  100. cmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  101. cmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
  102. cmd(t, "run", "-d", "--name", "testinspectlink", "--link", "container1:alias1", "--link", "container2:alias2", "busybox", "true")
  103. links, err := inspectField("testinspectlink", "HostConfig.Links")
  104. if err != nil {
  105. t.Fatal(err)
  106. }
  107. if expected := "[/container1:/testinspectlink/alias1 /container2:/testinspectlink/alias2]"; links != expected {
  108. t.Fatalf("Links %s, but expected %s", links, expected)
  109. }
  110. logDone("link - links in stopped container inspect")
  111. }