diff --git a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go index d3cbfecc787..47219676e6d 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_whitebox_test.go @@ -154,6 +154,34 @@ func (d *dummyStorage) injectError(err error) { } func TestGetListCacheBypass(t *testing.T) { + type testCase struct { + opts storage.ListOptions + expectBypass bool + } + testCases := []testCase{ + {opts: storage.ListOptions{ResourceVersion: ""}, expectBypass: true}, + {opts: storage.ListOptions{ResourceVersion: "0"}, expectBypass: false}, + {opts: storage.ListOptions{ResourceVersion: "1"}, expectBypass: false}, + + {opts: storage.ListOptions{ResourceVersion: "", Predicate: storage.SelectionPredicate{Continue: "a"}}, expectBypass: true}, + {opts: storage.ListOptions{ResourceVersion: "0", Predicate: storage.SelectionPredicate{Continue: "a"}}, expectBypass: true}, + {opts: storage.ListOptions{ResourceVersion: "1", Predicate: storage.SelectionPredicate{Continue: "a"}}, expectBypass: true}, + + {opts: storage.ListOptions{ResourceVersion: "", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: true}, + {opts: storage.ListOptions{ResourceVersion: "0", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: false}, + {opts: storage.ListOptions{ResourceVersion: "1", Predicate: storage.SelectionPredicate{Limit: 500}}, expectBypass: true}, + + {opts: storage.ListOptions{ResourceVersion: "", ResourceVersionMatch: metav1.ResourceVersionMatchExact}, expectBypass: true}, + {opts: storage.ListOptions{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchExact}, expectBypass: true}, + {opts: storage.ListOptions{ResourceVersion: "1", ResourceVersionMatch: metav1.ResourceVersionMatchExact}, expectBypass: true}, + } + + for _, tc := range testCases { + testGetListCacheBypass(t, tc.opts, tc.expectBypass) + } +} + +func testGetListCacheBypass(t *testing.T, options storage.ListOptions, expectBypass bool) { backingStorage := &dummyStorage{} cacher, _, err := newTestCacher(backingStorage) if err != nil { @@ -161,34 +189,21 @@ func TestGetListCacheBypass(t *testing.T) { } defer cacher.Stop() - pred := storage.SelectionPredicate{ - Limit: 500, - } result := &example.PodList{} // Wait until cacher is initialized. if err := cacher.ready.wait(context.Background()); err != nil { t.Fatalf("unexpected error waiting for the cache to be ready") } - // Inject error to underlying layer and check if cacher is not bypassed. backingStorage.injectError(errDummy) - err = cacher.GetList(context.TODO(), "pods/ns", storage.ListOptions{ - ResourceVersion: "0", - Predicate: pred, - Recursive: true, - }, result) - if err != nil { - t.Errorf("GetList with Limit and RV=0 should be served from cache: %v", err) + err = cacher.GetList(context.TODO(), "pods/ns", options, result) + if err != nil && err != errDummy { + t.Fatalf("Unexpected error for List request with options: %v, err: %v", options, err) } - - err = cacher.GetList(context.TODO(), "pods/ns", storage.ListOptions{ - ResourceVersion: "", - Predicate: pred, - Recursive: true, - }, result) - if err != errDummy { - t.Errorf("GetList with Limit without RV=0 should bypass cacher: %v", err) + gotBypass := err == errDummy + if gotBypass != expectBypass { + t.Errorf("Unexpected bypass result for List request with options %+v, bypass expected: %v, got: %v", options, expectBypass, gotBypass) } }