diff --git a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go index ea690524068..38ea9f0341f 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/cacher/cacher_test.go @@ -166,6 +166,12 @@ func TestPreconditionalDeleteWithSuggestionPass(t *testing.T) { storagetesting.RunTestPreconditionalDeleteWithOnlySuggestionPass(ctx, t, cacher) } +func TestListPaging(t *testing.T) { + ctx, cacher, terminate := testSetup(t) + t.Cleanup(terminate) + storagetesting.RunTestListPaging(ctx, t, cacher) +} + func TestList(t *testing.T) { featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ConsistentListFromCache, false) ctx, cacher, server, terminate := testSetupWithEtcdServer(t) diff --git a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go index 2b596c0e52c..8a84e5569e8 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go @@ -161,6 +161,11 @@ func TestPreconditionalDeleteWithSuggestionPass(t *testing.T) { storagetesting.RunTestPreconditionalDeleteWithOnlySuggestionPass(ctx, t, store) } +func TestListPaging(t *testing.T) { + ctx, store, _ := testSetup(t) + storagetesting.RunTestListPaging(ctx, t, store) +} + func TestGetListNonRecursive(t *testing.T) { ctx, store, client := testSetup(t) storagetesting.RunTestGetListNonRecursive(ctx, t, compactStorage(client), store) diff --git a/staging/src/k8s.io/apiserver/pkg/storage/testing/store_tests.go b/staging/src/k8s.io/apiserver/pkg/storage/testing/store_tests.go index 73667abc7c1..0c17de99b76 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/testing/store_tests.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/testing/store_tests.go @@ -2707,3 +2707,55 @@ func RunTestCount(ctx context.Context, t *testing.T, store storage.Interface) { t.Fatalf("store.Count for resource %s: expected %d but got %d", resourceA, resourceACountExpected, resourceACountGot) } } + +func RunTestListPaging(ctx context.Context, t *testing.T, store storage.Interface) { + out := &example.Pod{} + for i := 0; i < 4; i++ { + name := fmt.Sprintf("test-%d", i) + pod := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "paging"}} + key := computePodKey(pod) + if err := store.Create(ctx, key, pod, out, 0); err != nil { + t.Fatal(err) + } + } + + var names []string + opts := storage.ListOptions{ + Recursive: true, + Predicate: storage.SelectionPredicate{ + Label: labels.Everything(), + Field: fields.Everything(), + Limit: 1, + }, + } + calls := 0 + for { + calls++ + listOut := &example.PodList{} + err := store.GetList(ctx, "/pods", opts, listOut) + if err != nil { + t.Fatalf("Unexpected error %s", err) + } + for _, item := range listOut.Items { + names = append(names, item.Name) + } + if listOut.Continue == "" { + break + } + if calls == 2 { + name := "test-5" + pod := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "paging"}} + key := computePodKey(pod) + if err := store.Create(ctx, key, pod, out, 0); err != nil { + t.Fatal(err) + } + } + opts.Predicate.Continue = listOut.Continue + } + if calls != 4 { + t.Errorf("unexpected list invocations: %d", calls) + } + if !reflect.DeepEqual(names, []string{"test-0", "test-1", "test-2", "test-3"}) { + t.Errorf("unexpected items: %#v", names) + } +}