From 86d98f5711ea4350dce6398339df5906eeaf62fe Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 10 Feb 2021 11:31:18 +0100 Subject: [PATCH] integration: update getExternalAddress to prefer IPv4 Rootlesskit doesn't currently handle IPv6 addresses, causing TestNetworkLoopbackNat and TestNetworkNat to fail; Error starting userland proxy: error while calling PortManager.AddPort(): listen tcp: address :::8080: too many colons in address This patch: - Updates `getExternalAddress()` to pick IPv4 address if both IPv6 and IPv4 are found - Update TestNetworkNat to net.JoinHostPort(), so that square brackets are used for IPv6 addresses (e.g. `[::]:8080`) Signed-off-by: Sebastiaan van Stijn (cherry picked from commit f845b98ca675c9e848a27f135eac9cd31aac78e5) Signed-off-by: Sebastiaan van Stijn --- integration/container/nat_test.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/integration/container/nat_test.go b/integration/container/nat_test.go index a8d1134e4d..aad5175fd5 100644 --- a/integration/container/nat_test.go +++ b/integration/container/nat_test.go @@ -30,7 +30,7 @@ func TestNetworkNat(t *testing.T) { startServerContainer(t, msg, 8080) endpoint := getExternalAddress(t) - conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", endpoint.String(), 8080)) + conn, err := net.Dial("tcp", net.JoinHostPort(endpoint.String(), "8080")) assert.NilError(t, err) defer conn.Close() @@ -115,6 +115,9 @@ func startServerContainer(t *testing.T, msg string, port int) string { return cID } +// 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. func getExternalAddress(t *testing.T) net.IP { t.Helper() iface, err := net.InterfaceByName("eth0") @@ -124,6 +127,17 @@ func getExternalAddress(t *testing.T) net.IP { assert.NilError(t, err) assert.Check(t, 0 != len(ifaceAddrs)) + 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 + } + } + } ifaceIP, _, err := net.ParseCIDR(ifaceAddrs[0].String()) assert.NilError(t, err)