From f3a5caefbe7064724a2f3d6a8db6a1af17142600 Mon Sep 17 00:00:00 2001 From: Boris Glimcher Date: Wed, 6 Sep 2023 05:55:22 +0300 Subject: [PATCH] test: use mock.Anything to simplify tests Signed-off-by: Boris Glimcher --- pkg/evpn/bridge_test.go | 50 +++++++++++++---------------------------- pkg/evpn/port_test.go | 4 +++- pkg/evpn/svi_test.go | 30 +++++++++---------------- pkg/evpn/vrf_test.go | 35 +++++++++++------------------ 4 files changed, 43 insertions(+), 76 deletions(-) diff --git a/pkg/evpn/bridge_test.go b/pkg/evpn/bridge_test.go index 24a87af4..e2767c18 100644 --- a/pkg/evpn/bridge_test.go +++ b/pkg/evpn/bridge_test.go @@ -7,14 +7,13 @@ package evpn import ( "context" - "encoding/binary" "errors" "fmt" "log" - "net" "reflect" "testing" + "github.com/stretchr/testify/mock" "github.com/vishvananda/netlink" "google.golang.org/grpc" @@ -140,7 +139,7 @@ func Test_CreateLogicalBridge(t *testing.T) { errMsg: "unable to find key br-tenant", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - mockNetlink.EXPECT().LinkByName(tenantbridgeName).Return(nil, errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkByName(mock.Anything).Return(nil, errors.New(errMsg)).Once() }, }, "failed LinkAdd call": { @@ -151,14 +150,9 @@ func Test_CreateLogicalBridge(t *testing.T) { errMsg: "Failed to call LinkAdd", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - // myip := net.ParseIP("10.0.0.2") - myip := make(net.IP, 4) - binary.BigEndian.PutUint32(myip, 167772162) - vxlanName := fmt.Sprintf("vni%d", *testLogicalBridge.Spec.Vni) - vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(*testLogicalBridge.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip} bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: tenantbridgeName}} - mockNetlink.EXPECT().LinkByName(tenantbridgeName).Return(bridge, nil).Once() - mockNetlink.EXPECT().LinkAdd(vxlan).Return(errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkByName(mock.Anything).Return(bridge, nil).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(errors.New(errMsg)).Once() }, }, "failed LinkSetMaster call": { @@ -169,14 +163,10 @@ func Test_CreateLogicalBridge(t *testing.T) { errMsg: "Failed to call LinkSetMaster", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - myip := make(net.IP, 4) - binary.BigEndian.PutUint32(myip, 167772162) - vxlanName := fmt.Sprintf("vni%d", *testLogicalBridge.Spec.Vni) - vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(*testLogicalBridge.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip} bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: tenantbridgeName}} - mockNetlink.EXPECT().LinkByName(tenantbridgeName).Return(bridge, nil).Once() - mockNetlink.EXPECT().LinkAdd(vxlan).Return(nil).Once() - mockNetlink.EXPECT().LinkSetMaster(vxlan, bridge).Return(errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkByName(mock.Anything).Return(bridge, nil).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkSetMaster(mock.Anything, mock.Anything).Return(errors.New(errMsg)).Once() }, }, "failed LinkSetUp call": { @@ -187,15 +177,11 @@ func Test_CreateLogicalBridge(t *testing.T) { errMsg: "Failed to call LinkSetUp", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - myip := make(net.IP, 4) - binary.BigEndian.PutUint32(myip, 167772162) - vxlanName := fmt.Sprintf("vni%d", *testLogicalBridge.Spec.Vni) - vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(*testLogicalBridge.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip} bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: tenantbridgeName}} - mockNetlink.EXPECT().LinkByName(tenantbridgeName).Return(bridge, nil).Once() - mockNetlink.EXPECT().LinkAdd(vxlan).Return(nil).Once() - mockNetlink.EXPECT().LinkSetMaster(vxlan, bridge).Return(nil).Once() - mockNetlink.EXPECT().LinkSetUp(vxlan).Return(errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkByName(mock.Anything).Return(bridge, nil).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkSetMaster(mock.Anything, mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkSetUp(mock.Anything).Return(errors.New(errMsg)).Once() }, }, "failed BridgeVlanAdd call": { @@ -206,16 +192,12 @@ func Test_CreateLogicalBridge(t *testing.T) { errMsg: "Failed to call BridgeVlanAdd", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - myip := make(net.IP, 4) - binary.BigEndian.PutUint32(myip, 167772162) - vxlanName := fmt.Sprintf("vni%d", *testLogicalBridge.Spec.Vni) - vxlan := &netlink.Vxlan{LinkAttrs: netlink.LinkAttrs{Name: vxlanName}, VxlanId: int(*testLogicalBridge.Spec.Vni), Port: 4789, Learning: false, SrcAddr: myip} bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: tenantbridgeName}} - mockNetlink.EXPECT().LinkByName(tenantbridgeName).Return(bridge, nil).Once() - mockNetlink.EXPECT().LinkAdd(vxlan).Return(nil).Once() - mockNetlink.EXPECT().LinkSetMaster(vxlan, bridge).Return(nil).Once() - mockNetlink.EXPECT().LinkSetUp(vxlan).Return(nil).Once() - mockNetlink.EXPECT().BridgeVlanAdd(vxlan, uint16(testLogicalBridge.Spec.VlanId), true, true, false, false).Return(errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkByName(mock.Anything).Return(bridge, nil).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkSetMaster(mock.Anything, mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkSetUp(mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().BridgeVlanAdd(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(errors.New(errMsg)).Once() }, }, } diff --git a/pkg/evpn/port_test.go b/pkg/evpn/port_test.go index 686895ab..17e7de9b 100644 --- a/pkg/evpn/port_test.go +++ b/pkg/evpn/port_test.go @@ -13,6 +13,8 @@ import ( "reflect" "testing" + "github.com/stretchr/testify/mock" + "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials/insecure" @@ -122,7 +124,7 @@ func Test_CreateBridgePort(t *testing.T) { errMsg: "unable to find key br-tenant", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - mockNetlink.EXPECT().LinkByName(tenantbridgeName).Return(nil, errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkByName(mock.Anything).Return(nil, errors.New(errMsg)).Once() }, }, } diff --git a/pkg/evpn/svi_test.go b/pkg/evpn/svi_test.go index 6fa07e83..0a7d2304 100644 --- a/pkg/evpn/svi_test.go +++ b/pkg/evpn/svi_test.go @@ -10,10 +10,10 @@ import ( "errors" "fmt" "log" - "net" "reflect" "testing" + "github.com/stretchr/testify/mock" "github.com/vishvananda/netlink" "google.golang.org/grpc" @@ -205,7 +205,7 @@ func Test_CreateSvi(t *testing.T) { errMsg: "unable to find key br-tenant", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - mockNetlink.EXPECT().LinkByName(tenantbridgeName).Return(nil, errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkByName(mock.Anything).Return(nil, errors.New(errMsg)).Once() }, }, "failed BridgeVlanAdd call": { @@ -216,10 +216,9 @@ func Test_CreateSvi(t *testing.T) { errMsg: "Failed to call BridgeVlanAdd", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - vid := uint16(testLogicalBridge.Spec.VlanId) bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: tenantbridgeName}} - mockNetlink.EXPECT().LinkByName(tenantbridgeName).Return(bridge, nil).Once() - mockNetlink.EXPECT().BridgeVlanAdd(bridge, vid, false, false, true, false).Return(errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkByName(mock.Anything).Return(bridge, nil).Once() + mockNetlink.EXPECT().BridgeVlanAdd(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(errors.New(errMsg)).Once() }, }, "failed LinkAdd call": { @@ -230,13 +229,10 @@ func Test_CreateSvi(t *testing.T) { errMsg: "Failed to call LinkAdd", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - vid := uint16(testLogicalBridge.Spec.VlanId) bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: tenantbridgeName}} - mockNetlink.EXPECT().LinkByName(tenantbridgeName).Return(bridge, nil).Once() - mockNetlink.EXPECT().BridgeVlanAdd(bridge, vid, false, false, true, false).Return(nil).Once() - vlanName := fmt.Sprintf("vlan%d", vid) - vlandev := &netlink.Vlan{LinkAttrs: netlink.LinkAttrs{Name: vlanName, ParentIndex: bridge.Attrs().Index}, VlanId: int(vid)} - mockNetlink.EXPECT().LinkAdd(vlandev).Return(errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkByName(mock.Anything).Return(bridge, nil).Once() + mockNetlink.EXPECT().BridgeVlanAdd(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(errors.New(errMsg)).Once() }, }, "failed LinkSetHardwareAddr call": { @@ -247,15 +243,11 @@ func Test_CreateSvi(t *testing.T) { errMsg: "Failed to call LinkSetHardwareAddr", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - vid := uint16(testLogicalBridge.Spec.VlanId) bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: tenantbridgeName}} - mockNetlink.EXPECT().LinkByName(tenantbridgeName).Return(bridge, nil).Once() - mockNetlink.EXPECT().BridgeVlanAdd(bridge, vid, false, false, true, false).Return(nil).Once() - vlanName := fmt.Sprintf("vlan%d", vid) - vlandev := &netlink.Vlan{LinkAttrs: netlink.LinkAttrs{Name: vlanName, ParentIndex: bridge.Attrs().Index}, VlanId: int(vid)} - mockNetlink.EXPECT().LinkAdd(vlandev).Return(nil).Once() - mac := net.HardwareAddr(testSvi.Spec.MacAddress[:]) - mockNetlink.EXPECT().LinkSetHardwareAddr(vlandev, mac).Return(errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkByName(mock.Anything).Return(bridge, nil).Once() + mockNetlink.EXPECT().BridgeVlanAdd(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkSetHardwareAddr(mock.Anything, mock.Anything).Return(errors.New(errMsg)).Once() }, }, } diff --git a/pkg/evpn/vrf_test.go b/pkg/evpn/vrf_test.go index 6502d0b9..68f36e0a 100644 --- a/pkg/evpn/vrf_test.go +++ b/pkg/evpn/vrf_test.go @@ -13,7 +13,7 @@ import ( "reflect" "testing" - "github.com/vishvananda/netlink" + "github.com/stretchr/testify/mock" "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -121,9 +121,8 @@ func Test_CreateVrf(t *testing.T) { errMsg: "", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - vrf := &netlink.Vrf{LinkAttrs: netlink.LinkAttrs{Name: testVrfID}, Table: 1000} - mockNetlink.EXPECT().LinkAdd(vrf).Return(nil).Once() - mockNetlink.EXPECT().LinkSetUp(vrf).Return(nil).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkSetUp(mock.Anything).Return(nil).Once() }, }, "failed LinkAdd call": { @@ -134,8 +133,7 @@ func Test_CreateVrf(t *testing.T) { errMsg: "Failed to call LinkAdd", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - vrf := &netlink.Vrf{LinkAttrs: netlink.LinkAttrs{Name: testVrfID}, Table: 1001} - mockNetlink.EXPECT().LinkAdd(vrf).Return(errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(errors.New(errMsg)).Once() }, }, "failed LinkSetUp call": { @@ -146,9 +144,8 @@ func Test_CreateVrf(t *testing.T) { errMsg: "Failed to call LinkSetUp", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - vrf := &netlink.Vrf{LinkAttrs: netlink.LinkAttrs{Name: testVrfID}, Table: 1001} - mockNetlink.EXPECT().LinkAdd(vrf).Return(nil).Once() - mockNetlink.EXPECT().LinkSetUp(vrf).Return(errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkSetUp(mock.Anything).Return(errors.New(errMsg)).Once() }, }, "failed bridge LinkAdd call": { @@ -159,12 +156,9 @@ func Test_CreateVrf(t *testing.T) { errMsg: "Failed to call LinkAdd", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - bridgeName := fmt.Sprintf("br%d", *testVrf.Spec.Vni) - vrf := &netlink.Vrf{LinkAttrs: netlink.LinkAttrs{Name: testVrfID}, Table: 1001} - bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: bridgeName}} - mockNetlink.EXPECT().LinkAdd(vrf).Return(nil).Once() - mockNetlink.EXPECT().LinkSetUp(vrf).Return(nil).Once() - mockNetlink.EXPECT().LinkAdd(bridge).Return(errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkSetUp(mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(errors.New(errMsg)).Once() }, }, "failed bridge LinkSetMaster call": { @@ -175,13 +169,10 @@ func Test_CreateVrf(t *testing.T) { errMsg: "Failed to call LinkSetMaster", exist: false, on: func(mockNetlink *mocks.Netlink, errMsg string) { - bridgeName := fmt.Sprintf("br%d", *testVrf.Spec.Vni) - vrf := &netlink.Vrf{LinkAttrs: netlink.LinkAttrs{Name: testVrfID}, Table: 1001} - bridge := &netlink.Bridge{LinkAttrs: netlink.LinkAttrs{Name: bridgeName}} - mockNetlink.EXPECT().LinkAdd(vrf).Return(nil).Once() - mockNetlink.EXPECT().LinkSetUp(vrf).Return(nil).Once() - mockNetlink.EXPECT().LinkAdd(bridge).Return(nil).Once() - mockNetlink.EXPECT().LinkSetMaster(bridge, vrf).Return(errors.New(errMsg)).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkSetUp(mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkAdd(mock.Anything).Return(nil).Once() + mockNetlink.EXPECT().LinkSetMaster(mock.Anything, mock.Anything).Return(errors.New(errMsg)).Once() }, }, }