Skip to content

Commit

Permalink
test: move mocks into table driver unittests
Browse files Browse the repository at this point in the history
Fixing leftover TODO to refactory EXPECT
instead of switch case, move expect statements
from mockery inside each individual test

Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
  • Loading branch information
glimchb committed Sep 6, 2023
1 parent 70ae1a0 commit 2b56e0b
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 114 deletions.
98 changes: 54 additions & 44 deletions pkg/evpn/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"log"
"net"
"reflect"
"strings"
"testing"

"github.com/vishvananda/netlink"
Expand Down Expand Up @@ -60,6 +59,7 @@ func Test_CreateLogicalBridge(t *testing.T) {
errCode codes.Code
errMsg string
exist bool
on func(mockNetlink *mocks.Netlink, errMsg string)
}{
"illegal resource_id": {
id: "CapitalLettersNotAllowed",
Expand All @@ -68,6 +68,7 @@ func Test_CreateLogicalBridge(t *testing.T) {
errCode: codes.Unknown,
errMsg: fmt.Sprintf("user-settable ID must only contain lowercase, numbers and hyphens (%v)", "got: 'C' in position 0"),
exist: false,
on: nil,
},
"no required bridge field": {
id: testLogicalBridgeID,
Expand All @@ -76,6 +77,7 @@ func Test_CreateLogicalBridge(t *testing.T) {
errCode: codes.Unknown,
errMsg: "missing required field: logical_bridge",
exist: false,
on: nil,
},
"no required vlan_id field": {
id: testLogicalBridgeID,
Expand All @@ -86,6 +88,7 @@ func Test_CreateLogicalBridge(t *testing.T) {
errCode: codes.Unknown,
errMsg: "missing required field: logical_bridge.spec.vlan_id",
exist: false,
on: nil,
},
"illegal VlanId": {
id: testLogicalBridgeID,
Expand All @@ -99,6 +102,7 @@ func Test_CreateLogicalBridge(t *testing.T) {
errCode: codes.InvalidArgument,
errMsg: fmt.Sprintf("VlanId value (%v) have to be between 1 and 4095", 4096),
exist: false,
on: nil,
},
"empty vni": {
id: testLogicalBridgeID,
Expand All @@ -118,6 +122,7 @@ func Test_CreateLogicalBridge(t *testing.T) {
errCode: codes.OK,
errMsg: "",
exist: false,
on: nil,
},
"already exists": {
id: testLogicalBridgeID,
Expand All @@ -134,6 +139,9 @@ func Test_CreateLogicalBridge(t *testing.T) {
errCode: codes.NotFound,
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()
},
},
"failed LinkAdd call": {
id: testLogicalBridgeID,
Expand All @@ -142,6 +150,16 @@ func Test_CreateLogicalBridge(t *testing.T) {
errCode: codes.Unknown,
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()
},
},
"failed LinkSetMaster call": {
id: testLogicalBridgeID,
Expand All @@ -150,6 +168,16 @@ func Test_CreateLogicalBridge(t *testing.T) {
errCode: codes.Unknown,
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()
},
},
"failed LinkSetUp call": {
id: testLogicalBridgeID,
Expand All @@ -158,6 +186,17 @@ func Test_CreateLogicalBridge(t *testing.T) {
errCode: codes.Unknown,
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()
},
},
"failed BridgeVlanAdd call": {
id: testLogicalBridgeID,
Expand All @@ -166,6 +205,18 @@ func Test_CreateLogicalBridge(t *testing.T) {
errCode: codes.Unknown,
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()
},
},
}

Expand Down Expand Up @@ -199,49 +250,8 @@ func Test_CreateLogicalBridge(t *testing.T) {
tt.out = protoClone(tt.out)
tt.out.Name = testLogicalBridgeName
}

// TODO: refactor this mocking
if strings.Contains(name, "failed LinkByName") {
mockNetlink.EXPECT().LinkByName(tenantbridgeName).Return(nil, errors.New(tt.errMsg)).Once()
} else if strings.Contains(name, "failed LinkAdd") {
// 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(tt.errMsg)).Once()
} else if strings.Contains(name, "failed LinkSetMaster") {
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(tt.errMsg)).Once()
} else if strings.Contains(name, "failed LinkSetUp") {
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(tt.errMsg)).Once()
} else if strings.Contains(name, "failed BridgeVlanAdd") {
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(tt.errMsg)).Once()
if tt.on != nil {
tt.on(mockNetlink, tt.errMsg)
}

request := &pb.CreateLogicalBridgeRequest{LogicalBridge: tt.in, LogicalBridgeId: tt.id}
Expand Down
17 changes: 12 additions & 5 deletions pkg/evpn/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"log"
"reflect"
"strings"
"testing"

"google.golang.org/grpc"
Expand Down Expand Up @@ -47,6 +46,7 @@ func Test_CreateBridgePort(t *testing.T) {
errCode codes.Code
errMsg string
exist bool
on func(mockNetlink *mocks.Netlink, errMsg string)
}{
"illegal resource_id": {
id: "CapitalLettersNotAllowed",
Expand All @@ -55,6 +55,7 @@ func Test_CreateBridgePort(t *testing.T) {
errCode: codes.Unknown,
errMsg: fmt.Sprintf("user-settable ID must only contain lowercase, numbers and hyphens (%v)", "got: 'C' in position 0"),
exist: false,
on: nil,
},
"already exists": {
id: testBridgePortID,
Expand All @@ -63,6 +64,7 @@ func Test_CreateBridgePort(t *testing.T) {
errCode: codes.OK,
errMsg: "",
exist: true,
on: nil,
},
"no required port field": {
id: testBridgePortID,
Expand All @@ -71,6 +73,7 @@ func Test_CreateBridgePort(t *testing.T) {
errCode: codes.Unknown,
errMsg: "missing required field: bridge_port",
exist: false,
on: nil,
},
"no required mac_address field": {
id: testBridgePortID,
Expand All @@ -81,6 +84,7 @@ func Test_CreateBridgePort(t *testing.T) {
errCode: codes.Unknown,
errMsg: "missing required field: bridge_port.spec.mac_address",
exist: false,
on: nil,
},
"no required ptype field": {
id: testBridgePortID,
Expand All @@ -93,6 +97,7 @@ func Test_CreateBridgePort(t *testing.T) {
errCode: codes.Unknown,
errMsg: "missing required field: bridge_port.spec.ptype",
exist: false,
on: nil,
},
"access port and list bridge": {
id: testBridgePortID,
Expand All @@ -107,6 +112,7 @@ func Test_CreateBridgePort(t *testing.T) {
errCode: codes.InvalidArgument,
errMsg: fmt.Sprintf("ACCESS type must have single LogicalBridge and not (%d)", len(testBridgePort.Spec.LogicalBridges)),
exist: false,
on: nil,
},
"failed LinkByName call": {
id: testBridgePortID,
Expand All @@ -115,6 +121,9 @@ func Test_CreateBridgePort(t *testing.T) {
errCode: codes.NotFound,
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()
},
},
}

Expand Down Expand Up @@ -148,10 +157,8 @@ func Test_CreateBridgePort(t *testing.T) {
tt.out = protoClone(tt.out)
tt.out.Name = testBridgePortName
}

// TODO: refactor this mocking
if strings.Contains(name, "LinkByName") {
mockNetlink.EXPECT().LinkByName(tenantbridgeName).Return(nil, errors.New(tt.errMsg)).Once()
if tt.on != nil {
tt.on(mockNetlink, tt.errMsg)
}

request := &pb.CreateBridgePortRequest{BridgePort: tt.in, BridgePortId: tt.id}
Expand Down
Loading

0 comments on commit 2b56e0b

Please sign in to comment.