1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-02-10 11:31:18 +01:00
parent 54ca929a70
commit f845b98ca6
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -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)