2014-04-16 21:44:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-03 01:05:23 +00:00
|
|
|
"io/ioutil"
|
2014-04-16 21:44:14 +00:00
|
|
|
"net"
|
2014-09-01 23:28:02 +00:00
|
|
|
"strings"
|
2015-04-18 16:46:47 +00:00
|
|
|
|
2016-12-30 17:23:00 +00:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2017-04-11 19:18:30 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2015-04-18 16:46:47 +00:00
|
|
|
"github.com/go-check/check"
|
2014-04-16 21:44:14 +00:00
|
|
|
)
|
|
|
|
|
2015-06-03 01:05:23 +00:00
|
|
|
func startServerContainer(c *check.C, msg string, port int) string {
|
2014-11-11 00:19:16 +00:00
|
|
|
name := "server"
|
2015-06-03 01:05:23 +00:00
|
|
|
cmd := []string{
|
2017-04-11 19:18:30 +00:00
|
|
|
"run",
|
|
|
|
"--name",
|
|
|
|
name,
|
2015-06-03 01:05:23 +00:00
|
|
|
"-d",
|
|
|
|
"-p", fmt.Sprintf("%d:%d", port, port),
|
|
|
|
"busybox",
|
|
|
|
"sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port),
|
|
|
|
}
|
2017-04-11 19:18:30 +00:00
|
|
|
cli.DockerCmd(c, cmd...)
|
|
|
|
cli.WaitRun(c, name)
|
2014-11-11 00:19:16 +00:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
func getExternalAddress(c *check.C) net.IP {
|
2014-04-16 21:44:14 +00:00
|
|
|
iface, err := net.InterfaceByName("eth0")
|
|
|
|
if err != nil {
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Skip(fmt.Sprintf("Test not running with `make test`. Interface eth0 not found: %v", err))
|
2014-04-16 21:44:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ifaceAddrs, err := iface.Addrs()
|
2015-10-14 12:31:37 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
c.Assert(ifaceAddrs, checker.Not(checker.HasLen), 0)
|
2014-04-16 21:44:14 +00:00
|
|
|
|
2014-10-06 14:26:55 +00:00
|
|
|
ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String())
|
2015-10-14 12:31:37 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
2014-04-16 21:44:14 +00:00
|
|
|
|
2014-11-11 00:19:16 +00:00
|
|
|
return ifaceIP
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestNetworkNat(c *check.C) {
|
2015-11-05 00:40:58 +00:00
|
|
|
testRequires(c, DaemonIsLinux, SameHostDaemon)
|
2015-06-03 01:05:23 +00:00
|
|
|
msg := "it works"
|
|
|
|
startServerContainer(c, msg, 8080)
|
2014-11-11 00:19:16 +00:00
|
|
|
endpoint := getExternalAddress(c)
|
2015-06-03 01:05:23 +00:00
|
|
|
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", endpoint.String(), 8080))
|
2015-10-14 12:31:37 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2015-06-03 01:05:23 +00:00
|
|
|
data, err := ioutil.ReadAll(conn)
|
|
|
|
conn.Close()
|
2015-10-14 12:31:37 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2015-06-03 01:05:23 +00:00
|
|
|
final := strings.TrimRight(string(data), "\n")
|
2015-10-14 12:31:37 +00:00
|
|
|
c.Assert(final, checker.Equals, msg)
|
2014-11-11 00:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestNetworkLocalhostTCPNat(c *check.C) {
|
2015-11-05 00:40:58 +00:00
|
|
|
testRequires(c, DaemonIsLinux, SameHostDaemon)
|
2015-06-03 01:05:23 +00:00
|
|
|
var (
|
|
|
|
msg = "hi yall"
|
|
|
|
)
|
|
|
|
startServerContainer(c, msg, 8081)
|
2014-11-11 00:19:16 +00:00
|
|
|
conn, err := net.Dial("tcp", "localhost:8081")
|
2015-10-14 12:31:37 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2015-06-03 01:05:23 +00:00
|
|
|
data, err := ioutil.ReadAll(conn)
|
|
|
|
conn.Close()
|
2015-10-14 12:31:37 +00:00
|
|
|
c.Assert(err, check.IsNil)
|
|
|
|
|
2015-06-03 01:05:23 +00:00
|
|
|
final := strings.TrimRight(string(data), "\n")
|
2015-10-14 12:31:37 +00:00
|
|
|
c.Assert(final, checker.Equals, msg)
|
2014-04-16 21:44:14 +00:00
|
|
|
}
|
2015-06-12 01:04:03 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestNetworkLoopbackNat(c *check.C) {
|
2015-11-05 00:40:58 +00:00
|
|
|
testRequires(c, DaemonIsLinux, SameHostDaemon, NotUserNamespace)
|
2015-06-12 01:04:03 +00:00
|
|
|
msg := "it works"
|
|
|
|
startServerContainer(c, msg, 8080)
|
|
|
|
endpoint := getExternalAddress(c)
|
2015-07-20 06:44:22 +00:00
|
|
|
out, _ := dockerCmd(c, "run", "-t", "--net=container:server", "busybox",
|
2015-06-12 01:04:03 +00:00
|
|
|
"sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String()))
|
|
|
|
final := strings.TrimRight(string(out), "\n")
|
2015-10-14 12:31:37 +00:00
|
|
|
c.Assert(final, checker.Equals, msg)
|
2015-06-12 01:04:03 +00:00
|
|
|
}
|