2014-04-16 17:44:14 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os/exec"
|
2014-09-01 19:28:02 -04:00
|
|
|
"strings"
|
2014-04-16 17:44:14 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetworkNat(t *testing.T) {
|
2015-02-25 23:16:44 -05:00
|
|
|
testRequires(t, SameHostDaemon, NativeExecDriver)
|
2015-02-20 01:56:02 -05:00
|
|
|
defer deleteAllContainers()
|
2015-02-13 17:53:39 -05:00
|
|
|
|
2014-04-16 17:44:14 -04:00
|
|
|
iface, err := net.InterfaceByName("eth0")
|
|
|
|
if err != nil {
|
2014-11-19 16:24:16 -05:00
|
|
|
t.Skipf("Test not running with `make test`. Interface eth0 not found: %s", err)
|
2014-04-16 17:44:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ifaceAddrs, err := iface.Addrs()
|
|
|
|
if err != nil || len(ifaceAddrs) == 0 {
|
|
|
|
t.Fatalf("Error retrieving addresses for eth0: %v (%d addresses)", err, len(ifaceAddrs))
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
t.Fatalf("Error retrieving the up for eth0: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-01 19:28:02 -04:00
|
|
|
runCmd := exec.Command(dockerBinary, "run", "-dt", "-p", "8080:8080", "busybox", "nc", "-lp", "8080")
|
2014-04-16 17:44:14 -04:00
|
|
|
out, _, err := runCommandWithOutput(runCmd)
|
2014-10-14 15:38:00 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-04-16 17:44:14 -04:00
|
|
|
|
2015-04-06 09:21:18 -04:00
|
|
|
cleanedContainerID := strings.TrimSpace(out)
|
2014-04-16 17:44:14 -04:00
|
|
|
|
2014-10-06 10:26:55 -04:00
|
|
|
runCmd = exec.Command(dockerBinary, "run", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | nc -w 30 %s 8080", ifaceIP))
|
2014-04-16 17:44:14 -04:00
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:38:00 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(out, err)
|
|
|
|
}
|
2014-04-16 17:44:14 -04:00
|
|
|
|
|
|
|
runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID)
|
|
|
|
out, _, err = runCommandWithOutput(runCmd)
|
2014-10-14 15:38:00 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to retrieve logs for container: %s, %v", out, err)
|
|
|
|
}
|
|
|
|
|
2014-09-01 19:28:02 -04:00
|
|
|
out = strings.Trim(out, "\r\n")
|
2014-04-16 17:44:14 -04:00
|
|
|
|
2014-09-01 19:28:02 -04:00
|
|
|
if expected := "hello world"; out != expected {
|
2014-10-06 10:26:55 -04:00
|
|
|
t.Fatalf("Unexpected output. Expected: %q, received: %q for iface %s", expected, out, ifaceIP)
|
2014-04-16 17:44:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID)
|
2014-10-14 15:38:00 -04:00
|
|
|
if out, _, err = runCommandWithOutput(killCmd); err != nil {
|
|
|
|
t.Fatalf("failed to kill container: %s, %v", out, err)
|
|
|
|
}
|
2014-04-16 17:44:14 -04:00
|
|
|
|
|
|
|
logDone("network - make sure nat works through the host")
|
|
|
|
}
|