storage/testing: wire things back together with imports

Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
This commit is contained in:
Steve Kuznetsov 2022-05-05 11:32:05 -07:00
parent 0bfeb72878
commit 4ff560e4fa
No known key found for this signature in database
GPG Key ID: 8821C29EC988D9B4
4 changed files with 84 additions and 83 deletions

View File

@ -164,27 +164,27 @@ func checkStorageInvariants(ctx context.Context, t *testing.T, etcdClient *clien
func TestCreateWithTTL(t *testing.T) { func TestCreateWithTTL(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestCreateWithTTL(ctx, t, store) storagetesting.RunTestCreateWithTTL(ctx, t, store)
} }
func TestCreateWithKeyExist(t *testing.T) { func TestCreateWithKeyExist(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestCreateWithKeyExist(ctx, t, store) storagetesting.RunTestCreateWithKeyExist(ctx, t, store)
} }
func TestGet(t *testing.T) { func TestGet(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestGet(ctx, t, store) storagetesting.RunTestGet(ctx, t, store)
} }
func TestUnconditionalDelete(t *testing.T) { func TestUnconditionalDelete(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestUnconditionalDelete(ctx, t, store) storagetesting.RunTestUnconditionalDelete(ctx, t, store)
} }
func TestConditionalDelete(t *testing.T) { func TestConditionalDelete(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestConditionalDelete(ctx, t, store) storagetesting.RunTestConditionalDelete(ctx, t, store)
} }
// The following set of Delete tests are testing the logic of adding `suggestion` // The following set of Delete tests are testing the logic of adding `suggestion`
@ -214,32 +214,32 @@ func TestConditionalDelete(t *testing.T) {
func TestDeleteWithSuggestion(t *testing.T) { func TestDeleteWithSuggestion(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestDeleteWithSuggestion(ctx, t, store) storagetesting.RunTestDeleteWithSuggestion(ctx, t, store)
} }
func TestDeleteWithSuggestionAndConflict(t *testing.T) { func TestDeleteWithSuggestionAndConflict(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestDeleteWithSuggestionAndConflict(ctx, t, store) storagetesting.RunTestDeleteWithSuggestionAndConflict(ctx, t, store)
} }
func TestDeleteWithSuggestionOfDeletedObject(t *testing.T) { func TestDeleteWithSuggestionOfDeletedObject(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestDeleteWithSuggestionOfDeletedObject(ctx, t, store) storagetesting.RunTestDeleteWithSuggestionOfDeletedObject(ctx, t, store)
} }
func TestValidateDeletionWithSuggestion(t *testing.T) { func TestValidateDeletionWithSuggestion(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestValidateDeletionWithSuggestion(ctx, t, store) storagetesting.RunTestValidateDeletionWithSuggestion(ctx, t, store)
} }
func TestPreconditionalDeleteWithSuggestion(t *testing.T) { func TestPreconditionalDeleteWithSuggestion(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestPreconditionalDeleteWithSuggestion(ctx, t, store) storagetesting.RunTestPreconditionalDeleteWithSuggestion(ctx, t, store)
} }
func TestGetListNonRecursive(t *testing.T) { func TestGetListNonRecursive(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestGetListNonRecursive(ctx, t, store) storagetesting.RunTestGetListNonRecursive(ctx, t, store)
} }
func TestGuaranteedUpdate(t *testing.T) { func TestGuaranteedUpdate(t *testing.T) {
@ -326,7 +326,7 @@ func TestGuaranteedUpdate(t *testing.T) {
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 := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}}) key, storeObj := storagetesting.TestPropogateStore(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)
@ -397,7 +397,7 @@ func TestGuaranteedUpdate(t *testing.T) {
func TestGuaranteedUpdateWithTTL(t *testing.T) { func TestGuaranteedUpdateWithTTL(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestGuaranteedUpdateWithTTL(ctx, t, store) storagetesting.RunTestGuaranteedUpdateWithTTL(ctx, t, store)
} }
func TestGuaranteedUpdateChecksStoredData(t *testing.T) { func TestGuaranteedUpdateChecksStoredData(t *testing.T) {
@ -465,12 +465,12 @@ func TestGuaranteedUpdateChecksStoredData(t *testing.T) {
func TestGuaranteedUpdateWithConflict(t *testing.T) { func TestGuaranteedUpdateWithConflict(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestGuaranteedUpdateWithConflict(ctx, t, store) storagetesting.RunTestGuaranteedUpdateWithConflict(ctx, t, store)
} }
func TestGuaranteedUpdateWithSuggestionAndConflict(t *testing.T) { func TestGuaranteedUpdateWithSuggestionAndConflict(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestGuaranteedUpdateWithSuggestionAndConflict(ctx, t, store) storagetesting.RunTestGuaranteedUpdateWithSuggestionAndConflict(ctx, t, store)
} }
func TestTransformationFailure(t *testing.T) { func TestTransformationFailure(t *testing.T) {
@ -1060,7 +1060,7 @@ func TestList(t *testing.T) {
} }
for j, wantPod := range tt.expectedOut { for j, wantPod := range tt.expectedOut {
getPod := &out.Items[j] getPod := &out.Items[j]
expectNoDiff(t, fmt.Sprintf("%s: incorrect pod", tt.name), wantPod, getPod) storagetesting.ExpectNoDiff(t, fmt.Sprintf("%s: incorrect pod", tt.name), wantPod, getPod)
} }
}) })
} }
@ -1136,7 +1136,7 @@ func TestListContinuation(t *testing.T) {
if len(out.Continue) == 0 { if len(out.Continue) == 0 {
t.Fatalf("No continuation token set") t.Fatalf("No continuation token set")
} }
expectNoDiff(t, "incorrect first page", []example.Pod{*preset[0].storedObj}, out.Items) storagetesting.ExpectNoDiff(t, "incorrect first page", []example.Pod{*preset[0].storedObj}, out.Items)
if transformer.reads != 1 { if transformer.reads != 1 {
t.Errorf("unexpected reads: %d", transformer.reads) t.Errorf("unexpected reads: %d", transformer.reads)
} }
@ -1163,7 +1163,7 @@ func TestListContinuation(t *testing.T) {
} }
key, rv, err := decodeContinue(continueFromSecondItem, "/") key, rv, err := decodeContinue(continueFromSecondItem, "/")
t.Logf("continue token was %d %s %v", rv, key, err) t.Logf("continue token was %d %s %v", rv, key, err)
expectNoDiff(t, "incorrect second page", []example.Pod{*preset[1].storedObj, *preset[2].storedObj}, out.Items) storagetesting.ExpectNoDiff(t, "incorrect second page", []example.Pod{*preset[1].storedObj, *preset[2].storedObj}, out.Items)
if transformer.reads != 2 { if transformer.reads != 2 {
t.Errorf("unexpected reads: %d", transformer.reads) t.Errorf("unexpected reads: %d", transformer.reads)
} }
@ -1186,7 +1186,7 @@ func TestListContinuation(t *testing.T) {
if len(out.Continue) == 0 { if len(out.Continue) == 0 {
t.Fatalf("No continuation token set") t.Fatalf("No continuation token set")
} }
expectNoDiff(t, "incorrect second page", []example.Pod{*preset[1].storedObj}, out.Items) storagetesting.ExpectNoDiff(t, "incorrect second page", []example.Pod{*preset[1].storedObj}, out.Items)
if transformer.reads != 1 { if transformer.reads != 1 {
t.Errorf("unexpected reads: %d", transformer.reads) t.Errorf("unexpected reads: %d", transformer.reads)
} }
@ -1210,7 +1210,7 @@ func TestListContinuation(t *testing.T) {
if len(out.Continue) != 0 { if len(out.Continue) != 0 {
t.Fatalf("Unexpected continuation token set") t.Fatalf("Unexpected continuation token set")
} }
expectNoDiff(t, "incorrect third page", []example.Pod{*preset[2].storedObj}, out.Items) storagetesting.ExpectNoDiff(t, "incorrect third page", []example.Pod{*preset[2].storedObj}, out.Items)
if transformer.reads != 1 { if transformer.reads != 1 {
t.Errorf("unexpected reads: %d", transformer.reads) t.Errorf("unexpected reads: %d", transformer.reads)
} }
@ -1358,7 +1358,7 @@ func TestListContinuationWithFilter(t *testing.T) {
if len(out.Continue) == 0 { if len(out.Continue) == 0 {
t.Errorf("No continuation token set") t.Errorf("No continuation token set")
} }
expectNoDiff(t, "incorrect first page", []example.Pod{*preset[0].storedObj, *preset[2].storedObj}, out.Items) storagetesting.ExpectNoDiff(t, "incorrect first page", []example.Pod{*preset[0].storedObj, *preset[2].storedObj}, out.Items)
if transformer.reads != 3 { if transformer.reads != 3 {
t.Errorf("unexpected reads: %d", transformer.reads) t.Errorf("unexpected reads: %d", transformer.reads)
} }
@ -1390,7 +1390,7 @@ func TestListContinuationWithFilter(t *testing.T) {
if len(out.Continue) != 0 { if len(out.Continue) != 0 {
t.Errorf("Unexpected continuation token set") t.Errorf("Unexpected continuation token set")
} }
expectNoDiff(t, "incorrect second page", []example.Pod{*preset[3].storedObj}, out.Items) storagetesting.ExpectNoDiff(t, "incorrect second page", []example.Pod{*preset[3].storedObj}, out.Items)
if transformer.reads != 1 { if transformer.reads != 1 {
t.Errorf("unexpected reads: %d", transformer.reads) t.Errorf("unexpected reads: %d", transformer.reads)
} }
@ -1468,7 +1468,7 @@ func TestListInconsistentContinuation(t *testing.T) {
if len(out.Continue) == 0 { if len(out.Continue) == 0 {
t.Fatalf("No continuation token set") t.Fatalf("No continuation token set")
} }
expectNoDiff(t, "incorrect first page", []example.Pod{*preset[0].storedObj}, out.Items) storagetesting.ExpectNoDiff(t, "incorrect first page", []example.Pod{*preset[0].storedObj}, out.Items)
continueFromSecondItem := out.Continue continueFromSecondItem := out.Continue
@ -1535,7 +1535,7 @@ func TestListInconsistentContinuation(t *testing.T) {
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) storagetesting.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)
} }
@ -1553,7 +1553,7 @@ func TestListInconsistentContinuation(t *testing.T) {
if len(out.Continue) != 0 { if len(out.Continue) != 0 {
t.Fatalf("Unexpected continuation token set") t.Fatalf("Unexpected continuation token set")
} }
expectNoDiff(t, "incorrect third page", []example.Pod{*preset[2].storedObj}, out.Items) storagetesting.ExpectNoDiff(t, "incorrect third page", []example.Pod{*preset[2].storedObj}, out.Items)
if out.ResourceVersion != resolvedResourceVersionFromThirdItem { if out.ResourceVersion != resolvedResourceVersionFromThirdItem {
t.Fatalf("Expected list resource version to be %s, got %s", resolvedResourceVersionFromThirdItem, out.ResourceVersion) t.Fatalf("Expected list resource version to be %s, got %s", resolvedResourceVersionFromThirdItem, out.ResourceVersion)
} }
@ -1880,7 +1880,7 @@ func TestConsistentList(t *testing.T) {
t.Fatalf("failed to list objects: %v", err) t.Fatalf("failed to list objects: %v", err)
} }
expectNoDiff(t, "incorrect lists", result1, result2) storagetesting.ExpectNoDiff(t, "incorrect lists", result1, result2)
// Now also verify the ResourceVersionMatchNotOlderThan. // Now also verify the ResourceVersionMatchNotOlderThan.
options.ResourceVersionMatch = metav1.ResourceVersionMatchNotOlderThan options.ResourceVersionMatch = metav1.ResourceVersionMatchNotOlderThan
@ -1898,12 +1898,12 @@ func TestConsistentList(t *testing.T) {
t.Fatalf("failed to list objects: %v", err) t.Fatalf("failed to list objects: %v", err)
} }
expectNoDiff(t, "incorrect lists", result3, result4) storagetesting.ExpectNoDiff(t, "incorrect lists", result3, result4)
} }
func TestCount(t *testing.T) { func TestCount(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestCount(ctx, t, store) storagetesting.RunTestCount(ctx, t, store)
} }
func TestLeaseMaxObjectCount(t *testing.T) { func TestLeaseMaxObjectCount(t *testing.T) {

View File

@ -25,6 +25,7 @@ import (
"go.etcd.io/etcd/api/v3/mvccpb" "go.etcd.io/etcd/api/v3/mvccpb"
clientv3 "go.etcd.io/etcd/client/v3" clientv3 "go.etcd.io/etcd/client/v3"
storagetesting "k8s.io/apiserver/pkg/storage/testing"
"k8s.io/apimachinery/pkg/api/apitesting" "k8s.io/apimachinery/pkg/api/apitesting"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -40,12 +41,12 @@ import (
func TestWatch(t *testing.T) { func TestWatch(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestWatch(ctx, t, store) storagetesting.RunTestWatch(ctx, t, store)
} }
func TestDeleteTriggerWatch(t *testing.T) { func TestDeleteTriggerWatch(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestDeleteTriggerWatch(ctx, t, store) storagetesting.RunTestDeleteTriggerWatch(ctx, t, store)
} }
// TestWatchFromZero tests that // TestWatchFromZero tests that
@ -53,13 +54,13 @@ func TestDeleteTriggerWatch(t *testing.T) {
// - 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 TestWatchFromZero(t *testing.T) { func TestWatchFromZero(t *testing.T) {
ctx, store, client := testSetup(t) ctx, store, client := testSetup(t)
key, storedObj := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "ns"}}) key, storedObj := storagetesting.TestPropogateStore(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) storagetesting.TestCheckResult(t, watch.Added, w, storedObj)
w.Stop() w.Stop()
// Update // Update
@ -77,7 +78,7 @@ func TestWatchFromZero(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Watch failed: %v", err) t.Fatalf("Watch failed: %v", err)
} }
testCheckResult(t, watch.Added, w, out) storagetesting.TestCheckResult(t, watch.Added, w, out)
w.Stop() w.Stop()
// Update again // Update again
@ -105,14 +106,14 @@ func TestWatchFromZero(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Watch failed: %v", err) t.Fatalf("Watch failed: %v", err)
} }
testCheckResult(t, watch.Added, w, out) storagetesting.TestCheckResult(t, watch.Added, w, out)
} }
// TestWatchFromNoneZero tests that // TestWatchFromNoneZero tests that
// - watch from non-0 should just watch changes after given version // - watch from non-0 should just watch changes after given version
func TestWatchFromNoneZero(t *testing.T) { func TestWatchFromNoneZero(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestWatchFromNoneZero(ctx, t, store) storagetesting.RunTestWatchFromNoneZero(ctx, t, store)
} }
func TestWatchError(t *testing.T) { func TestWatchError(t *testing.T) {
@ -131,7 +132,7 @@ func TestWatchError(t *testing.T) {
}), nil); err != nil { }), nil); err != nil {
t.Fatalf("GuaranteedUpdate failed: %v", err) t.Fatalf("GuaranteedUpdate failed: %v", err)
} }
testCheckEventType(t, watch.Error, w) storagetesting.TestCheckEventType(t, watch.Error, w)
} }
func TestWatchContextCancel(t *testing.T) { func TestWatchContextCancel(t *testing.T) {
@ -179,7 +180,7 @@ func TestWatchErrResultNotBlockAfterCancel(t *testing.T) {
func TestWatchDeleteEventObjectHaveLatestRV(t *testing.T) { func TestWatchDeleteEventObjectHaveLatestRV(t *testing.T) {
ctx, store, client := testSetup(t) ctx, store, client := testSetup(t)
key, storedObj := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, storedObj := storagetesting.TestPropogateStore(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 {
@ -235,7 +236,7 @@ func deletedRevision(ctx context.Context, watch <-chan clientv3.WatchResponse) (
func TestWatchInitializationSignal(t *testing.T) { func TestWatchInitializationSignal(t *testing.T) {
ctx, store, _ := testSetup(t) ctx, store, _ := testSetup(t)
RunTestWatchInitializationSignal(ctx, t, store) storagetesting.RunTestWatchInitializationSignal(ctx, t, store)
} }
func TestProgressNotify(t *testing.T) { func TestProgressNotify(t *testing.T) {
@ -263,7 +264,7 @@ func TestProgressNotify(t *testing.T) {
// 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 { storagetesting.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 {
@ -279,7 +280,7 @@ func TestProgressNotify(t *testing.T) {
return fmt.Errorf("got %T, not *example.Pod", object) return fmt.Errorf("got %T, not *example.Pod", object)
} }
pod.ResourceVersion = "" pod.ResourceVersion = ""
expectNoDiff(t, "bookmark event should contain an object with no fields set other than resourceVersion", newPod(), pod) storagetesting.ExpectNoDiff(t, "bookmark event should contain an object with no fields set other than resourceVersion", newPod(), pod)
return nil return nil
}) })
} }

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package etcd3 package testing
import ( import (
"context" "context"
@ -50,12 +50,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, _ := testPropogateStore(ctx, t, store, obj) key, _ := TestPropogateStore(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) {
@ -65,7 +65,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 := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, createdObj := TestPropogateStore(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"}
@ -159,13 +159,13 @@ func RunTestGet(ctx context.Context, t *testing.T, store storage.Interface) {
if err != nil { if err != nil {
t.Fatalf("Get failed: %v", err) t.Fatalf("Get failed: %v", err)
} }
expectNoDiff(t, fmt.Sprintf("%s: incorrect pod", tt.name), tt.expectedOut, out) ExpectNoDiff(t, fmt.Sprintf("%s: incorrect pod", tt.name), tt.expectedOut, out)
}) })
} }
} }
func RunTestUnconditionalDelete(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestUnconditionalDelete(ctx context.Context, t *testing.T, store storage.Interface) {
key, storedObj := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, storedObj := TestPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
tests := []struct { tests := []struct {
name string name string
@ -197,13 +197,13 @@ func RunTestUnconditionalDelete(ctx context.Context, t *testing.T, store storage
if err != nil { if err != nil {
t.Fatalf("%s: Delete failed: %v", tt.name, err) t.Fatalf("%s: Delete failed: %v", tt.name, err)
} }
expectNoDiff(t, fmt.Sprintf("%s: incorrect pod:", tt.name), tt.expectedObj, out) ExpectNoDiff(t, fmt.Sprintf("%s: incorrect pod:", tt.name), tt.expectedObj, out)
}) })
} }
} }
func RunTestConditionalDelete(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestConditionalDelete(ctx context.Context, t *testing.T, store storage.Interface) {
key, storedObj := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}}) key, storedObj := TestPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}})
tests := []struct { tests := []struct {
name string name string
@ -232,8 +232,8 @@ func RunTestConditionalDelete(ctx context.Context, t *testing.T, store storage.I
if err != nil { if err != nil {
t.Fatalf("%s: Delete failed: %v", tt.name, err) t.Fatalf("%s: Delete failed: %v", tt.name, err)
} }
expectNoDiff(t, fmt.Sprintf("%s: incorrect pod", tt.name), storedObj, out) ExpectNoDiff(t, fmt.Sprintf("%s: incorrect pod", tt.name), storedObj, out)
key, storedObj = testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}}) key, storedObj = TestPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}})
}) })
} }
} }
@ -265,7 +265,7 @@ func RunTestConditionalDelete(ctx context.Context, t *testing.T, store storage.I
func RunTestDeleteWithSuggestion(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestDeleteWithSuggestion(ctx context.Context, t *testing.T, store storage.Interface) {
key, originalPod := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}}) key, originalPod := TestPropogateStore(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 {
@ -279,7 +279,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 := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}}) key, originalPod := TestPropogateStore(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{}
@ -304,7 +304,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 := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}}) key, originalPod := TestPropogateStore(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{}
@ -321,7 +321,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 := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}}) key, originalPod := TestPropogateStore(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
@ -374,7 +374,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 := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name"}}) key, originalPod := TestPropogateStore(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{}
@ -400,11 +400,11 @@ func RunTestPreconditionalDeleteWithSuggestion(ctx context.Context, t *testing.T
} }
func RunTestGetListNonRecursive(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestGetListNonRecursive(ctx context.Context, t *testing.T, store storage.Interface) {
prevKey, prevStoredObj := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "prev"}}) prevKey, prevStoredObj := TestPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "prev"}})
prevRV, _ := strconv.Atoi(prevStoredObj.ResourceVersion) prevRV, _ := strconv.Atoi(prevStoredObj.ResourceVersion)
key, storedObj := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, storedObj := TestPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
currentRV, _ := strconv.Atoi(storedObj.ResourceVersion) currentRV, _ := strconv.Atoi(storedObj.ResourceVersion)
@ -524,7 +524,7 @@ func RunTestGetListNonRecursive(ctx context.Context, t *testing.T, store storage
} }
for j, wantPod := range tt.expectedOut { for j, wantPod := range tt.expectedOut {
getPod := &out.Items[j] getPod := &out.Items[j]
expectNoDiff(t, fmt.Sprintf("%s: incorrect pod", tt.name), wantPod, getPod) ExpectNoDiff(t, fmt.Sprintf("%s: incorrect pod", tt.name), wantPod, getPod)
} }
}) })
} }
@ -549,11 +549,11 @@ 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 RunTestGuaranteedUpdateWithConflict(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestGuaranteedUpdateWithConflict(ctx context.Context, t *testing.T, store storage.Interface) {
key, _ := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, _ := TestPropogateStore(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
@ -598,7 +598,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 := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, originalPod := TestPropogateStore(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{}
@ -672,16 +672,16 @@ func RunTestGuaranteedUpdateWithSuggestionAndConflict(ctx context.Context, t *te
} }
} }
// testPropogateStore helps propagates store with objects, automates key generation, and returns // TestPropogateStore helps propagates store with objects, automates key generation, and returns
// keys and stored objects. // keys and stored objects.
func testPropogateStore(ctx context.Context, t *testing.T, store storage.Interface, obj *example.Pod) (string, *example.Pod) { func TestPropogateStore(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 := "/testkey" key := "/testkey"
return key, testPropogateStoreWithKey(ctx, t, store, key, obj) return key, TestPropogateStoreWithKey(ctx, t, store, key, obj)
} }
// testPropogateStoreWithKey helps propagate store with objects, the given object will be stored at the specified key. // TestPropogateStoreWithKey helps propagate store with objects, the given object will be stored at the specified key.
func testPropogateStoreWithKey(ctx context.Context, t *testing.T, store storage.Interface, key string, obj *example.Pod) *example.Pod { func TestPropogateStoreWithKey(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) {
@ -707,7 +707,7 @@ 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)
testPropogateStoreWithKey(ctx, t, store, key, obj) TestPropogateStoreWithKey(ctx, t, store, key, obj)
} }
resourceBCount := 4 resourceBCount := 4
@ -715,7 +715,7 @@ 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)
testPropogateStoreWithKey(ctx, t, store, key, obj) TestPropogateStoreWithKey(ctx, t, store, key, obj)
} }
resourceACountGot, err := store.Count(resourceA) resourceACountGot, err := store.Count(resourceA)
@ -730,7 +730,7 @@ func RunTestCount(ctx context.Context, t *testing.T, store storage.Interface) {
} }
} }
func expectNoDiff(t *testing.T, msg string, expected, got interface{}) { func ExpectNoDiff(t *testing.T, msg string, expected, got interface{}) {
t.Helper() t.Helper()
if !reflect.DeepEqual(expected, got) { if !reflect.DeepEqual(expected, got) {
if diff := cmp.Diff(expected, got); diff != "" { if diff := cmp.Diff(expected, got); diff != "" {

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
package etcd3 package testing
import ( import (
"context" "context"
@ -111,18 +111,18 @@ 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)
}) })
} }
} }
func RunTestDeleteTriggerWatch(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestDeleteTriggerWatch(ctx context.Context, t *testing.T, store storage.Interface) {
key, storedObj := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, storedObj := TestPropogateStore(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)
@ -130,11 +130,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 := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, storedObj := TestPropogateStore(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 {
@ -145,7 +145,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 RunTestWatchInitializationSignal(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestWatchInitializationSignal(ctx context.Context, t *testing.T, store storage.Interface) {
@ -153,7 +153,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 := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}) key, storedObj := TestPropogateStore(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)
@ -168,7 +168,7 @@ type testWatchStruct struct {
watchType watch.EventType watchType watch.EventType
} }
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 {
@ -179,14 +179,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 {
@ -201,7 +201,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 {