docker_cli_nat_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. "github.com/go-check/check"
  9. )
  10. func startServerContainer(c *check.C, proto string, port int) string {
  11. cmd := []string{"-d", "-p", fmt.Sprintf("%d:%d", port, port), "busybox", "nc", "-lp", strconv.Itoa(port)}
  12. if proto == "udp" {
  13. cmd = append(cmd, "-u")
  14. }
  15. name := "server"
  16. if err := waitForContainer(name, cmd...); err != nil {
  17. c.Fatalf("Failed to launch server container: %v", err)
  18. }
  19. return name
  20. }
  21. func getExternalAddress(c *check.C) net.IP {
  22. iface, err := net.InterfaceByName("eth0")
  23. if err != nil {
  24. c.Skip(fmt.Sprintf("Test not running with `make test`. Interface eth0 not found: %v", err))
  25. }
  26. ifaceAddrs, err := iface.Addrs()
  27. if err != nil || len(ifaceAddrs) == 0 {
  28. c.Fatalf("Error retrieving addresses for eth0: %v (%d addresses)", err, len(ifaceAddrs))
  29. }
  30. ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String())
  31. if err != nil {
  32. c.Fatalf("Error retrieving the up for eth0: %s", err)
  33. }
  34. return ifaceIP
  35. }
  36. func getContainerLogs(c *check.C, containerID string) string {
  37. runCmd := exec.Command(dockerBinary, "logs", containerID)
  38. out, _, err := runCommandWithOutput(runCmd)
  39. if err != nil {
  40. c.Fatal(out, err)
  41. }
  42. return strings.Trim(out, "\r\n")
  43. }
  44. func getContainerStatus(c *check.C, containerID string) string {
  45. runCmd := exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", containerID)
  46. out, _, err := runCommandWithOutput(runCmd)
  47. if err != nil {
  48. c.Fatal(out, err)
  49. }
  50. return strings.Trim(out, "\r\n")
  51. }
  52. func (s *DockerSuite) TestNetworkNat(c *check.C) {
  53. testRequires(c, SameHostDaemon, NativeExecDriver)
  54. defer deleteAllContainers()
  55. srv := startServerContainer(c, "tcp", 8080)
  56. // Spawn a new container which connects to the server through the
  57. // interface address.
  58. endpoint := getExternalAddress(c)
  59. runCmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | nc -w 30 %s 8080", endpoint))
  60. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  61. c.Fatalf("Failed to connect to server: %v (output: %q)", err, string(out))
  62. }
  63. result := getContainerLogs(c, srv)
  64. if expected := "hello world"; result != expected {
  65. c.Fatalf("Unexpected output. Expected: %q, received: %q", expected, result)
  66. }
  67. }
  68. func (s *DockerSuite) TestNetworkLocalhostTCPNat(c *check.C) {
  69. testRequires(c, SameHostDaemon, NativeExecDriver)
  70. defer deleteAllContainers()
  71. srv := startServerContainer(c, "tcp", 8081)
  72. // Attempt to connect from the host to the listening container.
  73. conn, err := net.Dial("tcp", "localhost:8081")
  74. if err != nil {
  75. c.Fatalf("Failed to connect to container (%v)", err)
  76. }
  77. if _, err := conn.Write([]byte("hello world\n")); err != nil {
  78. c.Fatal(err)
  79. }
  80. conn.Close()
  81. result := getContainerLogs(c, srv)
  82. if expected := "hello world"; result != expected {
  83. c.Fatalf("Unexpected output. Expected: %q, received: %q", expected, result)
  84. }
  85. }