github.com/nguyentm83/docker@v1.5.0/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 iface, err := net.InterfaceByName("eth0") 13 if err != nil { 14 t.Skipf("Test not running with `make test`. Interface eth0 not found: %s", err) 15 } 16 17 ifaceAddrs, err := iface.Addrs() 18 if err != nil || len(ifaceAddrs) == 0 { 19 t.Fatalf("Error retrieving addresses for eth0: %v (%d addresses)", err, len(ifaceAddrs)) 20 } 21 22 ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String()) 23 if err != nil { 24 t.Fatalf("Error retrieving the up for eth0: %s", err) 25 } 26 27 runCmd := exec.Command(dockerBinary, "run", "-dt", "-p", "8080:8080", "busybox", "nc", "-lp", "8080") 28 out, _, err := runCommandWithOutput(runCmd) 29 if err != nil { 30 t.Fatal(out, err) 31 } 32 33 cleanedContainerID := stripTrailingCharacters(out) 34 35 runCmd = exec.Command(dockerBinary, "run", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | nc -w 30 %s 8080", ifaceIP)) 36 out, _, err = runCommandWithOutput(runCmd) 37 if err != nil { 38 t.Fatal(out, err) 39 } 40 41 runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) 42 out, _, err = runCommandWithOutput(runCmd) 43 if err != nil { 44 t.Fatalf("failed to retrieve logs for container: %s, %v", out, err) 45 } 46 47 out = strings.Trim(out, "\r\n") 48 49 if expected := "hello world"; out != expected { 50 t.Fatalf("Unexpected output. Expected: %q, received: %q for iface %s", expected, out, ifaceIP) 51 } 52 53 killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID) 54 if out, _, err = runCommandWithOutput(killCmd); err != nil { 55 t.Fatalf("failed to kill container: %s, %v", out, err) 56 } 57 deleteAllContainers() 58 59 logDone("network - make sure nat works through the host") 60 }