Browse code

Make nat test less racy and accurate

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>

Michael Crosby authored on 2014/09/02 08:28:02
Showing 1 changed files
... ...
@@ -1,13 +1,11 @@
1 1
 package main
2 2
 
3 3
 import (
4
-	"encoding/json"
5 4
 	"fmt"
6 5
 	"net"
7 6
 	"os/exec"
7
+	"strings"
8 8
 	"testing"
9
-
10
-	"github.com/docker/docker/daemon"
11 9
 )
12 10
 
13 11
 func TestNetworkNat(t *testing.T) {
... ...
@@ -26,39 +24,23 @@ func TestNetworkNat(t *testing.T) {
26 26
 		t.Fatalf("Error retrieving the up for eth0: %s", err)
27 27
 	}
28 28
 
29
-	runCmd := exec.Command(dockerBinary, "run", "-d", "-p", "8080", "busybox", "nc", "-lp", "8080")
29
+	runCmd := exec.Command(dockerBinary, "run", "-dt", "-p", "8080:8080", "busybox", "nc", "-lp", "8080")
30 30
 	out, _, err := runCommandWithOutput(runCmd)
31 31
 	errorOut(err, t, fmt.Sprintf("run1 failed with errors: %v (%s)", err, out))
32 32
 
33 33
 	cleanedContainerID := stripTrailingCharacters(out)
34 34
 
35
-	inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID)
36
-	inspectOut, _, err := runCommandWithOutput(inspectCmd)
37
-	errorOut(err, t, fmt.Sprintf("out should've been a container id: %v %v", inspectOut, err))
38
-
39
-	containers := []*daemon.Container{}
40
-	if err := json.Unmarshal([]byte(inspectOut), &containers); err != nil {
41
-		t.Fatalf("Error inspecting the container: %s", err)
42
-	}
43
-	if len(containers) != 1 {
44
-		t.Fatalf("Unepexted container count. Expected 0, recieved: %d", len(containers))
45
-	}
46
-
47
-	port8080, exists := containers[0].NetworkSettings.Ports["8080/tcp"]
48
-	if !exists || len(port8080) == 0 {
49
-		t.Fatal("Port 8080/tcp not found in NetworkSettings")
50
-	}
51
-
52
-	runCmd = exec.Command(dockerBinary, "run", "-p", "8080", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | nc -w 30 %s %s", ifaceIp, port8080[0].HostPort))
35
+	runCmd = exec.Command(dockerBinary, "run", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | nc -w 30 %s 8080", ifaceIp))
53 36
 	out, _, err = runCommandWithOutput(runCmd)
54 37
 	errorOut(err, t, fmt.Sprintf("run2 failed with errors: %v (%s)", err, out))
55 38
 
56 39
 	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
57 40
 	out, _, err = runCommandWithOutput(runCmd)
58 41
 	errorOut(err, t, fmt.Sprintf("failed to retrieve logs for container: %v %v", cleanedContainerID, err))
42
+	out = strings.Trim(out, "\r\n")
59 43
 
60
-	if expected := "hello world\n"; out != expected {
61
-		t.Fatalf("Unexpected output. Expected: %s, recieved: -->%s<--", expected, out)
44
+	if expected := "hello world"; out != expected {
45
+		t.Fatalf("Unexpected output. Expected: %q, received: %q for iface %s", expected, out, ifaceIp)
62 46
 	}
63 47
 
64 48
 	killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)