1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #13683 from crosbymichael/nat-test-fixes

Fix nat integration tests
This commit is contained in:
Jessie Frazelle 2015-06-02 18:54:17 -07:00
commit 63823cd6c1

View file

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os/exec" "os/exec"
"strings" "strings"
@ -9,15 +10,14 @@ import (
"github.com/go-check/check" "github.com/go-check/check"
) )
func startServerContainer(c *check.C, proto string, port int) string { func startServerContainer(c *check.C, msg string, port int) string {
pStr := fmt.Sprintf("%d:%d", port, port)
bCmd := fmt.Sprintf("nc -lp %d && echo bye", port)
cmd := []string{"-d", "-p", pStr, "busybox", "sh", "-c", bCmd}
if proto == "udp" {
cmd = append(cmd, "-u")
}
name := "server" name := "server"
cmd := []string{
"-d",
"-p", fmt.Sprintf("%d:%d", port, port),
"busybox",
"sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port),
}
if err := waitForContainer(name, cmd...); err != nil { if err := waitForContainer(name, cmd...); err != nil {
c.Fatalf("Failed to launch server container: %v", err) c.Fatalf("Failed to launch server container: %v", err)
} }
@ -60,52 +60,41 @@ func getContainerStatus(c *check.C, containerID string) string {
func (s *DockerSuite) TestNetworkNat(c *check.C) { func (s *DockerSuite) TestNetworkNat(c *check.C) {
testRequires(c, SameHostDaemon, NativeExecDriver) testRequires(c, SameHostDaemon, NativeExecDriver)
msg := "it works"
srv := startServerContainer(c, "tcp", 8080) startServerContainer(c, msg, 8080)
// Spawn a new container which connects to the server through the
// interface address.
endpoint := getExternalAddress(c) endpoint := getExternalAddress(c)
runCmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | nc -w 30 %s 8080", endpoint)) conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", endpoint.String(), 8080))
if out, _, err := runCommandWithOutput(runCmd); err != nil { if err != nil {
c.Fatalf("Failed to connect to server: %v (output: %q)", err, string(out)) c.Fatalf("Failed to connect to container (%v)", err)
} }
data, err := ioutil.ReadAll(conn)
result := getContainerLogs(c, srv) conn.Close()
if err != nil {
// Ideally we'd like to check for "hello world" but sometimes c.Fatal(err)
// nc doesn't show the data it received so instead let's look for }
// the output of the 'echo bye' that should be printed once final := strings.TrimRight(string(data), "\n")
// the nc command gets a connection if final != msg {
expected := "bye" c.Fatalf("Expected message %q but received %q", msg, final)
if !strings.Contains(result, expected) {
c.Fatalf("Unexpected output. Expected: %q, received: %q", expected, result)
} }
} }
func (s *DockerSuite) TestNetworkLocalhostTCPNat(c *check.C) { func (s *DockerSuite) TestNetworkLocalhostTCPNat(c *check.C) {
testRequires(c, SameHostDaemon, NativeExecDriver) testRequires(c, SameHostDaemon, NativeExecDriver)
var (
srv := startServerContainer(c, "tcp", 8081) msg = "hi yall"
)
// Attempt to connect from the host to the listening container. startServerContainer(c, msg, 8081)
conn, err := net.Dial("tcp", "localhost:8081") conn, err := net.Dial("tcp", "localhost:8081")
if err != nil { if err != nil {
c.Fatalf("Failed to connect to container (%v)", err) c.Fatalf("Failed to connect to container (%v)", err)
} }
if _, err := conn.Write([]byte("hello world\n")); err != nil { data, err := ioutil.ReadAll(conn)
conn.Close()
if err != nil {
c.Fatal(err) c.Fatal(err)
} }
conn.Close() final := strings.TrimRight(string(data), "\n")
if final != msg {
result := getContainerLogs(c, srv) c.Fatalf("Expected message %q but received %q", msg, final)
// Ideally we'd like to check for "hello world" but sometimes
// nc doesn't show the data it received so instead let's look for
// the output of the 'echo bye' that should be printed once
// the nc command gets a connection
expected := "bye"
if !strings.Contains(result, expected) {
c.Fatalf("Unexpected output. Expected: %q, received: %q", expected, result)
} }
} }