docker_cli_nat_test.go 1.7 KB

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