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"
|
|
|
|
"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)
|
2021-02-10 05:31:18 -05:00
|
|
|
conn, err := net.Dial("tcp", net.JoinHostPort(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()
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
data, err := io.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()
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
data, err := io.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) {
|
2022-08-30 07:34:30 -04:00
|
|
|
skip.If(t, testEnv.GitHubActions, "FIXME: https://github.com/moby/moby/issues/41561")
|
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()
|
|
|
|
|
2021-01-29 11:28:46 -05: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()
|
|
|
|
|
2021-01-29 11:28:46 -05: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) {
|
|
|
|
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
|
|
|
},
|
2021-01-29 11:28:46 -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
|
|
|
}
|
|
|
|
|
2021-02-10 05:31:18 -05:00
|
|
|
// getExternalAddress() returns the external IP-address from eth0. If eth0 has
|
|
|
|
// multiple IP-addresses, it returns the first IPv4 IP-address; if no IPv4
|
|
|
|
// address is present, it returns the first IP-address found.
|
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
|
|
|
|
2021-02-10 05:31:18 -05:00
|
|
|
if len(ifaceAddrs) > 1 {
|
|
|
|
// Prefer IPv4 address if multiple addresses found, as rootlesskit
|
|
|
|
// does not handle IPv6 currently https://github.com/moby/moby/pull/41908#issuecomment-774200001
|
|
|
|
for _, a := range ifaceAddrs {
|
|
|
|
ifaceIP, _, err := net.ParseCIDR(a.String())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
if ifaceIP.To4() != nil {
|
|
|
|
return ifaceIP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|