diff --git a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go index 205dfce65c8..f371558784d 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/etcd3/store_test.go @@ -109,39 +109,12 @@ func newPod() runtime.Object { func TestCreate(t *testing.T) { ctx, store, etcdClient := testSetup(t) - - key := "/testkey" - out := &example.Pod{} - obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", SelfLink: "testlink"}} - - // verify that kv pair is empty before set - getResp, err := etcdClient.KV.Get(ctx, key) - if err != nil { - t.Fatalf("etcdClient.KV.Get failed: %v", err) - } - if len(getResp.Kvs) != 0 { - t.Fatalf("expecting empty result on key: %s", key) - } - - err = store.Create(ctx, key, obj, out, 0) - if err != nil { - t.Fatalf("Set failed: %v", err) - } - // basic tests of the output - if obj.ObjectMeta.Name != out.ObjectMeta.Name { - t.Errorf("pod name want=%s, get=%s", obj.ObjectMeta.Name, out.ObjectMeta.Name) - } - if out.ResourceVersion == "" { - t.Errorf("output should have non-empty resource version") - } - if out.SelfLink != "" { - t.Errorf("output should have empty selfLink") - } - - checkStorageInvariants(ctx, t, etcdClient, store, key) + storagetesting.RunTestCreate(ctx, t, store, func(ctx context.Context, t *testing.T, key string) { + checkStorageInvariants(ctx, t, etcdClient, store.codec, key) + }) } -func checkStorageInvariants(ctx context.Context, t *testing.T, etcdClient *clientv3.Client, store *store, key string) { +func checkStorageInvariants(ctx context.Context, t *testing.T, etcdClient *clientv3.Client, codec runtime.Codec, key string) { getResp, err := etcdClient.KV.Get(ctx, key) if err != nil { t.Fatalf("etcdClient.KV.Get failed: %v", err) @@ -149,7 +122,7 @@ func checkStorageInvariants(ctx context.Context, t *testing.T, etcdClient *clien if len(getResp.Kvs) == 0 { t.Fatalf("expecting non empty result on key: %s", key) } - decoded, err := runtime.Decode(store.codec, getResp.Kvs[0].Value[len(defaultTestPrefix):]) + decoded, err := runtime.Decode(codec, getResp.Kvs[0].Value[len(defaultTestPrefix):]) if err != nil { t.Fatalf("expecting successful decode of object from %v\n%v", err, string(getResp.Kvs[0].Value)) } @@ -379,7 +352,7 @@ func TestGuaranteedUpdate(t *testing.T) { } // verify that kv pair is not empty after set and that the underlying data matches expectations - checkStorageInvariants(ctx, t, etcdClient, store, key) + checkStorageInvariants(ctx, t, etcdClient, store.codec, key) switch tt.expectNoUpdate { case true: diff --git a/staging/src/k8s.io/apiserver/pkg/storage/testing/store_tests.go b/staging/src/k8s.io/apiserver/pkg/storage/testing/store_tests.go index 6db38d80880..d272a1a6ae6 100644 --- a/staging/src/k8s.io/apiserver/pkg/storage/testing/store_tests.go +++ b/staging/src/k8s.io/apiserver/pkg/storage/testing/store_tests.go @@ -37,6 +37,35 @@ import ( "k8s.io/apiserver/pkg/storage" ) +type KeyValidation func(ctx context.Context, t *testing.T, key string) + +func RunTestCreate(ctx context.Context, t *testing.T, store storage.Interface, validation KeyValidation) { + key := "/testkey" + out := &example.Pod{} + obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", SelfLink: "testlink"}} + + // verify that kv pair is empty before set + if err := store.Get(ctx, key, storage.GetOptions{}, out); !storage.IsNotFound(err) { + t.Fatalf("expecting empty result on key %s, got %v", key, err) + } + + if err := store.Create(ctx, key, obj, out, 0); err != nil { + t.Fatalf("Set failed: %v", err) + } + // basic tests of the output + if obj.ObjectMeta.Name != out.ObjectMeta.Name { + t.Errorf("pod name want=%s, get=%s", obj.ObjectMeta.Name, out.ObjectMeta.Name) + } + if out.ResourceVersion == "" { + t.Errorf("output should have non-empty resource version") + } + if out.SelfLink != "" { + t.Errorf("output should have empty selfLink") + } + + validation(ctx, t, key) +} + func RunTestCreateWithTTL(ctx context.Context, t *testing.T, store storage.Interface) { input := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}} key := "/somekey"