github.com/damirazo/docker@v1.9.0/integration-cli/docker_cli_nat_test.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net"
     7  	"strings"
     8  
     9  	"github.com/go-check/check"
    10  )
    11  
    12  func startServerContainer(c *check.C, msg string, port int) string {
    13  	name := "server"
    14  	cmd := []string{
    15  		"-d",
    16  		"-p", fmt.Sprintf("%d:%d", port, port),
    17  		"busybox",
    18  		"sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port),
    19  	}
    20  	if err := waitForContainer(name, cmd...); err != nil {
    21  		c.Fatalf("Failed to launch server container: %v", err)
    22  	}
    23  	return name
    24  }
    25  
    26  func getExternalAddress(c *check.C) net.IP {
    27  	iface, err := net.InterfaceByName("eth0")
    28  	if err != nil {
    29  		c.Skip(fmt.Sprintf("Test not running with `make test`. Interface eth0 not found: %v", err))
    30  	}
    31  
    32  	ifaceAddrs, err := iface.Addrs()
    33  	if err != nil || len(ifaceAddrs) == 0 {
    34  		c.Fatalf("Error retrieving addresses for eth0: %v (%d addresses)", err, len(ifaceAddrs))
    35  	}
    36  
    37  	ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String())
    38  	if err != nil {
    39  		c.Fatalf("Error retrieving the up for eth0: %s", err)
    40  	}
    41  
    42  	return ifaceIP
    43  }
    44  
    45  func getContainerLogs(c *check.C, containerID string) string {
    46  	out, _ := dockerCmd(c, "logs", containerID)
    47  	return strings.Trim(out, "\r\n")
    48  }
    49  
    50  func getContainerStatus(c *check.C, containerID string) string {
    51  	out, err := inspectField(containerID, "State.Running")
    52  	c.Assert(err, check.IsNil)
    53  	return out
    54  }
    55  
    56  func (s *DockerSuite) TestNetworkNat(c *check.C) {
    57  	testRequires(c, DaemonIsLinux)
    58  	testRequires(c, SameHostDaemon, NativeExecDriver)
    59  	msg := "it works"
    60  	startServerContainer(c, msg, 8080)
    61  	endpoint := getExternalAddress(c)
    62  	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", endpoint.String(), 8080))
    63  	if err != nil {
    64  		c.Fatalf("Failed to connect to container (%v)", err)
    65  	}
    66  	data, err := ioutil.ReadAll(conn)
    67  	conn.Close()
    68  	if err != nil {
    69  		c.Fatal(err)
    70  	}
    71  	final := strings.TrimRight(string(data), "\n")
    72  	if final != msg {
    73  		c.Fatalf("Expected message %q but received %q", msg, final)
    74  	}
    75  }
    76  
    77  func (s *DockerSuite) TestNetworkLocalhostTCPNat(c *check.C) {
    78  	testRequires(c, DaemonIsLinux)
    79  	testRequires(c, SameHostDaemon, NativeExecDriver)
    80  	var (
    81  		msg = "hi yall"
    82  	)
    83  	startServerContainer(c, msg, 8081)
    84  	conn, err := net.Dial("tcp", "localhost:8081")
    85  	if err != nil {
    86  		c.Fatalf("Failed to connect to container (%v)", err)
    87  	}
    88  	data, err := ioutil.ReadAll(conn)
    89  	conn.Close()
    90  	if err != nil {
    91  		c.Fatal(err)
    92  	}
    93  	final := strings.TrimRight(string(data), "\n")
    94  	if final != msg {
    95  		c.Fatalf("Expected message %q but received %q", msg, final)
    96  	}
    97  }
    98  
    99  func (s *DockerSuite) TestNetworkLoopbackNat(c *check.C) {
   100  	testRequires(c, DaemonIsLinux)
   101  	testRequires(c, SameHostDaemon, NativeExecDriver, NotUserNamespace)
   102  	msg := "it works"
   103  	startServerContainer(c, msg, 8080)
   104  	endpoint := getExternalAddress(c)
   105  	out, _ := dockerCmd(c, "run", "-t", "--net=container:server", "busybox",
   106  		"sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String()))
   107  	final := strings.TrimRight(string(out), "\n")
   108  	if final != msg {
   109  		c.Fatalf("Expected message %q but received %q", msg, final)
   110  	}
   111  }