Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#857 from TaoZou1/v42movemock
Browse files Browse the repository at this point in the history
[cherry-pick]Move services_mock.go to pkg/mock
  • Loading branch information
TaoZou1 authored Nov 4, 2024
2 parents a6c6609 + 52d2200 commit d7b6a05
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 36 deletions.
25 changes: 13 additions & 12 deletions pkg/controllers/common/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/vmware-tanzu/nsx-operator/pkg/apis/vpc/v1alpha1"
pkg_mock "github.com/vmware-tanzu/nsx-operator/pkg/mock"
mock_client "github.com/vmware-tanzu/nsx-operator/pkg/mock/controller-runtime/client"
servicecommon "github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
)
Expand Down Expand Up @@ -83,7 +84,7 @@ func TestAllocateSubnetFromSubnetSet(t *testing.T) {
{
name: "AvailableSubnet",
prepareFunc: func(t *testing.T, vsp servicecommon.VPCServiceProvider, ssp servicecommon.SubnetServiceProvider, spsp servicecommon.SubnetPortServiceProvider) {
ssp.(*servicecommon.MockSubnetServiceProvider).On("GetSubnetsByIndex", mock.Anything, mock.Anything).
ssp.(*pkg_mock.MockSubnetServiceProvider).On("GetSubnetsByIndex", mock.Anything, mock.Anything).
Return([]*model.VpcSubnet{
{
Id: servicecommon.String("id-1"),
Expand All @@ -92,37 +93,37 @@ func TestAllocateSubnetFromSubnetSet(t *testing.T) {
IpAddresses: []string{"10.0.0.1/28"},
},
})
spsp.(*servicecommon.MockSubnetPortServiceProvider).On("GetPortsOfSubnet", mock.Anything).Return([]*model.VpcSubnetPort{})
spsp.(*pkg_mock.MockSubnetPortServiceProvider).On("GetPortsOfSubnet", mock.Anything).Return([]*model.VpcSubnetPort{})
},
expectedResult: expectedSubnetPath,
},
{
name: "ListVPCFailure",
prepareFunc: func(t *testing.T, vsp servicecommon.VPCServiceProvider, ssp servicecommon.SubnetServiceProvider, spsp servicecommon.SubnetPortServiceProvider) {
ssp.(*servicecommon.MockSubnetServiceProvider).On("GetSubnetsByIndex", mock.Anything, mock.Anything).
ssp.(*pkg_mock.MockSubnetServiceProvider).On("GetSubnetsByIndex", mock.Anything, mock.Anything).
Return([]*model.VpcSubnet{})
ssp.(*servicecommon.MockSubnetServiceProvider).On("GenerateSubnetNSTags", mock.Anything)
vsp.(*servicecommon.MockVPCServiceProvider).On("ListVPCInfo", mock.Anything).Return([]servicecommon.VPCResourceInfo{})
ssp.(*pkg_mock.MockSubnetServiceProvider).On("GenerateSubnetNSTags", mock.Anything)
vsp.(*pkg_mock.MockVPCServiceProvider).On("ListVPCInfo", mock.Anything).Return([]servicecommon.VPCResourceInfo{})
},
expectedErr: "no VPC found",
},
{
name: "CreateSubnet",
prepareFunc: func(t *testing.T, vsp servicecommon.VPCServiceProvider, ssp servicecommon.SubnetServiceProvider, spsp servicecommon.SubnetPortServiceProvider) {
ssp.(*servicecommon.MockSubnetServiceProvider).On("GetSubnetsByIndex", mock.Anything, mock.Anything).
ssp.(*pkg_mock.MockSubnetServiceProvider).On("GetSubnetsByIndex", mock.Anything, mock.Anything).
Return([]*model.VpcSubnet{})
ssp.(*servicecommon.MockSubnetServiceProvider).On("GenerateSubnetNSTags", mock.Anything)
vsp.(*servicecommon.MockVPCServiceProvider).On("ListVPCInfo", mock.Anything).Return([]servicecommon.VPCResourceInfo{{}})
ssp.(*servicecommon.MockSubnetServiceProvider).On("CreateOrUpdateSubnet", mock.Anything, mock.Anything, mock.Anything).Return(expectedSubnetPath, nil)
ssp.(*pkg_mock.MockSubnetServiceProvider).On("GenerateSubnetNSTags", mock.Anything)
vsp.(*pkg_mock.MockVPCServiceProvider).On("ListVPCInfo", mock.Anything).Return([]servicecommon.VPCResourceInfo{{}})
ssp.(*pkg_mock.MockSubnetServiceProvider).On("CreateOrUpdateSubnet", mock.Anything, mock.Anything, mock.Anything).Return(expectedSubnetPath, nil)
},
expectedResult: expectedSubnetPath,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vps := &servicecommon.MockVPCServiceProvider{}
ssp := &servicecommon.MockSubnetServiceProvider{}
spsp := &servicecommon.MockSubnetPortServiceProvider{}
vps := &pkg_mock.MockVPCServiceProvider{}
ssp := &pkg_mock.MockSubnetServiceProvider{}
spsp := &pkg_mock.MockSubnetPortServiceProvider{}
tt.prepareFunc(t, vps, ssp, spsp)
subnetPath, err := AllocateSubnetFromSubnetSet(&v1alpha1.SubnetSet{
ObjectMeta: metav1.ObjectMeta{
Expand Down
9 changes: 5 additions & 4 deletions pkg/controllers/namespace/namespace_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/vmware-tanzu/nsx-operator/pkg/config"
pkg_mock "github.com/vmware-tanzu/nsx-operator/pkg/mock"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/vpc"
Expand Down Expand Up @@ -98,16 +99,16 @@ func TestInsertNamespaceNetworkconfigBinding(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r.VPCService = &common.MockVPCServiceProvider{}
r.VPCService = &pkg_mock.MockVPCServiceProvider{}
patch := gomonkey.ApplyPrivateMethod(reflect.TypeOf(r), "getDefaultNetworkConfigName", func(_ *NamespaceReconciler, ns string, anno map[string]string) (string, error) {
return tt.ncName, tt.err
})
r.VPCService.(*common.MockVPCServiceProvider).On("RegisterNamespaceNetworkconfigBinding", mock.Anything, mock.Anything).Return(nil)
r.VPCService.(*pkg_mock.MockVPCServiceProvider).On("RegisterNamespaceNetworkconfigBinding", mock.Anything, mock.Anything).Return(nil)
err := r.insertNamespaceNetworkconfigBinding(tt.ns, tt.annos)
if err != nil {
r.VPCService.(*common.MockVPCServiceProvider).AssertNotCalled(t, "RegisterNamespaceNetworkconfigBinding")
r.VPCService.(*pkg_mock.MockVPCServiceProvider).AssertNotCalled(t, "RegisterNamespaceNetworkconfigBinding")
} else {
r.VPCService.(*common.MockVPCServiceProvider).AssertCalled(t, "RegisterNamespaceNetworkconfigBinding", tt.ns, tt.expectName)
r.VPCService.(*pkg_mock.MockVPCServiceProvider).AssertCalled(t, "RegisterNamespaceNetworkconfigBinding", tt.ns, tt.expectName)
}
patch.Reset()
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package common
package mock

import (
"github.com/stretchr/testify/mock"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
"sigs.k8s.io/controller-runtime/pkg/client"

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

type MockVPCServiceProvider struct {
Expand All @@ -14,7 +16,7 @@ func (m *MockVPCServiceProvider) GetNamespacesByNetworkconfigName(nc string) []s
return nil
}

func (m *MockVPCServiceProvider) RegisterVPCNetworkConfig(ncCRName string, info VPCNetworkConfigInfo) {
func (m *MockVPCServiceProvider) RegisterVPCNetworkConfig(ncCRName string, info common.VPCNetworkConfigInfo) {
}

func (m *MockVPCServiceProvider) RegisterNamespaceNetworkconfigBinding(ns string, ncCRName string) {
Expand All @@ -25,29 +27,29 @@ func (m *MockVPCServiceProvider) UnRegisterNamespaceNetworkconfigBinding(ns stri
m.Called(ns)
}

func (m *MockVPCServiceProvider) GetVPCNetworkConfig(ncCRName string) (VPCNetworkConfigInfo, bool) {
func (m *MockVPCServiceProvider) GetVPCNetworkConfig(ncCRName string) (common.VPCNetworkConfigInfo, bool) {
m.Called(ncCRName)
return VPCNetworkConfigInfo{}, false
return common.VPCNetworkConfigInfo{}, false
}

func (m *MockVPCServiceProvider) ValidateNetworkConfig(nc VPCNetworkConfigInfo) bool {
func (m *MockVPCServiceProvider) ValidateNetworkConfig(nc common.VPCNetworkConfigInfo) bool {
m.Called(nc)
return true
}

func (m *MockVPCServiceProvider) GetVPCNetworkConfigByNamespace(ns string) *VPCNetworkConfigInfo {
func (m *MockVPCServiceProvider) GetVPCNetworkConfigByNamespace(ns string) *common.VPCNetworkConfigInfo {
m.Called()
return nil
}

func (m *MockVPCServiceProvider) GetDefaultNetworkConfig() (bool, *VPCNetworkConfigInfo) {
func (m *MockVPCServiceProvider) GetDefaultNetworkConfig() (bool, *common.VPCNetworkConfigInfo) {
m.Called()
return false, nil
}

func (m *MockVPCServiceProvider) ListVPCInfo(ns string) []VPCResourceInfo {
func (m *MockVPCServiceProvider) ListVPCInfo(ns string) []common.VPCResourceInfo {
arg := m.Called(ns)
return arg.Get(0).([]VPCResourceInfo)
return arg.Get(0).([]common.VPCResourceInfo)
}

type MockSubnetServiceProvider struct {
Expand All @@ -67,7 +69,7 @@ func (m *MockSubnetServiceProvider) GetSubnetsByIndex(key, value string) []*mode
return arg.Get(0).([]*model.VpcSubnet)
}

func (m *MockSubnetServiceProvider) CreateOrUpdateSubnet(obj client.Object, vpcInfo VPCResourceInfo, tags []model.Tag) (string, error) {
func (m *MockSubnetServiceProvider) CreateOrUpdateSubnet(obj client.Object, vpcInfo common.VPCResourceInfo, tags []model.Tag) (string, error) {
arg := m.Called(obj, vpcInfo, tags)
return arg.Get(0).(string), arg.Error(1)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/nsx/services/securitypolicy/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/vmware-tanzu/nsx-operator/pkg/apis/legacy/v1alpha1"
"github.com/vmware-tanzu/nsx-operator/pkg/config"
"github.com/vmware-tanzu/nsx-operator/pkg/mock"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
nsxutil "github.com/vmware-tanzu/nsx-operator/pkg/nsx/util"
)
Expand Down Expand Up @@ -168,7 +169,7 @@ func TestBuildSecurityPolicyForVPC(t *testing.T) {

fakeService := fakeSecurityPolicyService()
fakeService.NSXConfig.EnableVPCNetwork = true
mockVPCService := common.MockVPCServiceProvider{}
mockVPCService := mock.MockVPCServiceProvider{}
fakeService.vpcService = &mockVPCService

// For VPC mode
Expand Down
3 changes: 2 additions & 1 deletion pkg/nsx/services/securitypolicy/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/vmware-tanzu/nsx-operator/pkg/apis/legacy/v1alpha1"
"github.com/vmware-tanzu/nsx-operator/pkg/config"
pkg_mock "github.com/vmware-tanzu/nsx-operator/pkg/mock"
mock_client "github.com/vmware-tanzu/nsx-operator/pkg/mock/controller-runtime/client"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
Expand Down Expand Up @@ -346,7 +347,7 @@ func TestExpandRule(t *testing.T) {
},
).AnyTimes()

mockVPCService := common.MockVPCServiceProvider{}
mockVPCService := pkg_mock.MockVPCServiceProvider{}
mockVPCService.On("ListVPCInfo", mock.Anything).Return([]common.VPCResourceInfo{{
OrgID: "default",
ProjectID: "pro1",
Expand Down
9 changes: 5 additions & 4 deletions pkg/nsx/services/securitypolicy/firewall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/vmware-tanzu/nsx-operator/pkg/apis/legacy/v1alpha1"
"github.com/vmware-tanzu/nsx-operator/pkg/config"
"github.com/vmware-tanzu/nsx-operator/pkg/mock"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
)

Expand Down Expand Up @@ -1070,7 +1071,7 @@ func TestCreateOrUpdateSecurityPolicy(t *testing.T) {

fakeService := fakeSecurityPolicyService()
fakeService.NSXConfig.EnableVPCNetwork = true
mockVPCService := common.MockVPCServiceProvider{}
mockVPCService := mock.MockVPCServiceProvider{}
fakeService.vpcService = &mockVPCService

podSelectorRule0IDPort000 := fakeService.buildExpandedRuleID(&spWithPodSelector, 0, common.ResourceTypeSecurityPolicy, nil)
Expand Down Expand Up @@ -1220,7 +1221,7 @@ func TestGetFinalSecurityPolicyResouce(t *testing.T) {
VPCInfo[0].VPCID = "vpc1"

fakeService := fakeSecurityPolicyService()
mockVPCService := common.MockVPCServiceProvider{}
mockVPCService := mock.MockVPCServiceProvider{}
fakeService.vpcService = &mockVPCService

type args struct {
Expand Down Expand Up @@ -1355,7 +1356,7 @@ func TestConvertNetworkPolicyToInternalSecurityPolicies(t *testing.T) {

fakeService := fakeSecurityPolicyService()
fakeService.NSXConfig.EnableVPCNetwork = true
mockVPCService := common.MockVPCServiceProvider{}
mockVPCService := mock.MockVPCServiceProvider{}
fakeService.vpcService = &mockVPCService

tests := []struct {
Expand Down Expand Up @@ -1448,7 +1449,7 @@ func TestGetFinalSecurityPolicyResouceFromNetworkPolicy(t *testing.T) {

fakeService := fakeSecurityPolicyService()
fakeService.NSXConfig.EnableVPCNetwork = true
mockVPCService := common.MockVPCServiceProvider{}
mockVPCService := mock.MockVPCServiceProvider{}
fakeService.vpcService = &mockVPCService

destinationPorts := data.NewListValue()
Expand Down
10 changes: 6 additions & 4 deletions pkg/nsx/services/staticroute/staticroute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,11 @@ func TestStaticRouteService_Cleanup(t *testing.T) {
Path: &staticRoutePath2,
}

service.StaticRouteStore.Add(staticRoute1)
service.StaticRouteStore.Add(staticRoute2)

t.Run("Successful cleanup", func(t *testing.T) {
service.StaticRouteStore.Add(staticRoute1)
service.StaticRouteStore.Add(staticRoute2)
mockStaticRouteclient = mocks.NewMockStaticRoutesClient(mockController)
service.NSXClient.StaticRouteClient = mockStaticRouteclient
mockStaticRouteclient.EXPECT().Delete("org1", "project1", "vpc1", "staticroute1").Return(nil).Times(1)
mockStaticRouteclient.EXPECT().Delete("org2", "project2", "vpc2", "staticroute2").Return(nil).Times(1)

Expand All @@ -361,7 +362,8 @@ func TestStaticRouteService_Cleanup(t *testing.T) {

t.Run("Delete static route error", func(t *testing.T) {
service.StaticRouteStore.Add(staticRoute1)
service.StaticRouteStore.Add(staticRoute2)
mockStaticRouteclient = mocks.NewMockStaticRoutesClient(mockController)
service.NSXClient.StaticRouteClient = mockStaticRouteclient

mockStaticRouteclient.EXPECT().Delete("org1", "project1", "vpc1", "staticroute1").Return(fmt.Errorf("delete error")).Times(1)

Expand Down

0 comments on commit d7b6a05

Please sign in to comment.