Skip to content

Commit

Permalink
test(svi): missing bridge and vrf resourcename
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 30, 2023
1 parent 74b9eca commit 46a842c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
36 changes: 15 additions & 21 deletions pkg/evpn/svi.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,27 @@ func (s *Server) CreateSvi(_ context.Context, in *pb.CreateSviRequest) (*pb.Svi,
log.Printf("error: %v", err)
return nil, err
}
// not found, so create a new one
bridge, err := s.nLink.LinkByName(tenantbridgeName)
if err != nil {
err := status.Errorf(codes.NotFound, "unable to find key %s", tenantbridgeName)
log.Printf("error: %v", err)
return nil, err
}
// now get LogicalBridge object to fetch VID field
bridgeObject, ok := s.Bridges[in.Svi.Spec.LogicalBridge]
if !ok {
err := status.Errorf(codes.NotFound, "unable to find key %s", in.Svi.Spec.LogicalBridge)
log.Printf("error: %v", err)
return nil, err
}
// now get Vrf to plug this vlandev into
vrf, ok := s.Vrfs[in.Svi.Spec.Vrf]
if !ok {
err := status.Errorf(codes.NotFound, "unable to find key %s", in.Svi.Spec.Vrf)
log.Printf("error: %v", err)
return nil, err
}
// not found, so create a new one
bridge, err := s.nLink.LinkByName(tenantbridgeName)
if err != nil {
err := status.Errorf(codes.NotFound, "unable to find key %s", tenantbridgeName)
log.Printf("error: %v", err)
return nil, err
}
vid := uint16(bridgeObject.Spec.VlanId)
// Example: bridge vlan add dev br-tenant vid <vlan-id> self
if err := s.nLink.BridgeVlanAdd(bridge, vid, false, false, true, false); err != nil {
Expand All @@ -85,13 +92,7 @@ func (s *Server) CreateSvi(_ context.Context, in *pb.CreateSviRequest) (*pb.Svi,
}
// Example: ip link add link br-tenant name <link_svi> type vlan id <vlan-id>
vlanName := fmt.Sprintf("vlan%d", vid)
vlandev := &netlink.Vlan{
LinkAttrs: netlink.LinkAttrs{
Name: vlanName,
ParentIndex: bridge.Attrs().Index,
},
VlanId: int(bridgeObject.Spec.VlanId),
}
vlandev := &netlink.Vlan{LinkAttrs: netlink.LinkAttrs{Name: vlanName, ParentIndex: bridge.Attrs().Index}, VlanId: int(vid)}
log.Printf("Creating VLAN %v", vlandev)
if err := s.nLink.LinkAdd(vlandev); err != nil {
fmt.Printf("Failed to create vlan link: %v", err)
Expand All @@ -115,13 +116,6 @@ func (s *Server) CreateSvi(_ context.Context, in *pb.CreateSviRequest) (*pb.Svi,
return nil, err
}
}
// now get Vrf to plug this vlandev into
vrf, ok := s.Vrfs[in.Svi.Spec.Vrf]
if !ok {
err := status.Errorf(codes.NotFound, "unable to find key %s", in.Svi.Spec.Vrf)
log.Printf("error: %v", err)
return nil, err
}
// get net device by name
vrfdev, err := s.nLink.LinkByName(path.Base(vrf.Name))
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions pkg/evpn/svi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,36 @@ func Test_CreateSvi(t *testing.T) {
fmt.Sprintf("segment '%s': not a valid DNS name", "-ABC-DEF"),
false,
},
"missing LogicalBridge name": {
testSviID,
&pb.Svi{
Spec: &pb.SviSpec{
Vrf: testVrfName,
LogicalBridge: "unknown-bridge-id",
MacAddress: []byte{0xCB, 0xB8, 0x33, 0x4C, 0x88, 0x4F},
GwIpPrefix: []*pc.IPPrefix{{Len: 24}},
},
},
nil,
codes.NotFound,
fmt.Sprintf("unable to find key %v", "unknown-bridge-id"),
false,
},
"missing Vrf name": {
testSviID,
&pb.Svi{
Spec: &pb.SviSpec{
Vrf: "unknown-vrf-id",
LogicalBridge: testLogicalBridgeName,
MacAddress: []byte{0xCB, 0xB8, 0x33, 0x4C, 0x88, 0x4F},
GwIpPrefix: []*pc.IPPrefix{{Len: 24}},
},
},
nil,
codes.NotFound,
fmt.Sprintf("unable to find key %v", "unknown-vrf-id"),
false,
},
"failed LinkByName call": {
testSviID,
&testSvi,
Expand Down Expand Up @@ -218,6 +248,7 @@ func Test_CreateSvi(t *testing.T) {
if tt.out != nil {
tt.out.Name = testSviName
}
opi.Vrfs[testVrfName] = &testVrf
opi.Bridges[testLogicalBridgeName] = &testLogicalBridge

// TODO: refactor this mocking
Expand Down

0 comments on commit 46a842c

Please sign in to comment.