docker_cli_nat_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "os/exec"
  6. "strings"
  7. "testing"
  8. )
  9. func TestNetworkNat(t *testing.T) {
  10. testRequires(t, SameHostDaemon)
  11. defer deleteAllContainers()
  12. iface, err := net.InterfaceByName("eth0")
  13. if err != nil {
  14. t.Skipf("Test not running with `make test`. Interface eth0 not found: %s", err)
  15. }
  16. ifaceAddrs, err := iface.Addrs()
  17. if err != nil || len(ifaceAddrs) == 0 {
  18. t.Fatalf("Error retrieving addresses for eth0: %v (%d addresses)", err, len(ifaceAddrs))
  19. }
  20. ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String())
  21. if err != nil {
  22. t.Fatalf("Error retrieving the up for eth0: %s", err)
  23. }
  24. runCmd := exec.Command(dockerBinary, "run", "-dt", "-p", "8080:8080", "busybox", "nc", "-lp", "8080")
  25. out, _, err := runCommandWithOutput(runCmd)
  26. if err != nil {
  27. t.Fatal(out, err)
  28. }
  29. cleanedContainerID := stripTrailingCharacters(out)
  30. runCmd = exec.Command(dockerBinary, "run", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | nc -w 30 %s 8080", ifaceIP))
  31. out, _, err = runCommandWithOutput(runCmd)
  32. if err != nil {
  33. t.Fatal(out, err)
  34. }
  35. runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
  36. out, _, err = runCommandWithOutput(runCmd)
  37. if err != nil {
  38. t.Fatalf("failed to retrieve logs for container: %s, %v", out, err)
  39. }
  40. out = strings.Trim(out, "\r\n")
  41. if expected := "hello world"; out != expected {
  42. t.Fatalf("Unexpected output. Expected: %q, received: %q for iface %s", expected, out, ifaceIP)
  43. }
  44. killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
  45. if out, _, err = runCommandWithOutput(killCmd); err != nil {
  46. t.Fatalf("failed to kill container: %s, %v", out, err)
  47. }
  48. logDone("network - make sure nat works through the host")
  49. }