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"
|
2014-09-01 19:28:02 -04:00
|
|
|
"strings"
|
2015-04-18 12:46:47 -04:00
|
|
|
|
2016-12-30 12:23:00 -05:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
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),
|
|
|
|
}
|
2015-10-14 08:31:37 -04:00
|
|
|
c.Assert(waitForContainer(name, cmd...), check.IsNil)
|
2014-11-10 19:19:16 -05:00
|
|
|
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()
|
2015-10-14 08:31:37 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(ifaceAddrs, checker.Not(checker.HasLen), 0)
|
2014-04-16 17:44:14 -04:00
|
|
|
|
2014-10-06 10:26:55 -04:00
|
|
|
ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String())
|
2015-10-14 08:31:37 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
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 {
|
2015-07-20 02:44:22 -04:00
|
|
|
out, _ := dockerCmd(c, "logs", containerID)
|
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 {
|
2016-01-28 09:19:25 -05:00
|
|
|
out := inspectField(c, containerID, "State.Running")
|
2015-05-17 22:06:13 -04:00
|
|
|
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) {
|
2015-11-04 19:40:58 -05:00
|
|
|
testRequires(c, DaemonIsLinux, SameHostDaemon)
|
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))
|
2015-10-14 08:31:37 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2015-06-02 21:05:23 -04:00
|
|
|
data, err := ioutil.ReadAll(conn)
|
|
|
|
conn.Close()
|
2015-10-14 08:31:37 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2015-06-02 21:05:23 -04:00
|
|
|
final := strings.TrimRight(string(data), "\n")
|
2015-10-14 08:31:37 -04:00
|
|
|
c.Assert(final, checker.Equals, msg)
|
2014-11-10 19:19:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestNetworkLocalhostTCPNat(c *check.C) {
|
2015-11-04 19:40:58 -05:00
|
|
|
testRequires(c, DaemonIsLinux, SameHostDaemon)
|
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")
|
2015-10-14 08:31:37 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2015-06-02 21:05:23 -04:00
|
|
|
data, err := ioutil.ReadAll(conn)
|
|
|
|
conn.Close()
|
2015-10-14 08:31:37 -04:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2015-06-02 21:05:23 -04:00
|
|
|
final := strings.TrimRight(string(data), "\n")
|
2015-10-14 08:31:37 -04:00
|
|
|
c.Assert(final, checker.Equals, msg)
|
2014-04-16 17:44:14 -04:00
|
|
|
}
|
2015-06-11 21:04:03 -04:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestNetworkLoopbackNat(c *check.C) {
|
2015-11-04 19:40:58 -05:00
|
|
|
testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace)
|
2015-06-11 21:04:03 -04:00
|
|
|
msg := "it works"
|
|
|
|
startServerContainer(c, msg, 8080)
|
|
|
|
endpoint := getExternalAddress(c)
|
2015-07-20 02:44:22 -04:00
|
|
|
out, _ := dockerCmd(c, "run", "-t", "--net=container:server", "busybox",
|
2015-06-11 21:04:03 -04:00
|
|
|
"sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String()))
|
|
|
|
final := strings.TrimRight(string(out), "\n")
|
2015-10-14 08:31:37 -04:00
|
|
|
c.Assert(final, checker.Equals, msg)
|
2015-06-11 21:04:03 -04:00
|
|
|
}
|