Skip to content

Commit

Permalink
IpAssigner vlan & nonvlan UT
Browse files Browse the repository at this point in the history
Signed-off-by: Rajnish Kumar <[email protected]>
  • Loading branch information
rajnkamr committed Jul 19, 2024
1 parent 1aea270 commit 149e206
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
20 changes: 9 additions & 11 deletions pkg/agent/ipassigner/ip_assigner_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ var (
netlinkAddrList = netlink.AddrList
)

func advertiseFnc(ip net.IP, externalInterface *net.Interface) {
}
type advertiseArpNdp func(a *assignee, ip net.IP)

var advertiseResponder advertiseArpNdp = (*assignee).advertise

// VLAN interfaces created by antrea-agent will be named with the prefix.
// For example, when VLAN ID is 10, the name will be antrea-ext.10.
Expand All @@ -69,8 +70,7 @@ type assignee struct {
// NDP queries itself.
ndpResponder responder.Responder
// ips tracks IPs that have been assigned to this assignee.
ips sets.Set[string]
advertiseFn func(ip net.IP, externalInterface *net.Interface)
ips sets.Set[string]
}

// deletable returns whether this assignee can be safely deleted.
Expand Down Expand Up @@ -126,20 +126,20 @@ func (as *assignee) assign(ip net.IP, subnetInfo *crdv1b1.SubnetInfo) error {
}
}
// Always advertise the IP when the IP is newly assigned to this Node.
as.advertiseFn(ip, as.logicalInterface)
advertiseResponder(as, ip)
as.ips.Insert(ip.String())
return nil
}

