Debian Codegraph @

pkg/storage/testing/store_tests.go

raw
1 /*
2 Copyright 2016 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package testing
18
19 import (
20 "context"
21 "encoding/base64"
22 "errors"
23 "fmt"
24 "math"
25 "reflect"
26 "sort"
27 "strconv"
28 "strings"
29 "sync"
30 "testing"
31
32 "github.com/google/go-cmp/cmp" //nolint:depguard
33 "github.com/stretchr/testify/assert"
34 "github.com/stretchr/testify/require"
35 apierrors "k8s.io/apimachinery/pkg/api/errors"
36 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37 "k8s.io/apimachinery/pkg/fields"
38 "k8s.io/apimachinery/pkg/labels"
39 "k8s.io/apimachinery/pkg/runtime"
40 "k8s.io/apimachinery/pkg/watch"
41 "k8s.io/apiserver/pkg/apis/example"
42 "k8s.io/apiserver/pkg/storage"
43 "k8s.io/apiserver/pkg/storage/value"
44 utilpointer "k8s.io/utils/pointer"
45 )
46
47 type KeyValidation func(ctx context.Context, t *testing.T, key string)
48
49 func RunTestCreate(ctx context.Context, t *testing.T, store storage.Interface, validation KeyValidation) {
50 tests := []struct {
51 name string
52 inputObj *example.Pod
53 expectedError error
54 }{{
55 name: "successful create",
56 inputObj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns"}},
57 }, {
58 name: "create with ResourceVersion set",
59 inputObj: &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar", Namespace: "test-ns", ResourceVersion: "1"}},
60 expectedError: storage.ErrResourceVersionSetOnCreate,
61 }}
62
63 for _, tt := range tests {
64 t.Run(tt.name, func(t *testing.T) {
65 out := &example.Pod{} // reset
66 // verify that kv pair is empty before set
67 key := computePodKey(tt.inputObj)
68 if err := store.Get(ctx, key, storage.GetOptions{}, out); !storage.IsNotFound(err) {
69 t.Fatalf("expecting empty result on key %s, got %v", key, err)
70 }
71
72 err := store.Create(ctx, key, tt.inputObj, out, 0)
73 if !errors.Is(err, tt.expectedError) {
74 t.Errorf("expecting error %v, but get: %v", tt.expectedError, err)
75 }
76 if err != nil {
77 return
78 }
79 // basic tests of the output
80 if tt.inputObj.ObjectMeta.Name != out.ObjectMeta.Name {
81 t.Errorf("pod name want=%s, get=%s", tt.inputObj.ObjectMeta.Name, out.ObjectMeta.Name)
82 }
83 if out.ResourceVersion == "" {
84 t.Errorf("output should have non-empty resource version")
85 }
86 validation(ctx, t, key)
87 })
88 }
89 }
90
91 func RunTestCreateWithTTL(ctx context.Context, t *testing.T, store storage.Interface) {
92 input := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns"}}
93 out := &example.Pod{}
94
95 key := computePodKey(input)
96 if err := store.Create(ctx, key, input, out, 1); err != nil {
97 t.Fatalf("Create failed: %v", err)
98 }
99
100 w, err := store.Watch(ctx, key, storage.ListOptions{ResourceVersion: out.ResourceVersion, Predicate: storage.Everything})
101 if err != nil {
102 t.Fatalf("Watch failed: %v", err)
103 }
104 testCheckEventType(t, w, watch.Deleted)
105 }
106
107 func RunTestCreateWithKeyExist(ctx context.Context, t *testing.T, store storage.Interface) {
108 obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns"}}
109 key, _ := testPropagateStore(ctx, t, store, obj)
110 out := &example.Pod{}
111
112 err := store.Create(ctx, key, obj, out, 0)
113 if err == nil || !storage.IsExist(err) {
114 t.Errorf("expecting key exists error, but get: %s", err)
115 }
116 }
117
118 func RunTestGet(ctx context.Context, t *testing.T, store storage.Interface) {
119 // create an object to test
120 key, createdObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns"}})
121 // update the object once to allow get by exact resource version to be tested
122 updateObj := createdObj.DeepCopy()
123 updateObj.Annotations = map[string]string{"test-annotation": "1"}
124 storedObj := &example.Pod{}
125 err := store.GuaranteedUpdate(ctx, key, storedObj, true, nil,
126 func(_ runtime.Object, _ storage.ResponseMeta) (runtime.Object, *uint64, error) {
127 ttl := uint64(1)
128 return updateObj, &ttl, nil
129 }, nil)
130 if err != nil {
131 t.Fatalf("Update failed: %v", err)
132 }
133 // create an additional object to increment the resource version for pods above the resource version of the foo object
134 secondObj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar", Namespace: "test-ns"}}
135 lastUpdatedObj := &example.Pod{}
136 if err := store.Create(ctx, computePodKey(secondObj), secondObj, lastUpdatedObj, 0); err != nil {
137 t.Fatalf("Set failed: %v", err)
138 }
139
140 currentRV, _ := strconv.Atoi(storedObj.ResourceVersion)
141 lastUpdatedCurrentRV, _ := strconv.Atoi(lastUpdatedObj.ResourceVersion)
142
143 // TODO(jpbetz): Add exact test cases
144 tests := []struct {
145 name string
146 key string
147 ignoreNotFound bool
148 expectNotFoundErr bool
149 expectRVTooLarge bool
150 expectedOut *example.Pod
151 expectedAlternatives []*example.Pod
152 rv string
153 }{{
154 name: "get existing",
155 key: key,
156 ignoreNotFound: false,
157 expectNotFoundErr: false,
158 expectedOut: storedObj,
159 }, {
160 // For RV=0 arbitrarily old version is allowed, including from the moment
161 // when the object didn't yet exist.
162 // As a result, we allow it by setting ignoreNotFound and allowing an empty
163 // object in expectedOut.
164 name: "resource version 0",
165 key: key,
166 ignoreNotFound: true,
167 expectedAlternatives: []*example.Pod{{}, createdObj, storedObj},
168 rv: "0",
169 }, {
170 // Given that Get with set ResourceVersion is effectively always
171 // NotOlderThan semantic, both versions of object are allowed.
172 name: "object created resource version",
173 key: key,
174 expectedAlternatives: []*example.Pod{createdObj, storedObj},
175 rv: createdObj.ResourceVersion,
176 }, {
177 name: "current object resource version, match=NotOlderThan",
178 key: key,
179 expectedOut: storedObj,
180 rv: fmt.Sprintf("%d", currentRV),
181 }, {
182 name: "latest resource version",
183 key: key,
184 expectedOut: storedObj,
185 rv: fmt.Sprintf("%d", lastUpdatedCurrentRV),
186 }, {
187 name: "too high resource version",
188 key: key,
189 expectRVTooLarge: true,
190 rv: strconv.FormatInt(math.MaxInt64, 10),
191 }, {
192 name: "get non-existing",
193 key: "/non-existing",
194 ignoreNotFound: false,
195 expectNotFoundErr: true,
196 }, {
197 name: "get non-existing, ignore not found",
198 key: "/non-existing",
199 ignoreNotFound: true,
200 expectNotFoundErr: false,
201 expectedOut: &example.Pod{},
202 }}
203
204 for _, tt := range tests {
205 tt := tt
206 t.Run(tt.name, func(t *testing.T) {
207 // For some asynchronous implementations of storage interface (in particular watchcache),
208 // certain requests may impact result of further requests. As an example, if we first
209 // ensure that watchcache is synchronized up to ResourceVersion X (using Get/List requests
210 // with NotOlderThan semantic), the further requests (even specifying earlier resource
211 // version) will also return the result synchronized to at least ResourceVersion X.
212 // By parallelizing test cases we ensure that the order in which test cases are defined
213 // doesn't automatically preclude some scenarios from happening.
214 t.Parallel()
215
216 out := &example.Pod{}
217 err := store.Get(ctx, tt.key, storage.GetOptions{IgnoreNotFound: tt.ignoreNotFound, ResourceVersion: tt.rv}, out)
218 if tt.expectNotFoundErr {
219 if err == nil || !storage.IsNotFound(err) {
220 t.Errorf("expecting not found error, but get: %v", err)
221 }
222 return
223 }
224 if tt.expectRVTooLarge {
225 if err == nil || !storage.IsTooLargeResourceVersion(err) {
226 t.Errorf("expecting resource version too high error, but get: %v", err)
227 }
228 return
229 }
230 if err != nil {
231 t.Fatalf("Get failed: %v", err)
232 }
233
234 if tt.expectedAlternatives == nil {
235 expectNoDiff(t, fmt.Sprintf("%s: incorrect pod", tt.name), tt.expectedOut, out)
236 } else {
237 ExpectContains(t, fmt.Sprintf("%s: incorrect pod", tt.name), toInterfaceSlice(tt.expectedAlternatives), out)
238 }
239 })
240 }
241 }
242
243 func RunTestUnconditionalDelete(ctx context.Context, t *testing.T, store storage.Interface) {
244 key, storedObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns"}})
245
246 tests := []struct {
247 name string
248 key string
249 expectedObj *example.Pod
250 expectNotFoundErr bool
251 }{{
252 name: "existing key",
253 key: key,
254 expectedObj: storedObj,
255 expectNotFoundErr: false,
256 }, {
257 name: "non-existing key",
258 key: "/non-existing",
259 expectedObj: nil,
260 expectNotFoundErr: true,
261 }}
262
263 for _, tt := range tests {
264 t.Run(tt.name, func(t *testing.T) {
265 out := &example.Pod{} // reset
266 err := store.Delete(ctx, tt.key, out, nil, storage.ValidateAllObjectFunc, nil, storage.DeleteOptions{})
267 if tt.expectNotFoundErr {
268 if err == nil || !storage.IsNotFound(err) {
269 t.Errorf("expecting not found error, but get: %s", err)
270 }
271 return
272 }
273 if err != nil {
274 t.Fatalf("Delete failed: %v", err)
275 }
276 // We expect the resource version of the returned object to be
277 // updated compared to the last existing object.
278 if storedObj.ResourceVersion == out.ResourceVersion {
279 t.Errorf("expecting resource version to be updated, but get: %s", out.ResourceVersion)
280 }
281 out.ResourceVersion = storedObj.ResourceVersion
282 expectNoDiff(t, "incorrect pod:", tt.expectedObj, out)
283 })
284 }
285 }
286
287 func RunTestConditionalDelete(ctx context.Context, t *testing.T, store storage.Interface) {
288 obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns", UID: "A"}}
289 key, storedObj := testPropagateStore(ctx, t, store, obj)
290
291 tests := []struct {
292 name string
293 precondition *storage.Preconditions
294 expectInvalidObjErr bool
295 }{{
296 name: "UID match",
297 precondition: storage.NewUIDPreconditions("A"),
298 expectInvalidObjErr: false,
299 }, {
300 name: "UID mismatch",
301 precondition: storage.NewUIDPreconditions("B"),
302 expectInvalidObjErr: true,
303 }}
304
305 for _, tt := range tests {
306 t.Run(tt.name, func(t *testing.T) {
307 out := &example.Pod{}
308 err := store.Delete(ctx, key, out, tt.precondition, storage.ValidateAllObjectFunc, nil, storage.DeleteOptions{})
309 if tt.expectInvalidObjErr {
310 if err == nil || !storage.IsInvalidObj(err) {
311 t.Errorf("expecting invalid UID error, but get: %s", err)
312 }
313 return
314 }
315 if err != nil {
316 t.Fatalf("Delete failed: %v", err)
317 }
318 // We expect the resource version of the returned object to be
319 // updated compared to the last existing object.
320 if storedObj.ResourceVersion == out.ResourceVersion {
321 t.Errorf("expecting resource version to be updated, but get: %s", out.ResourceVersion)
322 }
323 out.ResourceVersion = storedObj.ResourceVersion
324 expectNoDiff(t, "incorrect pod:", storedObj, out)
325 obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns", UID: "A"}}
326 key, storedObj = testPropagateStore(ctx, t, store, obj)
327 })
328 }
329 }
330
331 // The following set of Delete tests are testing the logic of adding `suggestion`
332 // as a parameter with probably value of the current state.
333 // Introducing it for GuaranteedUpdate cause a number of issues, so we're addressing
334 // all of those upfront by adding appropriate tests:
335 // - https://github.com/kubernetes/kubernetes/pull/35415
336 // [DONE] Lack of tests originally - added TestDeleteWithSuggestion.
337 // - https://github.com/kubernetes/kubernetes/pull/40664
338 // [DONE] Irrelevant for delete, as Delete doesn't write data (nor compare it).
339 // - https://github.com/kubernetes/kubernetes/pull/47703
340 // [DONE] Irrelevant for delete, because Delete doesn't persist data.
341 // - https://github.com/kubernetes/kubernetes/pull/48394/
342 // [DONE] Irrelevant for delete, because Delete doesn't compare data.
343 // - https://github.com/kubernetes/kubernetes/pull/43152
344 // [DONE] Added TestDeleteWithSuggestionAndConflict
345 // - https://github.com/kubernetes/kubernetes/pull/54780
346 // [DONE] Irrelevant for delete, because Delete doesn't compare data.
347 // - https://github.com/kubernetes/kubernetes/pull/58375
348 // [DONE] Irrelevant for delete, because Delete doesn't compare data.
349 // - https://github.com/kubernetes/kubernetes/pull/77619
350 // [DONE] Added TestValidateDeletionWithSuggestion for corresponding delete checks.
351 // - https://github.com/kubernetes/kubernetes/pull/78713
352 // [DONE] Bug was in getState function which is shared with the new code.
353 // - https://github.com/kubernetes/kubernetes/pull/78713
354 // [DONE] Added TestPreconditionalDeleteWithSuggestion
355
356 func RunTestDeleteWithSuggestion(ctx context.Context, t *testing.T, store storage.Interface) {
357 key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name", Namespace: "test-ns"}})
358
359 out := &example.Pod{}
360 if err := store.Delete(ctx, key, out, nil, storage.ValidateAllObjectFunc, originalPod, storage.DeleteOptions{}); err != nil {
361 t.Errorf("Unexpected failure during deletion: %v", err)
362 }
363
364 if err := store.Get(ctx, key, storage.GetOptions{}, &example.Pod{}); !storage.IsNotFound(err) {
365 t.Errorf("Unexpected error on reading object: %v", err)
366 }
367 }
368
369 func RunTestDeleteWithSuggestionAndConflict(ctx context.Context, t *testing.T, store storage.Interface) {
370 key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name", Namespace: "test-ns"}})
371
372 // First update, so originalPod is outdated.
373 updatedPod := &example.Pod{}
374 if err := store.GuaranteedUpdate(ctx, key, updatedPod, false, nil,
375 storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
376 pod := obj.(*example.Pod)
377 pod.ObjectMeta.Labels = map[string]string{"foo": "bar"}
378 return pod, nil
379 }), nil); err != nil {
380 t.Errorf("Unexpected failure during updated: %v", err)
381 }
382
383 out := &example.Pod{}
384 if err := store.Delete(ctx, key, out, nil, storage.ValidateAllObjectFunc, originalPod, storage.DeleteOptions{}); err != nil {
385 t.Errorf("Unexpected failure during deletion: %v", err)
386 }
387
388 if err := store.Get(ctx, key, storage.GetOptions{}, &example.Pod{}); !storage.IsNotFound(err) {
389 t.Errorf("Unexpected error on reading object: %v", err)
390 }
391 updatedPod.ObjectMeta.ResourceVersion = out.ObjectMeta.ResourceVersion
392 expectNoDiff(t, "incorrect pod:", updatedPod, out)
393 }
394
395 // RunTestDeleteWithConflict tests the case when another conflicting update happened before the delete completed.
396 func RunTestDeleteWithConflict(ctx context.Context, t *testing.T, store storage.Interface) {
397 key, _ := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name", Namespace: "test-ns"}})
398
399 // First update, so originalPod is outdated.
400 updatedPod := &example.Pod{}
401 validateCount := 0
402 updateCount := 0
403 // Simulate a conflicting update in the middle of delete.
404 validateAllWithUpdate := func(_ context.Context, _ runtime.Object) error {
405 validateCount++
406 if validateCount > 1 {
407 return nil
408 }
409 if err := store.GuaranteedUpdate(ctx, key, updatedPod, false, nil,
410 storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
411 pod := obj.(*example.Pod)
412 pod.ObjectMeta.Labels = map[string]string{"foo": "bar"}
413 return pod, nil
414 }), nil); err != nil {
415 t.Errorf("Unexpected failure during updated: %v", err)
416 }
417 updateCount++
418 return nil
419 }
420
421 out := &example.Pod{}
422 if err := store.Delete(ctx, key, out, nil, validateAllWithUpdate, nil, storage.DeleteOptions{}); err != nil {
423 t.Errorf("Unexpected failure during deletion: %v", err)
424 }
425
426 if validateCount != 2 {
427 t.Errorf("Expect validateCount = %d, but got %d", 2, validateCount)
428 }
429 if updateCount != 1 {
430 t.Errorf("Expect updateCount = %d, but got %d", 1, updateCount)
431 }
432
433 if err := store.Get(ctx, key, storage.GetOptions{}, &example.Pod{}); !storage.IsNotFound(err) {
434 t.Errorf("Unexpected error on reading object: %v", err)
435 }
436 updatedPod.ObjectMeta.ResourceVersion = out.ObjectMeta.ResourceVersion
437 expectNoDiff(t, "incorrect pod:", updatedPod, out)
438 }
439
440 func RunTestDeleteWithSuggestionOfDeletedObject(ctx context.Context, t *testing.T, store storage.Interface) {
441 key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name", Namespace: "test-ns"}})
442
443 // First delete, so originalPod is outdated.
444 deletedPod := &example.Pod{}
445 if err := store.Delete(ctx, key, deletedPod, nil, storage.ValidateAllObjectFunc, originalPod, storage.DeleteOptions{}); err != nil {
446 t.Errorf("Unexpected failure during deletion: %v", err)
447 }
448
449 // Now try deleting with stale object.
450 out := &example.Pod{}
451 if err := store.Delete(ctx, key, out, nil, storage.ValidateAllObjectFunc, originalPod, storage.DeleteOptions{}); !storage.IsNotFound(err) {
452 t.Errorf("Unexpected error during deletion: %v, expected not-found", err)
453 }
454 }
455
456 func RunTestValidateDeletionWithSuggestion(ctx context.Context, t *testing.T, store storage.Interface) {
457 key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name", Namespace: "test-ns"}})
458
459 // Check that validaing fresh object fails is called once and fails.
460 validationCalls := 0
461 validationError := fmt.Errorf("validation error")
462 validateNothing := func(_ context.Context, _ runtime.Object) error {
463 validationCalls++
464 return validationError
465 }
466 out := &example.Pod{}
467 // nolint:errorlint // not changing the level of assertion
468 if err := store.Delete(ctx, key, out, nil, validateNothing, originalPod, storage.DeleteOptions{}); err != validationError {
469 t.Errorf("Unexpected failure during deletion: %v", err)
470 }
471 if validationCalls != 1 {
472 t.Errorf("validate function should have been called once, called %d", validationCalls)
473 }
474
475 // First update, so originalPod is outdated.
476 updatedPod := &example.Pod{}
477 if err := store.GuaranteedUpdate(ctx, key, updatedPod, false, nil,
478 storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
479 pod := obj.(*example.Pod)
480 pod.ObjectMeta.Labels = map[string]string{"foo": "bar"}
481 return pod, nil
482 }), nil); err != nil {
483 t.Errorf("Unexpected failure during updated: %v", err)
484 }
485
486 calls := 0
487 validateFresh := func(_ context.Context, obj runtime.Object) error {
488 calls++
489 pod := obj.(*example.Pod)
490 if pod.ObjectMeta.Labels == nil || pod.ObjectMeta.Labels["foo"] != "bar" {
491 return fmt.Errorf("stale object")
492 }
493 return nil
494 }
495
496 if err := store.Delete(ctx, key, out, nil, validateFresh, originalPod, storage.DeleteOptions{}); err != nil {
497 t.Errorf("Unexpected failure during deletion: %v", err)
498 }
499
500 // Implementations of the storage interface are allowed to ignore the suggestion,
501 // in which case just one validation call is possible.
502 if calls > 2 {
503 t.Errorf("validate function should have been called at most twice, called %d", calls)
504 }
505
506 if err := store.Get(ctx, key, storage.GetOptions{}, &example.Pod{}); !storage.IsNotFound(err) {
507 t.Errorf("Unexpected error on reading object: %v", err)
508 }
509 }
510
511 // RunTestValidateDeletionWithOnlySuggestionValid tests the case of delete with validateDeletion function,
512 // when the suggested cachedExistingObject passes the validate function while the current version does not pass the validate function.
513 func RunTestValidateDeletionWithOnlySuggestionValid(ctx context.Context, t *testing.T, store storage.Interface) {
514 key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name", Namespace: "test-ns", Labels: map[string]string{"foo": "bar"}}})
515
516 // Check that validaing fresh object fails is called once and fails.
517 validationCalls := 0
518 validationError := fmt.Errorf("validation error")
519 validateNothing := func(_ context.Context, _ runtime.Object) error {
520 validationCalls++
521 return validationError
522 }
523 out := &example.Pod{}
524 // nolint:errorlint // not changing the level of assertion
525 if err := store.Delete(ctx, key, out, nil, validateNothing, originalPod, storage.DeleteOptions{}); err != validationError {
526 t.Errorf("Unexpected failure during deletion: %v", err)
527 }
528 if validationCalls != 1 {
529 t.Errorf("validate function should have been called once, called %d", validationCalls)
530 }
531
532 // First update, so originalPod is outdated.
533 updatedPod := &example.Pod{}
534 if err := store.GuaranteedUpdate(ctx, key, updatedPod, false, nil,
535 storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
536 pod := obj.(*example.Pod)
537 pod.ObjectMeta.Labels = map[string]string{"foo": "barbar"}
538 return pod, nil
539 }), nil); err != nil {
540 t.Errorf("Unexpected failure during updated: %v", err)
541 }
542
543 calls := 0
544 validateFresh := func(_ context.Context, obj runtime.Object) error {
545 calls++
546 pod := obj.(*example.Pod)
547 if pod.ObjectMeta.Labels == nil || pod.ObjectMeta.Labels["foo"] != "bar" {
548 return fmt.Errorf("stale object")
549 }
550 return nil
551 }
552
553 err := store.Delete(ctx, key, out, nil, validateFresh, originalPod, storage.DeleteOptions{})
554 if err == nil || err.Error() != "stale object" {
555 t.Errorf("expecting stale object error, but get: %s", err)
556 }
557
558 // Implementations of the storage interface are allowed to ignore the suggestion,
559 // in which case just one validation call is possible.
560 if calls > 2 {
561 t.Errorf("validate function should have been called at most twice, called %d", calls)
562 }
563
564 if err = store.Get(ctx, key, storage.GetOptions{}, out); err != nil {
565 t.Errorf("Unexpected error on reading object: %v", err)
566 }
567 expectNoDiff(t, "incorrect pod:", updatedPod, out)
568 }
569
570 func RunTestPreconditionalDeleteWithSuggestion(ctx context.Context, t *testing.T, store storage.Interface) {
571 key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name", Namespace: "test-ns"}})
572
573 // First update, so originalPod is outdated.
574 updatedPod := &example.Pod{}
575 if err := store.GuaranteedUpdate(ctx, key, updatedPod, false, nil,
576 storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
577 pod := obj.(*example.Pod)
578 pod.ObjectMeta.UID = "myUID"
579 return pod, nil
580 }), nil); err != nil {
581 t.Errorf("Unexpected failure during updated: %v", err)
582 }
583
584 prec := storage.NewUIDPreconditions("myUID")
585
586 out := &example.Pod{}
587 if err := store.Delete(ctx, key, out, prec, storage.ValidateAllObjectFunc, originalPod, storage.DeleteOptions{}); err != nil {
588 t.Errorf("Unexpected failure during deletion: %v", err)
589 }
590
591 if err := store.Get(ctx, key, storage.GetOptions{}, &example.Pod{}); !storage.IsNotFound(err) {
592 t.Errorf("Unexpected error on reading object: %v", err)
593 }
594 }
595
596 // RunTestPreconditionalDeleteWithOnlySuggestionPass tests the case of delete with preconditions,
597 // when the suggested cachedExistingObject passes the preconditions while the current version does not pass the preconditions.
598 func RunTestPreconditionalDeleteWithOnlySuggestionPass(ctx context.Context, t *testing.T, store storage.Interface) {
599 key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "name", Namespace: "test-ns", UID: "myUID"}})
600
601 // First update, so originalPod is outdated.
602 updatedPod := &example.Pod{}
603 if err := store.GuaranteedUpdate(ctx, key, updatedPod, false, nil,
604 storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
605 pod := obj.(*example.Pod)
606 pod.ObjectMeta.UID = "otherUID"
607 return pod, nil
608 }), nil); err != nil {
609 t.Errorf("Unexpected failure during updated: %v", err)
610 }
611
612 prec := storage.NewUIDPreconditions("myUID")
613 // Although originalPod passes the precondition, its delete would fail due to conflict.
614 // The 2nd try with updatedPod would fail the precondition.
615 out := &example.Pod{}
616 err := store.Delete(ctx, key, out, prec, storage.ValidateAllObjectFunc, originalPod, storage.DeleteOptions{})
617 if err == nil || !storage.IsInvalidObj(err) {
618 t.Errorf("expecting invalid UID error, but get: %s", err)
619 }
620
621 if err = store.Get(ctx, key, storage.GetOptions{}, out); err != nil {
622 t.Errorf("Unexpected error on reading object: %v", err)
623 }
624 expectNoDiff(t, "incorrect pod:", updatedPod, out)
625 }
626
627 func RunTestList(ctx context.Context, t *testing.T, store storage.Interface, increaseRV IncreaseRVFunc, ignoreWatchCacheTests bool) {
628 initialRV, createdPods, updatedPod, err := seedMultiLevelData(ctx, store)
629 if err != nil {
630 t.Fatal(err)
631 }
632
633 list := &example.PodList{}
634 storageOpts := storage.ListOptions{
635 // Ensure we're listing from "now".
636 ResourceVersion: "",
637 Predicate: storage.Everything,
638 Recursive: true,
639 }
640 if err := store.GetList(ctx, "/second", storageOpts, list); err != nil {
641 t.Errorf("Unexpected error: %v", err)
642 }
643 continueRV, _ := strconv.Atoi(list.ResourceVersion)
644 secondContinuation, err := storage.EncodeContinue("/second/foo", "/second/", int64(continueRV))
645 if err != nil {
646 t.Fatal(err)
647 }
648
649 getAttrs := func(obj runtime.Object) (labels.Set, fields.Set, error) {
650 pod := obj.(*example.Pod)
651 return nil, fields.Set{"metadata.name": pod.Name, "spec.nodeName": pod.Spec.NodeName}, nil
652 }
653 // Increase RV to test consistent List.
654 increaseRV(ctx, t)
655 currentRV := fmt.Sprintf("%d", continueRV+1)
656
657 tests := []struct {
658 name string
659 rv string
660 rvMatch metav1.ResourceVersionMatch
661 prefix string
662 pred storage.SelectionPredicate
663 ignoreForWatchCache bool
664 expectedOut []example.Pod
665 expectedAlternatives [][]example.Pod
666 expectContinue bool
667 expectContinueExact string
668 expectedRemainingItemCount *int64
669 expectError bool
670 expectRVTooLarge bool
671 expectRV string
672 expectRVFunc func(string) error
673 }{
674 {
675 name: "rejects invalid resource version",
676 prefix: "/pods",
677 pred: storage.Everything,
678 rv: "abc",
679 expectError: true,
680 },
681 {
682 name: "rejects resource version and continue token",
683 prefix: "/pods",
684 pred: storage.SelectionPredicate{
685 Label: labels.Everything(),
686 Field: fields.Everything(),
687 Limit: 1,
688 Continue: secondContinuation,
689 },
690 rv: "1",
691 expectError: true,
692 },
693 {
694 name: "rejects resource version set too high",
695 prefix: "/pods",
696 rv: strconv.FormatInt(math.MaxInt64, 10),
697 expectRVTooLarge: true,
698 },
699 {
700 name: "test List on existing key",
701 prefix: "/pods/first/",
702 pred: storage.Everything,
703 expectedOut: []example.Pod{*updatedPod},
704 },
705 {
706 name: "test List on existing key with resource version set to 0",
707 prefix: "/pods/first/",
708 pred: storage.Everything,
709 expectedAlternatives: [][]example.Pod{{}, {*updatedPod}},
710 rv: "0",
711 },
712 {
713 name: "test List on existing key with resource version set before first write, match=Exact",
714 prefix: "/pods/first/",
715 pred: storage.Everything,
716 expectedOut: []example.Pod{},
717 rv: initialRV,
718 rvMatch: metav1.ResourceVersionMatchExact,
719 expectRV: initialRV,
720 },
721 {
722 name: "test List on existing key with resource version set to 0, match=NotOlderThan",
723 prefix: "/pods/first/",
724 pred: storage.Everything,
725 expectedAlternatives: [][]example.Pod{{}, {*updatedPod}},
726 rv: "0",
727 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
728 },
729 {
730 name: "test List on existing key with resource version set to 0, match=Invalid",
731 prefix: "/pods/first/",
732 pred: storage.Everything,
733 rv: "0",
734 rvMatch: "Invalid",
735 expectError: true,
736 },
737 {
738 name: "test List on existing key with resource version set before first write, match=NotOlderThan",
739 prefix: "/pods/first/",
740 pred: storage.Everything,
741 expectedAlternatives: [][]example.Pod{{}, {*updatedPod}},
742 rv: initialRV,
743 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
744 },
745 {
746 name: "test List on existing key with resource version set before first write, match=Invalid",
747 prefix: "/pods/first/",
748 pred: storage.Everything,
749 rv: initialRV,
750 rvMatch: "Invalid",
751 expectError: true,
752 },
753 {
754 name: "test List on existing key with resource version set to current resource version",
755 prefix: "/pods/first/",
756 pred: storage.Everything,
757 expectedOut: []example.Pod{*updatedPod},
758 rv: list.ResourceVersion,
759 },
760 {
761 name: "test List on existing key with resource version set to current resource version, match=Exact",
762 prefix: "/pods/first/",
763 pred: storage.Everything,
764 expectedOut: []example.Pod{*updatedPod},
765 rv: list.ResourceVersion,
766 rvMatch: metav1.ResourceVersionMatchExact,
767 expectRV: list.ResourceVersion,
768 },
769 {
770 name: "test List on existing key with resource version set to current resource version, match=NotOlderThan",
771 prefix: "/pods/first/",
772 pred: storage.Everything,
773 expectedOut: []example.Pod{*updatedPod},
774 rv: list.ResourceVersion,
775 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
776 },
777 {
778 name: "test List on non-existing key",
779 prefix: "/pods/non-existing/",
780 pred: storage.Everything,
781 expectedOut: []example.Pod{},
782 },
783 {
784 name: "test List with pod name matching",
785 prefix: "/pods/first/",
786 pred: storage.SelectionPredicate{
787 Label: labels.Everything(),
788 Field: fields.ParseSelectorOrDie("metadata.name!=bar"),
789 },
790 expectedOut: []example.Pod{},
791 },
792 {
793 name: "test List with pod name matching with resource version set to current resource version, match=NotOlderThan",
794 prefix: "/pods/first/",
795 pred: storage.SelectionPredicate{
796 Label: labels.Everything(),
797 Field: fields.ParseSelectorOrDie("metadata.name!=bar"),
798 },
799 expectedOut: []example.Pod{},
800 rv: list.ResourceVersion,
801 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
802 },
803 {
804 name: "test List with limit",
805 prefix: "/pods/second/",
806 pred: storage.SelectionPredicate{
807 Label: labels.Everything(),
808 Field: fields.Everything(),
809 Limit: 1,
810 },
811 expectedOut: []example.Pod{*createdPods[1]},
812 expectRV: currentRV,
813 expectContinue: true,
814 expectContinueExact: encodeContinueOrDie(createdPods[1].Name+"\x00", int64(mustAtoi(currentRV))),
815 expectedRemainingItemCount: utilpointer.Int64(1),
816 },
817 {
818 name: "test List with limit at current resource version",
819 prefix: "/pods/second/",
820 pred: storage.SelectionPredicate{
821 Label: labels.Everything(),
822 Field: fields.Everything(),
823 Limit: 1,
824 },
825 expectedOut: []example.Pod{*createdPods[1]},
826 expectContinue: true,
827 expectContinueExact: encodeContinueOrDie(createdPods[1].Name+"\x00", int64(mustAtoi(list.ResourceVersion))),
828 expectedRemainingItemCount: utilpointer.Int64(1),
829 rv: list.ResourceVersion,
830 expectRV: list.ResourceVersion,
831 },
832 {
833 name: "test List with limit at current resource version and match=Exact",
834 prefix: "/pods/second/",
835 pred: storage.SelectionPredicate{
836 Label: labels.Everything(),
837 Field: fields.Everything(),
838 Limit: 1,
839 },
840 expectedOut: []example.Pod{*createdPods[1]},
841 expectContinue: true,
842 expectContinueExact: encodeContinueOrDie(createdPods[1].Name+"\x00", int64(mustAtoi(list.ResourceVersion))),
843 expectedRemainingItemCount: utilpointer.Int64(1),
844 rv: list.ResourceVersion,
845 rvMatch: metav1.ResourceVersionMatchExact,
846 expectRV: list.ResourceVersion,
847 },
848 {
849 name: "test List with limit at current resource version and match=NotOlderThan",
850 prefix: "/pods/second/",
851 pred: storage.SelectionPredicate{
852 Label: labels.Everything(),
853 Field: fields.Everything(),
854 Limit: 1,
855 },
856 expectedOut: []example.Pod{*createdPods[1]},
857 expectContinue: true,
858 expectedRemainingItemCount: utilpointer.Int64(1),
859 rv: list.ResourceVersion,
860 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
861 expectRVFunc: resourceVersionNotOlderThan(list.ResourceVersion),
862 },
863 {
864 name: "test List with limit at resource version 0",
865 prefix: "/pods/second/",
866 pred: storage.SelectionPredicate{
867 Label: labels.Everything(),
868 Field: fields.Everything(),
869 Limit: 1,
870 },
871 // TODO(#108003): As of now, watchcache is deliberately ignoring
872 // limit if RV=0 is specified, returning whole list of objects.
873 // While this should eventually get fixed, for now we're explicitly
874 // ignoring this testcase for watchcache.
875 ignoreForWatchCache: true,
876 expectedOut: []example.Pod{*createdPods[1]},
877 expectContinue: true,
878 expectedRemainingItemCount: utilpointer.Int64(1),
879 rv: "0",
880 expectRVFunc: resourceVersionNotOlderThan(list.ResourceVersion),
881 },
882 {
883 name: "test List with limit at resource version 0 match=NotOlderThan",
884 prefix: "/pods/second/",
885 pred: storage.SelectionPredicate{
886 Label: labels.Everything(),
887 Field: fields.Everything(),
888 Limit: 1,
889 },
890 // TODO(#108003): As of now, watchcache is deliberately ignoring
891 // limit if RV=0 is specified, returning whole list of objects.
892 // While this should eventually get fixed, for now we're explicitly
893 // ignoring this testcase for watchcache.
894 ignoreForWatchCache: true,
895 expectedOut: []example.Pod{*createdPods[1]},
896 expectContinue: true,
897 expectedRemainingItemCount: utilpointer.Int64(1),
898 rv: "0",
899 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
900 expectRVFunc: resourceVersionNotOlderThan(list.ResourceVersion),
901 },
902 {
903 name: "test List with limit at resource version before first write and match=Exact",
904 prefix: "/pods/second/",
905 pred: storage.SelectionPredicate{
906 Label: labels.Everything(),
907 Field: fields.Everything(),
908 Limit: 1,
909 },
910 expectedOut: []example.Pod{},
911 expectContinue: false,
912 rv: initialRV,
913 rvMatch: metav1.ResourceVersionMatchExact,
914 expectRV: initialRV,
915 },
916 {
917 name: "test List with pregenerated continue token",
918 prefix: "/pods/second/",
919 pred: storage.SelectionPredicate{
920 Label: labels.Everything(),
921 Field: fields.Everything(),
922 Limit: 1,
923 Continue: secondContinuation,
924 },
925 expectedOut: []example.Pod{*createdPods[2]},
926 },
927 {
928 name: "ignores resource version 0 for List with pregenerated continue token",
929 prefix: "/pods/second/",
930 pred: storage.SelectionPredicate{
931 Label: labels.Everything(),
932 Field: fields.Everything(),
933 Limit: 1,
934 Continue: secondContinuation,
935 },
936 rv: "0",
937 expectedOut: []example.Pod{*createdPods[2]},
938 },
939 {
940 name: "test List with multiple levels of directories and expect flattened result",
941 prefix: "/pods/second/",
942 pred: storage.Everything,
943 expectedOut: []example.Pod{*createdPods[1], *createdPods[2]},
944 },
945 {
946 name: "test List with multiple levels of directories and expect flattened result with current resource version and match=NotOlderThan",
947 prefix: "/pods/second/",
948 pred: storage.Everything,
949 rv: list.ResourceVersion,
950 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
951 expectedOut: []example.Pod{*createdPods[1], *createdPods[2]},
952 },
953 {
954 name: "test List with filter returning only one item, ensure only a single page returned",
955 prefix: "/pods",
956 pred: storage.SelectionPredicate{
957 Field: fields.OneTermEqualSelector("metadata.name", "barfoo"),
958 Label: labels.Everything(),
959 Limit: 1,
960 },
961 expectedOut: []example.Pod{*createdPods[3]},
962 expectContinue: true,
963 },
964 {
965 name: "test List with filter returning only one item, ensure only a single page returned with current resource version and match=NotOlderThan",
966 prefix: "/pods",
967 pred: storage.SelectionPredicate{
968 Field: fields.OneTermEqualSelector("metadata.name", "barfoo"),
969 Label: labels.Everything(),
970 Limit: 1,
971 },
972 rv: list.ResourceVersion,
973 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
974 expectedOut: []example.Pod{*createdPods[3]},
975 expectRVFunc: resourceVersionNotOlderThan(list.ResourceVersion),
976 expectContinue: true,
977 },
978 {
979 name: "test List with filter returning only one item, covers the entire list",
980 prefix: "/pods",
981 pred: storage.SelectionPredicate{
982 Field: fields.OneTermEqualSelector("metadata.name", "barfoo"),
983 Label: labels.Everything(),
984 Limit: 2,
985 },
986 expectedOut: []example.Pod{*createdPods[3]},
987 expectContinue: false,
988 },
989 {
990 name: "test List with filter returning only one item, covers the entire list with current resource version and match=NotOlderThan",
991 prefix: "/pods",
992 pred: storage.SelectionPredicate{
993 Field: fields.OneTermEqualSelector("metadata.name", "barfoo"),
994 Label: labels.Everything(),
995 Limit: 2,
996 },
997 rv: list.ResourceVersion,
998 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
999 expectedOut: []example.Pod{*createdPods[3]},
1000 expectRVFunc: resourceVersionNotOlderThan(list.ResourceVersion),
1001 expectContinue: false,
1002 },
1003 {
1004 name: "test List with filter returning only one item, covers the entire list, with resource version 0",
1005 prefix: "/pods",
1006 pred: storage.SelectionPredicate{
1007 Field: fields.OneTermEqualSelector("metadata.name", "barfoo"),
1008 Label: labels.Everything(),
1009 Limit: 2,
1010 },
1011 rv: "0",
1012 expectedAlternatives: [][]example.Pod{{}, {*createdPods[3]}},
1013 expectContinue: false,
1014 },
1015 {
1016 name: "test List with filter returning two items, more pages possible",
1017 prefix: "/pods",
1018 pred: storage.SelectionPredicate{
1019 Field: fields.OneTermEqualSelector("metadata.name", "bar"),
1020 Label: labels.Everything(),
1021 Limit: 2,
1022 },
1023 expectContinue: true,
1024 expectedOut: []example.Pod{*updatedPod, *createdPods[1]},
1025 },
1026 {
1027 name: "test List with filter returning two items, more pages possible with current resource version and match=NotOlderThan",
1028 prefix: "/pods",
1029 pred: storage.SelectionPredicate{
1030 Field: fields.OneTermEqualSelector("metadata.name", "bar"),
1031 Label: labels.Everything(),
1032 Limit: 2,
1033 },
1034 rv: list.ResourceVersion,
1035 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
1036 expectContinue: true,
1037 expectedOut: []example.Pod{*updatedPod, *createdPods[1]},
1038 },
1039 {
1040 name: "filter returns two items split across multiple pages",
1041 prefix: "/pods",
1042 pred: storage.SelectionPredicate{
1043 Field: fields.OneTermEqualSelector("metadata.name", "foo"),
1044 Label: labels.Everything(),
1045 Limit: 2,
1046 },
1047 expectedOut: []example.Pod{*createdPods[2], *createdPods[4]},
1048 },
1049 {
1050 name: "filter returns two items split across multiple pages with current resource version and match=NotOlderThan",
1051 prefix: "/pods",
1052 pred: storage.SelectionPredicate{
1053 Field: fields.OneTermEqualSelector("metadata.name", "foo"),
1054 Label: labels.Everything(),
1055 Limit: 2,
1056 },
1057 rv: list.ResourceVersion,
1058 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
1059 expectedOut: []example.Pod{*createdPods[2], *createdPods[4]},
1060 },
1061 {
1062 name: "filter returns one item for last page, ends on last item, not full",
1063 prefix: "/pods",
1064 pred: storage.SelectionPredicate{
1065 Field: fields.OneTermEqualSelector("metadata.name", "foo"),
1066 Label: labels.Everything(),
1067 Limit: 2,
1068 Continue: encodeContinueOrDie("third/barfoo", int64(continueRV)),
1069 },
1070 expectedOut: []example.Pod{*createdPods[4]},
1071 },
1072 {
1073 name: "filter returns one item for last page, starts on last item, full",
1074 prefix: "/pods",
1075 pred: storage.SelectionPredicate{
1076 Field: fields.OneTermEqualSelector("metadata.name", "foo"),
1077 Label: labels.Everything(),
1078 Limit: 1,
1079 Continue: encodeContinueOrDie("third/barfoo", int64(continueRV)),
1080 },
1081 expectedOut: []example.Pod{*createdPods[4]},
1082 },
1083 {
1084 name: "filter returns one item for last page, starts on last item, partial page",
1085 prefix: "/pods",
1086 pred: storage.SelectionPredicate{
1087 Field: fields.OneTermEqualSelector("metadata.name", "foo"),
1088 Label: labels.Everything(),
1089 Limit: 2,
1090 Continue: encodeContinueOrDie("third/barfoo", int64(continueRV)),
1091 },
1092 expectedOut: []example.Pod{*createdPods[4]},
1093 },
1094 {
1095 name: "filter returns two items, page size equal to total list size",
1096 prefix: "/pods",
1097 pred: storage.SelectionPredicate{
1098 Field: fields.OneTermEqualSelector("metadata.name", "foo"),
1099 Label: labels.Everything(),
1100 Limit: 5,
1101 },
1102 expectedOut: []example.Pod{*createdPods[2], *createdPods[4]},
1103 },
1104 {
1105 name: "filter returns two items, page size equal to total list size with current resource version and match=NotOlderThan",
1106 prefix: "/pods",
1107 pred: storage.SelectionPredicate{
1108 Field: fields.OneTermEqualSelector("metadata.name", "foo"),
1109 Label: labels.Everything(),
1110 Limit: 5,
1111 },
1112 rv: list.ResourceVersion,
1113 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
1114 expectedOut: []example.Pod{*createdPods[2], *createdPods[4]},
1115 },
1116 {
1117 name: "filter returns one item, page size equal to total list size",
1118 prefix: "/pods",
1119 pred: storage.SelectionPredicate{
1120 Field: fields.OneTermEqualSelector("metadata.name", "barfoo"),
1121 Label: labels.Everything(),
1122 Limit: 5,
1123 },
1124 expectedOut: []example.Pod{*createdPods[3]},
1125 },
1126 {
1127 name: "filter returns one item, page size equal to total list size with current resource version and match=NotOlderThan",
1128 prefix: "/pods",
1129 pred: storage.SelectionPredicate{
1130 Field: fields.OneTermEqualSelector("metadata.name", "barfoo"),
1131 Label: labels.Everything(),
1132 Limit: 5,
1133 },
1134 rv: list.ResourceVersion,
1135 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
1136 expectedOut: []example.Pod{*createdPods[3]},
1137 },
1138 {
1139 name: "list all items",
1140 prefix: "/pods",
1141 pred: storage.Everything,
1142 expectedOut: []example.Pod{*updatedPod, *createdPods[1], *createdPods[2], *createdPods[3], *createdPods[4]},
1143 },
1144 {
1145 name: "list all items with current resource version and match=NotOlderThan",
1146 prefix: "/pods",
1147 pred: storage.Everything,
1148 rv: list.ResourceVersion,
1149 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
1150 expectedOut: []example.Pod{*updatedPod, *createdPods[1], *createdPods[2], *createdPods[3], *createdPods[4]},
1151 },
1152 {
1153 name: "verify list returns updated version of object; filter returns one item, page size equal to total list size with current resource version and match=NotOlderThan",
1154 prefix: "/pods",
1155 pred: storage.SelectionPredicate{
1156 Field: fields.OneTermEqualSelector("spec.nodeName", "fakeNode"),
1157 Label: labels.Everything(),
1158 Limit: 5,
1159 },
1160 rv: list.ResourceVersion,
1161 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
1162 expectedOut: []example.Pod{*updatedPod},
1163 },
1164 {
1165 name: "verify list does not return deleted object; filter for deleted object, page size equal to total list size with current resource version and match=NotOlderThan",
1166 prefix: "/pods",
1167 pred: storage.SelectionPredicate{
1168 Field: fields.OneTermEqualSelector("metadata.name", "baz"),
1169 Label: labels.Everything(),
1170 Limit: 5,
1171 },
1172 rv: list.ResourceVersion,
1173 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
1174 expectedOut: []example.Pod{},
1175 },
1176 {
1177 name: "test consistent List",
1178 prefix: "/pods/empty",
1179 pred: storage.Everything,
1180 rv: "",
1181 expectRV: currentRV,
1182 expectedOut: []example.Pod{},
1183 },
1184 {
1185 name: "test non-consistent List",
1186 prefix: "/pods/empty",
1187 pred: storage.Everything,
1188 rv: "0",
1189 expectRVFunc: resourceVersionNotOlderThan(initialRV),
1190 expectedOut: []example.Pod{},
1191 },
1192 // match=Exact
1193 {
1194 name: "test List with resource version set before first write, match=Exact",
1195 prefix: "/pods/",
1196 pred: storage.Everything,
1197 expectedOut: []example.Pod{},
1198 rv: initialRV,
1199 rvMatch: metav1.ResourceVersionMatchExact,
1200 expectRV: initialRV,
1201 },
1202 {
1203 name: "test List with resource version of first write, match=Exact",
1204 prefix: "/pods/",
1205 pred: storage.Everything,
1206 expectedOut: []example.Pod{*createdPods[0]},
1207 rv: createdPods[0].ResourceVersion,
1208 rvMatch: metav1.ResourceVersionMatchExact,
1209 expectRV: createdPods[0].ResourceVersion,
1210 },
1211 {
1212 name: "test List with resource version of second write, match=Exact",
1213 prefix: "/pods/",
1214 pred: storage.Everything,
1215 expectedOut: []example.Pod{*createdPods[0], *createdPods[1]},
1216 rv: createdPods[1].ResourceVersion,
1217 rvMatch: metav1.ResourceVersionMatchExact,
1218 expectRV: createdPods[1].ResourceVersion,
1219 },
1220 {
1221 name: "test List with resource version of third write, match=Exact",
1222 prefix: "/pods/",
1223 pred: storage.Everything,
1224 expectedOut: []example.Pod{*createdPods[0], *createdPods[1], *createdPods[2]},
1225 rv: createdPods[2].ResourceVersion,
1226 rvMatch: metav1.ResourceVersionMatchExact,
1227 expectRV: createdPods[2].ResourceVersion,
1228 },
1229 {
1230 name: "test List with resource version of fourth write, match=Exact",
1231 prefix: "/pods/",
1232 pred: storage.Everything,
1233 expectedOut: []example.Pod{*createdPods[0], *createdPods[1], *createdPods[2], *createdPods[3]},
1234 rv: createdPods[3].ResourceVersion,
1235 rvMatch: metav1.ResourceVersionMatchExact,
1236 expectRV: createdPods[3].ResourceVersion,
1237 },
1238 {
1239 name: "test List with resource version of fifth write, match=Exact",
1240 prefix: "/pods/",
1241 pred: storage.Everything,
1242 expectedOut: []example.Pod{*createdPods[0], *createdPods[1], *createdPods[2], *createdPods[3], *createdPods[4]},
1243 rv: createdPods[4].ResourceVersion,
1244 rvMatch: metav1.ResourceVersionMatchExact,
1245 expectRV: createdPods[4].ResourceVersion,
1246 },
1247 {
1248 name: "test List with resource version of six write, match=Exact",
1249 prefix: "/pods/",
1250 pred: storage.Everything,
1251 expectedOut: []example.Pod{*createdPods[0], *createdPods[1], *createdPods[2], *createdPods[3], *createdPods[4], *createdPods[5]},
1252 rv: createdPods[5].ResourceVersion,
1253 rvMatch: metav1.ResourceVersionMatchExact,
1254 expectRV: createdPods[5].ResourceVersion,
1255 },
1256 {
1257 name: "test List with resource version of seventh write, match=Exact",
1258 prefix: "/pods/",
1259 pred: storage.Everything,
1260 expectedOut: []example.Pod{*updatedPod, *createdPods[1], *createdPods[2], *createdPods[3], *createdPods[4], *createdPods[5]},
1261 rv: updatedPod.ResourceVersion,
1262 rvMatch: metav1.ResourceVersionMatchExact,
1263 expectRV: updatedPod.ResourceVersion,
1264 },
1265 {
1266 name: "test List with resource version of eight write, match=Exact",
1267 prefix: "/pods/",
1268 pred: storage.Everything,
1269 expectedOut: []example.Pod{*updatedPod, *createdPods[1], *createdPods[2], *createdPods[3], *createdPods[4]},
1270 rv: fmt.Sprint(continueRV),
1271 rvMatch: metav1.ResourceVersionMatchExact,
1272 expectRV: fmt.Sprint(continueRV),
1273 },
1274 {
1275 name: "test List with resource version after writes, match=Exact",
1276 prefix: "/pods/",
1277 pred: storage.Everything,
1278 expectedOut: []example.Pod{*updatedPod, *createdPods[1], *createdPods[2], *createdPods[3], *createdPods[4]},
1279 rv: fmt.Sprint(continueRV + 1),
1280 rvMatch: metav1.ResourceVersionMatchExact,
1281 expectRV: fmt.Sprint(continueRV + 1),
1282 },
1283 {
1284 name: "test List with future resource version, match=Exact",
1285 prefix: "/pods/",
1286 pred: storage.Everything,
1287 rv: fmt.Sprint(continueRV + 2),
1288 rvMatch: metav1.ResourceVersionMatchExact,
1289 expectRVTooLarge: true,
1290 },
1291 // limit, match=Exact
1292 {
1293 name: "test List with limit, resource version of second write, match=Exact",
1294 prefix: "/pods/",
1295 pred: storage.SelectionPredicate{
1296 Label: labels.Everything(),
1297 Field: fields.Everything(),
1298 Limit: 1,
1299 },
1300 expectedOut: []example.Pod{*createdPods[0]},
1301 rv: createdPods[1].ResourceVersion,
1302 expectContinue: true,
1303 expectContinueExact: encodeContinueOrDie(createdPods[0].Namespace+"/"+createdPods[0].Name+"\x00", int64(mustAtoi(createdPods[1].ResourceVersion))),
1304 rvMatch: metav1.ResourceVersionMatchExact,
1305 expectRV: createdPods[1].ResourceVersion,
1306 expectedRemainingItemCount: utilpointer.Int64(1),
1307 },
1308 {
1309 name: "test List with limit, resource version of third write, match=Exact",
1310 prefix: "/pods/",
1311 pred: storage.SelectionPredicate{
1312 Label: labels.Everything(),
1313 Field: fields.Everything(),
1314 Limit: 2,
1315 },
1316 rv: createdPods[2].ResourceVersion,
1317 rvMatch: metav1.ResourceVersionMatchExact,
1318 expectedOut: []example.Pod{*createdPods[0], *createdPods[1]},
1319 expectContinue: true,
1320 expectContinueExact: encodeContinueOrDie(createdPods[1].Namespace+"/"+createdPods[1].Name+"\x00", int64(mustAtoi(createdPods[2].ResourceVersion))),
1321 expectRV: createdPods[2].ResourceVersion,
1322 expectedRemainingItemCount: utilpointer.Int64(1),
1323 },
1324 {
1325 name: "test List with limit, resource version of fourth write, match=Exact",
1326 prefix: "/pods/",
1327 pred: storage.SelectionPredicate{
1328 Label: labels.Everything(),
1329 Field: fields.Everything(),
1330 Limit: 4,
1331 },
1332 rv: createdPods[3].ResourceVersion,
1333 rvMatch: metav1.ResourceVersionMatchExact,
1334 expectedOut: []example.Pod{*createdPods[0], *createdPods[1], *createdPods[2], *createdPods[3]},
1335 expectRV: createdPods[3].ResourceVersion,
1336 },
1337 {
1338 name: "test List with limit, resource version of fifth write, match=Exact",
1339 prefix: "/pods/",
1340 pred: storage.SelectionPredicate{
1341 Label: labels.Everything(),
1342 Field: fields.Everything(),
1343 Limit: 1,
1344 },
1345 rv: createdPods[4].ResourceVersion,
1346 rvMatch: metav1.ResourceVersionMatchExact,
1347 expectedOut: []example.Pod{*createdPods[0]},
1348 expectRV: createdPods[4].ResourceVersion,
1349 expectContinue: true,
1350 expectContinueExact: encodeContinueOrDie(createdPods[0].Namespace+"/"+createdPods[0].Name+"\x00", int64(mustAtoi(createdPods[4].ResourceVersion))),
1351 expectedRemainingItemCount: utilpointer.Int64(4),
1352 },
1353 {
1354 name: "test List with limit, resource version of six write, match=Exact",
1355 prefix: "/pods/",
1356 pred: storage.SelectionPredicate{
1357 Label: labels.Everything(),
1358 Field: fields.Everything(),
1359 Limit: 2,
1360 },
1361 rv: createdPods[5].ResourceVersion,
1362 rvMatch: metav1.ResourceVersionMatchExact,
1363 expectedOut: []example.Pod{*createdPods[0], *createdPods[1]},
1364 expectRV: createdPods[5].ResourceVersion,
1365 expectContinue: true,
1366 expectContinueExact: encodeContinueOrDie(createdPods[1].Namespace+"/"+createdPods[1].Name+"\x00", int64(mustAtoi(createdPods[5].ResourceVersion))),
1367 expectedRemainingItemCount: utilpointer.Int64(4),
1368 },
1369 {
1370 name: "test List with limit, resource version of seventh write, match=Exact",
1371 prefix: "/pods/",
1372 pred: storage.SelectionPredicate{
1373 Label: labels.Everything(),
1374 Field: fields.Everything(),
1375 Limit: 4,
1376 },
1377 rv: updatedPod.ResourceVersion,
1378 rvMatch: metav1.ResourceVersionMatchExact,
1379 expectedOut: []example.Pod{*updatedPod, *createdPods[1], *createdPods[5], *createdPods[2]},
1380 expectRV: updatedPod.ResourceVersion,
1381 expectContinue: true,
1382 expectContinueExact: encodeContinueOrDie(createdPods[2].Namespace+"/"+createdPods[2].Name+"\x00", int64(mustAtoi(updatedPod.ResourceVersion))),
1383 expectedRemainingItemCount: utilpointer.Int64(2),
1384 },
1385 {
1386 name: "test List with limit, resource version of eight write, match=Exact",
1387 prefix: "/pods/",
1388 pred: storage.SelectionPredicate{
1389 Label: labels.Everything(),
1390 Field: fields.Everything(),
1391 Limit: 8,
1392 },
1393 expectedOut: []example.Pod{*updatedPod, *createdPods[1], *createdPods[2], *createdPods[3], *createdPods[4]},
1394 rv: fmt.Sprint(continueRV),
1395 rvMatch: metav1.ResourceVersionMatchExact,
1396 expectRV: fmt.Sprint(continueRV),
1397 },
1398 {
1399 name: "test List with limit, resource version after writes, match=Exact",
1400 prefix: "/pods/",
1401 pred: storage.SelectionPredicate{
1402 Label: labels.Everything(),
1403 Field: fields.Everything(),
1404 Limit: 1,
1405 },
1406 rv: fmt.Sprint(continueRV + 1),
1407 rvMatch: metav1.ResourceVersionMatchExact,
1408 expectedOut: []example.Pod{*updatedPod},
1409 expectRV: fmt.Sprint(continueRV + 1),
1410 expectContinue: true,
1411 expectContinueExact: encodeContinueOrDie(updatedPod.Namespace+"/"+updatedPod.Name+"\x00", int64(continueRV+1)),
1412 expectedRemainingItemCount: utilpointer.Int64(4),
1413 },
1414 // Continue
1415 {
1416 name: "test List with continue, resource version of second write",
1417 prefix: "/pods/",
1418 pred: storage.SelectionPredicate{
1419 Label: labels.Everything(),
1420 Field: fields.Everything(),
1421 Limit: 1,
1422 Continue: encodeContinueOrDie(createdPods[0].Namespace+"/"+createdPods[0].Name+"\x00", int64(mustAtoi(createdPods[1].ResourceVersion))),
1423 },
1424 expectedOut: []example.Pod{*createdPods[1]},
1425 expectRV: createdPods[1].ResourceVersion,
1426 },
1427 {
1428 name: "test List with continue, resource version of third write",
1429 prefix: "/pods/",
1430 pred: storage.SelectionPredicate{
1431 Label: labels.Everything(),
1432 Field: fields.Everything(),
1433 Limit: 2,
1434 Continue: encodeContinueOrDie(createdPods[1].Namespace+"/"+createdPods[1].Name+"\x00", int64(mustAtoi(createdPods[2].ResourceVersion))),
1435 },
1436 expectedOut: []example.Pod{*createdPods[2]},
1437 expectRV: createdPods[2].ResourceVersion,
1438 },
1439 {
1440 name: "test List with continue, resource version of fifth write",
1441 prefix: "/pods/",
1442 pred: storage.SelectionPredicate{
1443 Label: labels.Everything(),
1444 Field: fields.Everything(),
1445 Limit: 1,
1446 Continue: encodeContinueOrDie(createdPods[0].Namespace+"/"+createdPods[0].Name+"\x00", int64(mustAtoi(createdPods[4].ResourceVersion))),
1447 },
1448 expectedOut: []example.Pod{*createdPods[1]},
1449 expectRV: createdPods[4].ResourceVersion,
1450 expectContinue: true,
1451 expectContinueExact: encodeContinueOrDie(createdPods[1].Namespace+"/"+createdPods[1].Name+"\x00", int64(mustAtoi(createdPods[4].ResourceVersion))),
1452 expectedRemainingItemCount: utilpointer.Int64(3),
1453 },
1454 {
1455 name: "test List with continue, resource version of six write",
1456 prefix: "/pods/",
1457 pred: storage.SelectionPredicate{
1458 Label: labels.Everything(),
1459 Field: fields.Everything(),
1460 Limit: 2,
1461 Continue: encodeContinueOrDie(createdPods[1].Namespace+"/"+createdPods[1].Name+"\x00", int64(mustAtoi(createdPods[5].ResourceVersion))),
1462 },
1463 expectedOut: []example.Pod{*createdPods[5], *createdPods[2]},
1464 expectRV: createdPods[5].ResourceVersion,
1465 expectContinue: true,
1466 expectContinueExact: encodeContinueOrDie(createdPods[2].Namespace+"/"+createdPods[2].Name+"\x00", int64(mustAtoi(createdPods[5].ResourceVersion))),
1467 expectedRemainingItemCount: utilpointer.Int64(2),
1468 },
1469 {
1470 name: "test List with continue, resource version of seventh write",
1471 prefix: "/pods/",
1472 pred: storage.SelectionPredicate{
1473 Label: labels.Everything(),
1474 Field: fields.Everything(),
1475 Limit: 4,
1476 Continue: encodeContinueOrDie(createdPods[2].Namespace+"/"+createdPods[2].Name+"\x00", int64(mustAtoi(updatedPod.ResourceVersion))),
1477 },
1478 expectedOut: []example.Pod{*createdPods[3], *createdPods[4]},
1479 expectRV: updatedPod.ResourceVersion,
1480 },
1481 {
1482 name: "test List with continue, resource version after writes",
1483 prefix: "/pods/",
1484 pred: storage.SelectionPredicate{
1485 Label: labels.Everything(),
1486 Field: fields.Everything(),
1487 Limit: 1,
1488 Continue: encodeContinueOrDie(updatedPod.Namespace+"/"+updatedPod.Name+"\x00", int64(continueRV+1)),
1489 },
1490 expectedOut: []example.Pod{*createdPods[1]},
1491 expectRV: fmt.Sprint(continueRV + 1),
1492 expectContinue: true,
1493 expectContinueExact: encodeContinueOrDie(createdPods[1].Namespace+"/"+createdPods[1].Name+"\x00", int64(continueRV+1)),
1494 expectedRemainingItemCount: utilpointer.Int64(3),
1495 },
1496 {
1497 name: "test List with continue from second pod, negative resource version gives consistent read",
1498 prefix: "/pods/",
1499 pred: storage.SelectionPredicate{
1500 Label: labels.Everything(),
1501 Field: fields.Everything(),
1502 Continue: encodeContinueOrDie(createdPods[0].Namespace+"/"+createdPods[0].Name+"\x00", -1),
1503 },
1504 expectedOut: []example.Pod{*createdPods[1], *createdPods[2], *createdPods[3], *createdPods[4]},
1505 expectRV: currentRV,
1506 },
1507 {
1508 name: "test List with continue from second pod and limit, negative resource version gives consistent read",
1509 prefix: "/pods/",
1510 pred: storage.SelectionPredicate{
1511 Label: labels.Everything(),
1512 Field: fields.Everything(),
1513 Limit: 2,
1514 Continue: encodeContinueOrDie(createdPods[0].Namespace+"/"+createdPods[0].Name+"\x00", -1),
1515 },
1516 expectedOut: []example.Pod{*createdPods[1], *createdPods[2]},
1517 expectContinue: true,
1518 expectContinueExact: encodeContinueOrDie(createdPods[2].Namespace+"/"+createdPods[2].Name+"\x00", int64(continueRV+1)),
1519 expectRV: currentRV,
1520 expectedRemainingItemCount: utilpointer.Int64(2),
1521 },
1522 {
1523 name: "test List with continue from third pod, negative resource version gives consistent read",
1524 prefix: "/pods/",
1525 pred: storage.SelectionPredicate{
1526 Label: labels.Everything(),
1527 Field: fields.Everything(),
1528 Continue: encodeContinueOrDie(createdPods[2].Namespace+"/"+createdPods[2].Name+"\x00", -1),
1529 },
1530 expectedOut: []example.Pod{*createdPods[3], *createdPods[4]},
1531 expectRV: currentRV,
1532 },
1533 {
1534 name: "test List with continue from empty fails",
1535 prefix: "/pods/",
1536 pred: storage.SelectionPredicate{
1537 Label: labels.Everything(),
1538 Field: fields.Everything(),
1539 Continue: encodeContinueOrDie("", int64(continueRV)),
1540 },
1541 expectError: true,
1542 },
1543 {
1544 name: "test List with continue from first pod, empty resource version fails",
1545 prefix: "/pods/",
1546 pred: storage.SelectionPredicate{
1547 Label: labels.Everything(),
1548 Field: fields.Everything(),
1549 Continue: encodeContinueOrDie(createdPods[0].Namespace+"/"+createdPods[0].Name+"\x00", 0),
1550 },
1551 expectError: true,
1552 },
1553 {
1554 name: "test List with negative rv fails",
1555 prefix: "/pods/",
1556 rv: "-1",
1557 pred: storage.SelectionPredicate{
1558 Label: labels.Everything(),
1559 Field: fields.Everything(),
1560 },
1561 expectError: true,
1562 },
1563 }
1564
1565 for _, tt := range tests {
1566 tt := tt
1567 t.Run(tt.name, func(t *testing.T) {
1568 // For some asynchronous implementations of storage interface (in particular watchcache),
1569 // certain requests may impact result of further requests. As an example, if we first
1570 // ensure that watchcache is synchronized up to ResourceVersion X (using Get/List requests
1571 // with NotOlderThan semantic), the further requests (even specifying earlier resource
1572 // version) will also return the result synchronized to at least ResourceVersion X.
1573 // By parallelizing test cases we ensure that the order in which test cases are defined
1574 // doesn't automatically preclude some scenarios from happening.
1575 t.Parallel()
1576
1577 if ignoreWatchCacheTests && tt.ignoreForWatchCache {
1578 t.Skip()
1579 }
1580
1581 if tt.pred.GetAttrs == nil {
1582 tt.pred.GetAttrs = getAttrs
1583 }
1584
1585 out := &example.PodList{}
1586 storageOpts := storage.ListOptions{
1587 ResourceVersion: tt.rv,
1588 ResourceVersionMatch: tt.rvMatch,
1589 Predicate: tt.pred,
1590 Recursive: true,
1591 }
1592 err := store.GetList(ctx, tt.prefix, storageOpts, out)
1593 if tt.expectRVTooLarge {
1594 // TODO: Clasify etcd future revision error as TooLargeResourceVersion
1595 if err == nil || !(storage.IsTooLargeResourceVersion(err) || strings.Contains(err.Error(), "etcdserver: mvcc: required revision is a future revision")) {
1596 t.Fatalf("expecting resource version too high error, but get: %q", err)
1597 }
1598 return
1599 }
1600
1601 if err != nil {
1602 if !tt.expectError {
1603 t.Fatalf("GetList failed: %v", err)
1604 }
1605 return
1606 }
1607 if tt.expectError {
1608 t.Fatalf("expected error but got none")
1609 }
1610 if (len(out.Continue) > 0) != tt.expectContinue {
1611 t.Errorf("unexpected continue token: %q", out.Continue)
1612 }
1613
1614 if len(tt.expectContinueExact) > 0 {
1615 ExpectContinueMatches(t, tt.expectContinueExact, out.Continue)
1616 }
1617
1618 // If a client requests an exact resource version, it must be echoed back to them.
1619 if tt.expectRV != "" {
1620 if tt.expectRV != out.ResourceVersion {
1621 t.Errorf("resourceVersion in list response want=%s, got=%s", tt.expectRV, out.ResourceVersion)
1622 }
1623 }
1624 if tt.expectRVFunc != nil {
1625 if err := tt.expectRVFunc(out.ResourceVersion); err != nil {
1626 t.Errorf("resourceVersion in list response invalid: %v", err)
1627 }
1628 }
1629
1630 if tt.expectedAlternatives == nil {
1631 sort.Sort(sortablePodList(tt.expectedOut))
1632 expectNoDiff(t, "incorrect list pods", tt.expectedOut, out.Items)
1633 } else {
1634 ExpectContains(t, "incorrect list pods", toInterfaceSlice(tt.expectedAlternatives), out.Items)
1635 }
1636 if !cmp.Equal(tt.expectedRemainingItemCount, out.RemainingItemCount) {
1637 t.Fatalf("unexpected remainingItemCount, diff: %s", cmp.Diff(tt.expectedRemainingItemCount, out.RemainingItemCount))
1638 }
1639 })
1640 }
1641 }
1642
1643 func ExpectContinueMatches(t *testing.T, expect, got string) {
1644 t.Helper()
1645 if expect == got {
1646 return
1647 }
1648 expectDecoded, err := base64.RawURLEncoding.DecodeString(expect)
1649 if err != nil {
1650 t.Errorf("expected continue token: %q, got: %q", expect, got)
1651 return
1652 }
1653 gotDecoded, err := base64.RawURLEncoding.DecodeString(got)
1654 if err != nil {
1655 t.Errorf("expected continue token: %q, got: %q", expect, got)
1656 return
1657 }
1658 t.Errorf("expected continue token: %s, got: %s", expectDecoded, gotDecoded)
1659 }
1660
1661 func RunTestConsistentList(ctx context.Context, t *testing.T, store storage.Interface, increaseRV IncreaseRVFunc, cacheEnabled, consistentReadsSupported, listFromCacheSnapshot bool) {
1662 outPod := &example.Pod{}
1663 inPod := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "foo"}}
1664 err := store.Create(ctx, computePodKey(inPod), inPod, outPod, 0)
1665 require.NoError(t, err)
1666 lastResourceWriteRV, err := strconv.Atoi(outPod.ResourceVersion)
1667 require.NoError(t, err)
1668
1669 increaseRV(ctx, t)
1670 consistentRV := lastResourceWriteRV + 1
1671 cacheSyncRV := 0
1672
1673 tcs := []struct {
1674 name string
1675 requestRV string
1676 continueToken string
1677 validateResponseRV func(*testing.T, int)
1678 }{
1679 {
1680 name: "Non-consistent list before consistent read",
1681 requestRV: "0",
1682 validateResponseRV: func(t *testing.T, rv int) {
1683 if cacheEnabled {
1684 // Cache might not yet observed write
1685 assert.LessOrEqual(t, rv, lastResourceWriteRV)
1686 } else {
1687 // Etcd should always be up to date with consistent RV
1688 assert.Equal(t, consistentRV, rv)
1689 }
1690 },
1691 },
1692 {
1693 name: "LIST without RV returns consistent RV",
1694 requestRV: "",
1695 validateResponseRV: func(t *testing.T, rv int) {
1696 assert.Equal(t, consistentRV, rv)
1697 cacheSyncRV = rv
1698 },
1699 },
1700 {
1701 name: "List with negative continue RV returns consistent RV",
1702 continueToken: encodeContinueOrDie("/pods/a", -1),
1703 validateResponseRV: func(t *testing.T, rv int) {
1704 assert.Equal(t, consistentRV, rv)
1705 if listFromCacheSnapshot {
1706 cacheSyncRV = rv
1707 }
1708 },
1709 },
1710 {
1711 name: "Non-consistent request after consistent read",
1712 requestRV: "0",
1713 validateResponseRV: func(t *testing.T, rv int) {
1714 if cacheEnabled {
1715 if consistentReadsSupported {
1716 // Consistent read will sync cache
1717 assert.Equal(t, cacheSyncRV, rv)
1718 } else {
1719 // Without consisten reads cache is not synced
1720 assert.LessOrEqual(t, rv, lastResourceWriteRV)
1721 }
1722 } else {
1723 // Etcd always points to newest RV
1724 assert.Equal(t, consistentRV, rv)
1725 }
1726 },
1727 },
1728 }
1729 for _, tc := range tcs {
1730 t.Run(tc.name, func(t *testing.T) {
1731 out := &example.PodList{}
1732 opts := storage.ListOptions{
1733 Recursive: true,
1734 ResourceVersion: tc.requestRV,
1735 Predicate: storage.SelectionPredicate{
1736 Label: labels.Everything(),
1737 Field: fields.Everything(),
1738 Continue: tc.continueToken,
1739 },
1740 }
1741 err = store.GetList(ctx, "/pods/empty", opts, out)
1742 require.NoError(t, err)
1743
1744 parsedOutRV, err := strconv.Atoi(out.ResourceVersion)
1745 require.NoError(t, err)
1746 tc.validateResponseRV(t, parsedOutRV)
1747 })
1748 // Update RV on each read to test multiple reads for consistent RV.
1749 increaseRV(ctx, t)
1750 consistentRV++
1751 }
1752 }
1753
1754 // seedMultiLevelData creates a set of keys with a multi-level structure, returning a resourceVersion
1755 // from before any were created along with the full set of objects that were persisted
1756 func seedMultiLevelData(ctx context.Context, store storage.Interface) (initialRV string, created []*example.Pod, updated *example.Pod, err error) {
1757 // Setup storage with the following structure:
1758 // /
1759 // - first/
1760 // | - bar
1761 // |
1762 // - second/
1763 // | - bar
1764 // | - foo
1765 // | - [deleted] baz
1766 // |
1767 // - third/
1768 // | - barfoo
1769 // | - foo
1770 barFirst := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "first", Name: "bar"}}
1771 barSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "bar"}}
1772 fooSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "foo"}}
1773 bazSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "baz"}}
1774 barfooThird := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "third", Name: "barfoo"}}
1775 fooThird := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "third", Name: "foo"}}
1776
1777 preset := []struct {
1778 key string
1779 obj *example.Pod
1780 storedObj *example.Pod
1781 }{
1782 {
1783 key: computePodKey(barFirst),
1784 obj: barFirst,
1785 },
1786 {
1787 key: computePodKey(barSecond),
1788 obj: barSecond,
1789 },
1790 {
1791 key: computePodKey(fooSecond),
1792 obj: fooSecond,
1793 },
1794 {
1795 key: computePodKey(barfooThird),
1796 obj: barfooThird,
1797 },
1798 {
1799 key: computePodKey(fooThird),
1800 obj: fooThird,
1801 },
1802 {
1803 key: computePodKey(bazSecond),
1804 obj: bazSecond,
1805 },
1806 }
1807
1808 // we want to figure out the resourceVersion before we create anything
1809 initialList := &example.PodList{}
1810 if err := store.GetList(ctx, "/pods", storage.ListOptions{Predicate: storage.Everything, Recursive: true}, initialList); err != nil {
1811 return "", nil, nil, fmt.Errorf("failed to determine starting resourceVersion: %w", err)
1812 }
1813 initialRV = initialList.ResourceVersion
1814
1815 for i, ps := range preset {
1816 preset[i].storedObj = &example.Pod{}
1817 err := store.Create(ctx, ps.key, ps.obj, preset[i].storedObj, 0)
1818 if err != nil {
1819 return "", nil, nil, fmt.Errorf("failed to create object: %w", err)
1820 }
1821 }
1822
1823 // For barFirst, we first create it with key /pods/first/bar and then we update
1824 // it by changing its spec.nodeName. The point of doing this is to be able to
1825 // test that if a pod with key /pods/first/bar is in fact returned, the returned
1826 // pod is the updated one (i.e. with spec.nodeName changed).
1827 updated = &example.Pod{}
1828 if err := store.GuaranteedUpdate(ctx, computePodKey(barFirst), updated, true, nil,
1829 func(input runtime.Object, _ storage.ResponseMeta) (output runtime.Object, ttl *uint64, err error) {
1830 pod := input.(*example.Pod).DeepCopy()
1831 pod.Spec.NodeName = "fakeNode"
1832 return pod, nil, nil
1833 }, nil); err != nil {
1834 return "", nil, nil, fmt.Errorf("failed to update object: %w", err)
1835 }
1836
1837 // We now delete bazSecond provided it has been created first. We do this to enable
1838 // testing cases that had an object exist initially and then was deleted and how this
1839 // would be reflected in responses of different calls.
1840 storedObj := &example.Pod{}
1841 if err := store.Delete(ctx, computePodKey(bazSecond), storedObj, nil, storage.ValidateAllObjectFunc, nil, storage.DeleteOptions{}); err != nil {
1842 return "", nil, nil, fmt.Errorf("failed to delete object: %w", err)
1843 }
1844
1845 for _, item := range preset {
1846 created = append(created, item.storedObj)
1847 }
1848 return initialRV, created, updated, nil
1849 }
1850
1851 func RunTestGetListNonRecursive(ctx context.Context, t *testing.T, increaseRV IncreaseRVFunc, store storage.Interface) {
1852 key, prevStoredObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns"}})
1853 prevRV, _ := strconv.Atoi(prevStoredObj.ResourceVersion)
1854
1855 storedObj := &example.Pod{}
1856 if err := store.GuaranteedUpdate(ctx, key, storedObj, false, nil,
1857 func(_ runtime.Object, _ storage.ResponseMeta) (runtime.Object, *uint64, error) {
1858 newPod := prevStoredObj.DeepCopy()
1859 newPod.Annotations = map[string]string{"version": "second"}
1860 return newPod, nil, nil
1861 }, nil); err != nil {
1862 t.Fatalf("update failed: %v", err)
1863 }
1864 objRV, _ := strconv.Atoi(storedObj.ResourceVersion)
1865 // Increase RV to test consistent List.
1866 increaseRV(ctx, t)
1867
1868 tests := []struct {
1869 name string
1870 key string
1871 pred storage.SelectionPredicate
1872 expectedOut []example.Pod
1873 expectedAlternatives [][]example.Pod
1874 rv string
1875 rvMatch metav1.ResourceVersionMatch
1876 expectRVTooLarge bool
1877 }{{
1878 name: "existing key",
1879 key: key,
1880 pred: storage.Everything,
1881 expectedOut: []example.Pod{*storedObj},
1882 }, {
1883 name: "existing key, resourceVersion=0",
1884 key: key,
1885 pred: storage.Everything,
1886 expectedAlternatives: [][]example.Pod{{}, {*prevStoredObj}, {*storedObj}},
1887 rv: "0",
1888 }, {
1889 name: "existing key, resourceVersion=0, resourceVersionMatch=notOlderThan",
1890 key: key,
1891 pred: storage.Everything,
1892 expectedAlternatives: [][]example.Pod{{}, {*prevStoredObj}, {*storedObj}},
1893 rv: "0",
1894 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
1895 }, {
1896 name: "existing key, resourceVersion=current",
1897 key: key,
1898 pred: storage.Everything,
1899 expectedOut: []example.Pod{*storedObj},
1900 rv: fmt.Sprintf("%d", objRV),
1901 }, {
1902 name: "existing key, resourceVersion=current, resourceVersionMatch=notOlderThan",
1903 key: key,
1904 pred: storage.Everything,
1905 expectedOut: []example.Pod{*storedObj},
1906 rv: fmt.Sprintf("%d", objRV),
1907 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
1908 }, {
1909 name: "existing key, resourceVersion=previous, resourceVersionMatch=notOlderThan",
1910 key: key,
1911 pred: storage.Everything,
1912 expectedAlternatives: [][]example.Pod{{*prevStoredObj}, {*storedObj}},
1913 rv: fmt.Sprintf("%d", prevRV),
1914 rvMatch: metav1.ResourceVersionMatchNotOlderThan,
1915 }, {
1916 name: "existing key, resourceVersion=current, resourceVersionMatch=exact",
1917 key: key,
1918 pred: storage.Everything,
1919 expectedOut: []example.Pod{*storedObj},
1920 rv: fmt.Sprintf("%d", objRV),
1921 rvMatch: metav1.ResourceVersionMatchExact,
1922 }, {
1923 name: "existing key, resourceVersion=previous, resourceVersionMatch=exact",
1924 key: key,
1925 pred: storage.Everything,
1926 expectedOut: []example.Pod{*prevStoredObj},
1927 rv: fmt.Sprintf("%d", prevRV),
1928 rvMatch: metav1.ResourceVersionMatchExact,
1929 }, {
1930 name: "existing key, resourceVersion=too high",
1931 key: key,
1932 pred: storage.Everything,
1933 expectedOut: []example.Pod{*storedObj},
1934 rv: strconv.FormatInt(math.MaxInt64, 10),
1935 expectRVTooLarge: true,
1936 }, {
1937 name: "non-existing key",
1938 key: "/non-existing",
1939 pred: storage.Everything,
1940 expectedOut: []example.Pod{},
1941 }, {
1942 name: "with matching pod name",
1943 key: "/non-existing",
1944 pred: storage.SelectionPredicate{
1945 Label: labels.Everything(),
1946 Field: fields.ParseSelectorOrDie("metadata.name!=" + storedObj.Name),
1947 GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
1948 pod := obj.(*example.Pod)
1949 return nil, fields.Set{"metadata.name": pod.Name}, nil
1950 },
1951 },
1952 expectedOut: []example.Pod{},
1953 }, {
1954 name: "existing key, resourceVersion=current, with not matching pod name",
1955 key: key,
1956 pred: storage.SelectionPredicate{
1957 Label: labels.Everything(),
1958 Field: fields.ParseSelectorOrDie("metadata.name!=" + storedObj.Name),
1959 GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
1960 pod := obj.(*example.Pod)
1961 return nil, fields.Set{"metadata.name": pod.Name}, nil
1962 },
1963 },
1964 expectedOut: []example.Pod{},
1965 rv: fmt.Sprintf("%d", objRV),
1966 }}
1967
1968 for _, tt := range tests {
1969 tt := tt
1970 t.Run(tt.name, func(t *testing.T) {
1971 // For some asynchronous implementations of storage interface (in particular watchcache),
1972 // certain requests may impact result of further requests. As an example, if we first
1973 // ensure that watchcache is synchronized up to ResourceVersion X (using Get/List requests
1974 // with NotOlderThan semantic), the further requests (even specifying earlier resource
1975 // version) will also return the result synchronized to at least ResourceVersion X.
1976 // By parallelizing test cases we ensure that the order in which test cases are defined
1977 // doesn't automatically preclude some scenarios from happening.
1978 t.Parallel()
1979
1980 out := &example.PodList{}
1981 storageOpts := storage.ListOptions{
1982 ResourceVersion: tt.rv,
1983 ResourceVersionMatch: tt.rvMatch,
1984 Predicate: tt.pred,
1985 Recursive: false,
1986 }
1987 err := store.GetList(ctx, tt.key, storageOpts, out)
1988
1989 if tt.expectRVTooLarge {
1990 if err == nil || !storage.IsTooLargeResourceVersion(err) {
1991 t.Errorf("%s: expecting resource version too high error, but get: %s", tt.name, err)
1992 }
1993 return
1994 }
1995
1996 if err != nil {
1997 t.Fatalf("GetList failed: %v", err)
1998 }
1999 if len(out.ResourceVersion) == 0 {
2000 t.Errorf("%s: unset resourceVersion", tt.name)
2001 }
2002
2003 if tt.expectedAlternatives == nil {
2004 expectNoDiff(t, "incorrect list pods", tt.expectedOut, out.Items)
2005 } else {
2006 ExpectContains(t, "incorrect list pods", toInterfaceSlice(tt.expectedAlternatives), out.Items)
2007 }
2008 })
2009 }
2010 }
2011
2012 // RunTestGetListRecursivePrefix tests how recursive parameter works for object keys that are prefixes of each other.
2013 func RunTestGetListRecursivePrefix(ctx context.Context, t *testing.T, store storage.Interface) {
2014 fooKey, fooObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns"}})
2015 fooBarKey, fooBarObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foobar", Namespace: "test-ns"}})
2016 _, otherNamespaceObj := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "baz", Namespace: "test-ns2"}})
2017 lastRev := otherNamespaceObj.ResourceVersion
2018
2019 tests := []struct {
2020 name string
2021 key string
2022 recursive bool
2023 expectedOut []example.Pod
2024 }{
2025 {
2026 name: "NonRecursive on resource prefix doesn't return any objects",
2027 key: "/pods/",
2028 recursive: false,
2029 expectedOut: []example.Pod{},
2030 },
2031 {
2032 name: "Recursive on resource prefix returns all objects",
2033 key: "/pods/",
2034 recursive: true,
2035 expectedOut: []example.Pod{*fooObj, *fooBarObj, *otherNamespaceObj},
2036 },
2037 {
2038 name: "NonRecursive on namespace prefix doesn't return any objects",
2039 key: "/pods/test-ns/",
2040 recursive: false,
2041 expectedOut: []example.Pod{},
2042 },
2043 {
2044 name: "Recursive on resource prefix returns objects in the namespace",
2045 key: "/pods/test-ns/",
2046 recursive: true,
2047 expectedOut: []example.Pod{*fooObj, *fooBarObj},
2048 },
2049 {
2050 name: "NonRecursive on object key (prefix) returns object and no other objects with the same prefix",
2051 key: fooKey,
2052 recursive: false,
2053 expectedOut: []example.Pod{*fooObj},
2054 },
2055 {
2056 name: "Recursive on object key (prefix) doesn't return anything",
2057 key: fooKey,
2058 recursive: true,
2059 expectedOut: []example.Pod{},
2060 },
2061 {
2062 name: "NonRecursive on object key (no-prefix) return object",
2063 key: fooBarKey,
2064 recursive: false,
2065 expectedOut: []example.Pod{*fooBarObj},
2066 },
2067 {
2068 name: "Recursive on object key (no-prefix) doesn't return anything",
2069 key: fooBarKey,
2070 recursive: true,
2071 expectedOut: []example.Pod{},
2072 },
2073 }
2074
2075 listTypes := []struct {
2076 name string
2077 ResourceVersion string
2078 Match metav1.ResourceVersionMatch
2079 }{
2080 {
2081 name: "Exact",
2082 ResourceVersion: lastRev,
2083 Match: metav1.ResourceVersionMatchExact,
2084 },
2085 {
2086 name: "Consistent",
2087 ResourceVersion: "",
2088 },
2089 {
2090 name: "NotOlderThan",
2091 ResourceVersion: "0",
2092 Match: metav1.ResourceVersionMatchNotOlderThan,
2093 },
2094 }
2095
2096 for _, listType := range listTypes {
2097 listType := listType
2098 t.Run(listType.name, func(t *testing.T) {
2099 for _, tt := range tests {
2100 tt := tt
2101 t.Run(tt.name, func(t *testing.T) {
2102 out := &example.PodList{}
2103 storageOpts := storage.ListOptions{
2104 ResourceVersion: listType.ResourceVersion,
2105 ResourceVersionMatch: listType.Match,
2106 Recursive: tt.recursive,
2107 Predicate: storage.Everything,
2108 }
2109 err := store.GetList(ctx, tt.key, storageOpts, out)
2110 if err != nil {
2111 t.Fatalf("GetList failed: %v", err)
2112 }
2113 expectNoDiff(t, "incorrect list pods", tt.expectedOut, out.Items)
2114 })
2115 }
2116 })
2117 }
2118 }
2119
2120 type CallsValidation func(t *testing.T, pageSize, estimatedProcessedObjects uint64)
2121
2122 func RunTestListContinuation(ctx context.Context, t *testing.T, store storage.Interface, validation CallsValidation) {
2123 // Setup storage with the following structure:
2124 // /
2125 // - first/
2126 // | - bar
2127 // |
2128 // - second/
2129 // | - bar
2130 // | - foo
2131 barFirst := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "first", Name: "bar"}}
2132 barSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "bar"}}
2133 fooSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "foo"}}
2134 preset := []struct {
2135 key string
2136 obj *example.Pod
2137 storedObj *example.Pod
2138 }{
2139 {
2140 key: computePodKey(barFirst),
2141 obj: barFirst,
2142 },
2143 {
2144 key: computePodKey(barSecond),
2145 obj: barSecond,
2146 },
2147 {
2148 key: computePodKey(fooSecond),
2149 obj: fooSecond,
2150 },
2151 }
2152
2153 var currentRV string
2154 for i, ps := range preset {
2155 preset[i].storedObj = &example.Pod{}
2156 err := store.Create(ctx, ps.key, ps.obj, preset[i].storedObj, 0)
2157 if err != nil {
2158 t.Fatalf("Set failed: %v", err)
2159 }
2160 currentRV = preset[i].storedObj.ResourceVersion
2161 }
2162
2163 // test continuations
2164 out := &example.PodList{}
2165 pred := func(limit int64, continueValue string) storage.SelectionPredicate {
2166 return storage.SelectionPredicate{
2167 Limit: limit,
2168 Continue: continueValue,
2169 Label: labels.Everything(),
2170 Field: fields.Everything(),
2171 GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
2172 pod := obj.(*example.Pod)
2173 return nil, fields.Set{"metadata.name": pod.Name}, nil
2174 },
2175 }
2176 }
2177 options := storage.ListOptions{
2178 // Limit is ignored when ResourceVersion is set to 0.
2179 // Set it to consistent read.
2180 ResourceVersion: "",
2181 Predicate: pred(1, ""),
2182 Recursive: true,
2183 }
2184 if err := store.GetList(ctx, "/pods", options, out); err != nil {
2185 t.Fatalf("Unable to get initial list: %v", err)
2186 }
2187 if len(out.Continue) == 0 {
2188 t.Fatalf("No continuation token set")
2189 }
2190 expectNoDiff(t, "incorrect first page", []example.Pod{*preset[0].storedObj}, out.Items)
2191 if out.ResourceVersion != currentRV {
2192 t.Errorf("Expect output.ResourceVersion = %s, but got %s", currentRV, out.ResourceVersion)
2193 }
2194 if validation != nil {
2195 validation(t, 1, 1)
2196 }
2197
2198 continueFromSecondItem := out.Continue
2199
2200 // no limit, should get two items
2201 out = &example.PodList{}
2202 options = storage.ListOptions{
2203 // ResourceVersion should be unset when setting continuation token.
2204 ResourceVersion: "",
2205 Predicate: pred(0, continueFromSecondItem),
2206 Recursive: true,
2207 }
2208 if err := store.GetList(ctx, "/pods", options, out); err != nil {
2209 t.Fatalf("Unable to get second page: %v", err)
2210 }
2211 if len(out.Continue) != 0 {
2212 t.Fatalf("Unexpected continuation token set")
2213 }
2214 key, rv, err := storage.DecodeContinue(continueFromSecondItem, "/pods")
2215 t.Logf("continue token was %d %s %v", rv, key, err)
2216 expectNoDiff(t, "incorrect second page", []example.Pod{*preset[1].storedObj, *preset[2].storedObj}, out.Items)
2217 if out.ResourceVersion != currentRV {
2218 t.Errorf("Expect output.ResourceVersion = %s, but got %s", currentRV, out.ResourceVersion)
2219 }
2220 if validation != nil {
2221 validation(t, 0, 2)
2222 }
2223
2224 // limit, should get two more pages
2225 out = &example.PodList{}
2226 options = storage.ListOptions{
2227 // ResourceVersion should be unset when setting continuation token.
2228 ResourceVersion: "",
2229 Predicate: pred(1, continueFromSecondItem),
2230 Recursive: true,
2231 }
2232 if err := store.GetList(ctx, "/pods", options, out); err != nil {
2233 t.Fatalf("Unable to get second page: %v", err)
2234 }
2235 if len(out.Continue) == 0 {
2236 t.Fatalf("No continuation token set")
2237 }
2238 expectNoDiff(t, "incorrect second page", []example.Pod{*preset[1].storedObj}, out.Items)
2239 if out.ResourceVersion != currentRV {
2240 t.Errorf("Expect output.ResourceVersion = %s, but got %s", currentRV, out.ResourceVersion)
2241 }
2242 if validation != nil {
2243 validation(t, 1, 1)
2244 }
2245
2246 continueFromThirdItem := out.Continue
2247
2248 out = &example.PodList{}
2249 options = storage.ListOptions{
2250 // ResourceVersion should be unset when setting continuation token.
2251 ResourceVersion: "",
2252 Predicate: pred(1, continueFromThirdItem),
2253 Recursive: true,
2254 }
2255 if err := store.GetList(ctx, "/pods", options, out); err != nil {
2256 t.Fatalf("Unable to get second page: %v", err)
2257 }
2258 if len(out.Continue) != 0 {
2259 t.Fatalf("Unexpected continuation token set")
2260 }
2261 expectNoDiff(t, "incorrect third page", []example.Pod{*preset[2].storedObj}, out.Items)
2262 if out.ResourceVersion != currentRV {
2263 t.Errorf("Expect output.ResourceVersion = %s, but got %s", currentRV, out.ResourceVersion)
2264 }
2265 if validation != nil {
2266 validation(t, 1, 1)
2267 }
2268 }
2269
2270 func RunTestListPaginationRareObject(ctx context.Context, t *testing.T, store storage.Interface, validation CallsValidation) {
2271 podCount := 1000
2272 var pods []*example.Pod
2273 for i := 0; i < podCount; i++ {
2274 obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("pod-%d", i)}}
2275 key := computePodKey(obj)
2276 storedObj := &example.Pod{}
2277 err := store.Create(ctx, key, obj, storedObj, 0)
2278 if err != nil {
2279 t.Fatalf("Set failed: %v", err)
2280 }
2281 pods = append(pods, storedObj)
2282 }
2283
2284 out := &example.PodList{}
2285 options := storage.ListOptions{
2286 Predicate: storage.SelectionPredicate{
2287 Limit: 1,
2288 Label: labels.Everything(),
2289 Field: fields.OneTermEqualSelector("metadata.name", "pod-999"),
2290 GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
2291 pod := obj.(*example.Pod)
2292 return nil, fields.Set{"metadata.name": pod.Name}, nil
2293 },
2294 },
2295 Recursive: true,
2296 }
2297 if err := store.GetList(ctx, "/pods", options, out); err != nil {
2298 t.Fatalf("Unable to get initial list: %v", err)
2299 }
2300 if len(out.Continue) != 0 {
2301 t.Errorf("Unexpected continuation token set")
2302 }
2303 if len(out.Items) != 1 || !reflect.DeepEqual(&out.Items[0], pods[999]) {
2304 t.Fatalf("Unexpected first page: %#v", out.Items)
2305 }
2306 if validation != nil {
2307 validation(t, 1, uint64(podCount))
2308 }
2309 }
2310
2311 func RunTestListContinuationWithFilter(ctx context.Context, t *testing.T, store storage.Interface, validation CallsValidation) {
2312 foo1 := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "1", Name: "foo"}}
2313 bar2 := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "2", Name: "bar"}} // this should not match
2314 foo3 := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "3", Name: "foo"}}
2315 foo4 := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "4", Name: "foo"}}
2316 preset := []struct {
2317 key string
2318 obj *example.Pod
2319 storedObj *example.Pod
2320 }{
2321 {
2322 key: computePodKey(foo1),
2323 obj: foo1,
2324 },
2325 {
2326 key: computePodKey(bar2),
2327 obj: bar2,
2328 },
2329 {
2330 key: computePodKey(foo3),
2331 obj: foo3,
2332 },
2333 {
2334 key: computePodKey(foo4),
2335 obj: foo4,
2336 },
2337 }
2338
2339 var currentRV string
2340 for i, ps := range preset {
2341 preset[i].storedObj = &example.Pod{}
2342 err := store.Create(ctx, ps.key, ps.obj, preset[i].storedObj, 0)
2343 if err != nil {
2344 t.Fatalf("Set failed: %v", err)
2345 }
2346 currentRV = preset[i].storedObj.ResourceVersion
2347 }
2348
2349 // the first list call should try to get 2 items from etcd (and only those items should be returned)
2350 // the field selector should result in it reading 3 items via the transformer
2351 // the chunking should result in 2 etcd Gets
2352 // there should be a continueValue because there is more data
2353 out := &example.PodList{}
2354 pred := func(limit int64, continueValue string) storage.SelectionPredicate {
2355 return storage.SelectionPredicate{
2356 Limit: limit,
2357 Continue: continueValue,
2358 Label: labels.Everything(),
2359 Field: fields.OneTermNotEqualSelector("metadata.name", "bar"),
2360 GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
2361 pod := obj.(*example.Pod)
2362 return nil, fields.Set{"metadata.name": pod.Name}, nil
2363 },
2364 }
2365 }
2366 options := storage.ListOptions{
2367 // Limit is ignored when ResourceVersion is set to 0.
2368 // Set it to consistent read.
2369 ResourceVersion: "",
2370 Predicate: pred(2, ""),
2371 Recursive: true,
2372 }
2373 if err := store.GetList(ctx, "/pods", options, out); err != nil {
2374 t.Errorf("Unable to get initial list: %v", err)
2375 }
2376 if len(out.Continue) == 0 {
2377 t.Errorf("No continuation token set")
2378 }
2379 expectNoDiff(t, "incorrect first page", []example.Pod{*preset[0].storedObj, *preset[2].storedObj}, out.Items)
2380 if out.ResourceVersion != currentRV {
2381 t.Errorf("Expect output.ResourceVersion = %s, but got %s", currentRV, out.ResourceVersion)
2382 }
2383 if validation != nil {
2384 validation(t, 2, 3)
2385 }
2386
2387 // the rest of the test does not make sense if the previous call failed
2388 if t.Failed() {
2389 return
2390 }
2391
2392 cont := out.Continue
2393
2394 // the second list call should try to get 2 more items from etcd
2395 // but since there is only one item left, that is all we should get with no continueValue
2396 // both read counters should be incremented for the singular calls they make in this case
2397 out = &example.PodList{}
2398 options = storage.ListOptions{
2399 // ResourceVersion should be unset when setting continuation token.
2400 ResourceVersion: "",
2401 Predicate: pred(2, cont),
2402 Recursive: true,
2403 }
2404 if err := store.GetList(ctx, "/pods", options, out); err != nil {
2405 t.Errorf("Unable to get second page: %v", err)
2406 }
2407 if len(out.Continue) != 0 {
2408 t.Errorf("Unexpected continuation token set")
2409 }
2410 expectNoDiff(t, "incorrect second page", []example.Pod{*preset[3].storedObj}, out.Items)
2411 if out.ResourceVersion != currentRV {
2412 t.Errorf("Expect output.ResourceVersion = %s, but got %s", currentRV, out.ResourceVersion)
2413 }
2414 if validation != nil {
2415 validation(t, 2, 1)
2416 }
2417 }
2418
2419 type Compaction func(ctx context.Context, t *testing.T, resourceVersion string)
2420 type IncreaseRVFunc func(ctx context.Context, t *testing.T)
2421
2422 func RunTestListInconsistentContinuation(ctx context.Context, t *testing.T, store storage.Interface, compaction Compaction) {
2423 // Setup storage with the following structure:
2424 // /
2425 // - first/
2426 // | - bar
2427 // |
2428 // - second/
2429 // | - bar
2430 // | - foo
2431 barFirst := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "first", Name: "bar"}}
2432 barSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "bar"}}
2433 fooSecond := &example.Pod{ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "foo"}}
2434 preset := []struct {
2435 key string
2436 obj *example.Pod
2437 storedObj *example.Pod
2438 }{
2439 {
2440 key: computePodKey(barFirst),
2441 obj: barFirst,
2442 },
2443 {
2444 key: computePodKey(barSecond),
2445 obj: barSecond,
2446 },
2447 {
2448 key: computePodKey(fooSecond),
2449 obj: fooSecond,
2450 },
2451 }
2452 for i, ps := range preset {
2453 preset[i].storedObj = &example.Pod{}
2454 err := store.Create(ctx, ps.key, ps.obj, preset[i].storedObj, 0)
2455 if err != nil {
2456 t.Fatalf("Set failed: %v", err)
2457 }
2458 }
2459
2460 pred := func(limit int64, continueValue string) storage.SelectionPredicate {
2461 return storage.SelectionPredicate{
2462 Limit: limit,
2463 Continue: continueValue,
2464 Label: labels.Everything(),
2465 Field: fields.Everything(),
2466 GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
2467 pod := obj.(*example.Pod)
2468 return nil, fields.Set{"metadata.name": pod.Name}, nil
2469 },
2470 }
2471 }
2472
2473 out := &example.PodList{}
2474 options := storage.ListOptions{
2475 ResourceVersion: "0",
2476 Predicate: pred(1, ""),
2477 Recursive: true,
2478 }
2479 if err := store.GetList(ctx, "/pods", options, out); err != nil {
2480 t.Fatalf("Unable to get initial list: %v", err)
2481 }
2482 if len(out.Continue) == 0 {
2483 t.Fatalf("No continuation token set")
2484 }
2485 expectNoDiff(t, "incorrect first page", []example.Pod{*preset[0].storedObj}, out.Items)
2486
2487 continueFromSecondItem := out.Continue
2488
2489 // update /second/bar
2490 oldName := preset[2].obj.Name
2491 newPod := &example.Pod{
2492 ObjectMeta: metav1.ObjectMeta{
2493 Name: oldName,
2494 Labels: map[string]string{
2495 "state": "new",
2496 },
2497 },
2498 }
2499 if err := store.GuaranteedUpdate(ctx, preset[2].key, preset[2].storedObj, false, nil,
2500 func(_ runtime.Object, _ storage.ResponseMeta) (runtime.Object, *uint64, error) {
2501 return newPod, nil, nil
2502 }, newPod); err != nil {
2503 t.Fatalf("update failed: %v", err)
2504 }
2505
2506 // compact to latest revision.
2507 lastRVString := preset[2].storedObj.ResourceVersion
2508 compaction(ctx, t, lastRVString)
2509
2510 // The old continue token should have expired
2511 options = storage.ListOptions{
2512 ResourceVersion: "0",
2513 Predicate: pred(0, continueFromSecondItem),
2514 Recursive: true,
2515 }
2516 err := store.GetList(ctx, "/pods", options, out)
2517 if err == nil {
2518 t.Fatalf("unexpected no error")
2519 }
2520 if !strings.Contains(err.Error(), "The provided continue parameter is too old ") {
2521 t.Fatalf("unexpected error message %v", err)
2522 }
2523 status, ok := err.(apierrors.APIStatus)
2524 if !ok {
2525 t.Fatalf("expect error of implements the APIStatus interface, got %v", reflect.TypeOf(err))
2526 }
2527 inconsistentContinueFromSecondItem := status.Status().ListMeta.Continue
2528 if len(inconsistentContinueFromSecondItem) == 0 {
2529 t.Fatalf("expect non-empty continue token")
2530 }
2531
2532 out = &example.PodList{}
2533 options = storage.ListOptions{
2534 ResourceVersion: "0",
2535 Predicate: pred(1, inconsistentContinueFromSecondItem),
2536 Recursive: true,
2537 }
2538 if err := store.GetList(ctx, "/pods", options, out); err != nil {
2539 t.Fatalf("Unable to get second page: %v", err)
2540 }
2541 if len(out.Continue) == 0 {
2542 t.Fatalf("No continuation token set")
2543 }
2544 validateResourceVersion := resourceVersionNotOlderThan(lastRVString)
2545 expectNoDiff(t, "incorrect second page", []example.Pod{*preset[1].storedObj}, out.Items)
2546 if err := validateResourceVersion(out.ResourceVersion); err != nil {
2547 t.Fatal(err)
2548 }
2549 continueFromThirdItem := out.Continue
2550 resolvedResourceVersionFromThirdItem := out.ResourceVersion
2551 out = &example.PodList{}
2552 options = storage.ListOptions{
2553 ResourceVersion: "0",
2554 Predicate: pred(1, continueFromThirdItem),
2555 Recursive: true,
2556 }
2557 if err := store.GetList(ctx, "/pods", options, out); err != nil {
2558 t.Fatalf("Unable to get second page: %v", err)
2559 }
2560 if len(out.Continue) != 0 {
2561 t.Fatalf("Unexpected continuation token set")
2562 }
2563 expectNoDiff(t, "incorrect third page", []example.Pod{*preset[2].storedObj}, out.Items)
2564 if out.ResourceVersion != resolvedResourceVersionFromThirdItem {
2565 t.Fatalf("Expected list resource version to be %s, got %s", resolvedResourceVersionFromThirdItem, out.ResourceVersion)
2566 }
2567 }
2568
2569 type PrefixTransformerModifier func(*PrefixTransformer) value.Transformer
2570
2571 type InterfaceWithPrefixTransformer interface {
2572 storage.Interface
2573
2574 UpdatePrefixTransformer(PrefixTransformerModifier) func()
2575 }
2576
2577 type InterfaceWithCorruptTransformer interface {
2578 storage.Interface
2579 CorruptTransformer() func()
2580 }
2581
2582 func RunTestListResourceVersionMatch(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) {
2583 nextPod := func(index uint32) (string, *example.Pod) {
2584 obj := &example.Pod{
2585 ObjectMeta: metav1.ObjectMeta{
2586 Name: fmt.Sprintf("pod-%d", index),
2587 Labels: map[string]string{
2588 "even": strconv.FormatBool(index%2 == 0),
2589 },
2590 },
2591 }
2592 return computePodKey(obj), obj
2593 }
2594
2595 transformer := &reproducingTransformer{
2596 store: store,
2597 nextObject: nextPod,
2598 }
2599
2600 revertTransformer := store.UpdatePrefixTransformer(
2601 func(previousTransformer *PrefixTransformer) value.Transformer {
2602 transformer.wrapped = previousTransformer
2603 return transformer
2604 })
2605 defer revertTransformer()
2606
2607 for i := 0; i < 5; i++ {
2608 if err := transformer.createObject(ctx); err != nil {
2609 t.Fatalf("failed to create object: %v", err)
2610 }
2611 }
2612
2613 getAttrs := func(obj runtime.Object) (labels.Set, fields.Set, error) {
2614 pod, ok := obj.(*example.Pod)
2615 if !ok {
2616 return nil, nil, fmt.Errorf("invalid object")
2617 }
2618 return labels.Set(pod.Labels), nil, nil
2619 }
2620 predicate := storage.SelectionPredicate{
2621 Label: labels.Set{"even": "true"}.AsSelector(),
2622 GetAttrs: getAttrs,
2623 Limit: 4,
2624 }
2625
2626 result1 := example.PodList{}
2627 options := storage.ListOptions{
2628 Predicate: predicate,
2629 Recursive: true,
2630 }
2631 if err := store.GetList(ctx, "/pods", options, &result1); err != nil {
2632 t.Fatalf("failed to list objects: %v", err)
2633 }
2634
2635 // List objects from the returned resource version.
2636 options = storage.ListOptions{
2637 Predicate: predicate,
2638 ResourceVersion: result1.ResourceVersion,
2639 ResourceVersionMatch: metav1.ResourceVersionMatchExact,
2640 Recursive: true,
2641 }
2642
2643 result2 := example.PodList{}
2644 if err := store.GetList(ctx, "/pods", options, &result2); err != nil {
2645 t.Fatalf("failed to list objects: %v", err)
2646 }
2647
2648 expectNoDiff(t, "incorrect lists", result1, result2)
2649
2650 // Now also verify the ResourceVersionMatchNotOlderThan.
2651 options.ResourceVersionMatch = metav1.ResourceVersionMatchNotOlderThan
2652
2653 result3 := example.PodList{}
2654 if err := store.GetList(ctx, "/pods", options, &result3); err != nil {
2655 t.Fatalf("failed to list objects: %v", err)
2656 }
2657
2658 options.ResourceVersion = result3.ResourceVersion
2659 options.ResourceVersionMatch = metav1.ResourceVersionMatchExact
2660
2661 result4 := example.PodList{}
2662 if err := store.GetList(ctx, "/pods", options, &result4); err != nil {
2663 t.Fatalf("failed to list objects: %v", err)
2664 }
2665
2666 expectNoDiff(t, "incorrect lists", result3, result4)
2667 }
2668
2669 func RunTestGuaranteedUpdate(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer, validation KeyValidation) {
2670 inputObj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns", UID: "A"}}
2671 key := computePodKey(inputObj)
2672
2673 tests := []struct {
2674 name string
2675 key string
2676 ignoreNotFound bool
2677 precondition *storage.Preconditions
2678 expectNotFoundErr bool
2679 expectInvalidObjErr bool
2680 expectNoUpdate bool
2681 transformStale bool
2682 hasSelfLink bool
2683 }{{
2684 name: "non-existing key, ignoreNotFound=false",
2685 key: "/non-existing",
2686 ignoreNotFound: false,
2687 precondition: nil,
2688 expectNotFoundErr: true,
2689 expectInvalidObjErr: false,
2690 expectNoUpdate: false,
2691 }, {
2692 name: "non-existing key, ignoreNotFound=true",
2693 key: "/non-existing",
2694 ignoreNotFound: true,
2695 precondition: nil,
2696 expectNotFoundErr: false,
2697 expectInvalidObjErr: false,
2698 expectNoUpdate: false,
2699 }, {
2700 name: "existing key",
2701 key: key,
2702 ignoreNotFound: false,
2703 precondition: nil,
2704 expectNotFoundErr: false,
2705 expectInvalidObjErr: false,
2706 expectNoUpdate: false,
2707 }, {
2708 name: "same data",
2709 key: key,
2710 ignoreNotFound: false,
2711 precondition: nil,
2712 expectNotFoundErr: false,
2713 expectInvalidObjErr: false,
2714 expectNoUpdate: true,
2715 }, {
2716 name: "same data, a selfLink",
2717 key: key,
2718 ignoreNotFound: false,
2719 precondition: nil,
2720 expectNotFoundErr: false,
2721 expectInvalidObjErr: false,
2722 expectNoUpdate: true,
2723 hasSelfLink: true,
2724 }, {
2725 name: "same data, stale",
2726 key: key,
2727 ignoreNotFound: false,
2728 precondition: nil,
2729 expectNotFoundErr: false,
2730 expectInvalidObjErr: false,
2731 expectNoUpdate: false,
2732 transformStale: true,
2733 }, {
2734 name: "UID match",
2735 key: key,
2736 ignoreNotFound: false,
2737 precondition: storage.NewUIDPreconditions("A"),
2738 expectNotFoundErr: false,
2739 expectInvalidObjErr: false,
2740 expectNoUpdate: true,
2741 }, {
2742 name: "UID mismatch",
2743 key: key,
2744 ignoreNotFound: false,
2745 precondition: storage.NewUIDPreconditions("B"),
2746 expectNotFoundErr: false,
2747 expectInvalidObjErr: true,
2748 expectNoUpdate: true,
2749 }}
2750
2751 for i, tt := range tests {
2752 t.Run(tt.name, func(t *testing.T) {
2753 key, storeObj := testPropagateStore(ctx, t, store, inputObj)
2754
2755 out := &example.Pod{}
2756 annotations := map[string]string{"version": fmt.Sprintf("%d", i)}
2757 if tt.expectNoUpdate {
2758 annotations = nil
2759 }
2760
2761 if tt.transformStale {
2762 revertTransformer := store.UpdatePrefixTransformer(
2763 func(transformer *PrefixTransformer) value.Transformer {
2764 transformer.stale = true
2765 return transformer
2766 })
2767 defer revertTransformer()
2768 }
2769
2770 version := storeObj.ResourceVersion
2771 err := store.GuaranteedUpdate(ctx, tt.key, out, tt.ignoreNotFound, tt.precondition,
2772 storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
2773 if tt.expectNotFoundErr && tt.ignoreNotFound {
2774 if pod := obj.(*example.Pod); pod.Name != "" {
2775 t.Errorf("%s: expecting zero value, but get=%#v", tt.name, pod)
2776 }
2777 }
2778 pod := *storeObj
2779 if tt.hasSelfLink {
2780 pod.SelfLink = "testlink"
2781 }
2782 pod.Annotations = annotations
2783 return &pod, nil
2784 }), nil)
2785
2786 if tt.expectNotFoundErr {
2787 if err == nil || !storage.IsNotFound(err) {
2788 t.Errorf("%s: expecting not found error, but get: %v", tt.name, err)
2789 }
2790 return
2791 }
2792 if tt.expectInvalidObjErr {
2793 if err == nil || !storage.IsInvalidObj(err) {
2794 t.Errorf("%s: expecting invalid UID error, but get: %s", tt.name, err)
2795 }
2796 return
2797 }
2798 if err != nil {
2799 t.Fatalf("%s: GuaranteedUpdate failed: %v", tt.name, err)
2800 }
2801 if !reflect.DeepEqual(out.ObjectMeta.Annotations, annotations) {
2802 t.Errorf("%s: pod annotations want=%s, get=%s", tt.name, annotations, out.ObjectMeta.Annotations)
2803 }
2804 if out.SelfLink != "" {
2805 t.Errorf("%s: selfLink should not be set", tt.name)
2806 }
2807
2808 // verify that kv pair is not empty after set and that the underlying data matches expectations
2809 validation(ctx, t, key)
2810
2811 switch tt.expectNoUpdate {
2812 case true:
2813 if version != out.ResourceVersion {
2814 t.Errorf("%s: expect no version change, before=%s, after=%s", tt.name, version, out.ResourceVersion)
2815 }
2816 case false:
2817 if version == out.ResourceVersion {
2818 t.Errorf("%s: expect version change, but get the same version=%s", tt.name, version)
2819 }
2820 }
2821 })
2822 }
2823 }
2824
2825 func RunTestGuaranteedUpdateWithTTL(ctx context.Context, t *testing.T, store storage.Interface) {
2826 input := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns"}}
2827 key := computePodKey(input)
2828
2829 out := &example.Pod{}
2830 err := store.GuaranteedUpdate(ctx, key, out, true, nil,
2831 func(_ runtime.Object, _ storage.ResponseMeta) (runtime.Object, *uint64, error) {
2832 ttl := uint64(1)
2833 return input, &ttl, nil
2834 }, nil)
2835 if err != nil {
2836 t.Fatalf("Create failed: %v", err)
2837 }
2838
2839 w, err := store.Watch(ctx, key, storage.ListOptions{ResourceVersion: out.ResourceVersion, Predicate: storage.Everything})
2840 if err != nil {
2841 t.Fatalf("Watch failed: %v", err)
2842 }
2843 testCheckEventType(t, w, watch.Deleted)
2844 }
2845
2846 func RunTestGuaranteedUpdateChecksStoredData(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) {
2847 input := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns"}}
2848 key := computePodKey(input)
2849
2850 // serialize input into etcd with data that would be normalized by a write -
2851 // in this case, leading whitespace
2852 revertTransformer := store.UpdatePrefixTransformer(
2853 func(transformer *PrefixTransformer) value.Transformer {
2854 transformer.prefix = []byte(string(transformer.prefix) + " ")
2855 return transformer
2856 })
2857 _, initial := testPropagateStore(ctx, t, store, input)
2858 revertTransformer()
2859
2860 // this update should write the canonical value to etcd because the new serialization differs
2861 // from the stored serialization
2862 input.ResourceVersion = initial.ResourceVersion
2863 out := &example.Pod{}
2864 err := store.GuaranteedUpdate(ctx, key, out, true, nil,
2865 func(_ runtime.Object, _ storage.ResponseMeta) (runtime.Object, *uint64, error) {
2866 return input, nil, nil
2867 }, input)
2868 if err != nil {
2869 t.Fatalf("Update failed: %v", err)
2870 }
2871 if out.ResourceVersion == initial.ResourceVersion {
2872 t.Errorf("guaranteed update should have updated the serialized data, got %#v", out)
2873 }
2874
2875 lastVersion := out.ResourceVersion
2876
2877 // this update should not write to etcd because the input matches the stored data
2878 input = out
2879 out = &example.Pod{}
2880 err = store.GuaranteedUpdate(ctx, key, out, true, nil,
2881 func(_ runtime.Object, _ storage.ResponseMeta) (runtime.Object, *uint64, error) {
2882 return input, nil, nil
2883 }, input)
2884 if err != nil {
2885 t.Fatalf("Update failed: %v", err)
2886 }
2887 if out.ResourceVersion != lastVersion {
2888 t.Errorf("guaranteed update should have short-circuited write, got %#v", out)
2889 }
2890
2891 revertTransformer = store.UpdatePrefixTransformer(
2892 func(transformer *PrefixTransformer) value.Transformer {
2893 transformer.stale = true
2894 return transformer
2895 })
2896 defer revertTransformer()
2897
2898 // this update should write to etcd because the transformer reported stale
2899 err = store.GuaranteedUpdate(ctx, key, out, true, nil,
2900 func(_ runtime.Object, _ storage.ResponseMeta) (runtime.Object, *uint64, error) {
2901 return input, nil, nil
2902 }, input)
2903 if err != nil {
2904 t.Fatalf("Update failed: %v", err)
2905 }
2906 if out.ResourceVersion == lastVersion {
2907 t.Errorf("guaranteed update should have written to etcd when transformer reported stale, got %#v", out)
2908 }
2909 }
2910
2911 func RunTestGuaranteedUpdateWithConflict(ctx context.Context, t *testing.T, store storage.Interface) {
2912 key, _ := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns"}})
2913
2914 errChan := make(chan error, 1)
2915 var firstToFinish sync.WaitGroup
2916 var secondToEnter sync.WaitGroup
2917 firstToFinish.Add(1)
2918 secondToEnter.Add(1)
2919
2920 go func() {
2921 err := store.GuaranteedUpdate(ctx, key, &example.Pod{}, false, nil,
2922 storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
2923 pod := obj.(*example.Pod)
2924 pod.Name = "foo-1"
2925 secondToEnter.Wait()
2926 return pod, nil
2927 }), nil)
2928 firstToFinish.Done()
2929 errChan <- err
2930 }()
2931
2932 updateCount := 0
2933 err := store.GuaranteedUpdate(ctx, key, &example.Pod{}, false, nil,
2934 storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
2935 if updateCount == 0 {
2936 secondToEnter.Done()
2937 firstToFinish.Wait()
2938 }
2939 updateCount++
2940 pod := obj.(*example.Pod)
2941 pod.Name = "foo-2"
2942 return pod, nil
2943 }), nil)
2944 if err != nil {
2945 t.Fatalf("Second GuaranteedUpdate error %#v", err)
2946 }
2947 if err := <-errChan; err != nil {
2948 t.Fatalf("First GuaranteedUpdate error %#v", err)
2949 }
2950
2951 if updateCount != 2 {
2952 t.Errorf("Should have conflict and called update func twice")
2953 }
2954 }
2955
2956 func RunTestGuaranteedUpdateWithSuggestionAndConflict(ctx context.Context, t *testing.T, store storage.Interface) {
2957 key, originalPod := testPropagateStore(ctx, t, store, &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "test-ns"}})
2958
2959 // First, update without a suggestion so originalPod is outdated
2960 updatedPod := &example.Pod{}
2961 err := store.GuaranteedUpdate(ctx, key, updatedPod, false, nil,
2962 storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
2963 pod := obj.(*example.Pod)
2964 pod.Generation = 2
2965 return pod, nil
2966 }),
2967 nil,
2968 )
2969 if err != nil {
2970 t.Fatalf("unexpected error: %v", err)
2971 }
2972
2973 // Second, update using the outdated originalPod as the suggestion. Return a conflict error when
2974 // passed originalPod, and make sure that SimpleUpdate is called a second time after a live lookup
2975 // with the value of updatedPod.
2976 sawConflict := false
2977 updatedPod2 := &example.Pod{}
2978 err = store.GuaranteedUpdate(ctx, key, updatedPod2, false, nil,
2979 storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
2980 pod := obj.(*example.Pod)
2981 if pod.Generation != 2 {
2982 if sawConflict {
2983 t.Fatalf("unexpected second conflict")
2984 }
2985 sawConflict = true
2986 // simulated stale object - return a conflict
2987 return nil, apierrors.NewConflict(example.SchemeGroupVersion.WithResource("pods").GroupResource(), "name", errors.New("foo"))
2988 }
2989 pod.Generation = 3
2990 return pod, nil
2991 }),
2992 originalPod,
2993 )
2994 if err != nil {
2995 t.Fatalf("unexpected error: %v", err)
2996 }
2997 if updatedPod2.Generation != 3 {
2998 t.Errorf("unexpected pod generation: %q", updatedPod2.Generation)
2999 }
3000
3001 // Third, update using a current version as the suggestion.
3002 // Return an error and make sure that SimpleUpdate is NOT called a second time,
3003 // since the live lookup shows the suggestion was already up to date.
3004 attempts := 0
3005 updatedPod3 := &example.Pod{}
3006 err = store.GuaranteedUpdate(ctx, key, updatedPod3, false, nil,
3007 storage.SimpleUpdate(func(obj runtime.Object) (runtime.Object, error) {
3008 pod := obj.(*example.Pod)
3009 if pod.Generation != updatedPod2.Generation || pod.ResourceVersion != updatedPod2.ResourceVersion {
3010 t.Logf("stale object (rv=%s), expected rv=%s", pod.ResourceVersion, updatedPod2.ResourceVersion)
3011 }
3012 attempts++
3013 return nil, fmt.Errorf("validation or admission error")
3014 }),
3015 updatedPod2,
3016 )
3017 if err == nil {
3018 t.Fatalf("expected error, got none")
3019 }
3020 // Implementations of the storage interface are allowed to ignore the suggestion,
3021 // in which case two attempts are possible.
3022 if attempts > 2 {
3023 t.Errorf("update function should have been called at most twice, called %d", attempts)
3024 }
3025 }
3026
3027 func RunTestTransformationFailure(ctx context.Context, t *testing.T, store InterfaceWithPrefixTransformer) {
3028 barFirst := &example.Pod{
3029 ObjectMeta: metav1.ObjectMeta{Namespace: "first", Name: "bar"},
3030 Spec: DeepEqualSafePodSpec(),
3031 }
3032 bazSecond := &example.Pod{
3033 ObjectMeta: metav1.ObjectMeta{Namespace: "second", Name: "baz"},
3034 Spec: DeepEqualSafePodSpec(),
3035 }
3036
3037 preset := []struct {
3038 key string
3039 obj *example.Pod
3040 storedObj *example.Pod
3041 }{{
3042 key: computePodKey(barFirst),
3043 obj: barFirst,
3044 }, {
3045 key: computePodKey(bazSecond),
3046 obj: bazSecond,
3047 }}
3048 for i, ps := range preset[:1] {
3049 preset[i].storedObj = &example.Pod{}
3050 err := store.Create(ctx, ps.key, ps.obj, preset[:1][i].storedObj, 0)
3051 if err != nil {
3052 t.Fatalf("Set failed: %v", err)
3053 }
3054 }
3055
3056 // create a second resource with an invalid prefix
3057 revertTransformer := store.UpdatePrefixTransformer(
3058 func(transformer *PrefixTransformer) value.Transformer {
3059 return NewPrefixTransformer([]byte("otherprefix!"), false)
3060 })
3061 for i, ps := range preset[1:] {
3062 preset[1:][i].storedObj = &example.Pod{}
3063 err := store.Create(ctx, ps.key, ps.obj, preset[1:][i].storedObj, 0)
3064 if err != nil {
3065 t.Fatalf("Set failed: %v", err)
3066 }
3067 }
3068 revertTransformer()
3069
3070 // List should fail
3071 var got example.PodList
3072 storageOpts := storage.ListOptions{
3073 Predicate: storage.Everything,
3074 Recursive: true,
3075 }
3076 if err := store.GetList(ctx, "/pods", storageOpts, &got); !storage.IsInternalError(err) {
3077 t.Errorf("Unexpected error %v", err)
3078 }
3079
3080 // Get should fail
3081 if err := store.Get(ctx, preset[1].key, storage.GetOptions{}, &example.Pod{}); !storage.IsInternalError(err) {
3082 t.Errorf("Unexpected error: %v", err)
3083 }
3084
3085 updateFunc := func(input runtime.Object, res storage.ResponseMeta) (runtime.Object, *uint64, error) {
3086 return input, nil, nil
3087 }
3088 // GuaranteedUpdate without suggestion should return an error
3089 if err := store.GuaranteedUpdate(ctx, preset[1].key, &example.Pod{}, false, nil, updateFunc, nil); !storage.IsInternalError(err) {
3090 t.Errorf("Unexpected error: %v", err)
3091 }
3092 // GuaranteedUpdate with suggestion should return an error if we don't change the object
3093 if err := store.GuaranteedUpdate(ctx, preset[1].key, &example.Pod{}, false, nil, updateFunc, preset[1].obj); err == nil {
3094 t.Errorf("Unexpected error: %v", err)
3095 }
3096
3097 // Delete fails with internal error.
3098 if err := store.Delete(ctx, preset[1].key, &example.Pod{}, nil, storage.ValidateAllObjectFunc, nil, storage.DeleteOptions{}); !storage.IsInternalError(err) {
3099 t.Errorf("Unexpected error: %v", err)
3100 }
3101 if err := store.Get(ctx, preset[1].key, storage.GetOptions{}, &example.Pod{}); !storage.IsInternalError(err) {
3102 t.Errorf("Unexpected error: %v", err)
3103 }
3104 }
3105
3106 func RunTestCount(ctx context.Context, t *testing.T, store storage.Interface) {
3107 resourceA := "/foo.bar.io/abc"
3108
3109 // resourceA is intentionally a prefix of resourceB to ensure that the count
3110 // for resourceA does not include any objects from resourceB.
3111 resourceB := fmt.Sprintf("%sdef", resourceA)
3112
3113 resourceACountExpected := 5
3114 for i := 1; i <= resourceACountExpected; i++ {
3115 obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("foo-%d", i)}}
3116
3117 key := fmt.Sprintf("%s/%d", resourceA, i)
3118 if err := store.Create(ctx, key, obj, nil, 0); err != nil {
3119 t.Fatalf("Create failed: %v", err)
3120 }
3121 }
3122
3123 resourceBCount := 4
3124 for i := 1; i <= resourceBCount; i++ {
3125 obj := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("foo-%d", i)}}
3126
3127 key := fmt.Sprintf("%s/%d", resourceB, i)
3128 if err := store.Create(ctx, key, obj, nil, 0); err != nil {
3129 t.Fatalf("Create failed: %v", err)
3130 }
3131 }
3132
3133 resourceACountGot, err := store.Count(resourceA)
3134 if err != nil {
3135 t.Fatalf("store.Count failed: %v", err)
3136 }
3137
3138 // count for resourceA should not include the objects for resourceB
3139 // even though resourceA is a prefix of resourceB.
3140 if int64(resourceACountExpected) != resourceACountGot {
3141 t.Fatalf("store.Count for resource %s: expected %d but got %d", resourceA, resourceACountExpected, resourceACountGot)
3142 }
3143 }
3144
3145 func RunTestListPaging(ctx context.Context, t *testing.T, store storage.Interface) {
3146 out := &example.Pod{}
3147 for i := 0; i < 4; i++ {
3148 name := fmt.Sprintf("test-%d", i)
3149 pod := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "paging"}}
3150 key := computePodKey(pod)
3151 if err := store.Create(ctx, key, pod, out, 0); err != nil {
3152 t.Fatal(err)
3153 }
3154 }
3155
3156 var names []string
3157 opts := storage.ListOptions{
3158 Recursive: true,
3159 Predicate: storage.SelectionPredicate{
3160 Label: labels.Everything(),
3161 Field: fields.Everything(),
3162 Limit: 1,
3163 },
3164 }
3165 calls := 0
3166 for {
3167 calls++
3168 listOut := &example.PodList{}
3169 err := store.GetList(ctx, "/pods", opts, listOut)
3170 if err != nil {
3171 t.Fatalf("Unexpected error %s", err)
3172 }
3173 for _, item := range listOut.Items {
3174 names = append(names, item.Name)
3175 }
3176 if listOut.Continue == "" {
3177 break
3178 }
3179 if calls == 2 {
3180 name := "test-5"
3181 pod := &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "paging"}}
3182 key := computePodKey(pod)
3183 if err := store.Create(ctx, key, pod, out, 0); err != nil {
3184 t.Fatal(err)
3185 }
3186 }
3187 opts.Predicate.Continue = listOut.Continue
3188 }
3189 if calls != 4 {
3190 t.Errorf("unexpected list invocations: %d", calls)
3191 }
3192 if !reflect.DeepEqual(names, []string{"test-0", "test-1", "test-2", "test-3"}) {
3193 t.Errorf("unexpected items: %#v", names)
3194 }
3195 }
3196
3197 func RunTestNamespaceScopedList(ctx context.Context, t *testing.T, store storage.Interface) {
3198 tests := []struct {
3199 name string
3200 requestedNamespace string
3201 recursive bool
3202 indexFields []string
3203 fieldSelector func(namespace string) fields.Selector
3204 inputPods func(namespace string) []example.Pod
3205 expectPods func(namespace string) []example.Pod
3206 }{
3207 {
3208 name: "request without namespace, without field selector",
3209 recursive: true,
3210 fieldSelector: func(namespace string) fields.Selector { return fields.Everything() },
3211 inputPods: func(namespace string) []example.Pod {
3212 return []example.Pod{
3213 *baseNamespacedPod("foo1", "ns1"),
3214 *baseNamespacedPod("foo2", "ns2"),
3215 }
3216 },
3217 expectPods: func(namespace string) []example.Pod {
3218 return []example.Pod{
3219 *baseNamespacedPod("foo1", "ns1"),
3220 *baseNamespacedPod("foo2", "ns2"),
3221 }
3222 },
3223 },
3224 {
3225 name: "request without namespace, field selector with metadata.namespace",
3226 recursive: true,
3227 fieldSelector: func(namespace string) fields.Selector {
3228 return fields.SelectorFromSet(fields.Set{"metadata.namespace": namespace + "ns1"})
3229 },
3230 inputPods: func(namespace string) []example.Pod {
3231 return []example.Pod{
3232 *baseNamespacedPod("foo1", namespace+"ns1"),
3233 *baseNamespacedPod("foo1", namespace+"ns2"),
3234 }
3235 },
3236 expectPods: func(namespace string) []example.Pod {
3237 return []example.Pod{
3238 *baseNamespacedPod("foo1", namespace+"ns1"),
3239 }
3240 },
3241 },
3242 {
3243 name: "request without namespace, field selector with spec.nodename",
3244 recursive: true,
3245 fieldSelector: func(namespace string) fields.Selector {
3246 return fields.ParseSelectorOrDie("spec.nodeName=bar1")
3247 },
3248 indexFields: []string{"spec.nodeName"},
3249 inputPods: func(namespace string) []example.Pod {
3250 return []example.Pod{
3251 *baseNamespacedPodAssigned("foo1", namespace+"ns1", "bar1"),
3252 *baseNamespacedPodAssigned("foo2", namespace+"ns2", "bar2"),
3253 }
3254 },
3255 expectPods: func(namespace string) []example.Pod {
3256 return []example.Pod{
3257 *baseNamespacedPodAssigned("foo1", namespace+"ns1", "bar1"),
3258 }
3259 },
3260 },
3261 {
3262 name: "request without namespace, field selector with spec.nodename to filter out",
3263 recursive: true,
3264 fieldSelector: func(namespace string) fields.Selector { return fields.ParseSelectorOrDie("spec.nodeName!=bar1") },
3265 indexFields: []string{"spec.nodeName"},
3266 inputPods: func(namespace string) []example.Pod {
3267 return []example.Pod{
3268 *baseNamespacedPodAssigned("foo1", namespace+"ns1", "bar1"),
3269 *baseNamespacedPodAssigned("foo2", namespace+"ns2", "bar2"),
3270 }
3271 },
3272 expectPods: func(namespace string) []example.Pod {
3273 return []example.Pod{
3274 *baseNamespacedPodAssigned("foo2", namespace+"ns2", "bar2"),
3275 }
3276 },
3277 },
3278 {
3279 name: "request with namespace, without field selector",
3280 requestedNamespace: "ns1",
3281 recursive: true,
3282 fieldSelector: func(namespace string) fields.Selector { return fields.Everything() },
3283 inputPods: func(namespace string) []example.Pod {
3284 return []example.Pod{
3285 *baseNamespacedPod("foo1", namespace+"ns1"),
3286 *baseNamespacedPod("foo1", namespace+"ns2"),
3287 *baseNamespacedPod("foo2", namespace+"ns1"),
3288 }
3289 },
3290 expectPods: func(namespace string) []example.Pod {
3291 return []example.Pod{
3292 *baseNamespacedPod("foo1", namespace+"ns1"),
3293 *baseNamespacedPod("foo2", namespace+"ns1"),
3294 }
3295 },
3296 },
3297 {
3298 name: "request with namespace, field selector with matched metadata.namespace",
3299 requestedNamespace: "ns1",
3300 recursive: true,
3301 fieldSelector: func(namespace string) fields.Selector {
3302 return fields.SelectorFromSet(fields.Set{"metadata.namespace": namespace + "ns1"})
3303 },
3304 inputPods: func(namespace string) []example.Pod {
3305 return []example.Pod{
3306 *baseNamespacedPod("foo1", namespace+"ns1"),
3307 *baseNamespacedPod("foo1", namespace+"ns2"),
3308 }
3309 },
3310 expectPods: func(namespace string) []example.Pod {
3311 return []example.Pod{
3312 *baseNamespacedPod("foo1", namespace+"ns1"),
3313 }
3314 },
3315 },
3316 {
3317 name: "request with namespace, field selector with non-matched metadata.namespace",
3318 requestedNamespace: "ns1",
3319 recursive: true,
3320 fieldSelector: func(namespace string) fields.Selector {
3321 return fields.SelectorFromSet(fields.Set{"metadata.namespace": namespace + "ns2"})
3322 },
3323 inputPods: func(namespace string) []example.Pod {
3324 return []example.Pod{
3325 *baseNamespacedPod("foo1", namespace+"ns1"),
3326 *baseNamespacedPod("foo1", namespace+"ns2"),
3327 *baseNamespacedPodUpdated("foo2", namespace+"ns1"),
3328 *baseNamespacedPodUpdated("foo2", namespace+"ns2"),
3329 }
3330 },
3331 expectPods: func(namespace string) []example.Pod { return []example.Pod{} },
3332 },
3333 {
3334 name: "request with namespace, field selector with spec.nodename",
3335 requestedNamespace: "ns1",
3336 recursive: true,
3337 fieldSelector: func(namespace string) fields.Selector { return fields.ParseSelectorOrDie("spec.nodeName=bar2") },
3338 indexFields: []string{"spec.nodeName"},
3339 inputPods: func(namespace string) []example.Pod {
3340 return []example.Pod{
3341 *baseNamespacedPodAssigned("foo1", namespace+"ns1", "bar1"),
3342 *baseNamespacedPodAssigned("foo1", namespace+"ns2", "bar2"),
3343 *baseNamespacedPodAssigned("foo2", namespace+"ns1", "bar2"),
3344 }
3345 },
3346 expectPods: func(namespace string) []example.Pod {
3347 return []example.Pod{
3348 *baseNamespacedPodAssigned("foo2", namespace+"ns1", "bar2"),
3349 }
3350 },
3351 },
3352 {
3353 name: "request with namespace, field selector with spec.nodename to filter out",
3354 requestedNamespace: "ns2",
3355 recursive: true,
3356 fieldSelector: func(namespace string) fields.Selector { return fields.ParseSelectorOrDie("spec.nodeName!=bar1") },
3357 indexFields: []string{"spec.nodeName"},
3358 inputPods: func(namespace string) []example.Pod {
3359 return []example.Pod{
3360 *baseNamespacedPod("foo1", namespace+"ns1"),
3361 *baseNamespacedPod("foo1", namespace+"ns2"),
3362 *baseNamespacedPodAssigned("foo3", namespace+"ns2", "bar1"),
3363 *baseNamespacedPodAssigned("foo2", namespace+"ns2", "bar2"),
3364 }
3365 },
3366 expectPods: func(namespace string) []example.Pod {
3367 return []example.Pod{
3368 *baseNamespacedPod("foo1", namespace+"ns2"),
3369 *baseNamespacedPodAssigned("foo2", namespace+"ns2", "bar2"),
3370 }
3371 },
3372 },
3373 {
3374 name: "request without namespace, field selector with metadata.name",
3375 recursive: true,
3376 fieldSelector: func(namespace string) fields.Selector { return fields.ParseSelectorOrDie("metadata.name=foo1") },
3377 inputPods: func(namespace string) []example.Pod {
3378 return []example.Pod{
3379 *baseNamespacedPod("foo1", namespace+"ns1"),
3380 *baseNamespacedPod("foo1", namespace+"ns2"),
3381 *baseNamespacedPod("foo2", namespace+"ns1"),
3382 }
3383 },
3384 expectPods: func(namespace string) []example.Pod {
3385 return []example.Pod{
3386 *baseNamespacedPod("foo1", namespace+"ns1"),
3387 *baseNamespacedPod("foo1", namespace+"ns2"),
3388 }
3389 },
3390 },
3391 {
3392 name: "request without namespace, field selector with metadata.name and metadata.namespace",
3393 recursive: true,
3394 fieldSelector: func(namespace string) fields.Selector {
3395 return fields.SelectorFromSet(fields.Set{
3396 "metadata.name": "foo1",
3397 "metadata.namespace": namespace + "ns1",
3398 })
3399 },
3400 inputPods: func(namespace string) []example.Pod {
3401 return []example.Pod{
3402 *baseNamespacedPod("foo1", namespace+"ns1"),
3403 *baseNamespacedPod("foo2", namespace+"ns1"),
3404 *baseNamespacedPod("foo1", namespace+"ns2"),
3405 }
3406 },
3407 expectPods: func(namespace string) []example.Pod {
3408 return []example.Pod{
3409 *baseNamespacedPod("foo1", namespace+"ns1"),
3410 }
3411 },
3412 },
3413 {
3414 name: "request without namespace, field selector with metadata.name and spec.nodeName",
3415 recursive: true,
3416 fieldSelector: func(namespace string) fields.Selector {
3417 return fields.SelectorFromSet(fields.Set{
3418 "metadata.name": "foo1",
3419 "spec.nodeName": "bar1",
3420 })
3421 },
3422 indexFields: []string{"spec.nodeName"},
3423 inputPods: func(namespace string) []example.Pod {
3424 return []example.Pod{
3425 *baseNamespacedPodAssigned("foo1", namespace+"ns1", "bar1"),
3426 }
3427 },
3428 expectPods: func(namespace string) []example.Pod {
3429 return []example.Pod{
3430 *baseNamespacedPodAssigned("foo1", namespace+"ns1", "bar1"),
3431 }
3432 },
3433 },
3434 {
3435 name: "request without namespace, field selector with metadata.name, and with spec.nodeName to filter out watch",
3436 recursive: true,
3437 fieldSelector: func(namespace string) fields.Selector {
3438 return fields.AndSelectors(
3439 fields.ParseSelectorOrDie("spec.nodeName!=bar1"),
3440 fields.SelectorFromSet(fields.Set{"metadata.name": "foo1"}),
3441 )
3442 },
3443 indexFields: []string{"spec.nodeName"},
3444 inputPods: func(namespace string) []example.Pod {
3445 return []example.Pod{
3446 *baseNamespacedPod("foo1", namespace+"ns1"),
3447 *baseNamespacedPod("foo2", namespace+"ns1"),
3448 *baseNamespacedPodAssigned("foo1", namespace+"ns2", "bar1"),
3449 *baseNamespacedPodAssigned("foo2", namespace+"ns2", "bar1"),
3450 *baseNamespacedPodAssigned("foo1", namespace+"ns3", "bar2"),
3451 *baseNamespacedPodAssigned("foo2", namespace+"ns3", "bar2"),
3452 }
3453 },
3454 expectPods: func(namespace string) []example.Pod {
3455 return []example.Pod{
3456 *baseNamespacedPod("foo1", namespace+"ns1"),
3457 *baseNamespacedPodAssigned("foo1", namespace+"ns3", "bar2"),
3458 }
3459 },
3460 },
3461 {
3462 name: "request with namespace, with field selector metadata.name",
3463 requestedNamespace: "ns1",
3464 fieldSelector: func(namespace string) fields.Selector { return fields.ParseSelectorOrDie("metadata.name=foo1") },
3465 inputPods: func(namespace string) []example.Pod {
3466 return []example.Pod{
3467 *baseNamespacedPod("foo1", namespace+"ns1"),
3468 *baseNamespacedPod("foo2", namespace+"ns1"),
3469 *baseNamespacedPod("foo1", namespace+"ns2"),
3470 *baseNamespacedPod("foo2", namespace+"ns2"),
3471 }
3472 },
3473 expectPods: func(namespace string) []example.Pod {
3474 return []example.Pod{
3475 *baseNamespacedPod("foo1", namespace+"ns1"),
3476 }
3477 },
3478 },
3479 {
3480 name: "request with namespace, with field selector metadata.name and metadata.namespace",
3481 requestedNamespace: "ns1",
3482 fieldSelector: func(namespace string) fields.Selector {
3483 return fields.SelectorFromSet(fields.Set{
3484 "metadata.name": "foo1",
3485 "metadata.namespace": namespace + "ns1",
3486 })
3487 },
3488 inputPods: func(namespace string) []example.Pod {
3489 return []example.Pod{
3490 *baseNamespacedPod("foo1", namespace+"ns1"),
3491 *baseNamespacedPod("foo2", namespace+"ns1"),
3492 *baseNamespacedPod("foo1", namespace+"ns2"),
3493 *baseNamespacedPod("foo2", namespace+"ns2"),
3494 }
3495 },
3496 expectPods: func(namespace string) []example.Pod {
3497 return []example.Pod{
3498 *baseNamespacedPod("foo1", namespace+"ns1"),
3499 }
3500 },
3501 },
3502 {
3503 name: "request with namespace, with field selector metadata.name, metadata.namespace and spec.nodename",
3504 requestedNamespace: "ns2",
3505 fieldSelector: func(namespace string) fields.Selector {
3506 return fields.SelectorFromSet(fields.Set{
3507 "metadata.name": "foo2",
3508 "metadata.namespace": namespace + "ns2",
3509 "spec.nodeName": "bar1",
3510 })
3511 },
3512 indexFields: []string{"spec.nodeName"},
3513 inputPods: func(namespace string) []example.Pod {
3514 return []example.Pod{
3515 *baseNamespacedPod("foo1", namespace+"ns1"),
3516 *baseNamespacedPod("foo2", namespace+"ns1"),
3517 *baseNamespacedPod("foo1", namespace+"ns2"),
3518 *baseNamespacedPodAssigned("foo2", namespace+"ns2", "bar1"),
3519 *baseNamespacedPodAssigned("foo1", namespace+"ns3", "bar1"),
3520 *baseNamespacedPodAssigned("foo2", namespace+"ns3", "bar1"),
3521 }
3522 },
3523 expectPods: func(namespace string) []example.Pod {
3524 return []example.Pod{
3525 *baseNamespacedPodAssigned("foo2", namespace+"ns2", "bar1"),
3526 }
3527 },
3528 },
3529 {
3530 name: "request with namespace, with field selector metadata.name, metadata.namespace, and with spec.nodename to filter out",
3531 requestedNamespace: "ns2",
3532 fieldSelector: func(namespace string) fields.Selector {
3533 return fields.AndSelectors(
3534 fields.ParseSelectorOrDie("spec.nodeName!=bar2"),
3535 fields.SelectorFromSet(fields.Set{"metadata.name": "foo1", "metadata.namespace": namespace + "ns2"}),
3536 )
3537 },
3538 indexFields: []string{"spec.nodeName"},
3539 inputPods: func(namespace string) []example.Pod {
3540 return []example.Pod{
3541 *baseNamespacedPod("foo1", namespace+"ns1"),
3542 *baseNamespacedPod("foo2", namespace+"ns1"),
3543 *baseNamespacedPodAssigned("foo1", namespace+"ns2", "bar1"),
3544 *baseNamespacedPodAssigned("foo2", namespace+"ns2", "bar2"),
3545 *baseNamespacedPodAssigned("foo1", namespace+"ns3", "bar2"),
3546 }
3547 },
3548 expectPods: func(namespace string) []example.Pod {
3549 return []example.Pod{
3550 *baseNamespacedPodAssigned("foo1", namespace+"ns2", "bar1"),
3551 }
3552 },
3553 },
3554 }
3555 for i, tt := range tests {
3556 t.Run(tt.name, func(t *testing.T) {
3557 podNames := map[string]struct{}{}
3558 namespace := fmt.Sprintf("t%d-", i)
3559 for _, pod := range tt.inputPods(namespace) {
3560 out := &example.Pod{}
3561 key := computePodKey(&pod)
3562 podNames[key] = struct{}{}
3563 err := store.Create(ctx, key, &pod, out, 0)
3564 if err != nil {
3565 t.Fatalf("GuaranteedUpdate failed: %v", err)
3566 }
3567 }
3568 opts := storage.ListOptions{
3569 ResourceVersion: "",
3570 Predicate: CreatePodPredicate(tt.fieldSelector(namespace), true, tt.indexFields),
3571 Recursive: true,
3572 }
3573 listOut := &example.PodList{}
3574 path := "/pods/"
3575 if tt.requestedNamespace != "" {
3576 path += namespace + tt.requestedNamespace
3577 }
3578 if err := store.GetList(ctx, path, opts, listOut); err != nil {
3579 t.Errorf("Unexpected error: %v", err)
3580 }
3581 i := 0
3582 for _, pod := range listOut.Items {
3583 if _, found := podNames[computePodKey(&pod)]; !found {
3584 continue
3585 }
3586 pod.ResourceVersion = ""
3587 listOut.Items[i] = pod
3588 i++
3589 }
3590 listOut.Items = listOut.Items[:i]
3591
3592 expectNoDiff(t, "incorrect list pods", tt.expectPods(namespace), listOut.Items)
3593 })
3594 }
3595 }