github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/integration-cli/docker_cli_nat_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"os/exec"
     7  	"strings"
     8  
     9  	"github.com/go-check/check"
    10  )
    11  
    12  func (s *DockerSuite) TestNetworkNat(c *check.C) {
    13  	testRequires(c, SameHostDaemon, NativeExecDriver)
    14  
    15  	iface, err := net.InterfaceByName("eth0")
    16  	if err != nil {
    17  		c.Skip(fmt.Sprintf("Test not running with `make test`. Interface eth0 not found: %v", err))
    18  	}
    19  
    20  	ifaceAddrs, err := iface.Addrs()
    21  	if err != nil || len(ifaceAddrs) == 0 {
    22  		c.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  		c.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  		c.Fatal(out, err)
    34  	}
    35  
    36  	cleanedContainerID := strings.TrimSpace(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  		c.Fatal(out, err)
    42  	}
    43  
    44  	runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
    45  	out, _, err = runCommandWithOutput(runCmd)
    46  	if err != nil {
    47  		c.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  		c.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  		c.Fatalf("failed to kill container: %s, %v", out, err)
    59  	}
    60  
    61  }