docker_cli_nat_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/dotcloud/docker/daemon"
  6. "net"
  7. "os/exec"
  8. "path/filepath"
  9. "testing"
  10. )
  11. func TestNetworkNat(t *testing.T) {
  12. ncPath, err := exec.LookPath("nc")
  13. if err != nil {
  14. t.Skip("Test not running with `make test`. Netcat not found: %s", err)
  15. }
  16. ncPath, err = filepath.EvalSymlinks(ncPath)
  17. if err != nil {
  18. t.Fatalf("Error resolving netcat symlink: %s", err)
  19. }
  20. iface, err := net.InterfaceByName("eth0")
  21. if err != nil {
  22. t.Skip("Test not running with `make test`. Interface eth0 not found: %s", err)
  23. }
  24. ifaceAddrs, err := iface.Addrs()
  25. if err != nil || len(ifaceAddrs) == 0 {
  26. t.Fatalf("Error retrieving addresses for eth0: %v (%d addresses)", err, len(ifaceAddrs))
  27. }
  28. ifaceIp, _, err := net.ParseCIDR(ifaceAddrs[0].String())
  29. if err != nil {
  30. t.Fatalf("Error retrieving the up for eth0: %s", err)
  31. }
  32. runCmd := exec.Command(dockerBinary, "run", "-d",
  33. "-v", ncPath+":/bin/nc",
  34. "-v", "/lib/x86_64-linux-gnu/libc.so.6:/lib/libc.so.6", "-v", "/lib/x86_64-linux-gnu/libresolv.so.2:/lib/libresolv.so.2", "-v", "/lib/x86_64-linux-gnu/libbsd.so.0:/lib/libbsd.so.0", "-v", "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2:/lib/ld-linux-x86-64.so.2",
  35. "-p", "8080", "busybox", "/bin/nc", "-lp", "8080")
  36. out, _, err := runCommandWithOutput(runCmd)
  37. errorOut(err, t, fmt.Sprintf("run1 failed with errors: %v (%s)", err, out))
  38. cleanedContainerID := stripTrailingCharacters(out)
  39. inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
  40. inspectOut, _, err := runCommandWithOutput(inspectCmd)
  41. errorOut(err, t, fmt.Sprintf("out should've been a container id: %v %v", inspectOut, err))
  42. containers := []*daemon.Container{}
  43. if err := json.Unmarshal([]byte(inspectOut), &containers); err != nil {
  44. t.Fatalf("Error inspecting the container: %s", err)
  45. }
  46. if len(containers) != 1 {
  47. t.Fatalf("Unepexted container count. Expected 0, recieved: %d", len(containers))
  48. }
  49. port8080, exists := containers[0].NetworkSettings.Ports["8080/tcp"]
  50. if !exists || len(port8080) == 0 {
  51. t.Fatal("Port 8080/tcp not found in NetworkSettings")
  52. }
  53. runCmd = exec.Command(dockerBinary, "run",
  54. "-v", ncPath+":/bin/nc",
  55. "-v", "/lib/x86_64-linux-gnu/libc.so.6:/lib/libc.so.6", "-v", "/lib/x86_64-linux-gnu/libresolv.so.2:/lib/libresolv.so.2", "-v", "/lib/x86_64-linux-gnu/libbsd.so.0:/lib/libbsd.so.0", "-v", "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2:/lib/ld-linux-x86-64.so.2",
  56. "-p", "8080", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | /bin/nc -w 30 %s %s", ifaceIp, port8080[0].HostPort))
  57. out, _, err = runCommandWithOutput(runCmd)
  58. errorOut(err, t, fmt.Sprintf("run2 failed with errors: %v (%s)", err, out))
  59. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  60. out, _, err = runCommandWithOutput(runCmd)
  61. errorOut(err, t, fmt.Sprintf("failed to retrieve logs for container: %v %v", cleanedContainerID, err))
  62. if expected := "hello world\n"; out != expected {
  63. t.Fatalf("Unexpected output. Expected: %s, recieved: -->%s<--", expected, out)
  64. }
  65. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  66. out, _, err = runCommandWithOutput(killCmd)
  67. errorOut(err, t, fmt.Sprintf("failed to kill container: %v %v", out, err))
  68. deleteAllContainers()
  69. logDone("network - make sure nat works through the host")
  70. }