Skip to content

Commit

Permalink
Add UT
Browse files Browse the repository at this point in the history
Signed-off-by: Wenqi Qiu <wenqiq@vmware.com>
  • Loading branch information
wenqiq committed Sep 11, 2024
1 parent ba70b39 commit 4d0596a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
7 changes: 4 additions & 3 deletions pkg/nsx/services/vpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ func (s *VPCService) GetDefaultNSXLBSPathByVPC(vpcID string) string {
return *vpcLBS.Path
}

func (vpcService *VPCService) EdgeClusterEnabled(nc *common.VPCNetworkConfigInfo) bool {
func (vpcService *VPCService) edgeClusterEnabled(nc *common.VPCNetworkConfigInfo) bool {
isRetryableError := func(err error) bool {
if err == nil {
return false
Expand All @@ -1048,7 +1048,7 @@ func (vpcService *VPCService) EdgeClusterEnabled(nc *common.VPCNetworkConfigInfo
if getErr != nil {
return getErr
}
log.Info("VPC connectivity profile retrieved", "serviceGateway", *vpcConnectivityProfile.ServiceGateway)
log.Info("VPC connectivity profile retrieved", "profile", *vpcConnectivityProfile)
return nil
}); err != nil {
log.Error(err, "Failed to retrieve VPC connectivity profile", "profile", nc.VPCConnectivityProfile)
Expand Down Expand Up @@ -1091,11 +1091,12 @@ func (vpcService *VPCService) GetLBProvider() LBProvider {
}
nc := &netConfig

edgeEnable := vpcService.EdgeClusterEnabled(nc)
edgeEnable := vpcService.edgeClusterEnabled(nc)
globalLbProvider = vpcService.getLBProvider(edgeEnable)
log.Info("lb provider", "provider", globalLbProvider)
return globalLbProvider
}

func (vpcService *VPCService) getLBProvider(edgeEnable bool) LBProvider {
// if no Alb endpoint found, return nsx-lb
// if found, and nsx lbs found, return nsx-lb
Expand Down
18 changes: 13 additions & 5 deletions pkg/nsx/services/vpc/vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func TestGetLBProvider(t *testing.T) {

// Test when globalLbProvider is none lb
globalLbProvider = NoneLB
patch1 := gomonkey.ApplyMethod(reflect.TypeOf(vpcService), "EdgeClusterEnabled", func(_ *VPCService, _ *common.VPCNetworkConfigInfo) bool {
patch1 := gomonkey.ApplyMethod(reflect.TypeOf(vpcService), "edgeClusterEnabled", func(_ *VPCService, _ *common.VPCNetworkConfigInfo) bool {
return false
})
patch2 := gomonkey.ApplyPrivateMethod(reflect.TypeOf(vpcService), "getLBProvider", func(_ *VPCService, _ bool) LBProvider {
Expand Down Expand Up @@ -480,24 +480,32 @@ func TestEdgeClusterEnabled(t *testing.T) {
patch := gomonkey.ApplyMethod(reflect.TypeOf(vpcService), "GetVpcConnectivityProfile", func(_ *VPCService, _ *common.VPCNetworkConfigInfo, _ string) (*model.VpcConnectivityProfile, error) {
return &vpcConnPrfile, nil
})
enable := vpcService.EdgeClusterEnabled(&nc)
enable := vpcService.edgeClusterEnabled(&nc)
assert.Equal(t, false, enable)
vpcConnPrfile.ServiceGateway.Enable = common.Bool(true)
enable = vpcService.EdgeClusterEnabled(&nc)
enable = vpcService.edgeClusterEnabled(&nc)
assert.Equal(t, true, enable)
patch.Reset()

patch = gomonkey.ApplyMethod(reflect.TypeOf(vpcService), "GetVpcConnectivityProfile", func(_ *VPCService, _ *common.VPCNetworkConfigInfo, _ string) (*model.VpcConnectivityProfile, error) {
return &vpcConnPrfile, apierrors.NewNotFound()
})
enable = vpcService.EdgeClusterEnabled(&nc)
enable = vpcService.edgeClusterEnabled(&nc)
assert.Equal(t, false, enable)
patch.Reset()

patch = gomonkey.ApplyMethod(reflect.TypeOf(vpcService), "GetVpcConnectivityProfile", func(_ *VPCService, _ *common.VPCNetworkConfigInfo, _ string) (*model.VpcConnectivityProfile, error) {
return &vpcConnPrfile, apierrors.NewInternalServerError()
})
enable = vpcService.EdgeClusterEnabled(&nc)
enable = vpcService.edgeClusterEnabled(&nc)
assert.Equal(t, false, enable)
patch.Reset()

// Simulate the scenario when the VpcConnectivityProfile value returned by NSX is nil.
patch = gomonkey.ApplyMethod(reflect.TypeOf(vpcService), "GetVpcConnectivityProfile", func(_ *VPCService, _ *common.VPCNetworkConfigInfo, _ string) (*model.VpcConnectivityProfile, error) {
return &model.VpcConnectivityProfile{}, nil
})
enable = vpcService.edgeClusterEnabled(&nc)
assert.Equal(t, false, enable)
patch.Reset()
}
Expand Down

0 comments on commit 4d0596a

Please sign in to comment.