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"
|
|
|
|
"os/exec"
|
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 {
|
|
|
|
runCmd := exec.Command(dockerBinary, "logs", containerID)
|
2014-04-16 17:44:14 -04:00
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-14 15:38:00 -04:00
|
|
|
if err != nil {
|
2015-04-18 12:46:47 -04:00
|
|
|
c.Fatal(out, err)
|
2014-10-14 15:38:00 -04:00
|
|
|
}
|
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) {
|
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
testRequires(c, SameHostDaemon, NativeExecDriver)
|
|
|
|
msg := "it works"
|
|
|
|
startServerContainer(c, msg, 8080)
|
|
|
|
endpoint := getExternalAddress(c)
|
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-t", "--net=container:server", "busybox",
|
|
|
|
"sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String()))
|
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
|
|
|
if err != nil {
|
|
|
|
c.Fatal(out, err)
|
|
|
|
}
|
|
|
|
final := strings.TrimRight(string(out), "\n")
|
|
|
|
if final != msg {
|
|
|
|
c.Fatalf("Expected message %q but received %q", msg, final)
|
|
|
|
}
|
|
|
|
}
|