@@ -218,53 +218,87 @@ func (d *dummyCacher) Ready() bool {
218
218
219
219
func TestGetListCacheBypass (t * testing.T ) {
220
220
type opts struct {
221
+ Recursive bool
221
222
ResourceVersion string
222
223
ResourceVersionMatch metav1.ResourceVersionMatch
223
224
Limit int64
224
225
Continue string
225
226
}
227
+ toInternalOpts := func (opt opts ) * internalversion.ListOptions {
228
+ return & internalversion.ListOptions {
229
+ ResourceVersion : opt .ResourceVersion ,
230
+ ResourceVersionMatch : opt .ResourceVersionMatch ,
231
+ Limit : opt .Limit ,
232
+ Continue : opt .Continue ,
233
+ }
234
+ }
235
+
236
+ toStorageOpts := func (opt opts ) storage.ListOptions {
237
+ return storage.ListOptions {
238
+ Recursive : opt .Recursive ,
239
+ ResourceVersion : opt .ResourceVersion ,
240
+ ResourceVersionMatch : opt .ResourceVersionMatch ,
241
+ Predicate : storage.SelectionPredicate {
242
+ Continue : opt .Continue ,
243
+ Limit : opt .Limit ,
244
+ },
245
+ }
246
+ }
247
+
248
+ keyPrefix := "/pods/"
249
+ continueOnRev1 , err := storage .EncodeContinue (keyPrefix + "foo" , keyPrefix , 1 )
250
+ if err != nil {
251
+ t .Fatalf ("Unexpected error: %v" , err )
252
+ }
226
253
testCases := map [opts ]bool {}
227
- testCases [opts {}] = false
228
- testCases [opts {Limit : 100 }] = false
229
- testCases [opts {Continue : "continue" }] = true
230
- testCases [opts {Limit : 100 , Continue : "continue" }] = true
254
+ testCases [opts {}] = true
255
+ testCases [opts {Limit : 100 }] = true
256
+ testCases [opts {Continue : continueOnRev1 }] = true
257
+ testCases [opts {Limit : 100 , Continue : continueOnRev1 }] = true
231
258
testCases [opts {ResourceVersion : "0" }] = false
232
259
testCases [opts {ResourceVersion : "0" , Limit : 100 }] = false
233
- testCases [opts {ResourceVersion : "0" , Continue : "continue" }] = true
234
- testCases [opts {ResourceVersion : "0" , Limit : 100 , Continue : "continue" }] = true
260
+ testCases [opts {ResourceVersion : "0" , Continue : continueOnRev1 }] = true
261
+ testCases [opts {ResourceVersion : "0" , Limit : 100 , Continue : continueOnRev1 }] = true
235
262
testCases [opts {ResourceVersion : "0" , ResourceVersionMatch : metav1 .ResourceVersionMatchNotOlderThan }] = false
236
263
testCases [opts {ResourceVersion : "0" , ResourceVersionMatch : metav1 .ResourceVersionMatchNotOlderThan , Limit : 100 }] = false
237
264
testCases [opts {ResourceVersion : "1" }] = false
238
265
testCases [opts {ResourceVersion : "1" , Limit : 100 }] = true
239
- testCases [opts {ResourceVersion : "1" , Continue : "continue" }] = true
240
- testCases [opts {ResourceVersion : "1" , Limit : 100 , Continue : "continue" }] = true
241
266
testCases [opts {ResourceVersion : "1" , ResourceVersionMatch : metav1 .ResourceVersionMatchExact }] = true
242
267
testCases [opts {ResourceVersion : "1" , ResourceVersionMatch : metav1 .ResourceVersionMatchExact , Limit : 100 }] = true
243
268
testCases [opts {ResourceVersion : "1" , ResourceVersionMatch : metav1 .ResourceVersionMatchNotOlderThan }] = false
244
269
testCases [opts {ResourceVersion : "1" , ResourceVersionMatch : metav1 .ResourceVersionMatchNotOlderThan , Limit : 100 }] = false
245
270
271
+ // Bypass for most requests doesn't depend on Recursive
272
+ for opts , expectBypass := range testCases {
273
+ opts .Recursive = true
274
+ testCases [opts ] = expectBypass
275
+ }
276
+ // Continue is ignored on non recursive LIST
277
+ testCases [opts {ResourceVersion : "1" , Continue : continueOnRev1 }] = true
278
+ testCases [opts {ResourceVersion : "1" , Continue : continueOnRev1 , Limit : 100 }] = true
279
+
246
280
for _ , rv := range []string {"" , "0" , "1" } {
247
281
for _ , match := range []metav1.ResourceVersionMatch {"" , metav1 .ResourceVersionMatchExact , metav1 .ResourceVersionMatchNotOlderThan } {
248
- for _ , c := range []string {"" , "continue" } {
282
+ for _ , continueKey := range []string {"" , continueOnRev1 } {
249
283
for _ , limit := range []int64 {0 , 100 } {
250
- errs := validation . ValidateListOptions ( & internalversion. ListOptions {
251
- ResourceVersion : rv ,
252
- ResourceVersionMatch : match ,
253
- Limit : limit ,
254
- Continue : c ,
255
- }, false )
256
- if len ( errs ) != 0 {
257
- continue
258
- }
259
- opt := opts {
260
- ResourceVersion : rv ,
261
- ResourceVersionMatch : match ,
262
- Limit : limit ,
263
- Continue : c ,
264
- }
265
- _ , found := testCases [ opt ]
266
- if ! found {
267
- t . Errorf ( "Test case not covered, but passes validation: %+v" , opt )
284
+ for _ , recursive := range [] bool { true , false } {
285
+ opt := opts {
286
+ Recursive : recursive ,
287
+ ResourceVersion : rv ,
288
+ ResourceVersionMatch : match ,
289
+ Limit : limit ,
290
+ Continue : continueKey ,
291
+ }
292
+ if errs := validation . ValidateListOptions ( toInternalOpts ( opt ), false ); len ( errs ) != 0 {
293
+ continue
294
+ }
295
+ if _ , _ , err = storage . ValidateListOptions ( keyPrefix , storage. APIObjectVersioner {}, toStorageOpts ( opt )); err != nil {
296
+ continue
297
+ }
298
+ _ , found := testCases [ opt ]
299
+ if ! found {
300
+ t . Errorf ( "Test case not covered, but passes validation: %+v" , opt )
301
+ }
268
302
}
269
303
270
304
}
@@ -273,35 +307,37 @@ func TestGetListCacheBypass(t *testing.T) {
273
307
}
274
308
275
309
for opt := range testCases {
276
- errs := validation .ValidateListOptions (& internalversion.ListOptions {
277
- ResourceVersion : opt .ResourceVersion ,
278
- ResourceVersionMatch : opt .ResourceVersionMatch ,
279
- Limit : opt .Limit ,
280
- Continue : opt .Continue ,
281
- }, false )
282
- if len (errs ) != 0 {
310
+ if errs := validation .ValidateListOptions (toInternalOpts (opt ), false ); len (errs ) != 0 {
311
+ t .Errorf ("Invalid LIST request that should not be tested %+v" , opt )
312
+ continue
313
+ }
314
+ if _ , _ , err = storage .ValidateListOptions (keyPrefix , storage.APIObjectVersioner {}, toStorageOpts (opt )); err != nil {
283
315
t .Errorf ("Invalid LIST request that should not be tested %+v" , opt )
284
316
continue
285
317
}
286
318
}
287
319
288
- t .Run ("ConsistentListFromStorage" , func (t * testing.T ) {
289
- featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .ConsistentListFromCache , false )
290
- testCases [opts {}] = true
291
- testCases [opts {Limit : 100 }] = true
320
+ runTestCases := func (t * testing.T , testcases map [opts ]bool , overrides ... map [opts ]bool ) {
292
321
for opt , expectBypass := range testCases {
293
- testGetListCacheBypass (t , storage.ListOptions {
294
- ResourceVersion : opt .ResourceVersion ,
295
- ResourceVersionMatch : opt .ResourceVersionMatch ,
296
- Predicate : storage.SelectionPredicate {
297
- Continue : opt .Continue ,
298
- Limit : opt .Limit ,
299
- },
300
- }, expectBypass )
322
+ for _ , override := range overrides {
323
+ if bypass , ok := override [opt ]; ok {
324
+ expectBypass = bypass
325
+ }
326
+ }
327
+ testGetListCacheBypass (t , toStorageOpts (opt ), expectBypass )
301
328
}
329
+ }
330
+ consistentListFromCacheOverrides := map [opts ]bool {}
331
+ for _ , recursive := range []bool {true , false } {
332
+ consistentListFromCacheOverrides [opts {Recursive : recursive }] = false
333
+ consistentListFromCacheOverrides [opts {Limit : 100 , Recursive : recursive }] = false
334
+ }
302
335
336
+ t .Run ("ConsistentListFromCache=false" , func (t * testing.T ) {
337
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .ConsistentListFromCache , false )
338
+ runTestCases (t , testCases )
303
339
})
304
- t .Run ("ConsistentListFromCache" , func (t * testing.T ) {
340
+ t .Run ("ConsistentListFromCache=true " , func (t * testing.T ) {
305
341
featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .ConsistentListFromCache , true )
306
342
307
343
// TODO(p0lyn0mial): the following tests assume that etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
@@ -311,19 +347,7 @@ func TestGetListCacheBypass(t *testing.T) {
311
347
// However in CI all test are run and there must be a test(s) that properly
312
348
// initialize the storage layer so that the mentioned method evaluates to true
313
349
forceRequestWatchProgressSupport (t )
314
-
315
- testCases [opts {}] = false
316
- testCases [opts {Limit : 100 }] = false
317
- for opt , expectBypass := range testCases {
318
- testGetListCacheBypass (t , storage.ListOptions {
319
- ResourceVersion : opt .ResourceVersion ,
320
- ResourceVersionMatch : opt .ResourceVersionMatch ,
321
- Predicate : storage.SelectionPredicate {
322
- Continue : opt .Continue ,
323
- Limit : opt .Limit ,
324
- },
325
- }, expectBypass )
326
- }
350
+ runTestCases (t , testCases , consistentListFromCacheOverrides )
327
351
})
328
352
}
329
353
0 commit comments