-
Notifications
You must be signed in to change notification settings - Fork 368
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
base: main
Are you sure you want to change the base?
Conversation
96f7a5f
to
c730c77
Compare
e7c5d4c
to
b2d889c
Compare
b800266
to
591707d
Compare
netlinkAdd = netlink.LinkAdd | ||
netlinkDel = netlink.LinkDel | ||
ensureRPF = util.EnsureRPFilterOnInterface | ||
netlinkSetUp = netlink.LinkSetUp | ||
netInterfaceByName = net.InterfaceByName | ||
netlinkAddrAdd = netlink.AddrAdd | ||
netlinkAddrDel = netlink.AddrDel | ||
netlinkAddrList = netlink.AddrList |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
47d1b56
to
d5b8495
Compare
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 |
@@ -528,6 +544,7 @@ func (a *ipAssigner) addVLANAssignee(link netlink.Link, vlan int32) (*assignee, | |||
logicalInterface: iface, | |||
link: link, | |||
ips: sets.New[string](), | |||
advertiseFn: advertiseFnc, |
There was a problem hiding this comment.
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.
netlinkAdd = netlink.LinkAdd | ||
netlinkDel = netlink.LinkDel | ||
ensureRPF = util.EnsureRPFilterOnInterface | ||
netlinkSetUp = netlink.LinkSetUp | ||
netInterfaceByName = net.InterfaceByName | ||
netlinkAddrAdd = netlink.AddrAdd | ||
netlinkAddrDel = netlink.AddrDel | ||
netlinkAddrList = netlink.AddrList |
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func TestIPAssigner_UnAssignIP(t *testing.T) { | |
func TestIPAssigner_UnassignIP(t *testing.T) { |
Signed-off-by: Rajnish Kumar <[email protected]>
Signed-off-by: Rajnish Kumar <[email protected]>
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 |
for #4142
Added UT for pkg/agent/ipassigner/ip_assigner_linux.go