Merge pull request #130813 from serathius/watchcache-consistent-list-flake

Fix flaky RunTestConsistentList
This commit is contained in:
Kubernetes Prow Robot 2025-03-17 04:29:49 -07:00 committed by GitHub
commit d2ef120924
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -30,6 +30,8 @@ import (
"testing" "testing"
"github.com/google/go-cmp/cmp" //nolint:depguard "github.com/google/go-cmp/cmp" //nolint:depguard
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
apierrors "k8s.io/apimachinery/pkg/api/errors" apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/fields"
@ -1593,43 +1595,49 @@ func RunTestConsistentList(ctx context.Context, t *testing.T, store storage.Inte
outPod := &example.Pod{} outPod := &example.Pod{}
inPod := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "foo"}} inPod := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "foo"}}
err := store.Create(ctx, computePodKey(inPod), inPod, outPod, 0) err := store.Create(ctx, computePodKey(inPod), inPod, outPod, 0)
if err != nil { require.NoError(t, err)
t.Errorf("Unexpected error: %v", err) writeRV, err := strconv.Atoi(outPod.ResourceVersion)
} require.NoError(t, err)
lastObjecRV := outPod.ResourceVersion
increaseRV(ctx, t) increaseRV(ctx, t)
parsedRV, _ := strconv.Atoi(outPod.ResourceVersion) consistentRV := writeRV + 1
currentRV := fmt.Sprintf("%d", parsedRV+1)
firstNonConsistentReadRV := lastObjecRV
if consistentReadsSupported && !cacheEnabled {
firstNonConsistentReadRV = currentRV
}
secondNonConsistentReadRV := lastObjecRV
if !cacheEnabled || consistentReadsSupported {
secondNonConsistentReadRV = currentRV
}
tcs := []struct { tcs := []struct {
name string name string
requestRV string requestRV string
expectResponseRV string validateResponseRV func(*testing.T, int)
}{ }{
{ {
name: "Non-consistent list before sync", name: "Non-consistent list before sync",
requestRV: "0", requestRV: "0",
expectResponseRV: firstNonConsistentReadRV, validateResponseRV: func(t *testing.T, rv int) {
if cacheEnabled {
// Cache might not yet observed write
assert.LessOrEqual(t, rv, writeRV)
} else {
// Etcd should always be up to date with consistent RV
assert.Equal(t, consistentRV, rv)
}
},
}, },
{ {
name: "Consistent request returns currentRV", name: "Consistent request returns currentRV",
requestRV: "", requestRV: "",
expectResponseRV: currentRV, validateResponseRV: func(t *testing.T, rv int) {
assert.Equal(t, consistentRV, rv)
},
}, },
{ {
name: "Non-consistent request after sync returns currentRV", name: "Non-consistent request after sync",
requestRV: "0", requestRV: "0",
expectResponseRV: secondNonConsistentReadRV, validateResponseRV: func(t *testing.T, rv int) {
// Consistent read is required to sync cache
if cacheEnabled && !consistentReadsSupported {
assert.LessOrEqual(t, rv, writeRV)
} else {
assert.Equal(t, consistentRV, rv)
}
},
}, },
} }
for _, tc := range tcs { for _, tc := range tcs {
@ -1640,12 +1648,11 @@ func RunTestConsistentList(ctx context.Context, t *testing.T, store storage.Inte
Predicate: storage.Everything, Predicate: storage.Everything,
} }
err = store.GetList(ctx, "/pods/empty", opts, out) err = store.GetList(ctx, "/pods/empty", opts, out)
if err != nil { require.NoError(t, err)
t.Fatalf("GetList failed: %v", err)
} parsedOutRV, err := strconv.Atoi(out.ResourceVersion)
if out.ResourceVersion != tc.expectResponseRV { require.NoError(t, err)
t.Errorf("resourceVersion in list response want=%s, got=%s", tc.expectResponseRV, out.ResourceVersion) tc.validateResponseRV(t, parsedOutRV)
}
}) })
} }
} }