Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UT for pkg/agent/ipassigner/ip_assigner_linux #5402

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rajnkamr
Copy link
Contributor

@rajnkamr rajnkamr commented Aug 17, 2023

for #4142
Added UT for pkg/agent/ipassigner/ip_assigner_linux.go

@rajnkamr rajnkamr added the area/test Issues or PRs related to unit and integration tests. label Aug 24, 2023
@rajnkamr rajnkamr force-pushed the ipassigner branch 2 times, most recently from 96f7a5f to c730c77 Compare September 29, 2023 09:29
@rajnkamr rajnkamr marked this pull request as ready for review October 10, 2023 10:05
@rajnkamr rajnkamr added this to the Antrea v1.14 release milestone Oct 11, 2023
@luolanzone luolanzone removed this from the Antrea v1.15 release milestone Jan 8, 2024
@rajnkamr rajnkamr marked this pull request as draft January 10, 2024 04:13
@rajnkamr rajnkamr force-pushed the ipassigner branch 2 times, most recently from e7c5d4c to b2d889c Compare January 15, 2024 06:03
@rajnkamr rajnkamr force-pushed the ipassigner branch 3 times, most recently from b800266 to 591707d Compare February 5, 2024 05:38
@rajnkamr rajnkamr marked this pull request as ready for review February 8, 2024 06:33
Comment on lines +39 to +46
netlinkAdd = netlink.LinkAdd
netlinkDel = netlink.LinkDel
ensureRPF = util.EnsureRPFilterOnInterface
netlinkSetUp = netlink.LinkSetUp
netInterfaceByName = net.InterfaceByName
netlinkAddrAdd = netlink.AddrAdd
netlinkAddrDel = netlink.AddrDel
netlinkAddrList = netlink.AddrList
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assignee heavily relies on netlink and there is an interface defined for it: pkg/agent/util/netlink.Interface. Instead of replacing each call repeatedly, assignee should hold the interface, and we just replace it with a fake implementation in unit test. pkg/agent/route/route_linux.go can be an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have following 4 types supported in netlink interface, we can use mocked apis for these
netlink.AddrList
netlink.AddrDel
netlink.AddrAdd
netlink.LinkSetUp
However others like netlink.LinkAdd, netlink.LinkDel, net.InterfaceByName are not supported

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

netlink.LinkAdd, netlink.LinkDel can be added to the interface

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment hasn't been resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had Tried earlier replacing while holding the interface for assignee, it might not work unlike pkg/agent/route/route_linux.go as in route_linux.go, it is using netlink of type utilnetlink.Interface which matches type of interface pkg/agent/util/netlink/netlink_linux.go while replacing

// Client takes care of routing container packets in host network, coordinating ip route, ip rule, iptables and ipset.
type Client struct {
        netlink       utilnetlink.Interface

In assign function, it is directly using external netlink pkg

func (as *assignee) assign(ip net.IP, subnetInfo *crdv1b1.SubnetInfo) error {
        // If there is a real link, add the IP to its address list.
        if as.link != nil {
                addr := getIPNet(ip, subnetInfo)
                if err := netlink.AddrAdd(as.link, &netlink.Addr{IPNet: addr}); err != nil {

mockNetlink.EXPECT().AddrAdd(dummyDevice.Attrs().Name, &netlink.Addr{IPNet: expectedIPNet}).Return(nil)
For example above expectation are never met wrt interface defined in pkg/agent/util/netlink/netlink_linux.go ! similarly for all other netlink apis in ipassigner is used directly from netlink.

@@ -528,6 +544,7 @@ func (a *ipAssigner) addVLANAssignee(link netlink.Link, vlan int32) (*assignee,
logicalInterface: iface,
link: link,
ips: sets.New[string](),
advertiseFn: advertiseFnc,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why setting it to a dummy function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To replace advertise from test, specially for non vlan case

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't explain why it's set to a dummy function here. This is not test code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To mock advertise( replace defer and restore strategy for other apis was followed as mentioned in netlink, but not for advertise), advertise function is added in assignee struct, line 73 , advertiseFn func(ip net.IP, externalInterface *net.Interface), since vlan and non vlan use the structure assigne, a dummy function shld be added to avoid invalid memory address or nil pointer dereference

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. both vlan and non-vlan interfaces need to advertise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

advertise api syntax changed, now it is rebased

pkg/agent/ipassigner/ip_assigner_linux_test.go Outdated Show resolved Hide resolved
pkg/agent/ipassigner/ip_assigner_linux_test.go Outdated Show resolved Hide resolved
pkg/agent/ipassigner/ip_assigner_linux_test.go Outdated Show resolved Hide resolved
pkg/agent/ipassigner/ip_assigner_linux_test.go Outdated Show resolved Hide resolved
pkg/agent/ipassigner/ip_assigner_linux_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

github-actions bot commented Jul 8, 2024

This PR is stale because it has been open 90 days with no activity. Remove stale label or comment, or this will be closed in 90 days

@github-actions github-actions bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 8, 2024
@@ -528,6 +544,7 @@ func (a *ipAssigner) addVLANAssignee(link netlink.Link, vlan int32) (*assignee,
logicalInterface: iface,
link: link,
ips: sets.New[string](),
advertiseFn: advertiseFnc,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. both vlan and non-vlan interfaces need to advertise.

Comment on lines +39 to +46
netlinkAdd = netlink.LinkAdd
netlinkDel = netlink.LinkDel
ensureRPF = util.EnsureRPFilterOnInterface
netlinkSetUp = netlink.LinkSetUp
netInterfaceByName = net.InterfaceByName
netlinkAddrAdd = netlink.AddrAdd
netlinkAddrDel = netlink.AddrDel
netlinkAddrList = netlink.AddrList
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment hasn't been resolved.

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vishvananda/netlink"
gomock "go.uber.org/mock/gomock"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the alias is not needed

}
}

func TestIPAssigner_UnAssignIP(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func TestIPAssigner_UnAssignIP(t *testing.T) {
func TestIPAssigner_UnassignIP(t *testing.T) {

@github-actions github-actions bot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 9, 2024
Signed-off-by: Rajnish Kumar <rajnishk1@vmware.com>
Signed-off-by: Rajnish Kumar <rajnishk1@vmware.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/test Issues or PRs related to unit and integration tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants