Revision 9368f21c53b7aa96b8743a0b8b79a0ab44d963c6 authored by Jarno Rajahalme on 20 June 2024, 21:26:05 UTC, committed by Nathan Sweet on 21 June 2024, 15:57:37 UTC
AllowsIdentity is only used for testing, move it there. Add the missing InvertedPortMask field on the wildcard port lookup. Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
1 parent d97069c
address_linux_test.go
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
//go:build !darwin
package node
import (
"fmt"
"net"
"testing"
"github.com/stretchr/testify/require"
"github.com/vishvananda/netlink"
"github.com/cilium/cilium/pkg/testutils"
)
func setUpSuite(tb testing.TB) {
testutils.PrivilegedTest(tb)
}
func Test_firstGlobalV4Addr(t *testing.T) {
setUpSuite(t)
testCases := []struct {
name string
ipsOnInterface []string
preferredIP string
preferPublic bool
want string
}{
{
name: "public IP preferred by default",
ipsOnInterface: []string{"192.168.0.1", "21.0.0.1"},
want: "21.0.0.1",
},
{
name: "prefer IP when not preferPublic",
ipsOnInterface: []string{"192.168.0.1", "21.0.0.1"},
preferredIP: "192.168.0.1",
want: "192.168.0.1",
},
{
name: "preferPublic when not prefer IP",
ipsOnInterface: []string{"192.168.0.1", "21.0.0.1"},
preferPublic: true,
want: "21.0.0.1",
},
{
name: "preferPublic when prefer IP",
ipsOnInterface: []string{"192.168.0.1", "21.0.0.1"},
preferPublic: true,
preferredIP: "192.168.0.1",
want: "21.0.0.1",
},
{
name: "primary IP preferred by default",
ipsOnInterface: []string{"192.168.0.2", "192.168.0.1"},
want: "192.168.0.2",
},
}
const ifName = "dummy_iface"
for _, tc := range testCases {
err := setupDummyDevice(ifName, tc.ipsOnInterface...)
require.NoError(t, err)
got, err := firstGlobalV4Addr(ifName, net.ParseIP(tc.preferredIP), tc.preferPublic)
require.NoError(t, err)
require.Equal(t, tc.want, got.String())
removeDevice(ifName)
}
}
func setupDummyDevice(name string, ips ...string) error {
dummy := &netlink.Dummy{
LinkAttrs: netlink.LinkAttrs{
Name: name,
},
}
if err := netlink.LinkAdd(dummy); err != nil {
return fmt.Errorf("netlink.LinkAdd failed: %w", err)
}
if err := netlink.LinkSetUp(dummy); err != nil {
removeDevice(name)
return fmt.Errorf("netlink.LinkSetUp failed: %w", err)
}
for _, ipStr := range ips {
ip := net.ParseIP(ipStr)
if ip == nil || ip.To4() == nil {
removeDevice(name)
return fmt.Errorf("invalid ipv4 IP : %v", ipStr)
}
ipnet := &net.IPNet{IP: ip, Mask: net.CIDRMask(32, 32)}
addr := &netlink.Addr{IPNet: ipnet}
if err := netlink.AddrAdd(dummy, addr); err != nil {
removeDevice(name)
return err
}
}
return nil
}
func removeDevice(name string) {
l, err := netlink.LinkByName(name)
if err == nil {
netlink.LinkDel(l)
}
}
![swh spinner](/static/img/swh-spinner.gif)
Computing file changes ...