Skip to content

Commit

Permalink
test(krnode): add unit tests for new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
aauren committed Sep 29, 2024
1 parent e4fa335 commit 5e4db3f
Show file tree
Hide file tree
Showing 6 changed files with 769 additions and 48 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
Expand Down
48 changes: 1 addition & 47 deletions pkg/controllers/netpol/network_policy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/coreos/go-iptables/iptables"
"github.com/vishvananda/netlink"

netv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -758,51 +757,6 @@ func (ips *fakeIPSet) Name(name string) string {
return name
}

type fakeLocalLinkQuerier struct {
links []netlink.Link
addrs []*net.IPNet
}

func newFakeLocalLinkQuerier(addrStrings []string) *fakeLocalLinkQuerier {
links := make([]netlink.Link, len(addrStrings))
for idx := range addrStrings {
linkAttrs := netlink.LinkAttrs{
Index: idx,
}
linkDevice := netlink.Device{LinkAttrs: linkAttrs}
links[idx] = &linkDevice
}
addrs := make([]*net.IPNet, len(addrStrings))
for idx, addr := range addrStrings {
ip := net.ParseIP(addr)
var netMask net.IPMask
if ip.To4() != nil {
netMask = net.CIDRMask(24, 32)
} else {
netMask = net.CIDRMask(64, 128)
}
ipNet := &net.IPNet{
IP: ip,
Mask: netMask,
}
addrs[idx] = ipNet
}
return &fakeLocalLinkQuerier{
links: links,
addrs: addrs,
}
}

func (f *fakeLocalLinkQuerier) LinkList() ([]netlink.Link, error) {
return f.links, nil
}

func (f *fakeLocalLinkQuerier) AddrList(link netlink.Link, family int) ([]netlink.Addr, error) {
addrs := make([]netlink.Addr, 1)
addrs[0] = netlink.Addr{IPNet: f.addrs[link.Attrs().Index]}
return addrs, nil
}

func TestNetworkPolicyController(t *testing.T) {
curHostname, _ := os.Hostname()
testCases := []tNetPolConfigTestCase{
Expand Down Expand Up @@ -989,7 +943,7 @@ func TestNetworkPolicyController(t *testing.T) {
},
}
fakeNodeIPs := []string{"10.10.10.10", "2001:0db8:0042:0001:0000:0000:0000:0000"}
fakeLinkQuerier := newFakeLocalLinkQuerier(fakeNodeIPs)
fakeLinkQuerier := utils.NewFakeLocalLinkQuerier(fakeNodeIPs, nil)
client := fake.NewSimpleClientset(&v1.NodeList{Items: []v1.Node{*newFakeNode("node", fakeNodeIPs)}})
_, podInformer, nsInformer, netpolInformer := newFakeInformersFromClient(client)
for _, test := range testCases {
Expand Down
78 changes: 78 additions & 0 deletions pkg/utils/linux_routingtest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package utils

import (
"fmt"
"net"

"github.com/stretchr/testify/mock"
"github.com/vishvananda/netlink"
)

type FakeLocalLinkQuerier struct {
links []netlink.Link
addrs []*net.IPNet
}

func NewFakeLocalLinkQuerier(addrStrings []string, mtus []int) *FakeLocalLinkQuerier {
links := make([]netlink.Link, len(addrStrings))
for idx := range addrStrings {
mtu := 1
if idx < len(mtus) {
mtu = mtus[idx]
}
linkAttrs := netlink.LinkAttrs{
Index: idx,
MTU: mtu,
}
linkDevice := netlink.Device{LinkAttrs: linkAttrs}
links[idx] = &linkDevice
}
addrs := make([]*net.IPNet, len(addrStrings))
for idx, addr := range addrStrings {
ip := net.ParseIP(addr)
var netMask net.IPMask
if ip.To4() != nil {
//nolint:gomnd // Hardcoded value is used for testing purposes
netMask = net.CIDRMask(24, 32)
} else {
//nolint:gomnd // Hardcoded value is used for testing purposes
netMask = net.CIDRMask(64, 128)
}
ipNet := &net.IPNet{
IP: ip,
Mask: netMask,
}
addrs[idx] = ipNet
}
return &FakeLocalLinkQuerier{
links: links,
addrs: addrs,
}
}

func (f *FakeLocalLinkQuerier) LinkList() ([]netlink.Link, error) {
return f.links, nil
}

func (f *FakeLocalLinkQuerier) AddrList(link netlink.Link, family int) ([]netlink.Addr, error) {
addrs := make([]netlink.Addr, 1)
addrs[0] = netlink.Addr{IPNet: f.addrs[link.Attrs().Index]}
if link.Attrs().MTU == 0 {
return nil, fmt.Errorf("MTU was set to 0 to simulate an error")
}
return addrs, nil
}

type MockLocalLinkQuerier struct {
mock.Mock
}

func (m *MockLocalLinkQuerier) LinkList() ([]netlink.Link, error) {
args := m.Called()
return args.Get(0).([]netlink.Link), args.Error(1)
}

func (m *MockLocalLinkQuerier) AddrList(link netlink.Link, family int) ([]netlink.Addr, error) {
args := m.Called(link, family)
return args.Get(0).([]netlink.Addr), args.Error(1)
}
2 changes: 1 addition & 1 deletion pkg/utils/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func GetNodeSubnet(nodeIP net.IP, linkQ LocalLinkQuerier) (net.IPNet, string, er
for _, link := range links {
addresses, err := linkQ.AddrList(link, netlink.FAMILY_ALL)
if err != nil {
return net.IPNet{}, "", errors.New("failed to get list of addr")
return net.IPNet{}, "", errors.New("failed to get list of addrs")
}
for _, addr := range addresses {
if addr.IPNet.IP.Equal(nodeIP) {
Expand Down
Loading

0 comments on commit 5e4db3f

Please sign in to comment.