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) {
|
|
|
|
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
|
|
|
|
|
|
|
cleanedContainerID := stripTrailingCharacters(out)
|
|
|
|
|
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
|
|
|
deleteAllContainers()
|
|
|
|
|
|
|
|
logDone("network - make sure nat works through the host")
|
|
|
|
}
|