docker_cli_nat_test.go 1.6 KB

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