Docker-DCO-1.1-Signed-off-by: Guillaume J. Charmes <guillaume@charmes.net> (github: creack)
Guillaume J. Charmes authored on 2014/04/17 06:44:141 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,84 @@ |
0 |
+package main |
|
1 |
+ |
|
2 |
+import ( |
|
3 |
+ "encoding/json" |
|
4 |
+ "fmt" |
|
5 |
+ "github.com/dotcloud/docker/runtime" |
|
6 |
+ "net" |
|
7 |
+ "os/exec" |
|
8 |
+ "path/filepath" |
|
9 |
+ "testing" |
|
10 |
+) |
|
11 |
+ |
|
12 |
+func TestNetworkNat(t *testing.T) { |
|
13 |
+ ncPath, err := exec.LookPath("nc") |
|
14 |
+ if err != nil { |
|
15 |
+ t.Skip("Test not running with `make test`. Netcat not found: %s", err) |
|
16 |
+ } |
|
17 |
+ ncPath, err = filepath.EvalSymlinks(ncPath) |
|
18 |
+ if err != nil { |
|
19 |
+ t.Fatalf("Error resolving netcat symlink: %s", err) |
|
20 |
+ } |
|
21 |
+ iface, err := net.InterfaceByName("eth0") |
|
22 |
+ if err != nil { |
|
23 |
+ t.Skip("Test not running with `make test`. Interface eth0 not found: %s", err) |
|
24 |
+ } |
|
25 |
+ |
|
26 |
+ ifaceAddrs, err := iface.Addrs() |
|
27 |
+ if err != nil || len(ifaceAddrs) == 0 { |
|
28 |
+ t.Fatalf("Error retrieving addresses for eth0: %v (%d addresses)", err, len(ifaceAddrs)) |
|
29 |
+ } |
|
30 |
+ |
|
31 |
+ ifaceIp, _, err := net.ParseCIDR(ifaceAddrs[0].String()) |
|
32 |
+ if err != nil { |
|
33 |
+ t.Fatalf("Error retrieving the up for eth0: %s", err) |
|
34 |
+ } |
|
35 |
+ |
|
36 |
+ runCmd := exec.Command(dockerBinary, "run", "-d", |
|
37 |
+ "-v", ncPath+":/bin/nc", |
|
38 |
+ "-v", "/lib/x86_64-linux-gnu/libc.so.6:/lib/libc.so.6", "-v", "/lib/x86_64-linux-gnu/libresolv.so.2:/lib/libresolv.so.2", "-v", "/lib/x86_64-linux-gnu/libbsd.so.0:/lib/libbsd.so.0", "-v", "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2:/lib/ld-linux-x86-64.so.2", |
|
39 |
+ "-p", "8080", "busybox", "/bin/nc", "-lp", "8080") |
|
40 |
+ out, _, err := runCommandWithOutput(runCmd) |
|
41 |
+ errorOut(err, t, fmt.Sprintf("run1 failed with errors: %v (%s)", err, out)) |
|
42 |
+ |
|
43 |
+ cleanedContainerID := stripTrailingCharacters(out) |
|
44 |
+ |
|
45 |
+ inspectCmd := exec.Command(dockerBinary, "inspect", cleanedContainerID) |
|
46 |
+ inspectOut, _, err := runCommandWithOutput(inspectCmd) |
|
47 |
+ errorOut(err, t, fmt.Sprintf("out should've been a container id: %v %v", inspectOut, err)) |
|
48 |
+ |
|
49 |
+ containers := []*runtime.Container{} |
|
50 |
+ if err := json.Unmarshal([]byte(inspectOut), &containers); err != nil { |
|
51 |
+ t.Fatalf("Error inspecting the container: %s", err) |
|
52 |
+ } |
|
53 |
+ if len(containers) != 1 { |
|
54 |
+ t.Fatalf("Unepexted container count. Expected 0, recieved: %d", len(containers)) |
|
55 |
+ } |
|
56 |
+ |
|
57 |
+ port8080, exists := containers[0].NetworkSettings.Ports["8080/tcp"] |
|
58 |
+ if !exists || len(port8080) == 0 { |
|
59 |
+ t.Fatal("Port 8080/tcp not found in NetworkSettings") |
|
60 |
+ } |
|
61 |
+ |
|
62 |
+ runCmd = exec.Command(dockerBinary, "run", |
|
63 |
+ "-v", ncPath+":/bin/nc", |
|
64 |
+ "-v", "/lib/x86_64-linux-gnu/libc.so.6:/lib/libc.so.6", "-v", "/lib/x86_64-linux-gnu/libresolv.so.2:/lib/libresolv.so.2", "-v", "/lib/x86_64-linux-gnu/libbsd.so.0:/lib/libbsd.so.0", "-v", "/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2:/lib/ld-linux-x86-64.so.2", |
|
65 |
+ "-p", "8080", "busybox", "sh", "-c", fmt.Sprintf("echo hello world | /bin/nc -w 30 %s %s", ifaceIp, port8080[0].HostPort)) |
|
66 |
+ out, _, err = runCommandWithOutput(runCmd) |
|
67 |
+ errorOut(err, t, fmt.Sprintf("run2 failed with errors: %v (%s)", err, out)) |
|
68 |
+ |
|
69 |
+ runCmd = exec.Command(dockerBinary, "logs", cleanedContainerID) |
|
70 |
+ out, _, err = runCommandWithOutput(runCmd) |
|
71 |
+ errorOut(err, t, fmt.Sprintf("failed to retrieve logs for container: %v %v", cleanedContainerID, err)) |
|
72 |
+ |
|
73 |
+ if expected := "hello world\n"; out != expected { |
|
74 |
+ t.Fatalf("Unexpected output. Expected: %s, recieved: -->%s<--", expected, out) |
|
75 |
+ } |
|
76 |
+ |
|
77 |
+ killCmd := exec.Command(dockerBinary, "kill", cleanedContainerID) |
|
78 |
+ out, _, err = runCommandWithOutput(killCmd) |
|
79 |
+ errorOut(err, t, fmt.Sprintf("failed to kill container: %v %v", out, err)) |
|
80 |
+ deleteAllContainers() |
|
81 |
+ |
|
82 |
+ logDone("network - make sure nat works through the host") |
|
83 |
+} |