Stop exporting storage testing utility functions

Kubernetes-commit: 37db332298fc6d14a798f610ce6049792299f0b1
This commit is contained in:
Wojciech Tyczyński 2022-11-03 08:47:38 +01:00 committed by Kubernetes Publisher
parent 6c101e715a
commit b312c7eb5b
3 changed files with 57 additions and 62 deletions

View File

@ -85,12 +85,12 @@ func RunTestCreateWithTTL(ctx context.Context, t *testing.T, store storage.Inter
if err != nil { if err != nil {
t.Fatalf("Watch failed: %v", err) t.Fatalf("Watch failed: %v", err)
} }
TestCheckEventType(t, watch.Deleted, w) testCheckEventType(t, watch.Deleted, w)
} }
func RunTestCreateWithKeyExist(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestCreateWithKeyExist(ctx context.Context, t *testing.T, store storage.Interface) {
obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}} obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
key, _ := TestPropagateStore(ctx, t, store, obj) key, _ := testPropagateStore(ctx, t, store, obj)
out := &example.Pod{} out := &example.Pod{}
err := store.Create(ctx, key, obj, out, 0) err := store.Create(ctx, key, obj, out, 0)
if err == nil || !storage.IsExist(err) { if err == nil || !storage.IsExist(err) {
@ -100,7 +100,7 @@ func RunTestCreateWithKeyExist(ctx context.Context, t *testing.T, store storage.
func RunTestGet(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestGet(ctx context.Context, t *testing.T, store storage.Interface) {
// create an object to test // create an object to test
key, createdObj := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, createdObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
// update the object once to allow get by exact resource version to be tested // update the object once to allow get by exact resource version to be tested
updateObj := createdObj.DeepCopy() updateObj := createdObj.DeepCopy()
updateObj.Annotations = map[string]string{"test-annotation": "1"} updateObj.Annotations = map[string]string{"test-annotation": "1"}
@ -219,7 +219,7 @@ func RunTestGet(ctx context.Context, t *testing.T, store storage.Interface) {
} }
func RunTestUnconditionalDelete(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestUnconditionalDelete(ctx context.Context, t *testing.T, store storage.Interface) {
key, storedObj := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, storedObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
tests := []struct { tests := []struct {
name string name string
@ -263,7 +263,7 @@ func RunTestUnconditionalDelete(ctx context.Context, t *testing.T, store storage
} }
func RunTestConditionalDelete(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestConditionalDelete(ctx context.Context, t *testing.T, store storage.Interface) {
key, storedObj := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}}) key, storedObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}})
tests := []struct { tests := []struct {
name string name string
@ -299,7 +299,7 @@ func RunTestConditionalDelete(ctx context.Context, t *testing.T, store storage.I
} }
out.ResourceVersion = storedObj.ResourceVersion out.ResourceVersion = storedObj.ResourceVersion
ExpectNoDiff(t, "incorrect pod:", storedObj, out) ExpectNoDiff(t, "incorrect pod:", storedObj, out)
key, storedObj = TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}}) key, storedObj = testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}})
}) })
} }
} }
@ -330,7 +330,7 @@ func RunTestConditionalDelete(ctx context.Context, t *testing.T, store storage.I
// [DONE] Added TestPreconditionalDeleteWithSuggestion // [DONE] Added TestPreconditionalDeleteWithSuggestion
func RunTestDeleteWithSuggestion(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestDeleteWithSuggestion(ctx context.Context, t *testing.T, store storage.Interface) {
key, originalPod := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}}) key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}})
out := &example.Pod{} out := &example.Pod{}
if err := store.Delete(ctx, key, out, nil, storage.ValidateAllObjectFunc, originalPod); err != nil { if err := store.Delete(ctx, key, out, nil, storage.ValidateAllObjectFunc, originalPod); err != nil {
@ -343,7 +343,7 @@ func RunTestDeleteWithSuggestion(ctx context.Context, t *testing.T, store storag
} }
func RunTestDeleteWithSuggestionAndConflict(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestDeleteWithSuggestionAndConflict(ctx context.Context, t *testing.T, store storage.Interface) {
key, originalPod := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}}) key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}})
// First update, so originalPod is outdated. // First update, so originalPod is outdated.
updatedPod := &example.Pod{} updatedPod := &example.Pod{}
@ -367,7 +367,7 @@ func RunTestDeleteWithSuggestionAndConflict(ctx context.Context, t *testing.T, s
} }
func RunTestDeleteWithSuggestionOfDeletedObject(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestDeleteWithSuggestionOfDeletedObject(ctx context.Context, t *testing.T, store storage.Interface) {
key, originalPod := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}}) key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}})
// First delete, so originalPod is outdated. // First delete, so originalPod is outdated.
deletedPod := &example.Pod{} deletedPod := &example.Pod{}
@ -383,7 +383,7 @@ func RunTestDeleteWithSuggestionOfDeletedObject(ctx context.Context, t *testing.
} }
func RunTestValidateDeletionWithSuggestion(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestValidateDeletionWithSuggestion(ctx context.Context, t *testing.T, store storage.Interface) {
key, originalPod := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}}) key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}})
// Check that validaing fresh object fails is called once and fails. // Check that validaing fresh object fails is called once and fails.
validationCalls := 0 validationCalls := 0
@ -435,7 +435,7 @@ func RunTestValidateDeletionWithSuggestion(ctx context.Context, t *testing.T, st
} }
func RunTestPreconditionalDeleteWithSuggestion(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestPreconditionalDeleteWithSuggestion(ctx context.Context, t *testing.T, store storage.Interface) {
key, originalPod := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}}) key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}})
// First update, so originalPod is outdated. // First update, so originalPod is outdated.
updatedPod := &example.Pod{} updatedPod := &example.Pod{}
@ -673,7 +673,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
expectContinue: true, expectContinue: true,
expectedRemainingItemCount: utilpointer.Int64Ptr(1), expectedRemainingItemCount: utilpointer.Int64Ptr(1),
rv: "0", rv: "0",
expectRVFunc: ResourceVersionNotOlderThan(list.ResourceVersion), expectRVFunc: resourceVersionNotOlderThan(list.ResourceVersion),
}, },
{ {
name: "test List with limit at resource version 0 match=NotOlderThan", name: "test List with limit at resource version 0 match=NotOlderThan",
@ -688,7 +688,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
expectedRemainingItemCount: utilpointer.Int64Ptr(1), expectedRemainingItemCount: utilpointer.Int64Ptr(1),
rv: "0", rv: "0",
rvMatch: metav1.ResourceVersionMatchNotOlderThan, rvMatch: metav1.ResourceVersionMatchNotOlderThan,
expectRVFunc: ResourceVersionNotOlderThan(list.ResourceVersion), expectRVFunc: resourceVersionNotOlderThan(list.ResourceVersion),
}, },
{ {
name: "test List with limit at resource version before first write and match=Exact", name: "test List with limit at resource version before first write and match=Exact",
@ -795,7 +795,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
Field: fields.OneTermEqualSelector("metadata.name", "bar"), Field: fields.OneTermEqualSelector("metadata.name", "bar"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 2, Limit: 2,
Continue: EncodeContinueOrDie("z-level/3", int64(continueRV)), Continue: encodeContinueOrDie("z-level/3", int64(continueRV)),
}, },
expectedOut: []*example.Pod{preset[4]}, expectedOut: []*example.Pod{preset[4]},
}, },
@ -806,7 +806,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
Field: fields.OneTermEqualSelector("metadata.name", "bar"), Field: fields.OneTermEqualSelector("metadata.name", "bar"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 1, Limit: 1,
Continue: EncodeContinueOrDie("z-level/3/test-2", int64(continueRV)), Continue: encodeContinueOrDie("z-level/3/test-2", int64(continueRV)),
}, },
expectedOut: []*example.Pod{preset[4]}, expectedOut: []*example.Pod{preset[4]},
}, },
@ -817,7 +817,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
Field: fields.OneTermEqualSelector("metadata.name", "bar"), Field: fields.OneTermEqualSelector("metadata.name", "bar"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 2, Limit: 2,
Continue: EncodeContinueOrDie("z-level/3/test-2", int64(continueRV)), Continue: encodeContinueOrDie("z-level/3/test-2", int64(continueRV)),
}, },
expectedOut: []*example.Pod{preset[4]}, expectedOut: []*example.Pod{preset[4]},
}, },
@ -1048,10 +1048,10 @@ func seedMultiLevelData(ctx context.Context, store storage.Interface) (string, [
} }
func RunTestGetListNonRecursive(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestGetListNonRecursive(ctx context.Context, t *testing.T, store storage.Interface) {
prevKey, prevStoredObj := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "prev"}}) prevKey, prevStoredObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "prev"}})
prevRV, _ := strconv.Atoi(prevStoredObj.ResourceVersion) prevRV, _ := strconv.Atoi(prevStoredObj.ResourceVersion)
key, storedObj := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, storedObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
currentRV, _ := strconv.Atoi(storedObj.ResourceVersion) currentRV, _ := strconv.Atoi(storedObj.ResourceVersion)
tests := []struct { tests := []struct {
@ -1572,7 +1572,7 @@ func RunTestListInconsistentContinuation(ctx context.Context, t *testing.T, stor
if len(out.Continue) == 0 { if len(out.Continue) == 0 {
t.Fatalf("No continuation token set") t.Fatalf("No continuation token set")
} }
validateResourceVersion := ResourceVersionNotOlderThan(lastRVString) validateResourceVersion := resourceVersionNotOlderThan(lastRVString)
ExpectNoDiff(t, "incorrect second page", []example.Pod{*preset[1].storedObj}, out.Items) ExpectNoDiff(t, "incorrect second page", []example.Pod{*preset[1].storedObj}, out.Items)
if err := validateResourceVersion(out.ResourceVersion); err != nil { if err := validateResourceVersion(out.ResourceVersion); err != nil {
t.Fatal(err) t.Fatal(err)
@ -1776,7 +1776,7 @@ func RunTestGuaranteedUpdate(ctx context.Context, t *testing.T, store InterfaceW
for i, tt := range tests { for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
key, storeObj := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}}) key, storeObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}})
out := &example.Pod{} out := &example.Pod{}
name := fmt.Sprintf("foo-%d", i) name := fmt.Sprintf("foo-%d", i)
@ -1866,7 +1866,7 @@ func RunTestGuaranteedUpdateWithTTL(ctx context.Context, t *testing.T, store sto
if err != nil { if err != nil {
t.Fatalf("Watch failed: %v", err) t.Fatalf("Watch failed: %v", err)
} }
TestCheckEventType(t, watch.Deleted, w) testCheckEventType(t, watch.Deleted, w)
} }
func RunTestGuaranteedUpdateChecksStoredData(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) { func RunTestGuaranteedUpdateChecksStoredData(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) {
@ -1880,7 +1880,7 @@ func RunTestGuaranteedUpdateChecksStoredData(ctx context.Context, t *testing.T,
transformer.prefix = []byte(string(transformer.prefix) + " ") transformer.prefix = []byte(string(transformer.prefix) + " ")
return transformer return transformer
}) })
_, initial := TestPropagateStore(ctx, t, store, input) _, initial := testPropagateStore(ctx, t, store, input)
revertTransformer() revertTransformer()
// this update should write the canonical value to etcd because the new serialization differs // this update should write the canonical value to etcd because the new serialization differs
@ -1935,7 +1935,7 @@ func RunTestGuaranteedUpdateChecksStoredData(ctx context.Context, t *testing.T,
} }
func RunTestGuaranteedUpdateWithConflict(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestGuaranteedUpdateWithConflict(ctx context.Context, t *testing.T, store storage.Interface) {
key, _ := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, _ := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
errChan := make(chan error, 1) errChan := make(chan error, 1)
var firstToFinish sync.WaitGroup var firstToFinish sync.WaitGroup
@ -1980,7 +1980,7 @@ func RunTestGuaranteedUpdateWithConflict(ctx context.Context, t *testing.T, stor
} }
func RunTestGuaranteedUpdateWithSuggestionAndConflict(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestGuaranteedUpdateWithSuggestionAndConflict(ctx context.Context, t *testing.T, store storage.Interface) {
key, originalPod := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
// First, update without a suggestion so originalPod is outdated // First, update without a suggestion so originalPod is outdated
updatedPod := &example.Pod{} updatedPod := &example.Pod{}
@ -2142,7 +2142,9 @@ func RunTestCount(ctx context.Context, t *testing.T, store storage.Interface) {
obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("foo-%d", i)}} obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("foo-%d", i)}}
key := fmt.Sprintf("%s/%d", resourceA, i) key := fmt.Sprintf("%s/%d", resourceA, i)
TestPropagateStoreWithKey(ctx, t, store, key, obj) if err := store.Create(ctx, key, obj, nil, 0); err != nil {
t.Fatalf("Create failed: %v", err)
}
} }
resourceBCount := 4 resourceBCount := 4
@ -2150,7 +2152,9 @@ func RunTestCount(ctx context.Context, t *testing.T, store storage.Interface) {
obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("foo-%d", i)}} obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("foo-%d", i)}}
key := fmt.Sprintf("%s/%d", resourceB, i) key := fmt.Sprintf("%s/%d", resourceB, i)
TestPropagateStoreWithKey(ctx, t, store, key, obj) if err := store.Create(ctx, key, obj, nil, 0); err != nil {
t.Fatalf("Create failed: %v", err)
}
} }
resourceACountGot, err := store.Count(resourceA) resourceACountGot, err := store.Count(resourceA)

