Skip to content

Commit 02365ec

Browse files
authored
Merge pull request kubernetes#124610 from wojtek-t/deflake_cacher_tests
Deflake watchcache tests
2 parents fc24cfc + 6d9edcc commit 02365ec

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,9 @@ func RunTestListContinuation(ctx context.Context, t *testing.T, store storage.In
16311631
}
16321632
}
16331633
options := storage.ListOptions{
1634-
ResourceVersion: "0",
1634+
// Limit is ignored when ResourceVersion is set to 0.
1635+
// Set it to consistent read.
1636+
ResourceVersion: "",
16351637
Predicate: pred(1, ""),
16361638
Recursive: true,
16371639
}
@@ -1654,7 +1656,8 @@ func RunTestListContinuation(ctx context.Context, t *testing.T, store storage.In
16541656
// no limit, should get two items
16551657
out = &example.PodList{}
16561658
options = storage.ListOptions{
1657-
ResourceVersion: "0",
1659+
// ResourceVersion should be unset when setting continuation token.
1660+
ResourceVersion: "",
16581661
Predicate: pred(0, continueFromSecondItem),
16591662
Recursive: true,
16601663
}
@@ -1677,7 +1680,8 @@ func RunTestListContinuation(ctx context.Context, t *testing.T, store storage.In
16771680
// limit, should get two more pages
16781681
out = &example.PodList{}
16791682
options = storage.ListOptions{
1680-
ResourceVersion: "0",
1683+
// ResourceVersion should be unset when setting continuation token.
1684+
ResourceVersion: "",
16811685
Predicate: pred(1, continueFromSecondItem),
16821686
Recursive: true,
16831687
}
@@ -1699,7 +1703,8 @@ func RunTestListContinuation(ctx context.Context, t *testing.T, store storage.In
16991703

17001704
out = &example.PodList{}
17011705
options = storage.ListOptions{
1702-
ResourceVersion: "0",
1706+
// ResourceVersion should be unset when setting continuation token.
1707+
ResourceVersion: "",
17031708
Predicate: pred(1, continueFromThirdItem),
17041709
Recursive: true,
17051710
}
@@ -1815,7 +1820,9 @@ func RunTestListContinuationWithFilter(ctx context.Context, t *testing.T, store
18151820
}
18161821
}
18171822
options := storage.ListOptions{
1818-
ResourceVersion: "0",
1823+
// Limit is ignored when ResourceVersion is set to 0.
1824+
// Set it to consistent read.
1825+
ResourceVersion: "",
18191826
Predicate: pred(2, ""),
18201827
Recursive: true,
18211828
}
@@ -1845,7 +1852,8 @@ func RunTestListContinuationWithFilter(ctx context.Context, t *testing.T, store
18451852
// both read counters should be incremented for the singular calls they make in this case
18461853
out = &example.PodList{}
18471854
options = storage.ListOptions{
1848-
ResourceVersion: "0",
1855+
// ResourceVersion should be unset when setting continuation token.
1856+
ResourceVersion: "",
18491857
Predicate: pred(2, cont),
18501858
Recursive: true,
18511859
}
@@ -2407,7 +2415,7 @@ func RunTestGuaranteedUpdateWithSuggestionAndConflict(ctx context.Context, t *te
24072415
err := store.GuaranteedUpdate(ctx, key, updatedPod, false, nil,
24082416
storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
24092417
pod := obj.(*example.Pod)
2410-
pod.Name = "foo-2"
2418+
pod.Generation = 2
24112419
return pod, nil
24122420
}),
24132421
nil,
@@ -2424,24 +2432,24 @@ func RunTestGuaranteedUpdateWithSuggestionAndConflict(ctx context.Context, t *te
24242432
err = store.GuaranteedUpdate(ctx, key, updatedPod2, false, nil,
24252433
storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
24262434
pod := obj.(*example.Pod)
2427-
if pod.Name != "foo-2" {
2435+
if pod.Generation != 2 {
24282436
if sawConflict {
24292437
t.Fatalf("unexpected second conflict")
24302438
}
24312439
sawConflict = true
24322440
// simulated stale object - return a conflict
24332441
return nil, apierrors.NewConflict(example.SchemeGroupVersion.WithResource("pods").GroupResource(), "name", errors.New("foo"))
24342442
}
2435-
pod.Name = "foo-3"
2443+
pod.Generation = 3
24362444
return pod, nil
24372445
}),
24382446
originalPod,
24392447
)
24402448
if err != nil {
24412449
t.Fatalf("unexpected error: %v", err)
24422450
}
2443-
if updatedPod2.Name != "foo-3" {
2444-
t.Errorf("unexpected pod name: %q", updatedPod2.Name)
2451+
if updatedPod2.Generation != 3 {
2452+
t.Errorf("unexpected pod generation: %q", updatedPod2.Generation)
24452453
}
24462454

24472455
// Third, update using a current version as the suggestion.
@@ -2452,14 +2460,8 @@ func RunTestGuaranteedUpdateWithSuggestionAndConflict(ctx context.Context, t *te
24522460
err = store.GuaranteedUpdate(ctx, key, updatedPod3, false, nil,
24532461
storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
24542462
pod := obj.(*example.Pod)
2455-
if pod.Name != updatedPod2.Name || pod.ResourceVersion != updatedPod2.ResourceVersion {
2456-
t.Errorf(
2457-
"unexpected live object (name=%s, rv=%s), expected name=%s, rv=%s",
2458-
pod.Name,
2459-
pod.ResourceVersion,
2460-
updatedPod2.Name,
2461-
updatedPod2.ResourceVersion,
2462-
)
2463+
if pod.Generation != updatedPod2.Generation || pod.ResourceVersion != updatedPod2.ResourceVersion {
2464+
t.Logf("stale object (rv=%s), expected rv=%s", pod.ResourceVersion, updatedPod2.ResourceVersion)
24632465
}
24642466
attempts++
24652467
return nil, fmt.Errorf("validation or admission error")
@@ -2469,8 +2471,10 @@ func RunTestGuaranteedUpdateWithSuggestionAndConflict(ctx context.Context, t *te
24692471
if err == nil {
24702472
t.Fatalf("expected error, got none")
24712473
}
2472-
if attempts != 1 {
2473-
t.Errorf("expected 1 attempt, got %d", attempts)
2474+
// Implementations of the storage interface are allowed to ignore the suggestion,
2475+
// in which case two attempts are possible.
2476+
if attempts > 2 {
2477+
t.Errorf("update function should have been called at most twice, called %d", attempts)
24742478
}
24752479
}
24762480

0 commit comments

Comments
 (0)