-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathclean_test.go
More file actions
264 lines (227 loc) · 10.2 KB
/
clean_test.go
File metadata and controls
264 lines (227 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package clean
import (
"context"
"errors"
"reflect"
"testing"
"github.com/agiledragon/gomonkey/v2"
"github.com/go-logr/logr"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/vmware-tanzu/nsx-operator/pkg/config"
"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/inventory"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/ipaddressallocation"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/nsxserviceaccount"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/securitypolicy"
sr "github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/staticroute"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/subnet"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/subnetbinding"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/subnetipreservation"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/subnetport"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/vpc"
)
var (
cf = &config.NSXOperatorConfig{NsxConfig: &config.NsxConfig{NsxApiManagers: []string{"10.0.0.1"}}}
)
func TestClean_ValidationFailed(t *testing.T) {
ctx := context.Background()
log := logr.Discard()
debug := false
logLevel := 0
patches := gomonkey.ApplyMethod(reflect.TypeOf(cf.NsxConfig), "ValidateConfigFromCmd", func(_ *config.NsxConfig) error {
return errors.New("validation failed")
})
defer patches.Reset()
err := Clean(ctx, cf, &log, debug, logLevel, false)
assert.Error(t, err)
assert.Contains(t, err.Error(), "validation failed")
}
func TestClean_GetClientFailed(t *testing.T) {
ctx := context.Background()
log := logr.Discard()
debug := false
logLevel := 0
patches := gomonkey.ApplyMethod(reflect.TypeOf(cf.NsxConfig), "ValidateConfigFromCmd", func(_ *config.NsxConfig) error {
return nil
})
defer patches.Reset()
patches.ApplyFunc(nsx.GetClient, func(_ *config.NSXOperatorConfig) *nsx.Client {
return nil
})
err := Clean(ctx, cf, &log, debug, logLevel, false)
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to get nsx client")
}
func TestClean_InitError(t *testing.T) {
ctx := context.Background()
log := logr.Discard()
debug := false
logLevel := 0
patches := gomonkey.ApplyMethod(reflect.TypeOf(cf.NsxConfig), "ValidateConfigFromCmd", func(_ *config.NsxConfig) error {
return nil
})
defer patches.Reset()
patches.ApplyFunc(nsx.GetClient, func(_ *config.NSXOperatorConfig) *nsx.Client {
return &nsx.Client{}
})
patches.ApplyFunc(InitializeCleanupService, func(_ *config.NSXOperatorConfig, _ *nsx.Client, _ *logr.Logger) (*CleanupService, error) {
return nil, errors.New("init cleanup service failed")
})
err := Clean(ctx, cf, &log, debug, logLevel, false)
assert.Error(t, err)
assert.Contains(t, err.Error(), "init cleanup service failed")
}
func TestClean_Cleanup(t *testing.T) {
ctx := context.Background()
debug := false
logLevel := 0
patches := gomonkey.ApplyMethod(reflect.TypeOf(cf.NsxConfig), "ValidateConfigFromCmd", func(_ *config.NsxConfig) error {
return nil
})
defer patches.Reset()
patches.ApplyFunc(nsx.GetClient, func(_ *config.NSXOperatorConfig) *nsx.Client {
return &nsx.Client{}
})
cleanupService := &CleanupService{
vpcService: &vpc.VPCService{},
}
clean := &MockCleanup{}
cleanupService.AddCleanupService(func() (interface{}, error) {
return clean, nil
})
patches.ApplyFunc(InitializeCleanupService, func(_ *config.NSXOperatorConfig, _ *nsx.Client, _ *logr.Logger) (*CleanupService, error) {
return cleanupService, nil
})
patches.ApplyMethod(reflect.TypeOf(cleanupService.vpcService), "ListAutoCreatedVPCPaths", func(_ *vpc.VPCService) sets.Set[string] {
return sets.New[string]("/orgs/default/projects/p1/vpcs/vpc-1")
})
patches.ApplyMethod(reflect.TypeOf(cleanupService.vpcService), "DeleteVPC", func(_ *vpc.VPCService, path string) error {
return nil
})
err := Clean(ctx, cf, nil, debug, logLevel, false)
assert.Nil(t, err)
assert.True(t, clean.vpcPreCleanupCalled)
assert.True(t, clean.vpcChildrenCleanupCalled)
assert.True(t, clean.infraCleanupCalled)
assert.ElementsMatch(t, []string{"/orgs/default/projects/p1/vpcs/vpc-1", ""}, clean.cleanedVPCs)
}
type MockCleanup struct {
CleanupFunc func(ctx context.Context) error
vpcPreCleanupCalled bool
vpcChildrenCleanupCalled bool
infraCleanupCalled bool
cleanedVPCs []string
}
func (m *MockCleanup) Cleanup(ctx context.Context) error {
return m.CleanupFunc(ctx)
}
func (m *MockCleanup) CleanupBeforeVPCDeletion(ctx context.Context) error {
m.vpcPreCleanupCalled = true
return nil
}
func (m *MockCleanup) CleanupVPCChildResources(ctx context.Context, vpcPath string) error {
m.vpcChildrenCleanupCalled = true
m.cleanedVPCs = append(m.cleanedVPCs, vpcPath)
return nil
}
func (m *MockCleanup) CleanupInfraResources(ctx context.Context) error {
m.infraCleanupCalled = true
return nil
}
func TestInitializeCleanupService_Success(t *testing.T) {
fakeService := common.Service{}
nsxClient := &nsx.Client{}
cf := &config.NSXOperatorConfig{
CoeConfig: &config.CoeConfig{Cluster: "test-cluster"},
}
log := logr.Discard() // Use a discard logger instead of nil
patches := gomonkey.ApplyFunc(vpc.InitializeVPC, func(service common.Service) (*vpc.VPCService, error) {
return &vpc.VPCService{}, nil
})
defer patches.Reset()
patches.ApplyFunc(subnet.InitializeSubnetService, func(service common.Service) (*subnet.SubnetService, error) {
return &subnet.SubnetService{}, nil
})
patches.ApplyFunc(securitypolicy.InitializeSecurityPolicy, func(service common.Service, vpcService common.VPCServiceProvider, forCleanup bool) (*securitypolicy.SecurityPolicyService, error) {
return &securitypolicy.SecurityPolicyService{}, nil
})
patches.ApplyFunc(sr.InitializeStaticRoute, func(service common.Service, vpcService common.VPCServiceProvider) (*sr.StaticRouteService, error) {
return &sr.StaticRouteService{}, nil
})
patches.ApplyFunc(subnetport.InitializeSubnetPort, func(service common.Service) (*subnetport.SubnetPortService, error) {
return &subnetport.SubnetPortService{}, nil
})
patches.ApplyFunc(ipaddressallocation.InitializeIPAddressAllocation, func(service common.Service, vpcService common.VPCServiceProvider, flag bool) (*ipaddressallocation.IPAddressAllocationService, error) {
return &ipaddressallocation.IPAddressAllocationService{}, nil
})
patches.ApplyFunc(subnetbinding.InitializeService, func(service common.Service) (*subnetbinding.BindingService, error) {
return &subnetbinding.BindingService{}, nil
})
patches.ApplyFunc(subnetipreservation.InitializeService, func(service common.Service) (*subnetipreservation.IPReservationService, error) {
return &subnetipreservation.IPReservationService{}, nil
})
patches.ApplyFunc(inventory.InitializeService, func(service common.Service, _ bool) (*inventory.InventoryService, error) {
return &inventory.InventoryService{}, nil
})
patches.ApplyFunc(nsxserviceaccount.InitializeNSXServiceAccount, func(service common.Service) (*nsxserviceaccount.NSXServiceAccountService, error) {
return &nsxserviceaccount.NSXServiceAccountService{}, nil
})
// Mock the NewHealthCleaner function to avoid nil pointer dereference
patches.ApplyFunc(NewHealthCleaner, func(service common.Service, log *logr.Logger, nsxClient *nsx.Client, clusterID string) *HealthCleaner {
return &HealthCleaner{
Service: fakeService,
log: log,
nsxClient: nsxClient,
clusterID: "test-cluster",
}
})
cleanupService, err := InitializeCleanupService(cf, nsxClient, &log)
assert.NoError(t, err)
assert.NotNil(t, cleanupService)
// vpcPreCleaners: SubnetPort, SubnetBinding, SubnetIPReservation, Inventory, SecurityPolicy, LBInfraCleaner, NSXServiceAccount, HealthCleaner = 8
assert.Len(t, cleanupService.vpcPreCleaners, 7)
assert.Len(t, cleanupService.vpcChildrenCleaners, 5)
assert.Len(t, cleanupService.infraCleaners, 2)
}
func TestInitializeCleanupService_VPCError(t *testing.T) {
nsxClient := &nsx.Client{}
cf := &config.NSXOperatorConfig{}
log := logr.Discard() // Use a discard logger instead of nil
expectedError := errors.New("vpc init error")
patches := gomonkey.ApplyFunc(vpc.InitializeVPC, func(service common.Service) (*vpc.VPCService, error) {
return nil, expectedError
})
defer patches.Reset()
patches.ApplyFunc(subnet.InitializeSubnetService, func(service common.Service) (*subnet.SubnetService, error) {
return &subnet.SubnetService{}, nil
})
patches.ApplyFunc(securitypolicy.InitializeSecurityPolicy, func(service common.Service, vpcService common.VPCServiceProvider, forCleanup bool) (*securitypolicy.SecurityPolicyService, error) {
return &securitypolicy.SecurityPolicyService{}, nil
})
patches.ApplyFunc(sr.InitializeStaticRoute, func(service common.Service, vpcService common.VPCServiceProvider) (*sr.StaticRouteService, error) {
return &sr.StaticRouteService{}, nil
})
patches.ApplyFunc(subnetport.InitializeSubnetPort, func(service common.Service) (*subnetport.SubnetPortService, error) {
return &subnetport.SubnetPortService{}, nil
})
patches.ApplyFunc(ipaddressallocation.InitializeIPAddressAllocation, func(service common.Service, vpcService common.VPCServiceProvider, flag bool) (*ipaddressallocation.IPAddressAllocationService, error) {
return &ipaddressallocation.IPAddressAllocationService{}, nil
})
patches.ApplyFunc(subnetbinding.InitializeService, func(service common.Service) (*subnetbinding.BindingService, error) {
return &subnetbinding.BindingService{}, nil
})
patches.ApplyFunc(subnetipreservation.InitializeService, func(service common.Service) (*subnetipreservation.IPReservationService, error) {
return &subnetipreservation.IPReservationService{}, nil
})
cleanupService, err := InitializeCleanupService(cf, nsxClient, &log)
assert.NoError(t, err)
assert.NotNil(t, cleanupService)
// Note, the services added after VPCService should fail because of the error returned in `InitializeVPC`.
assert.Len(t, cleanupService.vpcChildrenCleaners, 3)
// vpcPreCleaners: SubnetPort, SubnetBinding, SubnetIPReservation, SecurityPolicy = 4 (services initialized before VPC error)
assert.Len(t, cleanupService.vpcPreCleaners, 4)
assert.Len(t, cleanupService.infraCleaners, 1)
assert.Equal(t, expectedError, cleanupService.svcErr)
}