View File

@ -37,11 +37,6 @@ import (
"k8s.io/apiserver/pkg/storage/value" "k8s.io/apiserver/pkg/storage/value"
) )
// CreateObj will create a single object using the storage interface.
func CreateObj(helper storage.Interface, name string, obj, out runtime.Object, ttl uint64) error {
return helper.Create(context.TODO(), name, obj, out, ttl)
}
// CreateObjList will create a list from the array of objects. // CreateObjList will create a list from the array of objects.
func CreateObjList(prefix string, helper storage.Interface, items []runtime.Object) error { func CreateObjList(prefix string, helper storage.Interface, items []runtime.Object) error {
for i := range items { for i := range items {
@ -50,7 +45,7 @@ func CreateObjList(prefix string, helper storage.Interface, items []runtime.Obje
if err != nil { if err != nil {
return err return err
} }
err = CreateObj(helper, path.Join(prefix, meta.GetName()), obj, obj, 0) err = helper.Create(context.Background(), path.Join(prefix, meta.GetName()), obj, obj, 0)
if err != nil { if err != nil {
return err return err
} }
@ -82,16 +77,12 @@ func DeepEqualSafePodSpec() example.PodSpec {
} }
} }
// TestPropagateStore helps propagates store with objects, automates key generation, and returns // testPropagateStore helps propagates store with objects, automates key generation, and returns
// keys and stored objects. // keys and stored objects.
func TestPropagateStore(ctx context.Context, t *testing.T, store storage.Interface, obj *example.Pod) (string, *example.Pod) { func testPropagateStore(ctx context.Context, t *testing.T, store storage.Interface, obj *example.Pod) (string, *example.Pod) {
// Setup store with a key and grab the output for returning. // Setup store with a key and grab the output for returning.
key := fmt.Sprintf("/%s/%s", obj.Namespace, obj.Name) key := fmt.Sprintf("/%s/%s", obj.Namespace, obj.Name)
return key, TestPropagateStoreWithKey(ctx, t, store, key, obj)
}
// TestPropagateStoreWithKey helps propagate store with objects, the given object will be stored at the specified key.
func TestPropagateStoreWithKey(ctx context.Context, t *testing.T, store storage.Interface, key string, obj *example.Pod) *example.Pod {
// Setup store with the specified key and grab the output for returning. // Setup store with the specified key and grab the output for returning.
err := store.Delete(ctx, key, &example.Pod{}, nil, storage.ValidateAllObjectFunc, nil) err := store.Delete(ctx, key, &example.Pod{}, nil, storage.ValidateAllObjectFunc, nil)
if err != nil && !storage.IsNotFound(err) { if err != nil && !storage.IsNotFound(err) {
@ -101,7 +92,7 @@ func TestPropagateStoreWithKey(ctx context.Context, t *testing.T, store storage.
if err := store.Create(ctx, key, obj, setOutput, 0); err != nil { if err := store.Create(ctx, key, obj, setOutput, 0); err != nil {
t.Fatalf("Set failed: %v", err) t.Fatalf("Set failed: %v", err)
} }
return setOutput return key, setOutput
} }
func ExpectNoDiff(t *testing.T, msg string, expected, got interface{}) { func ExpectNoDiff(t *testing.T, msg string, expected, got interface{}) {
@ -135,7 +126,7 @@ func ExpectContains(t *testing.T, msg string, expectedList []interface{}, got in
const dummyPrefix = "adapter" const dummyPrefix = "adapter"
func EncodeContinueOrDie(key string, resourceVersion int64) string { func encodeContinueOrDie(key string, resourceVersion int64) string {
token, err := storage.EncodeContinue(dummyPrefix+key, dummyPrefix, resourceVersion) token, err := storage.EncodeContinue(dummyPrefix+key, dummyPrefix, resourceVersion)
if err != nil { if err != nil {
panic(err) panic(err)
@ -143,7 +134,7 @@ func EncodeContinueOrDie(key string, resourceVersion int64) string {
return token return token
} }
func TestCheckEventType(t *testing.T, expectEventType watch.EventType, w watch.Interface) { func testCheckEventType(t *testing.T, expectEventType watch.EventType, w watch.Interface) {
select { select {
case res := <-w.ResultChan(): case res := <-w.ResultChan():
if res.Type != expectEventType { if res.Type != expectEventType {
@ -154,14 +145,14 @@ func TestCheckEventType(t *testing.T, expectEventType watch.EventType, w watch.I
} }
} }
func TestCheckResult(t *testing.T, expectEventType watch.EventType, w watch.Interface, expectObj *example.Pod) { func testCheckResult(t *testing.T, expectEventType watch.EventType, w watch.Interface, expectObj *example.Pod) {
TestCheckResultFunc(t, expectEventType, w, func(object runtime.Object) error { testCheckResultFunc(t, expectEventType, w, func(object runtime.Object) error {
ExpectNoDiff(t, "incorrect object", expectObj, object) ExpectNoDiff(t, "incorrect object", expectObj, object)
return nil return nil
}) })
} }
func TestCheckResultFunc(t *testing.T, expectEventType watch.EventType, w watch.Interface, check func(object runtime.Object) error) { func testCheckResultFunc(t *testing.T, expectEventType watch.EventType, w watch.Interface, check func(object runtime.Object) error) {
select { select {
case res := <-w.ResultChan(): case res := <-w.ResultChan():
if res.Type != expectEventType { if res.Type != expectEventType {
@ -176,7 +167,7 @@ func TestCheckResultFunc(t *testing.T, expectEventType watch.EventType, w watch.
} }
} }
func TestCheckStop(t *testing.T, w watch.Interface) { func testCheckStop(t *testing.T, w watch.Interface) {
select { select {
case e, ok := <-w.ResultChan(): case e, ok := <-w.ResultChan():
if ok { if ok {
@ -194,10 +185,10 @@ func TestCheckStop(t *testing.T, w watch.Interface) {
} }
} }
// ResourceVersionNotOlderThan returns a function to validate resource versions. Resource versions // resourceVersionNotOlderThan returns a function to validate resource versions. Resource versions
// referring to points in logical time before the sentinel generate an error. All logical times as // referring to points in logical time before the sentinel generate an error. All logical times as
// new as the sentinel or newer generate no error. // new as the sentinel or newer generate no error.
func ResourceVersionNotOlderThan(sentinel string) func(string) error { func resourceVersionNotOlderThan(sentinel string) func(string) error {
return func(resourceVersion string) error { return func(resourceVersion string) error {
objectVersioner := storage.APIObjectVersioner{} objectVersioner := storage.APIObjectVersioner{}
actualRV, err := objectVersioner.ParseResourceVersion(resourceVersion) actualRV, err := objectVersioner.ParseResourceVersion(resourceVersion)

View File

@ -113,12 +113,12 @@ func testWatch(ctx context.Context, t *testing.T, store storage.Interface, recur
expectObj = prevObj expectObj = prevObj
expectObj.ResourceVersion = out.ResourceVersion expectObj.ResourceVersion = out.ResourceVersion
} }
TestCheckResult(t, watchTest.watchType, w, expectObj) testCheckResult(t, watchTest.watchType, w, expectObj)
} }
prevObj = out prevObj = out
} }
w.Stop() w.Stop()
TestCheckStop(t, w) testCheckStop(t, w)
}) })
} }
} }
@ -127,13 +127,13 @@ func testWatch(ctx context.Context, t *testing.T, store storage.Interface, recur
// - watch from 0 should sync up and grab the object added before // - watch from 0 should sync up and grab the object added before
// - watch from 0 is able to return events for objects whose previous version has been compacted // - watch from 0 is able to return events for objects whose previous version has been compacted
func RunTestWatchFromZero(ctx context.Context, t *testing.T, store storage.Interface, compaction Compaction) { func RunTestWatchFromZero(ctx context.Context, t *testing.T, store storage.Interface, compaction Compaction) {
key, storedObj := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "ns"}}) key, storedObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "ns"}})
w, err := store.Watch(ctx, key, storage.ListOptions{ResourceVersion: "0", Predicate: storage.Everything}) w, err := store.Watch(ctx, key, storage.ListOptions{ResourceVersion: "0", Predicate: storage.Everything})
if err != nil { if err != nil {
t.Fatalf("Watch failed: %v", err) t.Fatalf("Watch failed: %v", err)
} }
TestCheckResult(t, watch.Added, w, storedObj) testCheckResult(t, watch.Added, w, storedObj)
w.Stop() w.Stop()
// Update // Update
@ -151,7 +151,7 @@ func RunTestWatchFromZero(ctx context.Context, t *testing.T, store storage.Inter
if err != nil { if err != nil {
t.Fatalf("Watch failed: %v", err) t.Fatalf("Watch failed: %v", err)
} }
TestCheckResult(t, watch.Added, w, out) testCheckResult(t, watch.Added, w, out)
w.Stop() w.Stop()
if compaction == nil { if compaction == nil {
@ -176,11 +176,11 @@ func RunTestWatchFromZero(ctx context.Context, t *testing.T, store storage.Inter
if err != nil { if err != nil {
t.Fatalf("Watch failed: %v", err) t.Fatalf("Watch failed: %v", err)
} }
TestCheckResult(t, watch.Added, w, out) testCheckResult(t, watch.Added, w, out)
} }
func RunTestDeleteTriggerWatch(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestDeleteTriggerWatch(ctx context.Context, t *testing.T, store storage.Interface) {
key, storedObj := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, storedObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
w, err := store.Watch(ctx, key, storage.ListOptions{ResourceVersion: storedObj.ResourceVersion, Predicate: storage.Everything}) w, err := store.Watch(ctx, key, storage.ListOptions{ResourceVersion: storedObj.ResourceVersion, Predicate: storage.Everything})
if err != nil { if err != nil {
t.Fatalf("Watch failed: %v", err) t.Fatalf("Watch failed: %v", err)
@ -188,11 +188,11 @@ func RunTestDeleteTriggerWatch(ctx context.Context, t *testing.T, store storage.
if err := store.Delete(ctx, key, &example.Pod{}, nil, storage.ValidateAllObjectFunc, nil); err != nil { if err := store.Delete(ctx, key, &example.Pod{}, nil, storage.ValidateAllObjectFunc, nil); err != nil {
t.Fatalf("Delete failed: %v", err) t.Fatalf("Delete failed: %v", err)
} }
TestCheckEventType(t, watch.Deleted, w) testCheckEventType(t, watch.Deleted, w)
} }
func RunTestWatchFromNoneZero(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestWatchFromNoneZero(ctx context.Context, t *testing.T, store storage.Interface) {
key, storedObj := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, storedObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
w, err := store.Watch(ctx, key, storage.ListOptions{ResourceVersion: storedObj.ResourceVersion, Predicate: storage.Everything}) w, err := store.Watch(ctx, key, storage.ListOptions{ResourceVersion: storedObj.ResourceVersion, Predicate: storage.Everything})
if err != nil { if err != nil {
@ -203,7 +203,7 @@ func RunTestWatchFromNoneZero(ctx context.Context, t *testing.T, store storage.I
func(runtime.Object) (runtime.Object, error) { func(runtime.Object) (runtime.Object, error) {
return &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}}, err return &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}}, err
}), nil) }), nil)
TestCheckResult(t, watch.Modified, w, out) testCheckResult(t, watch.Modified, w, out)
} }
func RunTestWatchError(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) { func RunTestWatchError(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) {
@ -236,7 +236,7 @@ func RunTestWatchError(ctx context.Context, t *testing.T, store InterfaceWithPre
if err != nil { if err != nil {
t.Fatalf("Watch failed: %v", err) t.Fatalf("Watch failed: %v", err)
} }
TestCheckEventType(t, watch.Error, w) testCheckEventType(t, watch.Error, w)
} }
func RunTestWatchContextCancel(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestWatchContextCancel(ctx context.Context, t *testing.T, store storage.Interface) {
@ -263,7 +263,7 @@ func RunTestWatchContextCancel(ctx context.Context, t *testing.T, store storage.
} }
func RunTestWatchDeleteEventObjectHaveLatestRV(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestWatchDeleteEventObjectHaveLatestRV(ctx context.Context, t *testing.T, store storage.Interface) {
key, storedObj := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, storedObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
watchCtx, _ := context.WithTimeout(ctx, wait.ForeverTestTimeout) watchCtx, _ := context.WithTimeout(ctx, wait.ForeverTestTimeout)
w, err := store.Watch(watchCtx, key, storage.ListOptions{ResourceVersion: storedObj.ResourceVersion, Predicate: storage.Everything}) w, err := store.Watch(watchCtx, key, storage.ListOptions{ResourceVersion: storedObj.ResourceVersion, Predicate: storage.Everything})
@ -295,7 +295,7 @@ func RunTestWatchInitializationSignal(ctx context.Context, t *testing.T, store s
initSignal := utilflowcontrol.NewInitializationSignal() initSignal := utilflowcontrol.NewInitializationSignal()
ctx = utilflowcontrol.WithInitializationSignal(ctx, initSignal) ctx = utilflowcontrol.WithInitializationSignal(ctx, initSignal)
key, storedObj := TestPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, storedObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
_, err := store.Watch(ctx, key, storage.ListOptions{ResourceVersion: storedObj.ResourceVersion, Predicate: storage.Everything}) _, err := store.Watch(ctx, key, storage.ListOptions{ResourceVersion: storedObj.ResourceVersion, Predicate: storage.Everything})
if err != nil { if err != nil {
t.Fatalf("Watch failed: %v", err) t.Fatalf("Watch failed: %v", err)
@ -315,7 +315,7 @@ func RunOptionalTestProgressNotify(ctx context.Context, t *testing.T, store stor
if err := store.Create(ctx, key, input, out, 0); err != nil { if err := store.Create(ctx, key, input, out, 0); err != nil {
t.Fatalf("Create failed: %v", err) t.Fatalf("Create failed: %v", err)
} }
validateResourceVersion := ResourceVersionNotOlderThan(out.ResourceVersion) validateResourceVersion := resourceVersionNotOlderThan(out.ResourceVersion)
opts := storage.ListOptions{ opts := storage.ListOptions{
ResourceVersion: out.ResourceVersion, ResourceVersion: out.ResourceVersion,
@ -329,7 +329,7 @@ func RunOptionalTestProgressNotify(ctx context.Context, t *testing.T, store stor
// when we send a bookmark event, the client expects the event to contain an // when we send a bookmark event, the client expects the event to contain an
// object of the correct type, but with no fields set other than the resourceVersion // object of the correct type, but with no fields set other than the resourceVersion
TestCheckResultFunc(t, watch.Bookmark, w, func(object runtime.Object) error { testCheckResultFunc(t, watch.Bookmark, w, func(object runtime.Object) error {
// first, check that we have the correct resource version // first, check that we have the correct resource version
obj, ok := object.(metav1.Object) obj, ok := object.(metav1.Object)
if !ok { if !ok {