Skip to content

Commit

Permalink
Add UT test for VPC service
Browse files Browse the repository at this point in the history
Per FSS enable requirement, UT coverage should be over 70%.
  • Loading branch information
seanpang-vmware committed Sep 29, 2024
1 parent aa84b47 commit 5b63649
Show file tree
Hide file tree
Showing 3 changed files with 347 additions and 8 deletions.
105 changes: 105 additions & 0 deletions pkg/nsx/services/vpc/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,108 @@ func TestBuildNSXVPC(t *testing.T) {
})
}
}

func Test_combineVPCIDAndLBSID(t *testing.T) {
type args struct {
vpcID string
lbsID string
}
tests := []struct {
name string
args args
want string
}{
{
name: "pass",
args: args{
vpcID: "fakeVpc",
lbsID: "fakeLbs",
},
want: "fakeVpc_fakeLbs",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := combineVPCIDAndLBSID(tt.args.vpcID, tt.args.lbsID); got != tt.want {
t.Errorf("combineVPCIDAndLBSID() = %v, want %v", got, tt.want)
}
})
}
}

func Test_generateLBSKey(t *testing.T) {
emptyPath := ""
emptyVpcPath := "/fake/path/empty/vpc/"
okPath := "/fake/path/vpc/fake-vpc"
okId := "fake-id"
type args struct {
lbs model.LBService
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "nil connectivity path",
args: args{
lbs: model.LBService{ConnectivityPath: nil},
},
want: "",
wantErr: true,
},
{
name: "empty connectivity path",
args: args{
lbs: model.LBService{ConnectivityPath: &emptyPath},
},
want: "",
wantErr: true,
},
{
name: "empty vpc id",
args: args{
lbs: model.LBService{ConnectivityPath: &emptyVpcPath},
},
want: "",
wantErr: true,
},
{
name: "nil lbs id",
args: args{
lbs: model.LBService{ConnectivityPath: &okPath, Id: nil},
},
want: "",
wantErr: true,
},
{
name: "empty lbs id",
args: args{
lbs: model.LBService{ConnectivityPath: &okPath, Id: &emptyPath},
},
want: "",
wantErr: true,
},
{
name: "empty lbs id",
args: args{
lbs: model.LBService{ConnectivityPath: &okPath, Id: &okId},
},
want: "fake-vpc_fake-id",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := generateLBSKey(tt.args.lbs)
if (err != nil) != tt.wantErr {
t.Errorf("generateLBSKey() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("generateLBSKey() = %v, want %v", got, tt.want)
}
})
}
}
45 changes: 45 additions & 0 deletions pkg/nsx/services/vpc/compare_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package vpc

import (
"testing"

"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"

"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
)

func TestIsVPCChanged(t *testing.T) {
type args struct {
nc common.VPCNetworkConfigInfo
vpc *model.Vpc
}
tests := []struct {
name string
args args
want bool
}{
{
name: "no change",
args: args{
nc: common.VPCNetworkConfigInfo{PrivateIPs: []string{"1.1.1.1"}},
vpc: &model.Vpc{PrivateIps: []string{"1.1.1.1"}},
},
want: false,
},
{
name: "changed",
args: args{
nc: common.VPCNetworkConfigInfo{PrivateIPs: []string{"1.1.1.1", "2.2.2.2"}},
vpc: &model.Vpc{PrivateIps: []string{"1.1.1.1"}},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsVPCChanged(tt.args.nc, tt.args.vpc); got != tt.want {
t.Errorf("IsVPCChanged() = %v, want %v", got, tt.want)
}
})
}
}
Loading

0 comments on commit 5b63649

Please sign in to comment.