2018-03-01 17:51:11 -05:00
|
|
|
package container // import "github.com/docker/docker/integration/container"
|
2018-02-02 17:52:18 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-02-12 18:08:25 -05:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2018-04-17 04:22:04 -04:00
|
|
|
"github.com/docker/docker/internal/test/request"
|
2018-02-02 17:52:18 -05:00
|
|
|
"github.com/docker/go-connections/nat"
|
2018-03-13 15:28:34 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2018-02-02 17:52:18 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/poll"
|
|
|
|
"github.com/gotestyourself/gotestyourself/skip"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetworkNat(t *testing.T) {
|
2018-03-02 08:45:14 -05:00
|
|
|
skip.If(t, testEnv.IsRemoteDaemon())
|
2018-02-02 17:52:18 -05:00
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
msg := "it works"
|
|
|
|
startServerContainer(t, msg, 8080)
|
|
|
|
|
|
|
|
endpoint := getExternalAddress(t)
|
|
|
|
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", endpoint.String(), 8080))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-02 17:52:18 -05:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(conn)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(msg, strings.TrimSpace(string(data))))
|
2018-02-02 17:52:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkLocalhostTCPNat(t *testing.T) {
|
2018-03-02 08:45:14 -05:00
|
|
|
skip.If(t, testEnv.IsRemoteDaemon())
|
2018-02-02 17:52:18 -05:00
|
|
|
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
msg := "hi yall"
|
|
|
|
startServerContainer(t, msg, 8081)
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", "localhost:8081")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-02 17:52:18 -05:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(conn)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Equal(msg, strings.TrimSpace(string(data))))
|
2018-02-02 17:52:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkLoopbackNat(t *testing.T) {
|
2018-03-02 08:45:14 -05:00
|
|
|
skip.If(t, testEnv.IsRemoteDaemon())
|
2018-02-02 17:52:18 -05:00
|
|
|
|
2018-03-19 10:55:50 -04:00
|
|
|
defer setupTest(t)()
|
|
|
|
|
2018-02-02 17:52:18 -05:00
|
|
|
msg := "it works"
|
2018-03-20 17:09:18 -04:00
|
|
|
serverContainerID := startServerContainer(t, msg, 8080)
|
2018-02-02 17:52:18 -05:00
|
|
|
|
|
|
|
endpoint := getExternalAddress(t)
|
|
|
|
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-03-20 17:09:18 -04:00
|
|
|
cID := container.Run(t, ctx, client, container.WithCmd("sh", "-c", fmt.Sprintf("stty raw && nc -w 5 %s 8080", endpoint.String())), container.WithTty(true), container.WithNetworkMode("container:"+serverContainerID))
|
2018-02-02 17:52:18 -05:00
|
|
|
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
|
2018-02-02 17:52:18 -05:00
|
|
|
|
2018-02-12 18:08:25 -05:00
|
|
|
body, err := client.ContainerLogs(ctx, cID, types.ContainerLogsOptions{
|
2018-02-02 17:52:18 -05:00
|
|
|
ShowStdout: true,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-02 17:52:18 -05:00
|
|
|
defer body.Close()
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
_, err = io.Copy(&b, body)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-02 17:52:18 -05:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.Equal(msg, strings.TrimSpace(b.String())))
|
2018-02-02 17:52:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func startServerContainer(t *testing.T, msg string, port int) string {
|
|
|
|
client := request.NewAPIClient(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2018-03-20 17:09:18 -04:00
|
|
|
cID := container.Run(t, ctx, client, container.WithName("server-"+t.Name()), container.WithCmd("sh", "-c", fmt.Sprintf("echo %q | nc -lp %d", msg, port)), container.WithExposedPorts(fmt.Sprintf("%d/tcp", port)), func(c *container.TestContainerConfig) {
|
2018-02-12 18:08:25 -05:00
|
|
|
c.HostConfig.PortBindings = nat.PortMap{
|
|
|
|
nat.Port(fmt.Sprintf("%d/tcp", port)): []nat.PortBinding{
|
|
|
|
{
|
|
|
|
HostPort: fmt.Sprintf("%d", port),
|
2018-02-02 17:52:18 -05:00
|
|
|
},
|
|
|
|
},
|
2018-02-12 18:08:25 -05:00
|
|
|
}
|
|
|
|
})
|
2018-02-02 17:52:18 -05:00
|
|
|
|
2018-02-22 05:30:51 -05:00
|
|
|
poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
|
2018-02-02 17:52:18 -05:00
|
|
|
|
2018-02-12 18:08:25 -05:00
|
|
|
return cID
|
2018-02-02 17:52:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func getExternalAddress(t *testing.T) net.IP {
|
|
|
|
iface, err := net.InterfaceByName("eth0")
|
|
|
|
skip.If(t, err != nil, "Test not running with `make test-integration`. Interface eth0 not found: %s", err)
|
|
|
|
|
|
|
|
ifaceAddrs, err := iface.Addrs()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, 0 != len(ifaceAddrs))
|
2018-02-02 17:52:18 -05:00
|
|
|
|
|
|
|
ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String())
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2018-02-02 17:52:18 -05:00
|
|
|
|
|
|
|
return ifaceIP
|
|
|
|
}
|