mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
storage: etcd: TestGuaranteedUpdate: use sub-tests
Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
This commit is contained in:
parent
f1ded3b0c3
commit
b6a6625217
@ -664,6 +664,7 @@ func TestGuaranteedUpdate(t *testing.T) {
|
|||||||
key := "/testkey"
|
key := "/testkey"
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
name string
|
||||||
key string
|
key string
|
||||||
ignoreNotFound bool
|
ignoreNotFound bool
|
||||||
precondition *storage.Preconditions
|
precondition *storage.Preconditions
|
||||||
@ -672,35 +673,40 @@ func TestGuaranteedUpdate(t *testing.T) {
|
|||||||
expectNoUpdate bool
|
expectNoUpdate bool
|
||||||
transformStale bool
|
transformStale bool
|
||||||
hasSelfLink bool
|
hasSelfLink bool
|
||||||
}{{ // GuaranteedUpdate on non-existing key with ignoreNotFound=false
|
}{{
|
||||||
|
name: "GuaranteedUpdate on non-existing key with ignoreNotFound=false",
|
||||||
key: "/non-existing",
|
key: "/non-existing",
|
||||||
ignoreNotFound: false,
|
ignoreNotFound: false,
|
||||||
precondition: nil,
|
precondition: nil,
|
||||||
expectNotFoundErr: true,
|
expectNotFoundErr: true,
|
||||||
expectInvalidObjErr: false,
|
expectInvalidObjErr: false,
|
||||||
expectNoUpdate: false,
|
expectNoUpdate: false,
|
||||||
}, { // GuaranteedUpdate on non-existing key with ignoreNotFound=true
|
}, {
|
||||||
|
name: "GuaranteedUpdate on non-existing key with ignoreNotFound=true",
|
||||||
key: "/non-existing",
|
key: "/non-existing",
|
||||||
ignoreNotFound: true,
|
ignoreNotFound: true,
|
||||||
precondition: nil,
|
precondition: nil,
|
||||||
expectNotFoundErr: false,
|
expectNotFoundErr: false,
|
||||||
expectInvalidObjErr: false,
|
expectInvalidObjErr: false,
|
||||||
expectNoUpdate: false,
|
expectNoUpdate: false,
|
||||||
}, { // GuaranteedUpdate on existing key
|
}, {
|
||||||
|
name: "GuaranteedUpdate on existing key",
|
||||||
key: key,
|
key: key,
|
||||||
ignoreNotFound: false,
|
ignoreNotFound: false,
|
||||||
precondition: nil,
|
precondition: nil,
|
||||||
expectNotFoundErr: false,
|
expectNotFoundErr: false,
|
||||||
expectInvalidObjErr: false,
|
expectInvalidObjErr: false,
|
||||||
expectNoUpdate: false,
|
expectNoUpdate: false,
|
||||||
}, { // GuaranteedUpdate with same data
|
}, {
|
||||||
|
name: "GuaranteedUpdate with same data",
|
||||||
key: key,
|
key: key,
|
||||||
ignoreNotFound: false,
|
ignoreNotFound: false,
|
||||||
precondition: nil,
|
precondition: nil,
|
||||||
expectNotFoundErr: false,
|
expectNotFoundErr: false,
|
||||||
expectInvalidObjErr: false,
|
expectInvalidObjErr: false,
|
||||||
expectNoUpdate: true,
|
expectNoUpdate: true,
|
||||||
}, { // GuaranteedUpdate with same data AND a selfLink
|
}, {
|
||||||
|
name: "GuaranteedUpdate with same data AND a selfLink",
|
||||||
key: key,
|
key: key,
|
||||||
ignoreNotFound: false,
|
ignoreNotFound: false,
|
||||||
precondition: nil,
|
precondition: nil,
|
||||||
@ -708,7 +714,8 @@ func TestGuaranteedUpdate(t *testing.T) {
|
|||||||
expectInvalidObjErr: false,
|
expectInvalidObjErr: false,
|
||||||
expectNoUpdate: true,
|
expectNoUpdate: true,
|
||||||
hasSelfLink: true,
|
hasSelfLink: true,
|
||||||
}, { // GuaranteedUpdate with same data but stale
|
}, {
|
||||||
|
name: "GuaranteedUpdate with same data but stale",
|
||||||
key: key,
|
key: key,
|
||||||
ignoreNotFound: false,
|
ignoreNotFound: false,
|
||||||
precondition: nil,
|
precondition: nil,
|
||||||
@ -716,14 +723,16 @@ func TestGuaranteedUpdate(t *testing.T) {
|
|||||||
expectInvalidObjErr: false,
|
expectInvalidObjErr: false,
|
||||||
expectNoUpdate: false,
|
expectNoUpdate: false,
|
||||||
transformStale: true,
|
transformStale: true,
|
||||||
}, { // GuaranteedUpdate with UID match
|
}, {
|
||||||
|
name: "GuaranteedUpdate with UID match",
|
||||||
key: key,
|
key: key,
|
||||||
ignoreNotFound: false,
|
ignoreNotFound: false,
|
||||||
precondition: storage.NewUIDPreconditions("A"),
|
precondition: storage.NewUIDPreconditions("A"),
|
||||||
expectNotFoundErr: false,
|
expectNotFoundErr: false,
|
||||||
expectInvalidObjErr: false,
|
expectInvalidObjErr: false,
|
||||||
expectNoUpdate: true,
|
expectNoUpdate: true,
|
||||||
}, { // GuaranteedUpdate with UID mismatch
|
}, {
|
||||||
|
name: "GuaranteedUpdate with UID mismatch",
|
||||||
key: key,
|
key: key,
|
||||||
ignoreNotFound: false,
|
ignoreNotFound: false,
|
||||||
precondition: storage.NewUIDPreconditions("B"),
|
precondition: storage.NewUIDPreconditions("B"),
|
||||||
@ -733,71 +742,73 @@ func TestGuaranteedUpdate(t *testing.T) {
|
|||||||
}}
|
}}
|
||||||
|
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
key, storeObj := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}})
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
key, storeObj := 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)
|
||||||
if tt.expectNoUpdate {
|
if tt.expectNoUpdate {
|
||||||
name = storeObj.Name
|
name = storeObj.Name
|
||||||
}
|
}
|
||||||
originalTransformer := store.transformer.(*prefixTransformer)
|
originalTransformer := store.transformer.(*prefixTransformer)
|
||||||
if tt.transformStale {
|
if tt.transformStale {
|
||||||
transformer := *originalTransformer
|
transformer := *originalTransformer
|
||||||
transformer.stale = true
|
transformer.stale = true
|
||||||
store.transformer = &transformer
|
store.transformer = &transformer
|
||||||
}
|
}
|
||||||
version := storeObj.ResourceVersion
|
version := storeObj.ResourceVersion
|
||||||
err := store.GuaranteedUpdate(ctx, tt.key, out, tt.ignoreNotFound, tt.precondition,
|
err := store.GuaranteedUpdate(ctx, tt.key, out, tt.ignoreNotFound, tt.precondition,
|
||||||
storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
|
storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
|
||||||
if tt.expectNotFoundErr && tt.ignoreNotFound {
|
if tt.expectNotFoundErr && tt.ignoreNotFound {
|
||||||
if pod := obj.(*example.Pod); pod.Name != "" {
|
if pod := obj.(*example.Pod); pod.Name != "" {
|
||||||
t.Errorf("#%d: expecting zero value, but get=%#v", i, pod)
|
t.Errorf("%s: expecting zero value, but get=%#v", tt.name, pod)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
pod := *storeObj
|
||||||
|
if tt.hasSelfLink {
|
||||||
|
pod.SelfLink = "testlink"
|
||||||
|
}
|
||||||
|
pod.Name = name
|
||||||
|
return &pod, nil
|
||||||
|
}), nil)
|
||||||
|
store.transformer = originalTransformer
|
||||||
|
|
||||||
|
if tt.expectNotFoundErr {
|
||||||
|
if err == nil || !storage.IsNotFound(err) {
|
||||||
|
t.Errorf("%s: expecting not found error, but get: %v", tt.name, err)
|
||||||
}
|
}
|
||||||
pod := *storeObj
|
return
|
||||||
if tt.hasSelfLink {
|
}
|
||||||
pod.SelfLink = "testlink"
|
if tt.expectInvalidObjErr {
|
||||||
|
if err == nil || !storage.IsInvalidObj(err) {
|
||||||
|
t.Errorf("%s: expecting invalid UID error, but get: %s", tt.name, err)
|
||||||
}
|
}
|
||||||
pod.Name = name
|
return
|
||||||
return &pod, nil
|
}
|
||||||
}), nil)
|
if err != nil {
|
||||||
store.transformer = originalTransformer
|
t.Fatalf("%s: GuaranteedUpdate failed: %v", tt.name, err)
|
||||||
|
}
|
||||||
|
if out.ObjectMeta.Name != name {
|
||||||
|
t.Errorf("%s: pod name want=%s, get=%s", tt.name, name, out.ObjectMeta.Name)
|
||||||
|
}
|
||||||
|
if out.SelfLink != "" {
|
||||||
|
t.Errorf("%s: selfLink should not be set", tt.name)
|
||||||
|
}
|
||||||
|
|
||||||
if tt.expectNotFoundErr {
|
// verify that kv pair is not empty after set and that the underlying data matches expectations
|
||||||
if err == nil || !storage.IsNotFound(err) {
|
checkStorageInvariants(ctx, t, etcdClient, store, key)
|
||||||
t.Errorf("#%d: expecting not found error, but get: %v", i, err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if tt.expectInvalidObjErr {
|
|
||||||
if err == nil || !storage.IsInvalidObj(err) {
|
|
||||||
t.Errorf("#%d: expecting invalid UID error, but get: %s", i, err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("GuaranteedUpdate failed: %v", err)
|
|
||||||
}
|
|
||||||
if out.ObjectMeta.Name != name {
|
|
||||||
t.Errorf("#%d: pod name want=%s, get=%s", i, name, out.ObjectMeta.Name)
|
|
||||||
}
|
|
||||||
if out.SelfLink != "" {
|
|
||||||
t.Errorf("#%d: selfLink should not be set", i)
|
|
||||||
}
|
|
||||||
|
|
||||||
// verify that kv pair is not empty after set and that the underlying data matches expectations
|
switch tt.expectNoUpdate {
|
||||||
checkStorageInvariants(ctx, t, etcdClient, store, key)
|
case true:
|
||||||
|
if version != out.ResourceVersion {
|
||||||
switch tt.expectNoUpdate {
|
t.Errorf("%s: expect no version change, before=%s, after=%s", tt.name, version, out.ResourceVersion)
|
||||||
case true:
|
}
|
||||||
if version != out.ResourceVersion {
|
case false:
|
||||||
t.Errorf("#%d: expect no version change, before=%s, after=%s", i, version, out.ResourceVersion)
|
if version == out.ResourceVersion {
|
||||||
|
t.Errorf("%s: expect version change, but get the same version=%s", tt.name, version)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case false:
|
})
|
||||||
if version == out.ResourceVersion {
|
|
||||||
t.Errorf("#%d: expect version change, but get the same version=%s", i, version)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user