Skip to content

Commit

Permalink
test: add required field tests
Browse files Browse the repository at this point in the history
Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
  • Loading branch information
glimchb committed Aug 9, 2023
1 parent 3195458 commit 7be41d1
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 3 deletions.
18 changes: 18 additions & 0 deletions pkg/evpn/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ func Test_CreateLogicalBridge(t *testing.T) {
fmt.Sprintf("user-settable ID must only contain lowercase, numbers and hyphens (%v)", "got: 'C' in position 0"),
false,
},
"no required bridge field": {
testLogicalBridgeID,
nil,
nil,
codes.Unknown,
"missing required field: logical_bridge",
false,
},
"no required vlan_id field": {
testLogicalBridgeID,
&pb.LogicalBridge{
Spec: &pb.LogicalBridgeSpec{},
},
nil,
codes.Unknown,
"missing required field: logical_bridge.spec.vlan_id",
false,
},
"illegal VlanId": {
testLogicalBridgeID,
&pb.LogicalBridge{
Expand Down
30 changes: 30 additions & 0 deletions pkg/evpn/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,36 @@ func Test_CreateBridgePort(t *testing.T) {
"",
true,
},
"no required port field": {
testBridgePortID,
nil,
nil,
codes.Unknown,
"missing required field: bridge_port",
false,
},
"no required mac_address field": {
testBridgePortID,
&pb.BridgePort{
Spec: &pb.BridgePortSpec{},
},
nil,
codes.Unknown,
"missing required field: bridge_port.spec.mac_address",
false,
},
"no required ptype field": {
testBridgePortID,
&pb.BridgePort{
Spec: &pb.BridgePortSpec{
MacAddress: []byte{0xCB, 0xB8, 0x33, 0x4C, 0x88, 0x4F},
},
},
nil,
codes.Unknown,
"missing required field: bridge_port.spec.ptype",
false,
},
"access port and list bridge": {
testBridgePortID,
&pb.BridgePort{
Expand Down
10 changes: 7 additions & 3 deletions pkg/evpn/svi.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ func (s *Server) CreateSvi(_ context.Context, in *pb.CreateSviRequest) (*pb.Svi,
},
VlanId: int(bridgeObject.Spec.VlanId),
}
if err := netlink.LinkAdd(vlandev); err != nil {
fmt.Printf("Failed to create vlan link: %v", err)
return nil, err
}
// Example: ip link set <link_svi> addr aa:bb:cc:00:00:41
if len(in.Svi.Spec.MacAddress) > 0 {
if err := netlink.LinkSetHardwareAddr(vlandev, in.Svi.Spec.MacAddress); err != nil {
Expand Down Expand Up @@ -165,19 +169,19 @@ func (s *Server) DeleteSvi(_ context.Context, in *pb.DeleteSviRequest) (*emptypb
}
resourceID := path.Base(obj.Name)
// use netlink to find vlan
vlan, err := netlink.LinkByName(resourceID)
vlandev, err := netlink.LinkByName(resourceID)
if err != nil {
err := status.Errorf(codes.NotFound, "unable to find key %s", resourceID)
log.Printf("error: %v", err)
return nil, err
}
// bring link down
if err := netlink.LinkSetDown(vlan); err != nil {
if err := netlink.LinkSetDown(vlandev); err != nil {
fmt.Printf("Failed to up link: %v", err)
return nil, err
}
// use netlink to delete vlan
if err := netlink.LinkDel(vlan); err != nil {
if err := netlink.LinkDel(vlandev); err != nil {
fmt.Printf("Failed to delete link: %v", err)
return nil, err
}
Expand Down
57 changes: 57 additions & 0 deletions pkg/evpn/svi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,63 @@ func Test_CreateSvi(t *testing.T) {
"",
true,
},
"no required svi field": {
testSviID,
nil,
nil,
codes.Unknown,
"missing required field: svi",
false,
},
"no required vrf field": {
testSviID,
&pb.Svi{
Spec: &pb.SviSpec{},
},
nil,
codes.Unknown,
"missing required field: svi.spec.vrf",
false,
},
"no required bridge field": {
testSviID,
&pb.Svi{
Spec: &pb.SviSpec{
Vrf: testVrfName,
},
},
nil,
codes.Unknown,
"missing required field: svi.spec.logical_bridge",
false,
},
"no required mac field": {
testSviID,
&pb.Svi{
Spec: &pb.SviSpec{
Vrf: testVrfName,
LogicalBridge: testLogicalBridgeName,
},
},
nil,
codes.Unknown,
"missing required field: svi.spec.mac_address",
false,
},
"no required gw ip field": {
testSviID,
&pb.Svi{
Spec: &pb.SviSpec{
Vrf: testVrfName,
LogicalBridge: testLogicalBridgeName,
MacAddress: []byte{0xCB, 0xB8, 0x33, 0x4C, 0x88, 0x4F},
},
},
nil,
codes.Unknown,
"missing required field: svi.spec.gw_ip_prefix",
false,
},
}

// run tests
Expand Down
18 changes: 18 additions & 0 deletions pkg/evpn/vrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ func Test_CreateVrf(t *testing.T) {
fmt.Sprintf("user-settable ID must only contain lowercase, numbers and hyphens (%v)", "got: 'C' in position 0"),
false,
},
"no required vrf field": {
testVrfID,
nil,
nil,
codes.Unknown,
"missing required field: vrf",
false,
},
"no required loopback_ip_prefix field": {
testVrfID,
&pb.Vrf{
Spec: &pb.VrfSpec{},
},
nil,
codes.Unknown,
"missing required field: vrf.spec.loopback_ip_prefix",
false,
},
"already exists": {
testVrfID,
&testVrf,
Expand Down

0 comments on commit 7be41d1

Please sign in to comment.