mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
etcd3/store: make creation test validation generic
Different callers to this test may need to do different backend-specific validation on the stored data, so we allow them a callback for this. Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
This commit is contained in:
parent
f894f8196d
commit
6d25e96ced
@ -109,7 +109,14 @@ func newPod() runtime.Object {
|
|||||||
|
|
||||||
func TestCreate(t *testing.T) {
|
func TestCreate(t *testing.T) {
|
||||||
ctx, store, etcdClient := testSetup(t)
|
ctx, store, etcdClient := testSetup(t)
|
||||||
|
RunTestCreate(ctx, t, store, func(ctx context.Context, t *testing.T, key string) {
|
||||||
|
checkStorageInvariants(ctx, t, etcdClient, store.codec, key)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
key := "/testkey"
|
||||||
out := &example.Pod{}
|
out := &example.Pod{}
|
||||||
obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", SelfLink: "testlink"}}
|
obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", SelfLink: "testlink"}}
|
||||||
@ -133,10 +140,10 @@ func TestCreate(t *testing.T) {
|
|||||||
t.Errorf("output should have empty selfLink")
|
t.Errorf("output should have empty selfLink")
|
||||||
}
|
}
|
||||||
|
|
||||||
checkStorageInvariants(ctx, t, etcdClient, store, key)
|
validation(ctx, t, 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)
|
getResp, err := etcdClient.KV.Get(ctx, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("etcdClient.KV.Get failed: %v", err)
|
t.Fatalf("etcdClient.KV.Get failed: %v", err)
|
||||||
@ -144,7 +151,7 @@ func checkStorageInvariants(ctx context.Context, t *testing.T, etcdClient *clien
|
|||||||
if len(getResp.Kvs) == 0 {
|
if len(getResp.Kvs) == 0 {
|
||||||
t.Fatalf("expecting non empty result on key: %s", key)
|
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 {
|
if err != nil {
|
||||||
t.Fatalf("expecting successful decode of object from %v\n%v", err, string(getResp.Kvs[0].Value))
|
t.Fatalf("expecting successful decode of object from %v\n%v", err, string(getResp.Kvs[0].Value))
|
||||||
}
|
}
|
||||||
@ -374,7 +381,7 @@ func TestGuaranteedUpdate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// verify that kv pair is not empty after set and that the underlying data matches expectations
|
// 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 {
|
switch tt.expectNoUpdate {
|
||||||
case true:
|
case true:
|
||||||
|
Loading…
Reference in New Issue
Block a user