forked from docker/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_cli_nat_test.go
94 lines (78 loc) · 2.46 KB
/
docker_cli_nat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"io/ioutil"
"net"
"strings"
"github.com/docker/docker/pkg/integration/checker"
"github.com/go-check/check"
)
func startServerContainer(c *check.C, msg string, port int) string {
name := "server"
cmd := []string{
"-d",
"-p", fmt.Sprintf("%d:%d", port, port),
"busybox",
"sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port),
}
c.Assert(waitForContainer(name, cmd...), check.IsNil)
return name
}
func getExternalAddress(c *check.C) net.IP {
iface, err := net.InterfaceByName("eth0")
if err != nil {
c.Skip(fmt.Sprintf("Test not running with `make test`. Interface eth0 not found: %v", err))
}
ifaceAddrs, err := iface.Addrs()
c.Assert(err, check.IsNil)
c.Assert(ifaceAddrs, checker.Not(checker.HasLen), 0)
ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String())
c.Assert(err, check.IsNil)
return ifaceIP
}
func getContainerLogs(c *check.C, containerID string) string {
out, _ := dockerCmd(c, "logs", containerID)
return strings.Trim(out, "\r\n")
}
func getContainerStatus(c *check.C, containerID string) string {
out, err := inspectField(containerID, "State.Running")
c.Assert(err, check.IsNil)
return out
}
func (s *DockerSuite) TestNetworkNat(c *check.C) {
testRequires(c, DaemonIsLinux, SameHostDaemon)
msg := "it works"
startServerContainer(c, msg, 8080)
endpoint := getExternalAddress(c)
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", endpoint.String(), 8080))
c.Assert(err, check.IsNil)
data, err := ioutil.ReadAll(conn)
conn.Close()
c.Assert(err, check.IsNil)
final := strings.TrimRight(string(data), "\n")
c.Assert(final, checker.Equals, msg)
}
func (s *DockerSuite) TestNetworkLocalhostTCPNat(c *check.C) {
testRequires(c, DaemonIsLinux, SameHostDaemon)
var (
msg = "hi yall"
)
startServerContainer(c, msg, 8081)
conn, err := net.Dial("tcp", "localhost:8081")
c.Assert(err, check.IsNil)
data, err := ioutil.ReadAll(conn)
conn.Close()
c.Assert(err, check.IsNil)
final := strings.TrimRight(string(data), "\n")
c.Assert(final, checker.Equals, msg)
}
func (s *DockerSuite) TestNetworkLoopbackNat(c *check.C) {
testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace)
msg := "it works"
startServerContainer(c, msg, 8080)
endpoint := getExternalAddress(c)
out, _ := dockerCmd(c, "run", "-t", "--net=container:server", "busybox",
"sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String()))
final := strings.TrimRight(string(out), "\n")
c.Assert(final, checker.Equals, msg)
}