func advertise(ip net.IP, logicalInterface *net.Interface) {
func (as *assignee) advertise(ip net.IP) {
if utilnet.IsIPv4(ip) {
klog.V(2).InfoS("Sending gratuitous ARP", "ip", ip)
if err := arping.GratuitousARPOverIface(ip, logicalInterface); err != nil {
if err := arping.GratuitousARPOverIface(ip, as.logicalInterface); err != nil {
klog.ErrorS(err, "Failed to send gratuitous ARP", "ip", ip)
}
} else {
klog.V(2).InfoS("Sending neighbor advertisement", "ip", ip)
if err := ndp.NeighborAdvertisement(ip, logicalInterface); err != nil {
if err := ndp.NeighborAdvertisement(ip, as.logicalInterface); err != nil {
klog.ErrorS(err, "Failed to send neighbor advertisement", "ip", ip)
}
}
Expand Down Expand Up @@ -282,7 +282,6 @@ func NewIPAssigner(nodeTransportInterface string, dummyDeviceName string) (IPAss
for _, vlan := range vlans {
a.addVLANAssignee(vlan, int32(vlan.VlanId))
}
a.defaultAssignee.advertiseFn = advertise
return a, nil
}

Expand Down Expand Up @@ -378,7 +377,7 @@ func (a *ipAssigner) AssignIP(ip string, subnetInfo *crdv1b1.SubnetInfo, forceAd
if crdv1b1.CompareSubnetInfo(subnetInfo, oldSubnetInfo, true) {
klog.V(2).InfoS("The IP is already assigned", "ip", ip)
if forceAdvertise {
as.advertiseFn(parsedIP, as.logicalInterface)
advertiseResponder(as, parsedIP)
}
return false, nil
}
Expand Down Expand Up @@ -544,7 +543,6 @@ func (a *ipAssigner) addVLANAssignee(link netlink.Link, vlan int32) (*assignee,
logicalInterface: iface,
link: link,
ips: sets.New[string](),
advertiseFn: advertiseFnc,
}
a.vlanAssignees[vlan] = as
return as, nil
Expand Down
48 changes: 41 additions & 7 deletions pkg/agent/ipassigner/ip_assigner_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func ensureRPFInt(name string, y int) error {
return nil
}

func dummyAdvertise(a *assignee, ip net.IP) {
}

func DummyInterfaceByName(name string) (*net.Interface, error) {
return &net.Interface{
Index: 0,
Expand Down Expand Up @@ -216,12 +219,17 @@ func TestIPAssigner_AssignIP(t *testing.T) {
mutex: sync.RWMutex{},
}
a.defaultAssignee.link = &dummyDeviceMock{}
a.defaultAssignee.advertiseFn = advertiseFnc
a.defaultAssignee.arpResponder = mockResponder
a.defaultAssignee.ndpResponder = mockResponder
tt.expectFunc(mockResponder)
tt.expectedCalls(mockNetlink)

originalAdvertiseResponder := advertiseResponder
advertiseResponder = dummyAdvertise
defer func() {
advertiseResponder = originalAdvertiseResponder
}()

_, err = a.AssignIP(tt.ip, subnetInfo, false)
if tt.expectedError {
require.Error(t, err)
Expand Down Expand Up @@ -374,6 +382,12 @@ func TestIPAssigner_AssignIPVlan(t *testing.T) {
defer func() { netInterfaceByName = netInterfaceByNameFunc }()
netInterfaceByName = DummyInterfaceByName

originalAdvertiseResponder := advertiseResponder
advertiseResponder = dummyAdvertise
defer func() {
advertiseResponder = originalAdvertiseResponder
}()

_, err = a.AssignIP(tt.ip, subnetInfo, false)
if tt.expectedError {
require.Error(t, err)
Expand All @@ -385,7 +399,7 @@ func TestIPAssigner_AssignIPVlan(t *testing.T) {
}
}

func TestIPAssigner_UnAssignIP(t *testing.T) {
func TestIPAssigner_UnassignIP(t *testing.T) {
controller := gomock.NewController(t)
mockResponder := respondertest.NewMockResponder(controller)
var subnetInfo *crdv1b1.SubnetInfo
Expand Down Expand Up @@ -487,12 +501,17 @@ func TestIPAssigner_UnAssignIP(t *testing.T) {
mutex: sync.RWMutex{},
}
a.defaultAssignee.link = &dummyDeviceMock{}
a.defaultAssignee.advertiseFn = advertiseFnc
a.defaultAssignee.arpResponder = mockResponder
a.defaultAssignee.ndpResponder = mockResponder
tt.expectFunc(mockResponder)
tt.expectedCalls(mockNetlink)

originalAdvertiseResponder := advertiseResponder
advertiseResponder = dummyAdvertise
defer func() {
advertiseResponder = originalAdvertiseResponder
}()

_, err := a.UnassignIP(tt.ip)
if tt.expectedError {
require.Error(t, err)
Expand All @@ -504,7 +523,7 @@ func TestIPAssigner_UnAssignIP(t *testing.T) {
}
}

func TestIPAssigner_UnAssignIPVlan(t *testing.T) {
func TestIPAssigner_UnassignIPVlan(t *testing.T) {
controller := gomock.NewController(t)
mockResponder := respondertest.NewMockResponder(controller)
var subnetInfo *crdv1b1.SubnetInfo
Expand Down Expand Up @@ -673,10 +692,15 @@ func TestIPAssigner_AssignedIPs(t *testing.T) {
mutex: sync.RWMutex{},
}
a.defaultAssignee.link = &dummyDeviceMock{}
a.defaultAssignee.advertiseFn = advertiseFnc
a.defaultAssignee.arpResponder = mockResponder
a.defaultAssignee.ndpResponder = mockResponder

originalAdvertiseResponder := advertiseResponder
advertiseResponder = dummyAdvertise
defer func() {
advertiseResponder = originalAdvertiseResponder
}()

ips := a.AssignedIPs()

expectedIPs := map[string]*crdv1b1.SubnetInfo{
Expand Down Expand Up @@ -888,14 +912,19 @@ func TestIPAssigner_InitIPs(t *testing.T) {
mutex: sync.RWMutex{},
}
a.defaultAssignee.link = &dummyDeviceMock{}
a.defaultAssignee.advertiseFn = advertiseFnc
a.defaultAssignee.arpResponder = mockResponder
a.defaultAssignee.ndpResponder = mockResponder
tt.expectFunc(mockResponder)
tt.expectedCalls(mockNetlink)

netlinkAddrDel = mockNetlink.AddrDel

originalAdvertiseResponder := advertiseResponder
advertiseResponder = dummyAdvertise
defer func() {
advertiseResponder = originalAdvertiseResponder
}()

err = a.InitIPs(tt.desiredIPs)
if tt.expectedError {
require.Error(t, err)
Expand Down Expand Up @@ -982,12 +1011,17 @@ func TestIPAssigner_InitIPsVlan(t *testing.T) {
mutex: sync.RWMutex{},
}
a.defaultAssignee.link = &dummyDeviceMock{}
a.defaultAssignee.advertiseFn = advertiseFnc
a.defaultAssignee.arpResponder = mockResponder
a.defaultAssignee.ndpResponder = mockResponder
tt.expectFunc(mockResponder)
tt.expectedCalls(mockNetlink)

originalAdvertiseResponder := advertiseResponder
advertiseResponder = dummyAdvertise
defer func() {
advertiseResponder = originalAdvertiseResponder
}()

ensRpfFunc := ensureRPF
defer func() { ensureRPF = ensRpfFunc }()
ensureRPF = ensureRPFInt
Expand Down

0 comments on commit 149e206

Please sign in to comment.