docker_cli_links_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/pkg/iptables"
  5. "os/exec"
  6. "testing"
  7. )
  8. func TestPingUnlinkedContainers(t *testing.T) {
  9. runCmd := exec.Command(dockerBinary, "run", "--rm", "busybox", "sh", "-c", "ping -c 1 alias1 -W 1 && ping -c 1 alias2 -W 1")
  10. exitCode, err := runCommand(runCmd)
  11. if exitCode == 0 {
  12. t.Fatal("run ping did not fail")
  13. } else if exitCode != 1 {
  14. errorOut(err, t, fmt.Sprintf("run ping failed with errors: %v", err))
  15. }
  16. }
  17. func TestPingLinkedContainers(t *testing.T) {
  18. var out string
  19. out, _, _ = cmd(t, "run", "-d", "--name", "container1", "busybox", "sleep", "10")
  20. idA := stripTrailingCharacters(out)
  21. out, _, _ = cmd(t, "run", "-d", "--name", "container2", "busybox", "sleep", "10")
  22. idB := stripTrailingCharacters(out)
  23. 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")
  24. cmd(t, "kill", idA)
  25. cmd(t, "kill", idB)
  26. deleteAllContainers()
  27. }
  28. func TestIpTablesRulesWhenLinkAndUnlink(t *testing.T) {
  29. cmd(t, "run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "sleep", "10")
  30. cmd(t, "run", "-d", "--name", "parent", "--link", "child:http", "busybox", "sleep", "10")
  31. childIp := findContainerIp(t, "child")
  32. parentIp := findContainerIp(t, "parent")
  33. sourceRule := []string{"FORWARD", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", childIp, "--sport", "80", "-d", parentIp, "-j", "ACCEPT"}
  34. destinationRule := []string{"FORWARD", "-i", "docker0", "-o", "docker0", "-p", "tcp", "-s", parentIp, "--dport", "80", "-d", childIp, "-j", "ACCEPT"}
  35. if !iptables.Exists(sourceRule...) || !iptables.Exists(destinationRule...) {
  36. t.Fatal("Iptables rules not found")
  37. }
  38. cmd(t, "rm", "--link", "parent/http")
  39. if iptables.Exists(sourceRule...) || iptables.Exists(destinationRule...) {
  40. t.Fatal("Iptables rules should be removed when unlink")
  41. }
  42. cmd(t, "kill", "child")
  43. cmd(t, "kill", "parent")
  44. deleteAllContainers()
  45. logDone("link - verify iptables when link and unlink")
  46. }