@@ -27,13 +27,12 @@ import (
27
27
apierrors "k8s.io/apimachinery/pkg/api/errors"
28
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
29
"k8s.io/apimachinery/pkg/runtime"
30
+ clientgotesting "k8s.io/client-go/testing"
30
31
cloudprovider "k8s.io/cloud-provider"
31
- "sigs.k8s.io/controller-runtime/pkg/client"
32
- fakeClient "sigs.k8s.io/controller-runtime/pkg/client/fake"
32
+ fakevmclient "k8s.io/cloud-provider-vsphere/pkg/cloudprovider/vsphereparavirtual/vmop/clientset/versioned/fake"
33
33
"sigs.k8s.io/controller-runtime/pkg/envtest"
34
34
35
35
"k8s.io/cloud-provider-vsphere/pkg/cloudprovider/vsphereparavirtual/vmservice"
36
- "k8s.io/cloud-provider-vsphere/pkg/util"
37
36
38
37
vmopv1alpha1 "github.com/vmware-tanzu/vm-operator-api/api/v1alpha1"
39
38
)
@@ -51,15 +50,13 @@ var (
51
50
}
52
51
)
53
52
54
- func newTestLoadBalancer () (cloudprovider.LoadBalancer , * util.FakeClientWrapper ) {
55
- scheme := runtime .NewScheme ()
56
- _ = vmopv1alpha1 .AddToScheme (scheme )
57
- fc := fakeClient .NewClientBuilder ().WithScheme (scheme ).Build ()
58
- fcw := util .NewFakeClientWrapper (fc )
59
- vms := vmservice .NewVMService (fcw , testClusterNameSpace , & testOwnerReference )
53
+ func newTestLoadBalancer () (cloudprovider.LoadBalancer , * fakevmclient.Clientset ) {
54
+ fc := fakevmclient .NewSimpleClientset ()
55
+
56
+ vms := vmservice .NewVMService (fc , testClusterNameSpace , & testOwnerReference )
60
57
return & loadBalancer {
61
58
vmService : vms ,
62
- }, fcw
59
+ }, fc
63
60
}
64
61
65
62
func TestNewLoadBalancer (t * testing.T ) {
@@ -155,7 +152,7 @@ func TestUpdateLoadBalancer(t *testing.T) {
155
152
156
153
for _ , testCase := range testCases {
157
154
t .Run (testCase .name , func (t * testing.T ) {
158
- lb , fcw := newTestLoadBalancer ()
155
+ lb , fc := newTestLoadBalancer ()
159
156
testK8sService := & v1.Service {
160
157
ObjectMeta : metav1.ObjectMeta {
161
158
Name : testK8sServiceName ,
@@ -181,9 +178,9 @@ func TestUpdateLoadBalancer(t *testing.T) {
181
178
182
179
if testCase .expectErr {
183
180
// Ensure that the client Update call returns an error on update
184
- fcw . UpdateFunc = func (ctx context. Context , obj client .Object , opts ... client. UpdateOption ) error {
185
- return fmt .Errorf ("Some undefined update error" )
186
- }
181
+ fc . PrependReactor ( "update" , "virtualmachineservices" , func (action clientgotesting. Action ) ( handled bool , ret runtime .Object , err error ) {
182
+ return true , & vmopv1alpha1. VirtualMachineService {}, fmt .Errorf ("Some undefined update error" )
183
+ })
187
184
err = lb .UpdateLoadBalancer (context .Background (), testClustername , testK8sService , []* v1.Node {})
188
185
assert .Error (t , err )
189
186
} else {
@@ -195,7 +192,7 @@ func TestUpdateLoadBalancer(t *testing.T) {
195
192
}
196
193
197
194
func TestEnsureLoadBalancer_VMServiceExternalTrafficPolicyLocal (t * testing.T ) {
198
- lb , fcw := newTestLoadBalancer ()
195
+ lb , fc := newTestLoadBalancer ()
199
196
testK8sService := & v1.Service {
200
197
ObjectMeta : metav1.ObjectMeta {
201
198
Name : testK8sServiceName ,
@@ -205,8 +202,9 @@ func TestEnsureLoadBalancer_VMServiceExternalTrafficPolicyLocal(t *testing.T) {
205
202
ExternalTrafficPolicy : v1 .ServiceExternalTrafficPolicyTypeLocal ,
206
203
},
207
204
}
208
- fcw .CreateFunc = func (ctx context.Context , obj client.Object , opts ... client.CreateOption ) error {
209
- vms := & vmopv1alpha1.VirtualMachineService {
205
+
206
+ fc .PrependReactor ("create" , "virtualmachineservices" , func (action clientgotesting.Action ) (handled bool , ret runtime.Object , err error ) {
207
+ return true , & vmopv1alpha1.VirtualMachineService {
210
208
Status : vmopv1alpha1.VirtualMachineServiceStatus {
211
209
LoadBalancer : vmopv1alpha1.LoadBalancerStatus {
212
210
Ingress : []vmopv1alpha1.LoadBalancerIngress {
@@ -216,10 +214,8 @@ func TestEnsureLoadBalancer_VMServiceExternalTrafficPolicyLocal(t *testing.T) {
216
214
},
217
215
},
218
216
},
219
- }
220
- vms .DeepCopyInto (obj .(* vmopv1alpha1.VirtualMachineService ))
221
- return nil
222
- }
217
+ }, nil
218
+ })
223
219
224
220
_ , ensureErr := lb .EnsureLoadBalancer (context .Background (), testClustername , testK8sService , []* v1.Node {})
225
221
assert .NoError (t , ensureErr )
@@ -231,32 +227,35 @@ func TestEnsureLoadBalancer_VMServiceExternalTrafficPolicyLocal(t *testing.T) {
231
227
func TestEnsureLoadBalancer (t * testing.T ) {
232
228
testCases := []struct {
233
229
name string
234
- createFunc func (ctx context. Context , obj client .Object , opts ... client. CreateOption ) error
230
+ createFunc func (action clientgotesting. Action ) ( handled bool , ret runtime .Object , err error )
235
231
expectErr error
236
232
}{
237
233
{
238
- name : "when VMService is created but IP not found" ,
234
+ name : "when VMService is created but IP not found" ,
235
+ createFunc : func (action clientgotesting.Action ) (handled bool , ret runtime.Object , err error ) {
236
+ return true , & vmopv1alpha1.VirtualMachineService {}, fmt .Errorf (vmservice .ErrVMServiceIPNotFound .Error ())
237
+ },
239
238
expectErr : vmservice .ErrVMServiceIPNotFound ,
240
239
},
241
240
{
242
241
name : "when VMService creation failed" ,
243
- createFunc : func (ctx context. Context , obj client .Object , opts ... client. CreateOption ) error {
244
- return fmt .Errorf (vmservice .ErrCreateVMService .Error ())
242
+ createFunc : func (action clientgotesting. Action ) ( handled bool , ret runtime .Object , err error ) {
243
+ return true , & vmopv1alpha1. VirtualMachineService {}, fmt .Errorf (vmservice .ErrCreateVMService .Error ())
245
244
},
246
245
expectErr : vmservice .ErrCreateVMService ,
247
246
},
248
247
}
249
248
250
249
for _ , testCase := range testCases {
251
250
t .Run (testCase .name , func (t * testing.T ) {
252
- lb , fcw := newTestLoadBalancer ()
251
+ lb , fc := newTestLoadBalancer ()
253
252
testK8sService := & v1.Service {
254
253
ObjectMeta : metav1.ObjectMeta {
255
254
Name : testK8sServiceName ,
256
255
Namespace : testK8sServiceNameSpace ,
257
256
},
258
257
}
259
- fcw . CreateFunc = testCase .createFunc
258
+ fc . PrependReactor ( "create" , "virtualmachineservices" , testCase .createFunc )
260
259
261
260
_ , ensureErr := lb .EnsureLoadBalancer (context .Background (), testClustername , testK8sService , []* v1.Node {})
262
261
assert .Equal (t , ensureErr .Error (), testCase .expectErr .Error ())
@@ -268,16 +267,16 @@ func TestEnsureLoadBalancer(t *testing.T) {
268
267
}
269
268
270
269
func TestEnsureLoadBalancer_VMServiceCreatedIPFound (t * testing.T ) {
271
- lb , fcw := newTestLoadBalancer ()
270
+ lb , fc := newTestLoadBalancer ()
272
271
testK8sService := & v1.Service {
273
272
ObjectMeta : metav1.ObjectMeta {
274
273
Name : testK8sServiceName ,
275
274
Namespace : testK8sServiceNameSpace ,
276
275
},
277
276
}
278
277
// Ensure that the client Create call returns a VMService with a valid IP
279
- fcw . CreateFunc = func (ctx context. Context , obj client .Object , opts ... client. CreateOption ) error {
280
- vms := & vmopv1alpha1.VirtualMachineService {
278
+ fc . PrependReactor ( "create" , "virtualmachineservices" , func (action clientgotesting. Action ) ( handled bool , ret runtime .Object , err error ) {
279
+ return true , & vmopv1alpha1.VirtualMachineService {
281
280
Status : vmopv1alpha1.VirtualMachineServiceStatus {
282
281
LoadBalancer : vmopv1alpha1.LoadBalancerStatus {
283
282
Ingress : []vmopv1alpha1.LoadBalancerIngress {
@@ -308,10 +307,8 @@ func TestEnsureLoadBalancer_VMServiceCreatedIPFound(t *testing.T) {
308
307
vmservice .NodeSelectorKey : vmservice .NodeRole ,
309
308
},
310
309
},
311
- }
312
- vms .DeepCopyInto (obj .(* vmopv1alpha1.VirtualMachineService ))
313
- return nil
314
- }
310
+ }, nil
311
+ })
315
312
316
313
status , ensureErr := lb .EnsureLoadBalancer (context .Background (), testClustername , testK8sService , []* v1.Node {})
317
314
assert .NoError (t , ensureErr )
@@ -324,27 +321,27 @@ func TestEnsureLoadBalancer_VMServiceCreatedIPFound(t *testing.T) {
324
321
func TestEnsureLoadBalancer_DeleteLB (t * testing.T ) {
325
322
testCases := []struct {
326
323
name string
327
- deleteFunc func (ctx context. Context , obj client .Object , opts ... client. DeleteOption ) error
324
+ deleteFunc func (action clientgotesting. Action ) ( handled bool , ret runtime .Object , err error )
328
325
expectErr string
329
326
}{
330
327
{
331
328
name : "should ignore not found error" ,
332
- deleteFunc : func (ctx context. Context , obj client .Object , opts ... client. DeleteOption ) error {
333
- return apierrors .NewNotFound (vmopv1alpha1 .Resource ("virtualmachineservice" ), testClustername )
329
+ deleteFunc : func (action clientgotesting. Action ) ( handled bool , ret runtime .Object , err error ) {
330
+ return true , nil , apierrors .NewNotFound (vmopv1alpha1 .Resource ("virtualmachineservice" ), testClustername )
334
331
},
335
332
},
336
333
{
337
334
name : "should return error" ,
338
- deleteFunc : func (ctx context. Context , obj client .Object , opts ... client. DeleteOption ) error {
339
- return fmt .Errorf ("an error occurred while deleting load balancer" )
335
+ deleteFunc : func (action clientgotesting. Action ) ( handled bool , ret runtime .Object , err error ) {
336
+ return true , nil , fmt .Errorf ("an error occurred while deleting load balancer" )
340
337
},
341
338
expectErr : "an error occurred while deleting load balancer" ,
342
339
},
343
340
}
344
341
345
342
for _ , testCase := range testCases {
346
343
t .Run (testCase .name , func (t * testing.T ) {
347
- lb , fcw := newTestLoadBalancer ()
344
+ lb , fc := newTestLoadBalancer ()
348
345
testK8sService := & v1.Service {
349
346
ObjectMeta : metav1.ObjectMeta {
350
347
Name : testK8sServiceName ,
@@ -356,7 +353,8 @@ func TestEnsureLoadBalancer_DeleteLB(t *testing.T) {
356
353
err := lb .EnsureLoadBalancerDeleted (context .Background (), testClustername , testK8sService )
357
354
assert .NoError (t , err )
358
355
359
- fcw .DeleteFunc = testCase .deleteFunc
356
+ fc .PrependReactor ("delete" , "virtualmachineservices" , testCase .deleteFunc )
357
+
360
358
err = lb .EnsureLoadBalancerDeleted (context .Background (), "test" , testK8sService )
361
359
if err != nil {
362
360
assert .Equal (t , err .Error (), testCase .expectErr )
0 commit comments