@@ -147,16 +147,30 @@ func generateUpdateErrorFunc(t *testing.T, failures int) clienttesting.ReactionF
147
147
}
148
148
149
149
func TestPVCProtectionController (t * testing.T ) {
150
- pvcVer := schema.GroupVersionResource {
150
+ pvcGVR := schema.GroupVersionResource {
151
151
Group : v1 .GroupName ,
152
152
Version : "v1" ,
153
153
Resource : "persistentvolumeclaims" ,
154
154
}
155
+ podGVR := schema.GroupVersionResource {
156
+ Group : v1 .GroupName ,
157
+ Version : "v1" ,
158
+ Resource : "pods" ,
159
+ }
160
+ podGVK := schema.GroupVersionKind {
161
+ Group : v1 .GroupName ,
162
+ Version : "v1" ,
163
+ Kind : "Pod" ,
164
+ }
155
165
156
166
tests := []struct {
157
167
name string
158
168
// Object to insert into fake kubeclient before the test starts.
159
169
initialObjects []runtime.Object
170
+ // Whether not to insert the content of initialObjects into the
171
+ // informers before the test starts. Set it to true to simulate the case
172
+ // where informers have not been notified yet of certain API objects.
173
+ informersAreLate bool
160
174
// Optional client reactors.
161
175
reactors []reaction
162
176
// PVC event to simulate. This PVC will be automatically added to
@@ -180,7 +194,7 @@ func TestPVCProtectionController(t *testing.T) {
180
194
name : "StorageObjectInUseProtection Enabled, PVC without finalizer -> finalizer is added" ,
181
195
updatedPVC : pvc (),
182
196
expectedActions : []clienttesting.Action {
183
- clienttesting .NewUpdateAction (pvcVer , defaultNS , withProtectionFinalizer (pvc ())),
197
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , withProtectionFinalizer (pvc ())),
184
198
},
185
199
storageObjectInUseProtectionEnabled : true ,
186
200
},
@@ -208,27 +222,29 @@ func TestPVCProtectionController(t *testing.T) {
208
222
},
209
223
expectedActions : []clienttesting.Action {
210
224
// This fails
211
- clienttesting .NewUpdateAction (pvcVer , defaultNS , withProtectionFinalizer (pvc ())),
225
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , withProtectionFinalizer (pvc ())),
212
226
// This fails too
213
- clienttesting .NewUpdateAction (pvcVer , defaultNS , withProtectionFinalizer (pvc ())),
227
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , withProtectionFinalizer (pvc ())),
214
228
// This succeeds
215
- clienttesting .NewUpdateAction (pvcVer , defaultNS , withProtectionFinalizer (pvc ())),
229
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , withProtectionFinalizer (pvc ())),
216
230
},
217
231
storageObjectInUseProtectionEnabled : true ,
218
232
},
219
233
{
220
234
name : "StorageObjectInUseProtection Enabled, deleted PVC with finalizer -> finalizer is removed" ,
221
235
updatedPVC : deleted (withProtectionFinalizer (pvc ())),
222
236
expectedActions : []clienttesting.Action {
223
- clienttesting .NewUpdateAction (pvcVer , defaultNS , deleted (pvc ())),
237
+ clienttesting .NewListAction (podGVR , podGVK , defaultNS , metav1.ListOptions {}),
238
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , deleted (pvc ())),
224
239
},
225
240
storageObjectInUseProtectionEnabled : true ,
226
241
},
227
242
{
228
243
name : "StorageObjectInUseProtection Disabled, deleted PVC with finalizer -> finalizer is removed" ,
229
244
updatedPVC : deleted (withProtectionFinalizer (pvc ())),
230
245
expectedActions : []clienttesting.Action {
231
- clienttesting .NewUpdateAction (pvcVer , defaultNS , deleted (pvc ())),
246
+ clienttesting .NewListAction (podGVR , podGVK , defaultNS , metav1.ListOptions {}),
247
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , deleted (pvc ())),
232
248
},
233
249
storageObjectInUseProtectionEnabled : false ,
234
250
},
@@ -243,43 +259,59 @@ func TestPVCProtectionController(t *testing.T) {
243
259
},
244
260
},
245
261
expectedActions : []clienttesting.Action {
262
+ clienttesting .NewListAction (podGVR , podGVK , defaultNS , metav1.ListOptions {}),
246
263
// Fails
247
- clienttesting .NewUpdateAction (pvcVer , defaultNS , deleted (pvc ())),
264
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , deleted (pvc ())),
265
+ clienttesting .NewListAction (podGVR , podGVK , defaultNS , metav1.ListOptions {}),
248
266
// Fails too
249
- clienttesting .NewUpdateAction (pvcVer , defaultNS , deleted (pvc ())),
267
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , deleted (pvc ())),
268
+ clienttesting .NewListAction (podGVR , podGVK , defaultNS , metav1.ListOptions {}),
250
269
// Succeeds
251
- clienttesting .NewUpdateAction (pvcVer , defaultNS , deleted (pvc ())),
270
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , deleted (pvc ())),
252
271
},
253
272
storageObjectInUseProtectionEnabled : true ,
254
273
},
255
274
{
256
- name : "deleted PVC with finalizer + pods with the PVC exists -> finalizer is not removed" ,
275
+ name : "deleted PVC with finalizer + pod with the PVC exists -> finalizer is not removed" ,
257
276
initialObjects : []runtime.Object {
258
277
withPVC (defaultPVCName , pod ()),
259
278
},
260
279
updatedPVC : deleted (withProtectionFinalizer (pvc ())),
261
280
expectedActions : []clienttesting.Action {},
262
281
},
263
282
{
264
- name : "deleted PVC with finalizer + pods with unrelated PVC and EmptyDir exists -> finalizer is removed" ,
283
+ name : "deleted PVC with finalizer + pod with unrelated PVC and EmptyDir exists -> finalizer is removed" ,
265
284
initialObjects : []runtime.Object {
266
285
withEmptyDir (withPVC ("unrelatedPVC" , pod ())),
267
286
},
268
287
updatedPVC : deleted (withProtectionFinalizer (pvc ())),
269
288
expectedActions : []clienttesting.Action {
270
- clienttesting .NewUpdateAction (pvcVer , defaultNS , deleted (pvc ())),
289
+ clienttesting .NewListAction (podGVR , podGVK , defaultNS , metav1.ListOptions {}),
290
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , deleted (pvc ())),
271
291
},
272
292
storageObjectInUseProtectionEnabled : true ,
273
293
},
274
294
{
275
- name : "deleted PVC with finalizer + pods with the PVC finished but is not deleted -> finalizer is not removed" ,
295
+ name : "deleted PVC with finalizer + pod with the PVC finished but is not deleted -> finalizer is not removed" ,
276
296
initialObjects : []runtime.Object {
277
297
withStatus (v1 .PodFailed , withPVC (defaultPVCName , pod ())),
278
298
},
279
299
updatedPVC : deleted (withProtectionFinalizer (pvc ())),
280
300
expectedActions : []clienttesting.Action {},
281
301
storageObjectInUseProtectionEnabled : true ,
282
302
},
303
+ {
304
+ name : "deleted PVC with finalizer + pod with the PVC exists but is not in the Informer's cache yet -> finalizer is not removed" ,
305
+ initialObjects : []runtime.Object {
306
+ withPVC (defaultPVCName , pod ()),
307
+ },
308
+ informersAreLate : true ,
309
+ updatedPVC : deleted (withProtectionFinalizer (pvc ())),
310
+ expectedActions : []clienttesting.Action {
311
+ clienttesting .NewListAction (podGVR , podGVK , defaultNS , metav1.ListOptions {}),
312
+ },
313
+ storageObjectInUseProtectionEnabled : true ,
314
+ },
283
315
//
284
316
// Pod events
285
317
//
@@ -308,7 +340,8 @@ func TestPVCProtectionController(t *testing.T) {
308
340
},
309
341
updatedPod : unscheduled (withPVC (defaultPVCName , pod ())),
310
342
expectedActions : []clienttesting.Action {
311
- clienttesting .NewUpdateAction (pvcVer , defaultNS , deleted (pvc ())),
343
+ clienttesting .NewListAction (podGVR , podGVK , defaultNS , metav1.ListOptions {}),
344
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , deleted (pvc ())),
312
345
},
313
346
storageObjectInUseProtectionEnabled : true ,
314
347
},
@@ -319,7 +352,8 @@ func TestPVCProtectionController(t *testing.T) {
319
352
},
320
353
deletedPod : withStatus (v1 .PodRunning , withPVC (defaultPVCName , pod ())),
321
354
expectedActions : []clienttesting.Action {
322
- clienttesting .NewUpdateAction (pvcVer , defaultNS , deleted (pvc ())),
355
+ clienttesting .NewListAction (podGVR , podGVK , defaultNS , metav1.ListOptions {}),
356
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , deleted (pvc ())),
323
357
},
324
358
storageObjectInUseProtectionEnabled : true ,
325
359
},
@@ -331,7 +365,8 @@ func TestPVCProtectionController(t *testing.T) {
331
365
deletedPod : withPVC (defaultPVCName , pod ()),
332
366
updatedPod : withUID ("uid2" , pod ()),
333
367
expectedActions : []clienttesting.Action {
334
- clienttesting .NewUpdateAction (pvcVer , defaultNS , deleted (pvc ())),
368
+ clienttesting .NewListAction (podGVR , podGVK , defaultNS , metav1.ListOptions {}),
369
+ clienttesting .NewUpdateAction (pvcGVR , defaultNS , deleted (pvc ())),
335
370
},
336
371
storageObjectInUseProtectionEnabled : true ,
337
372
},
@@ -368,15 +403,26 @@ func TestPVCProtectionController(t *testing.T) {
368
403
}
369
404
370
405
for _ , test := range tests {
371
- // Create client with initial data
372
- objs := test .initialObjects
406
+ // Create initial data for client and informers.
407
+ var (
408
+ clientObjs []runtime.Object
409
+ informersObjs []runtime.Object
410
+ )
373
411
if test .updatedPVC != nil {
374
- objs = append (objs , test .updatedPVC )
412
+ clientObjs = append (clientObjs , test .updatedPVC )
413
+ informersObjs = append (informersObjs , test .updatedPVC )
375
414
}
376
415
if test .updatedPod != nil {
377
- objs = append (objs , test .updatedPod )
416
+ clientObjs = append (clientObjs , test .updatedPod )
417
+ informersObjs = append (informersObjs , test .updatedPod )
418
+ }
419
+ clientObjs = append (clientObjs , test .initialObjects ... )
420
+ if ! test .informersAreLate {
421
+ informersObjs = append (informersObjs , test .initialObjects ... )
378
422
}
379
- client := fake .NewSimpleClientset (objs ... )
423
+
424
+ // Create client with initial data
425
+ client := fake .NewSimpleClientset (clientObjs ... )
380
426
381
427
// Create informers
382
428
informers := informers .NewSharedInformerFactory (client , controller .NoResyncPeriodFunc ())
@@ -385,7 +431,7 @@ func TestPVCProtectionController(t *testing.T) {
385
431
386
432
// Populate the informers with initial objects so the controller can
387
433
// Get() and List() it.
388
- for _ , obj := range objs {
434
+ for _ , obj := range informersObjs {
389
435
switch obj .(type ) {
390
436
case * v1.PersistentVolumeClaim :
391
437
pvcInformer .Informer ().GetStore ().Add (obj )
@@ -435,7 +481,7 @@ func TestPVCProtectionController(t *testing.T) {
435
481
}
436
482
currentActionCount := len (client .Actions ())
437
483
if currentActionCount < len (test .expectedActions ) {
438
- // Do not log evey wait, only when the action count changes.
484
+ // Do not log every wait, only when the action count changes.
439
485
if lastReportedActionCount < currentActionCount {
440
486
klog .V (5 ).Infof ("Test %q: got %d actions out of %d, waiting for the rest" , test .name , currentActionCount , len (test .expectedActions ))
441
487
lastReportedActionCount = currentActionCount
0 commit comments