Skip to content

Commit

Permalink
refactor: add ProtoClone helper function to clone gRPC structs
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 Sep 5, 2023
1 parent 85b1eef commit d82fb3c
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 38 deletions.
5 changes: 2 additions & 3 deletions pkg/evpn/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"go.einride.tech/aip/resourcename"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
)

Expand Down Expand Up @@ -94,7 +93,7 @@ func (s *Server) CreateLogicalBridge(_ context.Context, in *pb.CreateLogicalBrid
}
// TODO: bridge link set dev vxlan-<LB-vlan-id> neigh_suppress on
}
response := proto.Clone(in.LogicalBridge).(*pb.LogicalBridge)
response := protoClone(in.LogicalBridge)
response.Status = &pb.LogicalBridgeStatus{OperStatus: pb.LBOperStatus_LB_OPER_STATUS_UP}
s.Bridges[in.LogicalBridge.Name] = response
log.Printf("CreateLogicalBridge: Sending to client: %v", response)
Expand Down Expand Up @@ -198,7 +197,7 @@ func (s *Server) UpdateLogicalBridge(_ context.Context, in *pb.UpdateLogicalBrid
return nil, err
}
}
response := proto.Clone(in.LogicalBridge).(*pb.LogicalBridge)
response := protoClone(in.LogicalBridge)

Check warning on line 200 in pkg/evpn/bridge.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/bridge.go#L200

