Skip to content

Commit cee4304

Browse files
authored
Merge pull request kubernetes#126854 from serathius/pagination-tests
Add paging tests
2 parents 850bfd9 + 99e6956 commit cee4304

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ func TestPreconditionalDeleteWithSuggestionPass(t *testing.T) {
166166
storagetesting.RunTestPreconditionalDeleteWithOnlySuggestionPass(ctx, t, cacher)
167167
}
168168

169+
func TestListPaging(t *testing.T) {
170+
ctx, cacher, terminate := testSetup(t)
171+
t.Cleanup(terminate)
172+
storagetesting.RunTestListPaging(ctx, t, cacher)
173+
}
174+
169175
func TestList(t *testing.T) {
170176
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ConsistentListFromCache, false)
171177
ctx, cacher, server, terminate := testSetupWithEtcdServer(t)

staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ func TestPreconditionalDeleteWithSuggestionPass(t *testing.T) {
161161
storagetesting.RunTestPreconditionalDeleteWithOnlySuggestionPass(ctx, t, store)
162162
}
163163

164+
func TestListPaging(t *testing.T) {
165+
ctx, store, _ := testSetup(t)
166+
storagetesting.RunTestListPaging(ctx, t, store)
167+
}
168+
164169
func TestGetListNonRecursive(t *testing.T) {
165170
ctx, store, client := testSetup(t)
166171
storagetesting.RunTestGetListNonRecursive(ctx, t, compactStorage(client), store)

staging/src/k8s.io/apiserver/pkg/storage/testing/store_tests.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,3 +2707,55 @@ func RunTestCount(ctx context.Context, t *testing.T, store storage.Interface) {
27072707
t.Fatalf("store.Count for resource %s: expected %d but got %d", resourceA, resourceACountExpected, resourceACountGot)
27082708
}
27092709
}
2710+
2711+
func RunTestListPaging(ctx context.Context, t *testing.T, store storage.Interface) {
2712+
out := &example.Pod{}
2713+
for i := 0; i < 4; i++ {
2714+
name := fmt.Sprintf("test-%d", i)
2715+
pod := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "paging"}}
2716+
key := computePodKey(pod)
2717+
if err := store.Create(ctx, key, pod, out, 0); err != nil {
2718+
t.Fatal(err)
2719+
}
2720+
}
2721+
2722+
var names []string
2723+
opts := storage.ListOptions{
2724+
Recursive: true,
2725+
Predicate: storage.SelectionPredicate{
2726+
Label: labels.Everything(),
2727+
Field: fields.Everything(),
2728+
Limit: 1,
2729+
},
2730+
}
2731+
calls := 0
2732+
for {
2733+
calls++
2734+
listOut := &example.PodList{}
2735+
err := store.GetList(ctx, "/pods", opts, listOut)
2736+
if err != nil {
2737+
t.Fatalf("Unexpected error %s", err)
2738+
}
2739+
for _, item := range listOut.Items {
2740+
names = append(names, item.Name)
2741+
}
2742+
if listOut.Continue == "" {
2743+
break
2744+
}
2745+
if calls == 2 {
2746+
name := "test-5"
2747+
pod := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "paging"}}
2748+
key := computePodKey(pod)
2749+
if err := store.Create(ctx, key, pod, out, 0); err != nil {
2750+
t.Fatal(err)
2751+
}
2752+
}
2753+
opts.Predicate.Continue = listOut.Continue
2754+
}
2755+
if calls != 4 {
2756+
t.Errorf("unexpected list invocations: %d", calls)
2757+
}
2758+
if !reflect.DeepEqual(names, []string{"test-0", "test-1", "test-2", "test-3"}) {
2759+
t.Errorf("unexpected items: %#v", names)
2760+
}
2761+
}

0 commit comments

Comments
 (0)