Merge pull request #13087 from duglin/FixNatTest

Fix random errors in DockerSuite.TestNetworkNat
This commit is contained in:
Alexander Morozov 2015-05-14 08:35:27 -07:00
commit fe6f77df2c
1 changed files with 17 additions and 4 deletions

View File

@ -4,14 +4,15 @@ import (
"fmt" "fmt"
"net" "net"
"os/exec" "os/exec"
"strconv"
"strings" "strings"
"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, proto string, port int) string {
cmd := []string{"-d", "-p", fmt.Sprintf("%d:%d", port, port), "busybox", "nc", "-lp", strconv.Itoa(port)} 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" { if proto == "udp" {
cmd = append(cmd, "-u") cmd = append(cmd, "-u")
} }
@ -75,7 +76,13 @@ func (s *DockerSuite) TestNetworkNat(c *check.C) {
} }
result := getContainerLogs(c, srv) result := getContainerLogs(c, srv)
if expected := "hello world"; result != expected {
// 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) c.Fatalf("Unexpected output. Expected: %q, received: %q", expected, result)
} }
} }
@ -97,7 +104,13 @@ func (s *DockerSuite) TestNetworkLocalhostTCPNat(c *check.C) {
conn.Close() conn.Close()
result := getContainerLogs(c, srv) result := getContainerLogs(c, srv)
if expected := "hello world"; result != expected {
// 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) c.Fatalf("Unexpected output. Expected: %q, received: %q", expected, result)
} }
} }