2014-04-16 17:44:14 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-02 21:05:23 -04:00
|
|
|
"io/ioutil"
|
2014-04-16 17:44:14 -04:00
|
|
|
"net"
|
2014-09-01 19:28:02 -04:00
|
|
|
"strings"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2014-04-16 17:44:14 -04:00
|
|
|
)
|
|
|
|
|
2015-06-02 21:05:23 -04:00
|
|
|
func startServerContainer(c *check.C, msg string, port int) string {
|
2014-11-10 19:19:16 -05:00
|
|
|
name := "server"
|
2015-06-02 21:05:23 -04:00
|
|
|
cmd := []string{
|
|
|
|
"-d",
|
|
|
|
"-p", fmt.Sprintf("%d:%d", port, port),
|
|
|
|
"busybox",
|
|
|
|
"sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port),
|
|
|
|
}
|
2014-11-10 19:19:16 -05:00
|
|
|
if err := waitForContainer(name, cmd...); err != nil {
|
|
|
|
c.Fatalf("Failed to launch server container: %v", err)
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func getExternalAddress(c *check.C) net.IP {
|
2014-04-16 17:44:14 -04:00
|
|
|
iface, err := net.InterfaceByName("eth0")
|
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Skip(fmt.Sprintf("Test not running with `make test`. Interface eth0 not found: %v", err))
|
2014-04-16 17:44:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ifaceAddrs, err := iface.Addrs()
|
|
|
|
if err != nil || len(ifaceAddrs) == 0 {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Error retrieving addresses for eth0: %v (%d addresses)", err, len(ifaceAddrs))
|
2014-04-16 17:44:14 -04:00
|
|
|
}
|
|
|
|
|
2014-10-06 10:26:55 -04:00
|
|
|
ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String())
|
2014-04-16 17:44:14 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatalf("Error retrieving the up for eth0: %s", err)
|
2014-04-16 17:44:14 -04:00
|
|
|
}
|
|
|
|
|
2014-11-10 19:19:16 -05:00
|
|
|
return ifaceIP
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContainerLogs(c *check.C, containerID string) string {
|
2015-07-20 02:44:22 -04:00
|
|
|
out, _ := dockerCmd(c, "logs", containerID)
|
2014-11-10 19:19:16 -05:00
|
|
|
return strings.Trim(out, "\r\n")
|
|
|
|
}
|
2014-04-16 17:44:14 -04:00
|
|
|
|
2014-11-10 19:19:16 -05:00
|
|
|
func getContainerStatus(c *check.C, containerID string) string {
|
2015-05-17 22:06:13 -04:00
|
|
|
out, err := inspectField(containerID, "State.Running")
|
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
return out
|
2014-11-10 19:19:16 -05:00
|
|
|
}
|
2014-04-16 17:44:14 -04:00
|
|
|
|
2014-11-10 19:19:16 -05:00
|
|
|
func (s *DockerSuite) TestNetworkNat(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-11-10 19:19:16 -05:00
|
|
|
testRequires(c, SameHostDaemon, NativeExecDriver)
|
2015-06-02 21:05:23 -04:00
|
|
|
msg := "it works"
|
|
|
|
startServerContainer(c, msg, 8080)
|
2014-11-10 19:19:16 -05:00
|
|
|
endpoint := getExternalAddress(c)
|
2015-06-02 21:05:23 -04:00
|
|
|
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", endpoint.String(), 8080))
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Failed to connect to container (%v)", err)
|
2014-04-16 17:44:14 -04:00
|
|
|
}
|
2015-06-02 21:05:23 -04:00
|
|
|
data, err := ioutil.ReadAll(conn)
|
|
|
|
conn.Close()
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(err)
|
|
|
|
}
|
|
|
|
final := strings.TrimRight(string(data), "\n")
|
|
|
|
if final != msg {
|
|
|
|
c.Fatalf("Expected message %q but received %q", msg, final)
|
2014-11-10 19:19:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestNetworkLocalhostTCPNat(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2014-11-10 19:19:16 -05:00
|
|
|
testRequires(c, SameHostDaemon, NativeExecDriver)
|
2015-06-02 21:05:23 -04:00
|
|
|
var (
|
|
|
|
msg = "hi yall"
|
|
|
|
)
|
|
|
|
startServerContainer(c, msg, 8081)
|
2014-11-10 19:19:16 -05:00
|
|
|
conn, err := net.Dial("tcp", "localhost:8081")
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Failed to connect to container (%v)", err)
|
|
|
|
}
|
2015-06-02 21:05:23 -04:00
|
|
|
data, err := ioutil.ReadAll(conn)
|
|
|
|
conn.Close()
|
|
|
|
if err != nil {
|
2014-11-10 19:19:16 -05:00
|
|
|
c.Fatal(err)
|
2014-10-14 15:38:00 -04:00
|
|
|
}
|
2015-06-02 21:05:23 -04:00
|
|
|
final := strings.TrimRight(string(data), "\n")
|
|
|
|
if final != msg {
|
|
|
|
c.Fatalf("Expected message %q but received %q", msg, final)
|
2014-11-10 19:19:16 -05:00
|
|
|
}
|
2014-04-16 17:44:14 -04:00
|
|
|
}
|
2015-06-11 21:04:03 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestNetworkLoopbackNat(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-09-18 13:41:12 -04:00
|
|
|
testRequires(c, SameHostDaemon, NativeExecDriver, NotUserNamespace)
|
2015-06-11 21:04:03 -04:00
|
|
|
msg := "it works"
|
|
|
|
startServerContainer(c, msg, 8080)
|
|
|
|
endpoint := getExternalAddress(c)
|
2015-07-20 02:44:22 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-t", "--net=container:server", "busybox",
|
2015-06-11 21:04:03 -04:00
|
|
|
"sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String()))
|
|
|
|
final := strings.TrimRight(string(out), "\n")
|
|
|
|
if final != msg {
|
|
|
|
c.Fatalf("Expected message %q but received %q", msg, final)
|
|
|
|
}
|
|
|
|
}
|