docker_cli_nat_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. out, err := inspectField(containerID, "State.Running")
  47. c.Assert(err, check.IsNil)
  48. return out
  49. }
  50. func (s *DockerSuite) TestNetworkNat(c *check.C) {
  51. testRequires(c, SameHostDaemon, NativeExecDriver)
  52. srv := startServerContainer(c, "tcp", 8080)
  53. // Spawn a new container which connects to the server through the
  54. // interface address.
  55. endpoint := getExternalAddress(c)
  56. runCmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | nc -w 30 %s 8080", endpoint))
  57. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  58. c.Fatalf("Failed to connect to server: %v (output: %q)", err, string(out))
  59. }
  60. result := getContainerLogs(c, srv)
  61. // Ideally we'd like to check for "hello world" but sometimes
  62. // nc doesn't show the data it received so instead let's look for
  63. // the output of the 'echo bye' that should be printed once
  64. // the nc command gets a connection
  65. expected := "bye"
  66. if !strings.Contains(result, expected) {
  67. c.Fatalf("Unexpected output. Expected: %q, received: %q", expected, result)
  68. }
  69. }
  70. func (s *DockerSuite) TestNetworkLocalhostTCPNat(c *check.C) {
  71. testRequires(c, SameHostDaemon, NativeExecDriver)
  72. srv := startServerContainer(c, "tcp", 8081)
  73. // Attempt to connect from the host to the listening container.
  74. conn, err := net.Dial("tcp", "localhost:8081")
  75. if err != nil {
  76. c.Fatalf("Failed to connect to container (%v)", err)
  77. }
  78. if _, err := conn.Write([]byte("hello world\n")); err != nil {
  79. c.Fatal(err)
  80. }
  81. conn.Close()
  82. result := getContainerLogs(c, srv)
  83. // Ideally we'd like to check for "hello world" but sometimes
  84. // nc doesn't show the data it received so instead let's look for
  85. // the output of the 'echo bye' that should be printed once
  86. // the nc command gets a connection
  87. expected := "bye"
  88. if !strings.Contains(result, expected) {
  89. c.Fatalf("Unexpected output. Expected: %q, received: %q", expected, result)
  90. }
  91. }