github.com/mephux/docker@v1.6.0-rc5/integration-cli/docker_cli_nat_test.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "net" 6 "os/exec" 7 "strings" 8 "testing" 9 ) 10 11 func TestNetworkNat(t *testing.T) { 12 testRequires(t, SameHostDaemon, NativeExecDriver) 13 defer deleteAllContainers() 14 15 iface, err := net.InterfaceByName("eth0") 16 if err != nil { 17 t.Skipf("Test not running with `make test`. Interface eth0 not found: %s", err) 18 } 19 20 ifaceAddrs, err := iface.Addrs() 21 if err != nil || len(ifaceAddrs) == 0 { 22 t.Fatalf("Error retrieving addresses for eth0: %v (%d addresses)", err, len(ifaceAddrs)) 23 } 24 25 ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String()) 26 if err != nil { 27 t.Fatalf("Error retrieving the up for eth0: %s", err) 28 } 29 30 runCmd := exec.Command(dockerBinary, "run", "-dt", "-p", "8080:8080", "busybox", "nc", "-lp", "8080") 31 out, _, err := runCommandWithOutput(runCmd) 32 if err != nil { 33 t.Fatal(out, err) 34 } 35 36 cleanedContainerID := stripTrailingCharacters(out) 37 38 runCmd = exec.Command(dockerBinary, "run", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | nc -w 30 %s 8080", ifaceIP)) 39 out, _, err = runCommandWithOutput(runCmd) 40 if err != nil { 41 t.Fatal(out, err) 42 } 43 44 runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) 45 out, _, err = runCommandWithOutput(runCmd) 46 if err != nil { 47 t.Fatalf("failed to retrieve logs for container: %s, %v", out, err) 48 } 49 50 out = strings.Trim(out, "\r\n") 51 52 if expected := "hello world"; out != expected { 53 t.Fatalf("Unexpected output. Expected: %q, received: %q for iface %s", expected, out, ifaceIP) 54 } 55 56 killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID) 57 if out, _, err = runCommandWithOutput(killCmd); err != nil { 58 t.Fatalf("failed to kill container: %s, %v", out, err) 59 } 60 61 logDone("network - make sure nat works through the host") 62 }