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-02-02 17:52:18 -05:00
|
|
|
"github.com/docker/go-connections/nat"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/poll"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-02-02 17:52:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNetworkNat(t *testing.T) {
|
2019-01-02 08:16:25 -05:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "FIXME")
|
2019-01-03 06:19:15 -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) {
|
2019-01-03 06:19:15 -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) {
|
2019-01-02 08:16:25 -05:00
|
|
|
skip.If(t, testEnv.OSType == "windows", "FIXME")
|
2019-01-03 06:19:15 -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)
|
|
|
|
|
2019-01-02 08:16:25 -05:00
|
|
|
client := testEnv.APIClient()
|
2018-02-02 17:52:18 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-06-29 23:06:03 -04:00
|
|
|
cID := container.Run(ctx, t, client, container.WithCmd("sh", "-c", fmt.Sprintf("stty raw && nc -w 1 %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 {
|
2019-01-02 08:16:25 -05:00
|
|
|
t.Helper()
|
|
|
|
client := testEnv.APIClient()
|
2018-02-02 17:52:18 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-06-06 07:15:31 -04:00
|
|
|
cID := container.Run(ctx, t, 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 {
|
2019-01-02 08:16:25 -05:00
|
|
|
t.Helper()
|
2018-02-02 17:52:18 -05:00
|
|
|
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
|
|
|
|
}
|