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

refactor: add ProtoClone helper function to clone gRPC structs #152

Merged
merged 1 commit into from
Sep 5, 2023
Merged
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
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 @@
"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 @@
}
// 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 @@
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 @@
"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 @@
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 @@
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 @@
"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 @@
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 @@
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 @@
"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 @@
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 @@
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
Loading