Added line #L200 was not covered by tests
response.Status = &pb.LogicalBridgeStatus{OperStatus: pb.LBOperStatus_LB_OPER_STATUS_UP}
s.Bridges[in.LogicalBridge.Name] = response
log.Printf("UpdateLogicalBridge: Sending to client: %v", response)
Expand Down
12 changes: 6 additions & 6 deletions pkg/evpn/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ func Test_CreateLogicalBridge(t *testing.T) {
client := pb.NewLogicalBridgeServiceClient(conn)

if tt.exist {
opi.Bridges[testLogicalBridgeName] = proto.Clone(&testLogicalBridge).(*pb.LogicalBridge)
opi.Bridges[testLogicalBridgeName] = protoClone(&testLogicalBridge)
opi.Bridges[testLogicalBridgeName].Name = testLogicalBridgeName
}
if tt.out != nil {
tt.out = proto.Clone(tt.out).(*pb.LogicalBridge)
tt.out = protoClone(tt.out)
tt.out.Name = testLogicalBridgeName
}

Expand Down Expand Up @@ -325,7 +325,7 @@ func Test_DeleteLogicalBridge(t *testing.T) {
client := pb.NewLogicalBridgeServiceClient(conn)

fname1 := resourceIDToFullName("bridges", tt.in)
opi.Bridges[testLogicalBridgeName] = proto.Clone(&testLogicalBridge).(*pb.LogicalBridge)
opi.Bridges[testLogicalBridgeName] = protoClone(&testLogicalBridge)

request := &pb.DeleteLogicalBridgeRequest{Name: fname1, AllowMissing: tt.missing}
response, err := client.DeleteLogicalBridge(ctx, request)
Expand Down Expand Up @@ -414,11 +414,11 @@ func Test_UpdateLogicalBridge(t *testing.T) {
client := pb.NewLogicalBridgeServiceClient(conn)

if tt.exist {
opi.Bridges[testLogicalBridgeName] = proto.Clone(&testLogicalBridge).(*pb.LogicalBridge)
opi.Bridges[testLogicalBridgeName] = protoClone(&testLogicalBridge)
opi.Bridges[testLogicalBridgeName].Name = testLogicalBridgeName
}
if tt.out != nil {
tt.out = proto.Clone(tt.out).(*pb.LogicalBridge)
tt.out = protoClone(tt.out)
tt.out.Name = testLogicalBridgeName
}

Expand Down Expand Up @@ -494,7 +494,7 @@ func Test_GetLogicalBridge(t *testing.T) {
}(conn)
client := pb.NewLogicalBridgeServiceClient(conn)

opi.Bridges[testLogicalBridgeID] = proto.Clone(&testLogicalBridge).(*pb.LogicalBridge)
opi.Bridges[testLogicalBridgeID] = protoClone(&testLogicalBridge)

request := &pb.GetLogicalBridgeRequest{Name: tt.in}
response, err := client.GetLogicalBridge(ctx, request)
Expand Down
6 changes: 6 additions & 0 deletions pkg/evpn/evpn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"fmt"
"log"

"google.golang.org/protobuf/proto"

pe "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"

"github.com/opiproject/opi-evpn-bridge/pkg/utils"
Expand Down Expand Up @@ -57,6 +59,10 @@ func resourceIDToFullName(container string, resourceID string) string {
return fmt.Sprintf("//network.opiproject.org/%s/%s", container, resourceID)
}

func protoClone[T proto.Message](protoStruct T) T {
return proto.Clone(protoStruct).(T)
}

func generateRandMAC() ([]byte, error) {
buf := make([]byte, 6)
if _, err := rand.Read(buf); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions pkg/evpn/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"go.einride.tech/aip/resourcename"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
)

Expand Down Expand Up @@ -119,7 +118,7 @@ func (s *Server) CreateBridgePort(_ context.Context, in *pb.CreateBridgePortRequ
fmt.Printf("Failed to up iface link: %v", err)
return nil, err
}
response := proto.Clone(in.BridgePort).(*pb.BridgePort)
response := protoClone(in.BridgePort)

Check warning on line 121 in pkg/evpn/port.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/port.go#L121

Added line #L121 was not covered by tests
response.Status = &pb.BridgePortStatus{OperStatus: pb.BPOperStatus_BP_OPER_STATUS_UP}
s.Ports[in.BridgePort.Name] = response
log.Printf("CreateBridgePort: Sending to client: %v", response)
Expand Down Expand Up @@ -226,7 +225,7 @@ func (s *Server) UpdateBridgePort(_ context.Context, in *pb.UpdateBridgePortRequ
fmt.Printf("Failed to update link: %v", err)
return nil, err
}
response := proto.Clone(in.BridgePort).(*pb.BridgePort)
response := protoClone(in.BridgePort)

Check warning on line 228 in pkg/evpn/port.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/port.go#L228

Added line #L228 was not covered by tests
response.Status = &pb.BridgePortStatus{OperStatus: pb.BPOperStatus_BP_OPER_STATUS_UP}
s.Ports[in.BridgePort.Name] = response
log.Printf("UpdateBridgePort: Sending to client: %v", response)
Expand Down
12 changes: 6 additions & 6 deletions pkg/evpn/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ func Test_CreateBridgePort(t *testing.T) {
client := pb.NewBridgePortServiceClient(conn)

if tt.exist {
opi.Ports[testBridgePortName] = proto.Clone(&testBridgePort).(*pb.BridgePort)
opi.Ports[testBridgePortName] = protoClone(&testBridgePort)
opi.Ports[testBridgePortName].Name = testBridgePortName
}
if tt.out != nil {
tt.out = proto.Clone(tt.out).(*pb.BridgePort)
tt.out = protoClone(tt.out)
tt.out.Name = testBridgePortName
}

Expand Down Expand Up @@ -235,7 +235,7 @@ func Test_DeleteBridgePort(t *testing.T) {
client := pb.NewBridgePortServiceClient(conn)

fname1 := resourceIDToFullName("ports", tt.in)
opi.Ports[testBridgePortName] = proto.Clone(&testBridgePort).(*pb.BridgePort)
opi.Ports[testBridgePortName] = protoClone(&testBridgePort)

request := &pb.DeleteBridgePortRequest{Name: fname1, AllowMissing: tt.missing}
response, err := client.DeleteBridgePort(ctx, request)
Expand Down Expand Up @@ -325,11 +325,11 @@ func Test_UpdateBridgePort(t *testing.T) {
client := pb.NewBridgePortServiceClient(conn)

if tt.exist {
opi.Ports[testBridgePortName] = proto.Clone(&testBridgePort).(*pb.BridgePort)
opi.Ports[testBridgePortName] = protoClone(&testBridgePort)
opi.Ports[testBridgePortName].Name = testBridgePortName
}
if tt.out != nil {
tt.out = proto.Clone(tt.out).(*pb.BridgePort)
tt.out = protoClone(tt.out)
tt.out.Name = testBridgePortName
}

Expand Down Expand Up @@ -405,7 +405,7 @@ func Test_GetBridgePort(t *testing.T) {
}(conn)
client := pb.NewBridgePortServiceClient(conn)

opi.Ports[testBridgePortID] = proto.Clone(&testBridgePort).(*pb.BridgePort)
opi.Ports[testBridgePortID] = protoClone(&testBridgePort)

request := &pb.GetBridgePortRequest{Name: tt.in}
response, err := client.GetBridgePort(ctx, request)
Expand Down
5 changes: 2 additions & 3 deletions pkg/evpn/svi.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"go.einride.tech/aip/resourcename"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
)

Expand Down Expand Up @@ -133,7 +132,7 @@ func (s *Server) CreateSvi(_ context.Context, in *pb.CreateSviRequest) (*pb.Svi,
fmt.Printf("Failed to up link: %v", err)
return nil, err
}
response := proto.Clone(in.Svi).(*pb.Svi)
response := protoClone(in.Svi)

Check warning on line 135 in pkg/evpn/svi.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/svi.go#L135

Added line #L135 was not covered by tests
response.Status = &pb.SviStatus{OperStatus: pb.SVIOperStatus_SVI_OPER_STATUS_UP}
s.Svis[in.Svi.Name] = response
log.Printf("CreateSvi: Sending to client: %v", response)
Expand Down Expand Up @@ -252,7 +251,7 @@ func (s *Server) UpdateSvi(_ context.Context, in *pb.UpdateSviRequest) (*pb.Svi,
fmt.Printf("Failed to update link: %v", err)
return nil, err
}
response := proto.Clone(in.Svi).(*pb.Svi)
response := protoClone(in.Svi)

Check warning on line 254 in pkg/evpn/svi.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/svi.go#L254

Added line #L254 was not covered by tests
response.Status = &pb.SviStatus{OperStatus: pb.SVIOperStatus_SVI_OPER_STATUS_UP}
s.Svis[in.Svi.Name] = response
log.Printf("UpdateSvi: Sending to client: %v", response)
Expand Down
16 changes: 8 additions & 8 deletions pkg/evpn/svi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,15 @@ func Test_CreateSvi(t *testing.T) {
client := pb.NewSviServiceClient(conn)

if tt.exist {
opi.Svis[testSviName] = proto.Clone(&testSvi).(*pb.Svi)
opi.Svis[testSviName] = protoClone(&testSvi)
opi.Svis[testSviName].Name = testSviName
}
if tt.out != nil {
tt.out = proto.Clone(tt.out).(*pb.Svi)
tt.out = protoClone(tt.out)
tt.out.Name = testSviName
}
opi.Vrfs[testVrfName] = proto.Clone(&testVrf).(*pb.Vrf)
opi.Bridges[testLogicalBridgeName] = proto.Clone(&testLogicalBridge).(*pb.LogicalBridge)
opi.Vrfs[testVrfName] = protoClone(&testVrf)
opi.Bridges[testLogicalBridgeName] = protoClone(&testLogicalBridge)

// TODO: refactor this mocking
if strings.Contains(name, "failed LinkByName") {
Expand Down Expand Up @@ -367,7 +367,7 @@ func Test_DeleteSvi(t *testing.T) {
client := pb.NewSviServiceClient(conn)

fname1 := resourceIDToFullName("svis", tt.in)
opi.Svis[testSviName] = proto.Clone(&testSvi).(*pb.Svi)
opi.Svis[testSviName] = protoClone(&testSvi)

request := &pb.DeleteSviRequest{Name: fname1, AllowMissing: tt.missing}
response, err := client.DeleteSvi(ctx, request)
Expand Down Expand Up @@ -458,11 +458,11 @@ func Test_UpdateSvi(t *testing.T) {
client := pb.NewSviServiceClient(conn)

if tt.exist {
opi.Svis[testSviName] = proto.Clone(&testSvi).(*pb.Svi)
opi.Svis[testSviName] = protoClone(&testSvi)
opi.Svis[testSviName].Name = testSviName
}
if tt.out != nil {
tt.out = proto.Clone(tt.out).(*pb.Svi)
tt.out = protoClone(tt.out)
tt.out.Name = testSviName
}

Expand Down Expand Up @@ -538,7 +538,7 @@ func Test_GetSvi(t *testing.T) {
}(conn)
client := pb.NewSviServiceClient(conn)

opi.Svis[testSviName] = proto.Clone(&testSvi).(*pb.Svi)
opi.Svis[testSviName] = protoClone(&testSvi)

request := &pb.GetSviRequest{Name: tt.in}
response, err := client.GetSvi(ctx, request)
Expand Down
5 changes: 2 additions & 3 deletions pkg/evpn/vrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"go.einride.tech/aip/resourcename"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/emptypb"
)

Expand Down Expand Up @@ -140,7 +139,7 @@ func (s *Server) CreateVrf(_ context.Context, in *pb.CreateVrfRequest) (*pb.Vrf,
return nil, err
}
}
response := proto.Clone(in.Vrf).(*pb.Vrf)
response := protoClone(in.Vrf)
response.Status = &pb.VrfStatus{LocalAs: 4, RoutingTable: tableID, Rmac: mac}
s.Vrfs[in.Vrf.Name] = response
log.Printf("CreateVrf: Sending to client: %v", response)
Expand Down Expand Up @@ -274,7 +273,7 @@ func (s *Server) UpdateVrf(_ context.Context, in *pb.UpdateVrfRequest) (*pb.Vrf,
fmt.Printf("Failed to update link: %v", err)
return nil, err
}
response := proto.Clone(in.Vrf).(*pb.Vrf)
response := protoClone(in.Vrf)

Check warning on line 276 in pkg/evpn/vrf.go

View check run for this annotation

Codecov / codecov/patch

pkg/evpn/vrf.go#L276

Added line #L276 was not covered by tests
response.Status = &pb.VrfStatus{LocalAs: 4}
s.Vrfs[in.Vrf.Name] = response
log.Printf("UpdateVrf: Sending to client: %v", response)
Expand Down
12 changes: 6 additions & 6 deletions pkg/evpn/vrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ func Test_CreateVrf(t *testing.T) {
client := pb.NewVrfServiceClient(conn)

if tt.exist {
opi.Vrfs[testVrfName] = proto.Clone(&testVrf).(*pb.Vrf)
opi.Vrfs[testVrfName] = protoClone(&testVrf)
opi.Vrfs[testVrfName].Name = testVrfName
}
if tt.out != nil {
tt.out = proto.Clone(tt.out).(*pb.Vrf)
tt.out = protoClone(tt.out)
tt.out.Name = testVrfName
}

Expand Down Expand Up @@ -298,7 +298,7 @@ func Test_DeleteVrf(t *testing.T) {
client := pb.NewVrfServiceClient(conn)

fname1 := resourceIDToFullName("vrfs", tt.in)
opi.Vrfs[testVrfName] = proto.Clone(&testVrf).(*pb.Vrf)
opi.Vrfs[testVrfName] = protoClone(&testVrf)

request := &pb.DeleteVrfRequest{Name: fname1, AllowMissing: tt.missing}
response, err := client.DeleteVrf(ctx, request)
Expand Down Expand Up @@ -389,11 +389,11 @@ func Test_UpdateVrf(t *testing.T) {
client := pb.NewVrfServiceClient(conn)

if tt.exist {
opi.Vrfs[testVrfName] = proto.Clone(&testVrf).(*pb.Vrf)
opi.Vrfs[testVrfName] = protoClone(&testVrf)
opi.Vrfs[testVrfName].Name = testVrfName
}
if tt.out != nil {
tt.out = proto.Clone(tt.out).(*pb.Vrf)
tt.out = protoClone(tt.out)
tt.out.Name = testVrfName
}

Expand Down Expand Up @@ -469,7 +469,7 @@ func Test_GetVrf(t *testing.T) {
}(conn)
client := pb.NewVrfServiceClient(conn)

opi.Vrfs[testVrfName] = proto.Clone(&testVrf).(*pb.Vrf)
opi.Vrfs[testVrfName] = protoClone(&testVrf)

request := &pb.GetVrfRequest{Name: tt.in}
response, err := client.GetVrf(ctx, request)
Expand Down

0 comments on commit d82fb3c

Please sign in to comment.