Merge pull request #113666 from wojtek-t/storage_standard_test_naming

Standardize naming in generic storage tests
This commit is contained in:
Kubernetes Prow Robot 2022-11-07 09:18:40 -08:00 committed by GitHub
commit f8750e2faf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 139 additions and 129 deletions

View File

@ -46,11 +46,11 @@ import (
type KeyValidation func(ctx context.Context, t *testing.T, key string) type KeyValidation func(ctx context.Context, t *testing.T, key string)
func RunTestCreate(ctx context.Context, t *testing.T, store storage.Interface, validation KeyValidation) { func RunTestCreate(ctx context.Context, t *testing.T, store storage.Interface, validation KeyValidation) {
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"}}
// verify that kv pair is empty before set // verify that kv pair is empty before set
key := computePodKey(obj)
if err := store.Get(ctx, key, storage.GetOptions{}, out); !storage.IsNotFound(err) { if err := store.Get(ctx, key, storage.GetOptions{}, out); !storage.IsNotFound(err) {
t.Fatalf("expecting empty result on key %s, got %v", key, err) t.Fatalf("expecting empty result on key %s, got %v", key, err)
} }
@ -74,9 +74,9 @@ func RunTestCreate(ctx context.Context, t *testing.T, store storage.Interface, v
func RunTestCreateWithTTL(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestCreateWithTTL(ctx context.Context, t *testing.T, store storage.Interface) {
input := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}} input := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
key := "/somekey"
out := &example.Pod{} out := &example.Pod{}
key := computePodKey(input)
if err := store.Create(ctx, key, input, out, 1); err != nil { if err := store.Create(ctx, key, input, out, 1); err != nil {
t.Fatalf("Create failed: %v", err) t.Fatalf("Create failed: %v", err)
} }
@ -92,6 +92,7 @@ func RunTestCreateWithKeyExist(ctx context.Context, t *testing.T, store storage.
obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}} obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
key, _ := testPropagateStore(ctx, t, store, obj) key, _ := testPropagateStore(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) {
t.Errorf("expecting key exists error, but get: %s", err) t.Errorf("expecting key exists error, but get: %s", err)
@ -114,8 +115,9 @@ func RunTestGet(ctx context.Context, t *testing.T, store storage.Interface) {
t.Fatalf("Update failed: %v", err) t.Fatalf("Update failed: %v", err)
} }
// create an additional object to increment the resource version for pods above the resource version of the foo object // create an additional object to increment the resource version for pods above the resource version of the foo object
secondObj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}}
lastUpdatedObj := &example.Pod{} lastUpdatedObj := &example.Pod{}
if err := store.Create(ctx, "bar", &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}}, lastUpdatedObj, 0); err != nil { if err := store.Create(ctx, computePodKey(secondObj), secondObj, lastUpdatedObj, 0); err != nil {
t.Fatalf("Set failed: %v", err) t.Fatalf("Set failed: %v", err)
} }
@ -478,11 +480,11 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
Predicate: storage.Everything, Predicate: storage.Everything,
Recursive: true, Recursive: true,
} }
if err := store.GetList(ctx, "/two-level", storageOpts, list); err != nil { if err := store.GetList(ctx, "/second", storageOpts, list); err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} }
continueRV, _ := strconv.Atoi(list.ResourceVersion) continueRV, _ := strconv.Atoi(list.ResourceVersion)
secondContinuation, err := storage.EncodeContinue("/two-level/2", "/two-level/", int64(continueRV)) secondContinuation, err := storage.EncodeContinue("/second/foo", "/second/", int64(continueRV))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -533,20 +535,20 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List on existing key", name: "test List on existing key",
prefix: "/one-level/", prefix: "/first/",
pred: storage.Everything, pred: storage.Everything,
expectedOut: []*example.Pod{preset[0]}, expectedOut: []*example.Pod{preset[0]},
}, },
{ {
name: "test List on existing key with resource version set to 0", name: "test List on existing key with resource version set to 0",
prefix: "/one-level/", prefix: "/first/",
pred: storage.Everything, pred: storage.Everything,
expectedOut: []*example.Pod{preset[0]}, expectedOut: []*example.Pod{preset[0]},
rv: "0", rv: "0",
}, },
{ {
name: "test List on existing key with resource version set before first write, match=Exact", name: "test List on existing key with resource version set before first write, match=Exact",
prefix: "/one-level/", prefix: "/first/",
pred: storage.Everything, pred: storage.Everything,
expectedOut: []*example.Pod{}, expectedOut: []*example.Pod{},
rv: initialRV, rv: initialRV,
@ -555,7 +557,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List on existing key with resource version set to 0, match=NotOlderThan", name: "test List on existing key with resource version set to 0, match=NotOlderThan",
prefix: "/one-level/", prefix: "/first/",
pred: storage.Everything, pred: storage.Everything,
expectedOut: []*example.Pod{preset[0]}, expectedOut: []*example.Pod{preset[0]},
rv: "0", rv: "0",
@ -563,7 +565,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List on existing key with resource version set to 0, match=Invalid", name: "test List on existing key with resource version set to 0, match=Invalid",
prefix: "/one-level/", prefix: "/first/",
pred: storage.Everything, pred: storage.Everything,
rv: "0", rv: "0",
rvMatch: "Invalid", rvMatch: "Invalid",
@ -571,7 +573,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List on existing key with resource version set before first write, match=NotOlderThan", name: "test List on existing key with resource version set before first write, match=NotOlderThan",
prefix: "/one-level/", prefix: "/first/",
pred: storage.Everything, pred: storage.Everything,
expectedOut: []*example.Pod{preset[0]}, expectedOut: []*example.Pod{preset[0]},
rv: initialRV, rv: initialRV,
@ -579,7 +581,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List on existing key with resource version set before first write, match=Invalid", name: "test List on existing key with resource version set before first write, match=Invalid",
prefix: "/one-level/", prefix: "/first/",
pred: storage.Everything, pred: storage.Everything,
rv: initialRV, rv: initialRV,
rvMatch: "Invalid", rvMatch: "Invalid",
@ -587,14 +589,14 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List on existing key with resource version set to current resource version", name: "test List on existing key with resource version set to current resource version",
prefix: "/one-level/", prefix: "/first/",
pred: storage.Everything, pred: storage.Everything,
expectedOut: []*example.Pod{preset[0]}, expectedOut: []*example.Pod{preset[0]},
rv: list.ResourceVersion, rv: list.ResourceVersion,
}, },
{ {
name: "test List on existing key with resource version set to current resource version, match=Exact", name: "test List on existing key with resource version set to current resource version, match=Exact",
prefix: "/one-level/", prefix: "/first/",
pred: storage.Everything, pred: storage.Everything,
expectedOut: []*example.Pod{preset[0]}, expectedOut: []*example.Pod{preset[0]},
rv: list.ResourceVersion, rv: list.ResourceVersion,
@ -603,7 +605,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List on existing key with resource version set to current resource version, match=NotOlderThan", name: "test List on existing key with resource version set to current resource version, match=NotOlderThan",
prefix: "/one-level/", prefix: "/first/",
pred: storage.Everything, pred: storage.Everything,
expectedOut: []*example.Pod{preset[0]}, expectedOut: []*example.Pod{preset[0]},
rv: list.ResourceVersion, rv: list.ResourceVersion,
@ -617,16 +619,16 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List with pod name matching", name: "test List with pod name matching",
prefix: "/one-level/", prefix: "/first/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Label: labels.Everything(), Label: labels.Everything(),
Field: fields.ParseSelectorOrDie("metadata.name!=foo"), Field: fields.ParseSelectorOrDie("metadata.name!=bar"),
}, },
expectedOut: nil, expectedOut: nil,
}, },
{ {
name: "test List with limit", name: "test List with limit",
prefix: "/two-level/", prefix: "/second/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Label: labels.Everything(), Label: labels.Everything(),
Field: fields.Everything(), Field: fields.Everything(),
@ -638,7 +640,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List with limit at current resource version", name: "test List with limit at current resource version",
prefix: "/two-level/", prefix: "/second/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Label: labels.Everything(), Label: labels.Everything(),
Field: fields.Everything(), Field: fields.Everything(),
@ -652,7 +654,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List with limit at current resource version and match=Exact", name: "test List with limit at current resource version and match=Exact",
prefix: "/two-level/", prefix: "/second/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Label: labels.Everything(), Label: labels.Everything(),
Field: fields.Everything(), Field: fields.Everything(),
@ -667,7 +669,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List with limit at resource version 0", name: "test List with limit at resource version 0",
prefix: "/two-level/", prefix: "/second/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Label: labels.Everything(), Label: labels.Everything(),
Field: fields.Everything(), Field: fields.Everything(),
@ -681,7 +683,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List with limit at resource version 0 match=NotOlderThan", name: "test List with limit at resource version 0 match=NotOlderThan",
prefix: "/two-level/", prefix: "/second/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Label: labels.Everything(), Label: labels.Everything(),
Field: fields.Everything(), Field: fields.Everything(),
@ -696,7 +698,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List with limit at resource version before first write and match=Exact", name: "test List with limit at resource version before first write and match=Exact",
prefix: "/two-level/", prefix: "/second/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Label: labels.Everything(), Label: labels.Everything(),
Field: fields.Everything(), Field: fields.Everything(),
@ -710,7 +712,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List with pregenerated continue token", name: "test List with pregenerated continue token",
prefix: "/two-level/", prefix: "/second/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Label: labels.Everything(), Label: labels.Everything(),
Field: fields.Everything(), Field: fields.Everything(),
@ -721,7 +723,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "ignores resource version 0 for List with pregenerated continue token", name: "ignores resource version 0 for List with pregenerated continue token",
prefix: "/two-level/", prefix: "/second/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Label: labels.Everything(), Label: labels.Everything(),
Field: fields.Everything(), Field: fields.Everything(),
@ -733,7 +735,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
}, },
{ {
name: "test List with multiple levels of directories and expect flattened result", name: "test List with multiple levels of directories and expect flattened result",
prefix: "/two-level/", prefix: "/second/",
pred: storage.Everything, pred: storage.Everything,
expectedOut: []*example.Pod{preset[1], preset[2]}, expectedOut: []*example.Pod{preset[1], preset[2]},
}, },
@ -741,7 +743,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
name: "test List with filter returning only one item, ensure only a single page returned", name: "test List with filter returning only one item, ensure only a single page returned",
prefix: "/", prefix: "/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Field: fields.OneTermEqualSelector("metadata.name", "fourth"), Field: fields.OneTermEqualSelector("metadata.name", "barfoo"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 1, Limit: 1,
}, },
@ -752,7 +754,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
name: "test List with filter returning only one item, covers the entire list", name: "test List with filter returning only one item, covers the entire list",
prefix: "/", prefix: "/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Field: fields.OneTermEqualSelector("metadata.name", "fourth"), Field: fields.OneTermEqualSelector("metadata.name", "barfoo"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 2, Limit: 2,
}, },
@ -763,7 +765,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
name: "test List with filter returning only one item, covers the entire list, with resource version 0", name: "test List with filter returning only one item, covers the entire list, with resource version 0",
prefix: "/", prefix: "/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Field: fields.OneTermEqualSelector("metadata.name", "fourth"), Field: fields.OneTermEqualSelector("metadata.name", "barfoo"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 2, Limit: 2,
}, },
@ -775,7 +777,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
name: "test List with filter returning two items, more pages possible", name: "test List with filter returning two items, more pages possible",
prefix: "/", prefix: "/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Field: fields.OneTermEqualSelector("metadata.name", "foo"), Field: fields.OneTermEqualSelector("metadata.name", "bar"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 2, Limit: 2,
}, },
@ -786,7 +788,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
name: "filter returns two items split across multiple pages", name: "filter returns two items split across multiple pages",
prefix: "/", prefix: "/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Field: fields.OneTermEqualSelector("metadata.name", "bar"), Field: fields.OneTermEqualSelector("metadata.name", "foo"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 2, Limit: 2,
}, },
@ -796,10 +798,10 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
name: "filter returns one item for last page, ends on last item, not full", name: "filter returns one item for last page, ends on last item, not full",
prefix: "/", prefix: "/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Field: fields.OneTermEqualSelector("metadata.name", "bar"), Field: fields.OneTermEqualSelector("metadata.name", "foo"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 2, Limit: 2,
Continue: encodeContinueOrDie("z-level/3", int64(continueRV)), Continue: encodeContinueOrDie("third/barfoo", int64(continueRV)),
}, },
expectedOut: []*example.Pod{preset[4]}, expectedOut: []*example.Pod{preset[4]},
}, },
@ -807,10 +809,10 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
name: "filter returns one item for last page, starts on last item, full", name: "filter returns one item for last page, starts on last item, full",
prefix: "/", prefix: "/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Field: fields.OneTermEqualSelector("metadata.name", "bar"), Field: fields.OneTermEqualSelector("metadata.name", "foo"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 1, Limit: 1,
Continue: encodeContinueOrDie("z-level/3/test-2", int64(continueRV)), Continue: encodeContinueOrDie("third/barfoo", int64(continueRV)),
}, },
expectedOut: []*example.Pod{preset[4]}, expectedOut: []*example.Pod{preset[4]},
}, },
@ -818,10 +820,10 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
name: "filter returns one item for last page, starts on last item, partial page", name: "filter returns one item for last page, starts on last item, partial page",
prefix: "/", prefix: "/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Field: fields.OneTermEqualSelector("metadata.name", "bar"), Field: fields.OneTermEqualSelector("metadata.name", "foo"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 2, Limit: 2,
Continue: encodeContinueOrDie("z-level/3/test-2", int64(continueRV)), Continue: encodeContinueOrDie("third/barfoo", int64(continueRV)),
}, },
expectedOut: []*example.Pod{preset[4]}, expectedOut: []*example.Pod{preset[4]},
}, },
@ -829,7 +831,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
name: "filter returns two items, page size equal to total list size", name: "filter returns two items, page size equal to total list size",
prefix: "/", prefix: "/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Field: fields.OneTermEqualSelector("metadata.name", "bar"), Field: fields.OneTermEqualSelector("metadata.name", "foo"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 5, Limit: 5,
}, },
@ -839,7 +841,7 @@ func RunTestList(ctx context.Context, t *testing.T, store storage.Interface) {
name: "filter returns one item, page size equal to total list size", name: "filter returns one item, page size equal to total list size",
prefix: "/", prefix: "/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Field: fields.OneTermEqualSelector("metadata.name", "fourth"), Field: fields.OneTermEqualSelector("metadata.name", "barfoo"),
Label: labels.Everything(), Label: labels.Everything(),
Limit: 5, Limit: 5,
}, },
@ -934,7 +936,7 @@ func RunTestListWithoutPaging(ctx context.Context, t *testing.T, store storage.I
{ {
name: "test List with limit when paging disabled", name: "test List with limit when paging disabled",
disablePaging: true, disablePaging: true,
prefix: "/two-level/", prefix: "/second/",
pred: storage.SelectionPredicate{ pred: storage.SelectionPredicate{
Label: labels.Everything(), Label: labels.Everything(),
Field: fields.Everything(), Field: fields.Everything(),
@ -986,46 +988,46 @@ func RunTestListWithoutPaging(ctx context.Context, t *testing.T, store storage.I
func seedMultiLevelData(ctx context.Context, store storage.Interface) (string, []*example.Pod, error) { func seedMultiLevelData(ctx context.Context, store storage.Interface) (string, []*example.Pod, error) {
// Setup storage with the following structure: // Setup storage with the following structure:
// / // /
// - one-level/ // - first/
// | - test // | - bar
// | // |
// - two-level/ // - second/
// | - 1/ // | - bar
// | | - test // | - foo
// | |
// | - 2/
// | - test
// | // |
// - z-level/ // - third/
// - 3/ // | - barfoo
// | - test // | - foo
// | barFirst := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "first", Name: "bar"}}
// - 3/ barSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "bar"}}
// - test-2 fooSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "foo"}}
barfooThird := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "third", Name: "barfoo"}}
fooThird := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "third", Name: "foo"}}
preset := []struct { preset := []struct {
key string key string
obj *example.Pod obj *example.Pod
storedObj *example.Pod storedObj *example.Pod
}{ }{
{ {
key: "/one-level/test", key: computePodKey(barFirst),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, obj: barFirst,
}, },
{ {
key: "/two-level/1/test", key: computePodKey(barSecond),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, obj: barSecond,
}, },
{ {
key: "/two-level/2/test", key: computePodKey(fooSecond),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}}, obj: fooSecond,
}, },
{ {
key: "/z-level/3/test", key: computePodKey(barfooThird),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "fourth"}}, obj: barfooThird,
}, },
{ {
key: "/z-level/3/test-2", key: computePodKey(fooThird),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}}, obj: fooThird,
}, },
} }
@ -1202,32 +1204,31 @@ type CallsValidation func(t *testing.T, pageSize, estimatedProcessedObjects uint
func RunTestListContinuation(ctx context.Context, t *testing.T, store storage.Interface, validation CallsValidation) { func RunTestListContinuation(ctx context.Context, t *testing.T, store storage.Interface, validation CallsValidation) {
// Setup storage with the following structure: // Setup storage with the following structure:
// / // /
// - one-level/ // - first/
// | - test // | - bar
// | // |
// - two-level/ // - second/
// - 1/ // | - bar
// | - test // | - foo
// | barFirst := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "first", Name: "bar"}}
// - 2/ barSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "bar"}}
// - test fooSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "foo"}}
//
preset := []struct { preset := []struct {
key string key string
obj *example.Pod obj *example.Pod
storedObj *example.Pod storedObj *example.Pod
}{ }{
{ {
key: "/one-level/test", key: computePodKey(barFirst),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, obj: barFirst,
}, },
{ {
key: "/two-level/1/test", key: computePodKey(barSecond),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, obj: barSecond,
}, },
{ {
key: "/two-level/2/test", key: computePodKey(fooSecond),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}}, obj: fooSecond,
}, },
} }
@ -1333,8 +1334,8 @@ func RunTestListPaginationRareObject(ctx context.Context, t *testing.T, store st
podCount := 1000 podCount := 1000
var pods []*example.Pod var pods []*example.Pod
for i := 0; i < podCount; i++ { for i := 0; i < podCount; i++ {
key := fmt.Sprintf("/one-level/pod-%d", i)
obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("pod-%d", i)}} obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("pod-%d", i)}}
key := computePodKey(obj)
storedObj := &example.Pod{} storedObj := &example.Pod{}
err := store.Create(ctx, key, obj, storedObj, 0) err := store.Create(ctx, key, obj, storedObj, 0)
if err != nil { if err != nil {
@ -1371,26 +1372,30 @@ func RunTestListPaginationRareObject(ctx context.Context, t *testing.T, store st
} }
func RunTestListContinuationWithFilter(ctx context.Context, t *testing.T, store storage.Interface, validation CallsValidation) { func RunTestListContinuationWithFilter(ctx context.Context, t *testing.T, store storage.Interface, validation CallsValidation) {
foo1 := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "1", Name: "foo"}}
bar2 := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "2", Name: "bar"}} // this should not match
foo3 := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "3", Name: "foo"}}
foo4 := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "4", Name: "foo"}}
preset := []struct { preset := []struct {
key string key string
obj *example.Pod obj *example.Pod
storedObj *example.Pod storedObj *example.Pod
}{ }{
{ {
key: "/1", key: computePodKey(foo1),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, obj: foo1,
}, },
{ {
key: "/2", key: computePodKey(bar2),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}}, // this should not match obj: bar2,
}, },
{ {
key: "/3", key: computePodKey(foo3),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, obj: foo3,
}, },
{ {
key: "/4", key: computePodKey(foo4),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, obj: foo4,
}, },
} }
@ -1472,35 +1477,33 @@ func RunTestListInconsistentContinuation(ctx context.Context, t *testing.T, stor
// Setup storage with the following structure: // Setup storage with the following structure:
// / // /
// - one-level/ // - first/
// | - test // | - bar
// | // |
// - two-level/ // - second/
// - 1/ // | - bar
// | - test // | - foo
// | barFirst := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "first", Name: "bar"}}
// - 2/ barSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "bar"}}
// - test fooSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "foo"}}
//
preset := []struct { preset := []struct {
key string key string
obj *example.Pod obj *example.Pod
storedObj *example.Pod storedObj *example.Pod
}{ }{
{ {
key: "/one-level/test", key: computePodKey(barFirst),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, obj: barFirst,
}, },
{ {
key: "/two-level/1/test", key: computePodKey(barSecond),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}, obj: barSecond,
}, },
{ {
key: "/two-level/2/test", key: computePodKey(fooSecond),
obj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}}, obj: fooSecond,
}, },
} }
for i, ps := range preset { for i, ps := range preset {
preset[i].storedObj = &example.Pod{} preset[i].storedObj = &example.Pod{}
err := store.Create(ctx, ps.key, ps.obj, preset[i].storedObj, 0) err := store.Create(ctx, ps.key, ps.obj, preset[i].storedObj, 0)
@ -1538,7 +1541,7 @@ func RunTestListInconsistentContinuation(ctx context.Context, t *testing.T, stor
continueFromSecondItem := out.Continue continueFromSecondItem := out.Continue
// update /two-level/2/test/bar // update /second/bar
oldName := preset[2].obj.Name oldName := preset[2].obj.Name
newPod := &example.Pod{ newPod := &example.Pod{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
@ -1628,16 +1631,15 @@ type InterfaceWithPrefixTransformer interface {
func RunTestConsistentList(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) { func RunTestConsistentList(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) {
nextPod := func(index uint32) (string, *example.Pod) { nextPod := func(index uint32) (string, *example.Pod) {
key := fmt.Sprintf("pod-%d", index)
obj := &example.Pod{ obj := &example.Pod{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: key, Name: fmt.Sprintf("pod-%d", index),
Labels: map[string]string{ Labels: map[string]string{
"even": strconv.FormatBool(index%2 == 0), "even": strconv.FormatBool(index%2 == 0),
}, },
}, },
} }
return key, obj return computePodKey(obj), obj
} }
transformer := &reproducingTransformer{ transformer := &reproducingTransformer{
@ -1715,7 +1717,8 @@ func RunTestConsistentList(ctx context.Context, t *testing.T, store InterfaceWit
} }
func RunTestGuaranteedUpdate(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer, validation KeyValidation) { func RunTestGuaranteedUpdate(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer, validation KeyValidation) {
key := "/foo" inputObj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}}
key := computePodKey(inputObj)
tests := []struct { tests := []struct {
name string name string
@ -1797,12 +1800,12 @@ func RunTestGuaranteedUpdate(ctx context.Context, t *testing.T, store InterfaceW
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 := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "A"}}) key, storeObj := testPropagateStore(ctx, t, store, inputObj)
out := &example.Pod{} out := &example.Pod{}
name := fmt.Sprintf("foo-%d", i) annotations := map[string]string{"version": fmt.Sprintf("%d", i)}
if tt.expectNoUpdate { if tt.expectNoUpdate {
name = storeObj.Name annotations = nil
} }
if tt.transformStale { if tt.transformStale {
@ -1826,7 +1829,7 @@ func RunTestGuaranteedUpdate(ctx context.Context, t *testing.T, store InterfaceW
if tt.hasSelfLink { if tt.hasSelfLink {
pod.SelfLink = "testlink" pod.SelfLink = "testlink"
} }
pod.Name = name pod.Annotations = annotations
return &pod, nil return &pod, nil
}), nil) }), nil)
@ -1845,8 +1848,8 @@ func RunTestGuaranteedUpdate(ctx context.Context, t *testing.T, store InterfaceW
if err != nil { if err != nil {
t.Fatalf("%s: GuaranteedUpdate failed: %v", tt.name, err) t.Fatalf("%s: GuaranteedUpdate failed: %v", tt.name, err)
} }
if out.ObjectMeta.Name != name { if !reflect.DeepEqual(out.ObjectMeta.Annotations, annotations) {
t.Errorf("%s: pod name want=%s, get=%s", tt.name, name, out.ObjectMeta.Name) t.Errorf("%s: pod annotations want=%s, get=%s", tt.name, annotations, out.ObjectMeta.Annotations)
} }
if out.SelfLink != "" { if out.SelfLink != "" {
t.Errorf("%s: selfLink should not be set", tt.name) t.Errorf("%s: selfLink should not be set", tt.name)
@ -1871,7 +1874,7 @@ func RunTestGuaranteedUpdate(ctx context.Context, t *testing.T, store InterfaceW
func RunTestGuaranteedUpdateWithTTL(ctx context.Context, t *testing.T, store storage.Interface) { func RunTestGuaranteedUpdateWithTTL(ctx context.Context, t *testing.T, store storage.Interface) {
input := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}} input := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
key := "/somekey" key := computePodKey(input)
out := &example.Pod{} out := &example.Pod{}
err := store.GuaranteedUpdate(ctx, key, out, true, nil, err := store.GuaranteedUpdate(ctx, key, out, true, nil,
@ -1892,7 +1895,7 @@ func RunTestGuaranteedUpdateWithTTL(ctx context.Context, t *testing.T, store sto
func RunTestGuaranteedUpdateChecksStoredData(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) { func RunTestGuaranteedUpdateChecksStoredData(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) {
input := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}} input := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}}
key := "/somekey" key := computePodKey(input)
// serialize input into etcd with data that would be normalized by a write - // serialize input into etcd with data that would be normalized by a write -
// in this case, leading whitespace // in this case, leading whitespace
@ -2076,22 +2079,25 @@ func RunTestGuaranteedUpdateWithSuggestionAndConflict(ctx context.Context, t *te
} }
func RunTestTransformationFailure(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) { func RunTestTransformationFailure(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) {
barFirst := &example.Pod{
ObjectMeta: metav1.ObjectMeta{Namespace: "first", Name: "bar"},
Spec: DeepEqualSafePodSpec(),
}
bazSecond := &example.Pod{
ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "baz"},
Spec: DeepEqualSafePodSpec(),
}
preset := []struct { preset := []struct {
key string key string
obj *example.Pod obj *example.Pod
storedObj *example.Pod storedObj *example.Pod
}{{ }{{
key: "/one-level/test", key: computePodKey(barFirst),
obj: &example.Pod{ obj: barFirst,
ObjectMeta: metav1.ObjectMeta{Name: "bar"},
Spec: DeepEqualSafePodSpec(),
},
}, { }, {
key: "/two-level/1/test", key: computePodKey(bazSecond),
obj: &example.Pod{ obj: bazSecond,
ObjectMeta: metav1.ObjectMeta{Name: "baz"},
Spec: DeepEqualSafePodSpec(),
},
}} }}
for i, ps := range preset[:1] { for i, ps := range preset[:1] {
preset[i].storedObj = &example.Pod{} preset[i].storedObj = &example.Pod{}

View File

@ -77,11 +77,15 @@ func DeepEqualSafePodSpec() example.PodSpec {
} }
} }
func computePodKey(obj *example.Pod) string {
return fmt.Sprintf("/%s/%s", obj.Namespace, obj.Name)
}
// testPropagateStore helps propagates store with objects, automates key generation, and returns // testPropagateStore helps propagates store with objects, automates key generation, and returns
// keys and stored objects. // keys and stored objects.
func testPropagateStore(ctx context.Context, t *testing.T, store storage.Interface, obj *example.Pod) (string, *example.Pod) { func testPropagateStore(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 := fmt.Sprintf("/%s/%s", obj.Namespace, obj.Name) key := computePodKey(obj)
// 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)