Skip to content

Commit

Permalink
test: prevent global test objects modification
Browse files Browse the repository at this point in the history
This is done by creating object copy.
This reduces test dependencies.

Signed-off-by: Boris Glimcher <[email protected]>
  • Loading branch information
glimchb committed Sep 5, 2023
1 parent 7784b83 commit 85b1eef
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
12 changes: 8 additions & 4 deletions pkg/evpn/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,11 @@ func Test_CreateLogicalBridge(t *testing.T) {
client := pb.NewLogicalBridgeServiceClient(conn)

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

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

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

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

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

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

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

request := &pb.GetLogicalBridgeRequest{Name: tt.in}
response, err := client.GetLogicalBridge(ctx, request)
Expand Down
12 changes: 8 additions & 4 deletions pkg/evpn/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ func Test_CreateBridgePort(t *testing.T) {
client := pb.NewBridgePortServiceClient(conn)

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

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

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

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

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

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

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

request := &pb.GetBridgePortRequest{Name: tt.in}
response, err := client.GetBridgePort(ctx, request)
Expand Down
16 changes: 10 additions & 6 deletions pkg/evpn/svi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,15 @@ func Test_CreateSvi(t *testing.T) {
client := pb.NewSviServiceClient(conn)

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

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

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

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

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

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

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

request := &pb.GetSviRequest{Name: tt.in}
response, err := client.GetSvi(ctx, request)
Expand Down
12 changes: 8 additions & 4 deletions pkg/evpn/vrf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ func Test_CreateVrf(t *testing.T) {
client := pb.NewVrfServiceClient(conn)

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

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

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

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

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

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

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

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

0 comments on commit 85b1eef

Please sign in to comment.