Skip to content

Commit d263344

Browse files
committed
Simplify bypass test by just testing shouldDelegateList function
1 parent 611abd3 commit d263344

File tree

1 file changed

+5
-45
lines changed

1 file changed

+5
-45
lines changed

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

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (d *dummyCacher) Ready() bool {
216216
return d.ready
217217
}
218218

219-
func TestGetListCacheBypass(t *testing.T) {
219+
func TestShouldDelegateList(t *testing.T) {
220220
type opts struct {
221221
Recursive bool
222222
ResourceVersion string
@@ -324,7 +324,10 @@ func TestGetListCacheBypass(t *testing.T) {
324324
expectBypass = bypass
325325
}
326326
}
327-
testGetListCacheBypass(t, toStorageOpts(opt), expectBypass)
327+
gotBypass, _ := shouldDelegateList(toStorageOpts(opt))
328+
if gotBypass != expectBypass {
329+
t.Errorf("Unexpected bypass result for List request with options %+v, bypass expected: %v, got: %v", opt, expectBypass, gotBypass)
330+
}
328331
}
329332
}
330333
consistentListFromCacheOverrides := map[opts]bool{}
@@ -351,49 +354,6 @@ func TestGetListCacheBypass(t *testing.T) {
351354
})
352355
}
353356

354-
func testGetListCacheBypass(t *testing.T, options storage.ListOptions, expectBypass bool) {
355-
backingStorage := &dummyStorage{}
356-
cacher, _, err := newTestCacher(backingStorage)
357-
if err != nil {
358-
t.Fatalf("Couldn't create cacher: %v", err)
359-
}
360-
defer cacher.Stop()
361-
delegator := NewCacheDelegator(cacher, backingStorage)
362-
defer delegator.Stop()
363-
result := &example.PodList{}
364-
if !utilfeature.DefaultFeatureGate.Enabled(features.ResilientWatchCacheInitialization) {
365-
if err := cacher.ready.wait(context.Background()); err != nil {
366-
t.Fatalf("unexpected error waiting for the cache to be ready")
367-
}
368-
}
369-
// Inject error to underlying layer and check if cacher is not bypassed.
370-
backingStorage.getListFn = func(_ context.Context, key string, opts storage.ListOptions, listObj runtime.Object) error {
371-
currentResourceVersion := "42"
372-
switch {
373-
// request made by getCurrentResourceVersionFromStorage by checking Limit
374-
case key == cacher.resourcePrefix:
375-
podList := listObj.(*example.PodList)
376-
podList.ResourceVersion = currentResourceVersion
377-
return nil
378-
// request made by storage.GetList with revision from original request or
379-
// returned by getCurrentResourceVersionFromStorage
380-
case opts.ResourceVersion == options.ResourceVersion || opts.ResourceVersion == currentResourceVersion:
381-
return errDummy
382-
default:
383-
t.Fatalf("Unexpected request %+v", opts)
384-
return nil
385-
}
386-
}
387-
err = delegator.GetList(context.TODO(), "pods/ns", options, result)
388-
gotBypass := errors.Is(err, errDummy)
389-
if err != nil && !gotBypass {
390-
t.Fatalf("Unexpected error for List request with options: %v, err: %v", options, err)
391-
}
392-
if gotBypass != expectBypass {
393-
t.Errorf("Unexpected bypass result for List request with options %+v, bypass expected: %v, got: %v", options, expectBypass, gotBypass)
394-
}
395-
}
396-
397357
func TestConsistentReadFallback(t *testing.T) {
398358
tcs := []struct {
399359
name string

0 commit comments

Comments
 (0)