Skip to content

Commit 6d3bff8

Browse files
committed
Test recursive in TestGetListCacheBypass and separate overrides
1 parent 2e9bb32 commit 6d3bff8

File tree

1 file changed

+84
-60
lines changed

1 file changed

+84
-60
lines changed

staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go

Lines changed: 84 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -218,53 +218,87 @@ func (d *dummyCacher) Ready() bool {
218218

219219
func TestGetListCacheBypass(t *testing.T) {
220220
type opts struct {
221+
Recursive bool
221222
ResourceVersion string
222223
ResourceVersionMatch metav1.ResourceVersionMatch
223224
Limit int64
224225
Continue string
225226
}
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+
}
226253
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
231258
testCases[opts{ResourceVersion: "0"}] = false
232259
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
235262
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan}] = false
236263
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100}] = false
237264
testCases[opts{ResourceVersion: "1"}] = false
238265
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
241266
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact}] = true
242267
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100}] = true
243268
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan}] = false
244269
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100}] = false
245270

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+
246280
for _, rv := range []string{"", "0", "1"} {
247281
for _, match := range []metav1.ResourceVersionMatch{"", metav1.ResourceVersionMatchExact, metav1.ResourceVersionMatchNotOlderThan} {
248-
for _, c := range []string{"", "continue"} {
282+
for _, continueKey := range []string{"", continueOnRev1} {
249283
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+
}
268302
}
269303

270304
}
@@ -273,35 +307,37 @@ func TestGetListCacheBypass(t *testing.T) {
273307
}
274308

275309
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 {
283315
t.Errorf("Invalid LIST request that should not be tested %+v", opt)
284316
continue
285317
}
286318
}
287319

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) {
292321
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)
301328
}
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+
}
302335

336+
t.Run("ConsistentListFromCache=false", func(t *testing.T) {
337+
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ConsistentListFromCache, false)
338+
runTestCases(t, testCases)
303339
})
304-
t.Run("ConsistentListFromCache", func(t *testing.T) {
340+
t.Run("ConsistentListFromCache=true", func(t *testing.T) {
305341
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ConsistentListFromCache, true)
306342

307343
// TODO(p0lyn0mial): the following tests assume that etcdfeature.DefaultFeatureSupportChecker.Supports(storage.RequestWatchProgress)
@@ -311,19 +347,7 @@ func TestGetListCacheBypass(t *testing.T) {
311347
// However in CI all test are run and there must be a test(s) that properly
312348
// initialize the storage layer so that the mentioned method evaluates to true
313349
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)
327351
})
328352
}
329353

0 commit comments

Comments
 (0)