Skip to content

Commit 5a6ace2

Browse files
authored
Merge pull request kubernetes#130811 from serathius/watchcache-test-negative-rv
Add test cases for negative resource version in TestList
2 parents e2a77c2 + c4d77a0 commit 5a6ace2

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,23 @@ func TestShouldDelegateList(t *testing.T) {
250250
if err != nil {
251251
t.Fatalf("Unexpected error: %v", err)
252252
}
253+
continueOnNegativeRV, err := storage.EncodeContinue(keyPrefix+"foo", keyPrefix, -1)
254+
if err != nil {
255+
t.Fatalf("Unexpected error: %v", err)
256+
}
253257
testCases := map[opts]bool{}
254258
testCases[opts{}] = true
255259
testCases[opts{Limit: 100}] = true
256260
testCases[opts{Continue: continueOnRev1}] = true
257261
testCases[opts{Limit: 100, Continue: continueOnRev1}] = true
262+
testCases[opts{Continue: continueOnNegativeRV}] = true
263+
testCases[opts{Limit: 100, Continue: continueOnNegativeRV}] = true
258264
testCases[opts{ResourceVersion: "0"}] = false
259265
testCases[opts{ResourceVersion: "0", Limit: 100}] = false
260266
testCases[opts{ResourceVersion: "0", Continue: continueOnRev1}] = true
261267
testCases[opts{ResourceVersion: "0", Limit: 100, Continue: continueOnRev1}] = true
268+
testCases[opts{ResourceVersion: "0", Continue: continueOnNegativeRV}] = true
269+
testCases[opts{ResourceVersion: "0", Limit: 100, Continue: continueOnNegativeRV}] = true
262270
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan}] = false
263271
testCases[opts{ResourceVersion: "0", ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan, Limit: 100}] = false
264272
testCases[opts{ResourceVersion: "1"}] = false
@@ -276,10 +284,12 @@ func TestShouldDelegateList(t *testing.T) {
276284
// Continue is ignored on non recursive LIST
277285
testCases[opts{ResourceVersion: "1", Continue: continueOnRev1}] = true
278286
testCases[opts{ResourceVersion: "1", Continue: continueOnRev1, Limit: 100}] = true
287+
testCases[opts{ResourceVersion: "1", Continue: continueOnNegativeRV}] = true
288+
testCases[opts{ResourceVersion: "1", Continue: continueOnNegativeRV, Limit: 100}] = true
279289

280290
for _, rv := range []string{"", "0", "1"} {
281291
for _, match := range []metav1.ResourceVersionMatch{"", metav1.ResourceVersionMatchExact, metav1.ResourceVersionMatchNotOlderThan} {
282-
for _, continueKey := range []string{"", continueOnRev1} {
292+
for _, continueKey := range []string{"", continueOnRev1, continueOnNegativeRV} {
283293
for _, limit := range []int64{0, 100} {
284294
for _, recursive := range []bool{true, false} {
285295
opt := opts{

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,73 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface, inc
14931493
expectContinueExact: encodeContinueOrDie(createdPods[1].Namespace+"/"+createdPods[1].Name+"\x00", int64(continueRV+1)),
14941494
expectedRemainingItemCount: utilpointer.Int64(3),
14951495
},
1496+
{
1497+
name: "test List with continue from second pod, negative resource version gives consistent read",
1498+
prefix: "/pods/",
1499+
pred: storage.SelectionPredicate{
1500+
Label: labels.Everything(),
1501+
Field: fields.Everything(),
1502+
Continue: encodeContinueOrDie(createdPods[0].Namespace+"/"+createdPods[0].Name+"\x00", -1),
1503+
},
1504+
expectedOut: []example.Pod{*createdPods[1], *createdPods[2], *createdPods[3], *createdPods[4]},
1505+
expectRV: currentRV,
1506+
},
1507+
{
1508+
name: "test List with continue from second pod and limit, negative resource version gives consistent read",
1509+
prefix: "/pods/",
1510+
pred: storage.SelectionPredicate{
1511+
Label: labels.Everything(),
1512+
Field: fields.Everything(),
1513+
Limit: 2,
1514+
Continue: encodeContinueOrDie(createdPods[0].Namespace+"/"+createdPods[0].Name+"\x00", -1),
1515+
},
1516+
expectedOut: []example.Pod{*createdPods[1], *createdPods[2]},
1517+
expectContinue: true,
1518+
expectContinueExact: encodeContinueOrDie(createdPods[2].Namespace+"/"+createdPods[2].Name+"\x00", int64(continueRV+1)),
1519+
expectRV: currentRV,
1520+
expectedRemainingItemCount: utilpointer.Int64(2),
1521+
},
1522+
{
1523+
name: "test List with continue from third pod, negative resource version gives consistent read",
1524+
prefix: "/pods/",
1525+
pred: storage.SelectionPredicate{
1526+
Label: labels.Everything(),
1527+
Field: fields.Everything(),
1528+
Continue: encodeContinueOrDie(createdPods[2].Namespace+"/"+createdPods[2].Name+"\x00", -1),
1529+
},
1530+
expectedOut: []example.Pod{*createdPods[3], *createdPods[4]},
1531+
expectRV: currentRV,
1532+
},
1533+
{
1534+
name: "test List with continue from empty fails",
1535+
prefix: "/pods/",
1536+
pred: storage.SelectionPredicate{
1537+
Label: labels.Everything(),
1538+
Field: fields.Everything(),
1539+
Continue: encodeContinueOrDie("", int64(continueRV)),
1540+
},
1541+
expectError: true,
1542+
},
1543+
{
1544+
name: "test List with continue from first pod, empty resource version fails",
1545+
prefix: "/pods/",
1546+
pred: storage.SelectionPredicate{
1547+
Label: labels.Everything(),
1548+
Field: fields.Everything(),
1549+
Continue: encodeContinueOrDie(createdPods[0].Namespace+"/"+createdPods[0].Name+"\x00", 0),
1550+
},
1551+
expectError: true,
1552+
},
1553+
{
1554+
name: "test List with negative rv fails",
1555+
prefix: "/pods/",
1556+
rv: "-1",
1557+
pred: storage.SelectionPredicate{
1558+
Label: labels.Everything(),
1559+
Field: fields.Everything(),
1560+
},
1561+
expectError: true,
1562+
},
14961563
}
14971564

14981565
for _, tt := range tests {

0 commit comments

Comments
 (0)