github.com/k8snetworkplumbingwg/sriov-network-operator@v1.2.1-0.20240408194816-2d2e5a45d453/pkg/host/internal/network/network_test.go (about) 1 package network 2 3 import ( 4 "fmt" 5 6 . "github.com/onsi/ginkgo/v2" 7 . "github.com/onsi/gomega" 8 "github.com/vishvananda/netlink" 9 "github.com/vishvananda/netlink/nl" 10 11 "github.com/golang/mock/gomock" 12 13 hostMockPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/helper/mock" 14 dputilsMockPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/dputils/mock" 15 ethtoolMockPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/ethtool/mock" 16 netlinkMockPkg "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/netlink/mock" 17 "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types" 18 ) 19 20 func getDevlinkParam(t uint8, value interface{}) *netlink.DevlinkParam { 21 return &netlink.DevlinkParam{ 22 Name: "test_param", 23 Type: t, 24 Values: []netlink.DevlinkParamValue{ 25 {Data: value, CMODE: nl.DEVLINK_PARAM_CMODE_DRIVERINIT}}, 26 } 27 } 28 29 var _ = Describe("Network", func() { 30 var ( 31 n types.NetworkInterface 32 netlinkLibMock *netlinkMockPkg.MockNetlinkLib 33 ethtoolLibMock *ethtoolMockPkg.MockEthtoolLib 34 dputilsLibMock *dputilsMockPkg.MockDPUtilsLib 35 hostMock *hostMockPkg.MockHostHelpersInterface 36 37 testCtrl *gomock.Controller 38 testErr = fmt.Errorf("test") 39 ) 40 BeforeEach(func() { 41 testCtrl = gomock.NewController(GinkgoT()) 42 netlinkLibMock = netlinkMockPkg.NewMockNetlinkLib(testCtrl) 43 ethtoolLibMock = ethtoolMockPkg.NewMockEthtoolLib(testCtrl) 44 dputilsLibMock = dputilsMockPkg.NewMockDPUtilsLib(testCtrl) 45 hostMock = hostMockPkg.NewMockHostHelpersInterface(testCtrl) 46 47 n = New(hostMock, dputilsLibMock, netlinkLibMock, ethtoolLibMock) 48 }) 49 50 AfterEach(func() { 51 testCtrl.Finish() 52 }) 53 Context("GetDevlinkDeviceParam", func() { 54 It("get - string", func() { 55 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 56 getDevlinkParam(nl.DEVLINK_PARAM_TYPE_STRING, "test_value"), nil) 57 result, err := n.GetDevlinkDeviceParam("0000:d8:00.1", "param_name") 58 Expect(err).NotTo(HaveOccurred()) 59 Expect(result).To(Equal("test_value")) 60 }) 61 It("get - uint8", func() { 62 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 63 getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U8, uint8(8)), nil) 64 result, err := n.GetDevlinkDeviceParam("0000:d8:00.1", "param_name") 65 Expect(err).NotTo(HaveOccurred()) 66 Expect(result).To(Equal("8")) 67 }) 68 It("get - uint16", func() { 69 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 70 getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U16, uint16(16)), nil) 71 result, err := n.GetDevlinkDeviceParam("0000:d8:00.1", "param_name") 72 Expect(err).NotTo(HaveOccurred()) 73 Expect(result).To(Equal("16")) 74 }) 75 It("get - uint32", func() { 76 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 77 getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U32, uint32(32)), nil) 78 result, err := n.GetDevlinkDeviceParam("0000:d8:00.1", "param_name") 79 Expect(err).NotTo(HaveOccurred()) 80 Expect(result).To(Equal("32")) 81 }) 82 It("get - bool", func() { 83 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 84 getDevlinkParam(nl.DEVLINK_PARAM_TYPE_BOOL, false), nil) 85 result, err := n.GetDevlinkDeviceParam("0000:d8:00.1", "param_name") 86 Expect(err).NotTo(HaveOccurred()) 87 Expect(result).To(Equal("false")) 88 }) 89 It("failed", func() { 90 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return(nil, testErr) 91 _, err := n.GetDevlinkDeviceParam("0000:d8:00.1", "param_name") 92 Expect(err).To(HaveOccurred()) 93 }) 94 }) 95 Context("SetDevlinkDeviceParam", func() { 96 It("set - string", func() { 97 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 98 getDevlinkParam(nl.DEVLINK_PARAM_TYPE_STRING, "test_value"), nil) 99 netlinkLibMock.EXPECT().DevlinkSetDeviceParam("pci", "0000:d8:00.1", "param_name", 100 uint8(nl.DEVLINK_PARAM_CMODE_DRIVERINIT), "test_value").Return(nil) 101 err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "test_value") 102 Expect(err).NotTo(HaveOccurred()) 103 }) 104 It("set - uint8", func() { 105 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 106 getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U8, uint8(8)), nil) 107 netlinkLibMock.EXPECT().DevlinkSetDeviceParam("pci", "0000:d8:00.1", "param_name", 108 uint8(nl.DEVLINK_PARAM_CMODE_DRIVERINIT), uint8(100)).Return(nil) 109 err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "100") 110 Expect(err).NotTo(HaveOccurred()) 111 }) 112 It("set - uint16", func() { 113 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 114 getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U16, uint16(16)), nil) 115 netlinkLibMock.EXPECT().DevlinkSetDeviceParam("pci", "0000:d8:00.1", "param_name", 116 uint8(nl.DEVLINK_PARAM_CMODE_DRIVERINIT), uint16(100)).Return(nil) 117 err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "100") 118 Expect(err).NotTo(HaveOccurred()) 119 }) 120 It("set - uint32", func() { 121 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 122 getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U32, uint32(32)), nil) 123 netlinkLibMock.EXPECT().DevlinkSetDeviceParam("pci", "0000:d8:00.1", "param_name", 124 uint8(nl.DEVLINK_PARAM_CMODE_DRIVERINIT), uint32(100)).Return(nil) 125 err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "100") 126 Expect(err).NotTo(HaveOccurred()) 127 }) 128 It("set - bool", func() { 129 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 130 getDevlinkParam(nl.DEVLINK_PARAM_TYPE_BOOL, false), nil) 131 netlinkLibMock.EXPECT().DevlinkSetDeviceParam("pci", "0000:d8:00.1", "param_name", 132 uint8(nl.DEVLINK_PARAM_CMODE_DRIVERINIT), true).Return(nil) 133 err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "true") 134 Expect(err).NotTo(HaveOccurred()) 135 }) 136 It("failed to get", func() { 137 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 138 nil, testErr) 139 err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "true") 140 Expect(err).To(HaveOccurred()) 141 }) 142 It("failed to set", func() { 143 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 144 getDevlinkParam(nl.DEVLINK_PARAM_TYPE_BOOL, false), nil) 145 netlinkLibMock.EXPECT().DevlinkSetDeviceParam("pci", "0000:d8:00.1", "param_name", 146 uint8(nl.DEVLINK_PARAM_CMODE_DRIVERINIT), true).Return(testErr) 147 err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "true") 148 Expect(err).To(HaveOccurred()) 149 }) 150 It("failed to convert type on set", func() { 151 netlinkLibMock.EXPECT().DevlinkGetDeviceParamByName("pci", "0000:d8:00.1", "param_name").Return( 152 getDevlinkParam(nl.DEVLINK_PARAM_TYPE_U8, 10), nil) 153 // uint8 overflow 154 err := n.SetDevlinkDeviceParam("0000:d8:00.1", "param_name", "10000") 155 Expect(err).To(HaveOccurred()) 156 }) 157 }) 158 Context("EnableHwTcOffload", func() { 159 It("Enabled", func() { 160 ethtoolLibMock.EXPECT().FeatureNames("enp216s0f0np0").Return(map[string]uint{"hw-tc-offload": 42}, nil) 161 ethtoolLibMock.EXPECT().Features("enp216s0f0np0").Return(map[string]bool{"hw-tc-offload": false}, nil) 162 ethtoolLibMock.EXPECT().Change("enp216s0f0np0", map[string]bool{"hw-tc-offload": true}).Return(nil) 163 ethtoolLibMock.EXPECT().Features("enp216s0f0np0").Return(map[string]bool{"hw-tc-offload": true}, nil) 164 Expect(n.EnableHwTcOffload("enp216s0f0np0")).NotTo(HaveOccurred()) 165 }) 166 It("Feature unknown", func() { 167 ethtoolLibMock.EXPECT().FeatureNames("enp216s0f0np0").Return(map[string]uint{}, nil) 168 Expect(n.EnableHwTcOffload("enp216s0f0np0")).NotTo(HaveOccurred()) 169 }) 170 It("Already enabled", func() { 171 ethtoolLibMock.EXPECT().FeatureNames("enp216s0f0np0").Return(map[string]uint{"hw-tc-offload": 42}, nil) 172 ethtoolLibMock.EXPECT().Features("enp216s0f0np0").Return(map[string]bool{"hw-tc-offload": true}, nil) 173 Expect(n.EnableHwTcOffload("enp216s0f0np0")).NotTo(HaveOccurred()) 174 }) 175 It("not supported", func() { 176 ethtoolLibMock.EXPECT().FeatureNames("enp216s0f0np0").Return(map[string]uint{"hw-tc-offload": 42}, nil) 177 ethtoolLibMock.EXPECT().Features("enp216s0f0np0").Return(map[string]bool{"hw-tc-offload": false}, nil) 178 ethtoolLibMock.EXPECT().Change("enp216s0f0np0", map[string]bool{"hw-tc-offload": true}).Return(nil) 179 ethtoolLibMock.EXPECT().Features("enp216s0f0np0").Return(map[string]bool{"hw-tc-offload": false}, nil) 180 Expect(n.EnableHwTcOffload("enp216s0f0np0")).NotTo(HaveOccurred()) 181 }) 182 It("fail - can't list supported", func() { 183 ethtoolLibMock.EXPECT().FeatureNames("enp216s0f0np0").Return(nil, testErr) 184 Expect(n.EnableHwTcOffload("enp216s0f0np0")).To(MatchError(testErr)) 185 }) 186 It("fail - can't get features", func() { 187 ethtoolLibMock.EXPECT().FeatureNames("enp216s0f0np0").Return(map[string]uint{"hw-tc-offload": 42}, nil) 188 ethtoolLibMock.EXPECT().Features("enp216s0f0np0").Return(nil, testErr) 189 Expect(n.EnableHwTcOffload("enp216s0f0np0")).To(MatchError(testErr)) 190 }) 191 It("fail - can't change features", func() { 192 ethtoolLibMock.EXPECT().FeatureNames("enp216s0f0np0").Return(map[string]uint{"hw-tc-offload": 42}, nil) 193 ethtoolLibMock.EXPECT().Features("enp216s0f0np0").Return(map[string]bool{"hw-tc-offload": false}, nil) 194 ethtoolLibMock.EXPECT().Change("enp216s0f0np0", map[string]bool{"hw-tc-offload": true}).Return(testErr) 195 Expect(n.EnableHwTcOffload("enp216s0f0np0")).To(MatchError(testErr)) 196 }) 197 It("fail - can't reread features", func() { 198 ethtoolLibMock.EXPECT().FeatureNames("enp216s0f0np0").Return(map[string]uint{"hw-tc-offload": 42}, nil) 199 ethtoolLibMock.EXPECT().Features("enp216s0f0np0").Return(map[string]bool{"hw-tc-offload": false}, nil) 200 ethtoolLibMock.EXPECT().Change("enp216s0f0np0", map[string]bool{"hw-tc-offload": true}).Return(nil) 201 ethtoolLibMock.EXPECT().Features("enp216s0f0np0").Return(nil, testErr) 202 Expect(n.EnableHwTcOffload("enp216s0f0np0")).To(MatchError(testErr)) 203 }) 204 }) 205 })