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"
|
2017-04-11 15:18:30 -04:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
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{
|
2017-04-11 15:18:30 -04:00
|
|
|
"run",
|
|
|
|
"--name",
|
|
|
|
name,
|
2015-06-02 21:05:23 -04:00
|
|
|
"-d",
|
|
|
|
"-p", fmt.Sprintf("%d:%d", port, port),
|
|
|
|
"busybox",
|
|
|
|
"sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port),
|
|
|
|
}
|
2017-04-11 15:18:30 -04:00
|
|
|
cli.DockerCmd(c, cmd...)
|
|
|
|
cli.WaitRun(c, name)
|
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 (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
|
|
|
}
|