Skip to content

Commit 7ee3892

Browse files
Merge pull request vmware-tanzu#780 from seanpang-vmware/addvpcut
Add UT test for VPC service
2 parents eaaad7c + 5b63649 commit 7ee3892

File tree

3 files changed

+347
-8
lines changed

3 files changed

+347
-8
lines changed

pkg/nsx/services/vpc/builder_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,108 @@ func TestBuildNSXVPC(t *testing.T) {
205205
})
206206
}
207207
}
208+
209+
func Test_combineVPCIDAndLBSID(t *testing.T) {
210+
type args struct {
211+
vpcID string
212+
lbsID string
213+
}
214+
tests := []struct {
215+
name string
216+
args args
217+
want string
218+
}{
219+
{
220+
name: "pass",
221+
args: args{
222+
vpcID: "fakeVpc",
223+
lbsID: "fakeLbs",
224+
},
225+
want: "fakeVpc_fakeLbs",
226+
},
227+
}
228+
for _, tt := range tests {
229+
t.Run(tt.name, func(t *testing.T) {
230+
if got := combineVPCIDAndLBSID(tt.args.vpcID, tt.args.lbsID); got != tt.want {
231+
t.Errorf("combineVPCIDAndLBSID() = %v, want %v", got, tt.want)
232+
}
233+
})
234+
}
235+
}
236+
237+
func Test_generateLBSKey(t *testing.T) {
238+
emptyPath := ""
239+
emptyVpcPath := "/fake/path/empty/vpc/"
240+
okPath := "/fake/path/vpc/fake-vpc"
241+
okId := "fake-id"
242+
type args struct {
243+
lbs model.LBService
244+
}
245+
tests := []struct {
246+
name string
247+
args args
248+
want string
249+
wantErr bool
250+
}{
251+
{
252+
name: "nil connectivity path",
253+
args: args{
254+
lbs: model.LBService{ConnectivityPath: nil},
255+
},
256+
want: "",
257+
wantErr: true,
258+
},
259+
{
260+
name: "empty connectivity path",
261+
args: args{
262+
lbs: model.LBService{ConnectivityPath: &emptyPath},
263+
},
264+
want: "",
265+
wantErr: true,
266+
},
267+
{
268+
name: "empty vpc id",
269+
args: args{
270+
lbs: model.LBService{ConnectivityPath: &emptyVpcPath},
271+
},
272+
want: "",
273+
wantErr: true,
274+
},
275+
{
276+
name: "nil lbs id",
277+
args: args{
278+
lbs: model.LBService{ConnectivityPath: &okPath, Id: nil},
279+
},
280+
want: "",
281+
wantErr: true,
282+
},
283+
{
284+
name: "empty lbs id",
285+
args: args{
286+
lbs: model.LBService{ConnectivityPath: &okPath, Id: &emptyPath},
287+
},
288+
want: "",
289+
wantErr: true,
290+
},
291+
{
292+
name: "empty lbs id",
293+
args: args{
294+
lbs: model.LBService{ConnectivityPath: &okPath, Id: &okId},
295+
},
296+
want: "fake-vpc_fake-id",
297+
wantErr: false,
298+
},
299+
}
300+
for _, tt := range tests {
301+
t.Run(tt.name, func(t *testing.T) {
302+
got, err := generateLBSKey(tt.args.lbs)
303+
if (err != nil) != tt.wantErr {
304+
t.Errorf("generateLBSKey() error = %v, wantErr %v", err, tt.wantErr)
305+
return
306+
}
307+
if got != tt.want {
308+
t.Errorf("generateLBSKey() = %v, want %v", got, tt.want)
309+
}
310+
})
311+
}
312+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package vpc
2+
3+
import (
4+
"testing"
5+
6+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
7+
8+
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
9+
)
10+
11+
func TestIsVPCChanged(t *testing.T) {
12+
type args struct {
13+
nc common.VPCNetworkConfigInfo
14+
vpc *model.Vpc
15+
}
16+
tests := []struct {
17+
name string
18+
args args
19+
want bool
20+
}{
21+
{
22+
name: "no change",
23+
args: args{
24+
nc: common.VPCNetworkConfigInfo{PrivateIPs: []string{"1.1.1.1"}},
25+
vpc: &model.Vpc{PrivateIps: []string{"1.1.1.1"}},
26+
},
27+
want: false,
28+
},
29+
{
30+
name: "changed",
31+
args: args{
32+
nc: common.VPCNetworkConfigInfo{PrivateIPs: []string{"1.1.1.1", "2.2.2.2"}},
33+
vpc: &model.Vpc{PrivateIps: []string{"1.1.1.1"}},
34+
},
35+
want: true,
36+
},
37+
}
38+
for _, tt := range tests {
39+
t.Run(tt.name, func(t *testing.T) {
40+
if got := IsVPCChanged(tt.args.nc, tt.args.vpc); got != tt.want {
41+
t.Errorf("IsVPCChanged() = %v, want %v", got, tt.want)
42+
}
43+
})
44+
}
45+
}

0 commit comments

Comments
 (0)