storage: etcd: TestGuaranteedUpdate: use sub-tests

Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
This commit is contained in:
Steve Kuznetsov 2022-02-16 13:26:00 -08:00
parent f1ded3b0c3
commit b6a6625217
No known key found for this signature in database
GPG Key ID: 8821C29EC988D9B4

View File

@ -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,6 +742,7 @@ func TestGuaranteedUpdate(t *testing.T) {
}} }}
for i, tt := range tests { for i, tt := range tests {
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 := testPropogateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}})
out := &example.Pod{} out := &example.Pod{}
@ -751,7 +761,7 @@ func TestGuaranteedUpdate(t *testing.T) {
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 pod := *storeObj
@ -765,24 +775,24 @@ func TestGuaranteedUpdate(t *testing.T) {
if tt.expectNotFoundErr { if tt.expectNotFoundErr {
if err == nil || !storage.IsNotFound(err) { if err == nil || !storage.IsNotFound(err) {
t.Errorf("#%d: expecting not found error, but get: %v", i, err) t.Errorf("%s: expecting not found error, but get: %v", tt.name, err)
} }
continue return
} }
if tt.expectInvalidObjErr { if tt.expectInvalidObjErr {
if err == nil || !storage.IsInvalidObj(err) { if err == nil || !storage.IsInvalidObj(err) {
t.Errorf("#%d: expecting invalid UID error, but get: %s", i, err) t.Errorf("%s: expecting invalid UID error, but get: %s", tt.name, err)
} }
continue return
} }
if err != nil { if err != nil {
t.Fatalf("GuaranteedUpdate failed: %v", err) t.Fatalf("%s: GuaranteedUpdate failed: %v", tt.name, err)
} }
if out.ObjectMeta.Name != name { if out.ObjectMeta.Name != name {
t.Errorf("#%d: pod name want=%s, get=%s", i, name, out.ObjectMeta.Name) t.Errorf("%s: pod name want=%s, get=%s", tt.name, name, out.ObjectMeta.Name)
} }
if out.SelfLink != "" { if out.SelfLink != "" {
t.Errorf("#%d: selfLink should not be set", i) t.Errorf("%s: selfLink should not be set", tt.name)
} }
// 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
@ -791,13 +801,14 @@ func TestGuaranteedUpdate(t *testing.T) {
switch tt.expectNoUpdate { switch tt.expectNoUpdate {
case true: case true:
if version != out.ResourceVersion { if version != out.ResourceVersion {
t.Errorf("#%d: expect no version change, before=%s, after=%s", i, version, out.ResourceVersion) t.Errorf("%s: expect no version change, before=%s, after=%s", tt.name, version, out.ResourceVersion)
} }
case false: case false:
if version == out.ResourceVersion { if version == out.ResourceVersion {
t.Errorf("#%d: expect version change, but get the same version=%s", i, version) t.Errorf("%s: expect version change, but get the same version=%s", tt.name, version)
} }
} }
})
} }
} }