Skip to content

Commit faa6193

Browse files
committed
Add tests for how recursive parameter works with object prefixes
1 parent 27bdade commit faa6193

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ func TestGetListNonRecursiveWithConsistentListFromCache(t *testing.T) {
208208
storagetesting.RunTestGetListNonRecursive(ctx, t, compactStorage(cacher, server.V3Client), cacher)
209209
}
210210

211+
func TestGetListRecursivePrefix(t *testing.T) {
212+
ctx, store, _ := testSetup(t)
213+
storagetesting.RunTestGetListRecursivePrefix(ctx, t, store)
214+
}
215+
211216
func checkStorageCalls(t *testing.T, pageSize, estimatedProcessedObjects uint64) {
212217
// No-op function for now, since cacher passes pagination calls to underlying storage.
213218
}

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
@@ -166,6 +166,11 @@ func TestGetListNonRecursive(t *testing.T) {
166166
storagetesting.RunTestGetListNonRecursive(ctx, t, compactStorage(client), store)
167167
}
168168

169+
func TestGetListRecursivePrefix(t *testing.T) {
170+
ctx, store, _ := testSetup(t)
171+
storagetesting.RunTestGetListRecursivePrefix(ctx, t, store)
172+
}
173+
169174
type storeWithPrefixTransformer struct {
170175
*store
171176
}

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,85 @@ func RunTestGetListNonRecursive(ctx context.Context, t *testing.T, compaction Co
15731573
}
15741574
}
15751575

1576+
// RunTestGetListRecursivePrefix tests how recursive parameter works for object keys that are prefixes of each other.
1577+
func RunTestGetListRecursivePrefix(ctx context.Context, t *testing.T, store storage.Interface) {
1578+
fooKey, fooObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns"}})
1579+
fooBarKey, fooBarObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foobar", Namespace: "test-ns"}})
1580+
_, otherNamespaceObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test-ns2"}})
1581+
1582+
tests := []struct {
1583+
name string
1584+
key string
1585+
recursive bool
1586+
expectedOut []example.Pod
1587+
}{
1588+
{
1589+
name: "NonRecursive on resource prefix doesn't return any objects",
1590+
key: "/pods/",
1591+
recursive: false,
1592+
expectedOut: []example.Pod{},
1593+
},
1594+
{
1595+
name: "Recursive on resource prefix returns all objects",
1596+
key: "/pods/",
1597+
recursive: true,
1598+
expectedOut: []example.Pod{*fooObj, *fooBarObj, *otherNamespaceObj},
1599+
},
1600+
{
1601+
name: "NonRecursive on namespace prefix doesn't return any objects",
1602+
key: "/pods/test-ns/",
1603+
recursive: false,
1604+
expectedOut: []example.Pod{},
1605+
},
1606+
{
1607+
name: "Recursive on resource prefix returns objects in the namespace",
1608+
key: "/pods/test-ns/",
1609+
recursive: true,
1610+
expectedOut: []example.Pod{*fooObj, *fooBarObj},
1611+
},
1612+
{
1613+
name: "NonRecursive on object key (prefix) returns object and no other objects with the same prefix",
1614+
key: fooKey,
1615+
recursive: false,
1616+
expectedOut: []example.Pod{*fooObj},
1617+
},
1618+
{
1619+
name: "Recursive on object key (prefix) doesn't return anything",
1620+
key: fooKey,
1621+
recursive: true,
1622+
expectedOut: []example.Pod{},
1623+
},
1624+
{
1625+
name: "NonRecursive on object key (no-prefix) return object",
1626+
key: fooBarKey,
1627+
recursive: false,
1628+
expectedOut: []example.Pod{*fooBarObj},
1629+
},
1630+
{
1631+
name: "Recursive on object key (no-prefix) doesn't return anything",
1632+
key: fooBarKey,
1633+
recursive: true,
1634+
expectedOut: []example.Pod{},
1635+
},
1636+
}
1637+
1638+
for _, tt := range tests {
1639+
tt := tt
1640+
t.Run(tt.name, func(t *testing.T) {
1641+
out := &example.PodList{}
1642+
storageOpts := storage.ListOptions{
1643+
Recursive: tt.recursive,
1644+
Predicate: storage.Everything,
1645+
}
1646+
err := store.GetList(ctx, tt.key, storageOpts, out)
1647+
if err != nil {
1648+
t.Fatalf("GetList failed: %v", err)
1649+
}
1650+
expectNoDiff(t, "incorrect list pods", tt.expectedOut, out.Items)
1651+
})
1652+
}
1653+
}
1654+
15761655
type CallsValidation func(t *testing.T, pageSize, estimatedProcessedObjects uint64)
15771656

15781657
func RunTestListContinuation(ctx context.Context, t *testing.T, store storage.Interface, validation CallsValidation) {

0 commit comments

Comments
 (0)