2014-04-16 21:44:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os/exec"
|
2014-11-11 00:19:16 +00:00
|
|
|
"strconv"
|
2014-09-01 23:28:02 +00:00
|
|
|
"strings"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
|
|
|
"github.com/go-check/check"
|
2014-04-16 21:44:14 +00:00
|
|
|
)
|
|
|
|
|
2014-11-11 00:19:16 +00:00
|
|
|
func startServerContainer(c *check.C, proto string, port int) string {
|
|
|
|
cmd := []string{"-d", "-p", fmt.Sprintf("%d:%d", port, port), "busybox", "nc", "-lp", strconv.Itoa(port)}
|
|
|
|
if proto == "udp" {
|
|
|
|
cmd = append(cmd, "-u")
|
|
|
|
}
|
2015-02-13 22:53:39 +00:00
|
|
|
|
2014-11-11 00:19:16 +00:00
|
|
|
name := "server"
|
|
|
|
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 21:44:14 +00:00
|
|
|
iface, err := net.InterfaceByName("eth0")
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Skip(fmt.Sprintf("Test not running with `make test`. Interface eth0 not found: %v", err))
|
2014-04-16 21:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ifaceAddrs, err := iface.Addrs()
|
|
|
|
if err != nil || len(ifaceAddrs) == 0 {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Error retrieving addresses for eth0: %v (%d addresses)", err, len(ifaceAddrs))
|
2014-04-16 21:44:14 +00:00
|
|
|
}
|
|
|
|
|
2014-10-06 14:26:55 +00:00
|
|
|
ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String())
|
2014-04-16 21:44:14 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("Error retrieving the up for eth0: %s", err)
|
2014-04-16 21:44:14 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 00:19:16 +00:00
|
|
|
return ifaceIP
|
|
|
|
}
|
|
|
|
|
|
|
|
func getContainerLogs(c *check.C, containerID string) string {
|
|
|
|
runCmd := exec.Command(dockerBinary, "logs", containerID)
|
2014-04-16 21:44:14 +00:00
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-14 19:38:00 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 19:38:00 +00:00
|
|
|
}
|
2014-11-11 00:19:16 +00:00
|
|
|
return strings.Trim(out, "\r\n")
|
|
|
|
}
|
2014-04-16 21:44:14 +00:00
|
|
|
|
2014-11-11 00:19:16 +00:00
|
|
|
func getContainerStatus(c *check.C, containerID string) string {
|
|
|
|
runCmd := exec.Command(dockerBinary, "inspect", "-f", "{{.State.Running}}", containerID)
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-14 19:38:00 +00:00
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 19:38:00 +00:00
|
|
|
}
|
2014-11-11 00:19:16 +00:00
|
|
|
return strings.Trim(out, "\r\n")
|
|
|
|
}
|
2014-04-16 21:44:14 +00:00
|
|
|
|
2014-11-11 00:19:16 +00:00
|
|
|
func (s *DockerSuite) TestNetworkNat(c *check.C) {
|
|
|
|
testRequires(c, SameHostDaemon, NativeExecDriver)
|
|
|
|
defer deleteAllContainers()
|
2014-10-14 19:38:00 +00:00
|
|
|
|
2014-11-11 00:19:16 +00:00
|
|
|
srv := startServerContainer(c, "tcp", 8080)
|
2014-04-16 21:44:14 +00:00
|
|
|
|
2014-11-11 00:19:16 +00:00
|
|
|
// Spawn a new container which connects to the server through the
|
|
|
|
// interface address.
|
|
|
|
endpoint := getExternalAddress(c)
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | nc -w 30 %s 8080", endpoint))
|
|
|
|
if out, _, err := runCommandWithOutput(runCmd); err != nil {
|
|
|
|
c.Fatalf("Failed to connect to server: %v (output: %q)", err, string(out))
|
2014-04-16 21:44:14 +00:00
|
|
|
}
|
|
|
|
|
2014-11-11 00:19:16 +00:00
|
|
|
result := getContainerLogs(c, srv)
|
|
|
|
if expected := "hello world"; result != expected {
|
|
|
|
c.Fatalf("Unexpected output. Expected: %q, received: %q", expected, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestNetworkLocalhostTCPNat(c *check.C) {
|
|
|
|
testRequires(c, SameHostDaemon, NativeExecDriver)
|
|
|
|
defer deleteAllContainers()
|
|
|
|
|
|
|
|
srv := startServerContainer(c, "tcp", 8081)
|
|
|
|
|
|
|
|
// Attempt to connect from the host to the listening container.
|
|
|
|
conn, err := net.Dial("tcp", "localhost:8081")
|
|
|
|
if err != nil {
|
|
|
|
c.Fatalf("Failed to connect to container (%v)", err)
|
|
|
|
}
|
|
|
|
if _, err := conn.Write([]byte("hello world\n")); err != nil {
|
|
|
|
c.Fatal(err)
|
2014-10-14 19:38:00 +00:00
|
|
|
}
|
2014-11-11 00:19:16 +00:00
|
|
|
conn.Close()
|
2014-04-16 21:44:14 +00:00
|
|
|
|
2014-11-11 00:19:16 +00:00
|
|
|
result := getContainerLogs(c, srv)
|
|
|
|
if expected := "hello world"; result != expected {
|
|
|
|
c.Fatalf("Unexpected output. Expected: %q, received: %q", expected, result)
|
|
|
|
}
|
2014-04-16 21:44:14 +00:00
|
|
|
}
|