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

Make nat test less racy and accurate

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2014-09-01 16:28:02 -07:00
parent ca63cceae5
commit 72652c5973

View file

@ -1,13 +1,11 @@
package main
import (
"encoding/json"
"fmt"
"net"
"os/exec"
"strings"
"testing"
"github.com/docker/docker/daemon"
)
func TestNetworkNat(t *testing.T) {
@ -26,39 +24,23 @@ func TestNetworkNat(t *testing.T) {
t.Fatalf("Error retrieving the up for eth0: %s", err)
}
runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "8080", "busybox", "nc", "-lp", "8080")
runCmd := exec.Command(dockerBinary, "run", "-dt", "-p", "8080:8080", "busybox", "nc", "-lp", "8080")
out, _, err := runCommandWithOutput(runCmd)
errorOut(err, t, fmt.Sprintf("run1 failed with errors: %v (%s)", err, out))
cleanedContainerID := stripTrailingCharacters(out)
inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
inspectOut, _, err := runCommandWithOutput(inspectCmd)
errorOut(err, t, fmt.Sprintf("out should've been a container id: %v %v", inspectOut, err))
containers := []*daemon.Container{}
if err := json.Unmarshal([]byte(inspectOut), &containers); err != nil {
t.Fatalf("Error inspecting the container: %s", err)
}
if len(containers) != 1 {
t.Fatalf("Unepexted container count. Expected 0, recieved: %d", len(containers))
}
port8080, exists := containers[0].NetworkSettings.Ports["8080/tcp"]
if !exists || len(port8080) == 0 {
t.Fatal("Port 8080/tcp not found in NetworkSettings")
}
runCmd = exec.Command(dockerBinary, "run", "-p", "8080", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | nc -w 30 %s %s", ifaceIp, port8080[0].HostPort))
runCmd = exec.Command(dockerBinary, "run", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | nc -w 30 %s 8080", ifaceIp))
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, fmt.Sprintf("run2 failed with errors: %v (%s)", err, out))
runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
out, _, err = runCommandWithOutput(runCmd)
errorOut(err, t, fmt.Sprintf("failed to retrieve logs for container: %v %v", cleanedContainerID, err))
out = strings.Trim(out, "\r\n")
if expected := "hello world\n"; out != expected {
t.Fatalf("Unexpected output. Expected: %s, recieved: -->%s<--", expected, out)
if expected := "hello world"; out != expected {
t.Fatalf("Unexpected output. Expected: %q, received: %q for iface %s", expected, out, ifaceIp)
}
killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)