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

test: use mock.Anything to simplify tests #155

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 16 additions & 34 deletions pkg/evpn/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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": {
Expand All @@ -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": {
Expand All @@ -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": {
Expand All @@ -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": {
Expand All @@ -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()
},
},
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/evpn/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
},
},
}
Expand Down
30 changes: 11 additions & 19 deletions pkg/evpn/svi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"errors"
"fmt"
"log"
"net"
"reflect"
"testing"

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

"google.golang.org/grpc"
Expand Down Expand Up @@ -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": {
Expand All @@ -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": {
Expand All @@ -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": {
Expand All @@ -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()
},
},
}
Expand Down
35 changes: 13 additions & 22 deletions pkg/evpn/vrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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": {
Expand All @@ -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": {
Expand All @@ -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": {
Expand All @@ -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": {
Expand All @@ -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()
},
},
}
Expand Down
Loading