docker_cli_nat_test.go 3.4 KB

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