Skip to content

Commit 75531cc

Browse files
authored
Merge pull request kubernetes#129540 from serathius/test-list-cache-bypass
Test all possible combinations of input for shouldDelegateList
2 parents f34d791 + fe89556 commit 75531cc

File tree

1 file changed

+83
-30
lines changed

1 file changed

+83
-30
lines changed

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

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -198,34 +198,80 @@ func (d *dummyStorage) injectError(err error) {
198198
}
199199

200200
func TestGetListCacheBypass(t *testing.T) {
201-
type testCase struct {
202-
opts storage.ListOptions
203-
expectBypass bool
204-
}
205-
commonTestCases := []testCase{
206-
{opts: storage.ListOptions{ResourceVersion: "0"}, expectBypass: false},
207-
{opts: storage.ListOptions{ResourceVersion: "1"}, expectBypass: false},
208-
209-
{opts: storage.ListOptions{ResourceVersion: "", Predicate: storage.SelectionPredicate{Continue: "a"}}, expectBypass: true},
210-
{opts: storage.ListOptions{ResourceVersion: "0", Predicate: storage.SelectionPredicate{Continue: "a"}}, expectBypass: true},
211-
{opts: storage.ListOptions{ResourceVersion: "1", Predicate: storage.SelectionPredicate{Continue: "a"}}, expectBypass: true},
212-
213-
{opts: storage.ListOptions{ResourceVersion: "0", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: false},
214-
{opts: storage.ListOptions{ResourceVersion: "1", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: true},
215-
216-
{opts: storage.ListOptions{ResourceVersion: "", ResourceVersionMatch: metav1.ResourceVersionMatchExact}, expectBypass: true},
217-
{opts: storage.ListOptions{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchExact}, expectBypass: true},
218-
{opts: storage.ListOptions{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact}, expectBypass: true},
201+
type opts struct {
202+
ResourceVersion string
203+
ResourceVersionMatch metav1.ResourceVersionMatch
204+
Limit int64
205+
Continue string
206+
}
207+
testCases := map[opts]bool{}
208+
for _, rv := range []string{"", "0", "1"} {
209+
for _, match := range []metav1.ResourceVersionMatch{"", "Invalid", metav1.ResourceVersionMatchExact, metav1.ResourceVersionMatchNotOlderThan} {
210+
for _, c := range []string{"", "continue"} {
211+
for _, limit := range []int64{0, 100} {
212+
testCases[opts{
213+
ResourceVersion: rv,
214+
ResourceVersionMatch: match,
215+
Continue: c,
216+
Limit: limit,
217+
}] = false
218+
}
219+
}
220+
}
219221
}
222+
testCases[opts{ResourceVersion: "0", Continue: "continue"}] = true
223+
testCases[opts{ResourceVersion: "0", Limit: 100, Continue: "continue"}] = true
224+
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchExact}] = true
225+
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100}] = true
226+
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Continue: "continue"}] = true
227+
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100, Continue: "continue"}] = true
228+
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Continue: "continue"}] = true
229+
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100, Continue: "continue"}] = true
230+
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: "Invalid"}] = true
231+
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: "Invalid", Continue: "continue"}] = true
232+
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: "Invalid", Limit: 100}] = true
233+
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: "Invalid", Limit: 100, Continue: "continue"}] = true
234+
testCases[opts{ResourceVersion: "1", Limit: 100}] = true
235+
testCases[opts{ResourceVersion: "1", Continue: "continue"}] = true
236+
testCases[opts{ResourceVersion: "1", Limit: 100, Continue: "continue"}] = true
237+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Continue: "continue"}] = true
238+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100, Continue: "continue"}] = true
239+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact}] = true
240+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100}] = true
241+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Continue: "continue"}] = true
242+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100, Continue: "continue"}] = true
243+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: "Invalid"}] = true
244+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: "Invalid", Continue: "continue"}] = true
245+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: "Invalid", Limit: 100}] = true
246+
testCases[opts{ResourceVersion: "1", ResourceVersionMatch: "Invalid", Limit: 100, Continue: "continue"}] = true
247+
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100}] = true
248+
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Continue: "continue"}] = true
249+
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100, Continue: "continue"}] = true
250+
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchExact}] = true
251+
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchExact, Continue: "continue"}] = true
252+
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100, Continue: "continue"}] = true
253+
testCases[opts{ResourceVersionMatch: "Invalid"}] = true
254+
testCases[opts{ResourceVersionMatch: "Invalid", Continue: "continue"}] = true
255+
testCases[opts{ResourceVersionMatch: "Invalid", Limit: 100}] = true
256+
testCases[opts{ResourceVersionMatch: "Invalid", Limit: 100, Continue: "continue"}] = true
257+
testCases[opts{Limit: 100, Continue: "continue"}] = true
258+
testCases[opts{Continue: "continue"}] = true
220259

221260
t.Run("ConsistentListFromStorage", func(t *testing.T) {
222261
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ConsistentListFromCache, false)
223-
testCases := append(commonTestCases,
224-
testCase{opts: storage.ListOptions{ResourceVersion: ""}, expectBypass: true},
225-
testCase{opts: storage.ListOptions{ResourceVersion: "", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: true},
226-
)
227-
for _, tc := range testCases {
228-
testGetListCacheBypass(t, tc.opts, tc.expectBypass)
262+
testCases[opts{}] = true
263+
testCases[opts{Limit: 100}] = true
264+
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan}] = true
265+
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchExact, Limit: 100}] = true
266+
for opt, expectBypass := range testCases {
267+
testGetListCacheBypass(t, storage.ListOptions{
268+
ResourceVersion: opt.ResourceVersion,
269+
ResourceVersionMatch: opt.ResourceVersionMatch,
270+
Predicate: storage.SelectionPredicate{
271+
Continue: opt.Continue,
272+
Limit: opt.Limit,
273+
},
274+
}, expectBypass)
229275
}
230276

231277
})
@@ -240,12 +286,19 @@ func TestGetListCacheBypass(t *testing.T) {
240286
// initialize the storage layer so that the mentioned method evaluates to true
241287
forceRequestWatchProgressSupport(t)
242288

243-
testCases := append(commonTestCases,
244-
testCase{opts: storage.ListOptions{ResourceVersion: ""}, expectBypass: false},
245-
testCase{opts: storage.ListOptions{ResourceVersion: "", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: false},
246-
)
247-
for _, tc := range testCases {
248-
testGetListCacheBypass(t, tc.opts, tc.expectBypass)
289+
testCases[opts{}] = false
290+
testCases[opts{Limit: 100}] = false
291+
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan}] = false
292+
testCases[opts{ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100}] = false
293+
for opt, expectBypass := range testCases {
294+
testGetListCacheBypass(t, storage.ListOptions{
295+
ResourceVersion: opt.ResourceVersion,
296+
ResourceVersionMatch: opt.ResourceVersionMatch,
297+
Predicate: storage.SelectionPredicate{
298+
Continue: opt.Continue,
299+
Limit: opt.Limit,
300+
},
301+
}, expectBypass)
249302
}
250303
})
251304
}

0 commit comments

Comments
 (0)