Debian Codegraph @

pkg/storage/testing/utils.go

raw
1 /*
2 Copyright 2015 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 "bytes"
21 "context"
22 "fmt"
23 "path"
24 "reflect"
25 "strconv"
26 "sync"
27 "sync/atomic"
28 "testing"
29 "time"
30
31 "github.com/google/go-cmp/cmp" //nolint:depguard
32 "k8s.io/apimachinery/pkg/api/meta"
33 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34 "k8s.io/apimachinery/pkg/fields"
35 "k8s.io/apimachinery/pkg/labels"
36 "k8s.io/apimachinery/pkg/runtime"
37 "k8s.io/apimachinery/pkg/util/wait"
38 "k8s.io/apimachinery/pkg/watch"
39 "k8s.io/apiserver/pkg/apis/example"
40 "k8s.io/apiserver/pkg/storage"
41 "k8s.io/apiserver/pkg/storage/value"
42 )
43
44 // CreateObjList will create a list from the array of objects.
45 func CreateObjList(prefix string, helper storage.Interface, items []runtime.Object) error {
46 for i := range items {
47 obj := items[i]
48 meta, err := meta.Accessor(obj)
49 if err != nil {
50 return err
51 }
52 err = helper.Create(context.Background(), path.Join(prefix, meta.GetName()), obj, obj, 0)
53 if err != nil {
54 return err
55 }
56 items[i] = obj
57 }
58 return nil
59 }
60
61 // CreateList will properly create a list using the storage interface.
62 func CreateList(prefix string, helper storage.Interface, list runtime.Object) error {
63 items, err := meta.ExtractList(list)
64 if err != nil {
65 return err
66 }
67 err = CreateObjList(prefix, helper, items)
68 if err != nil {
69 return err
70 }
71 return meta.SetList(list, items)
72 }
73
74 // DeepEqualSafePodSpec returns an example.PodSpec safe for deep-equal operations.
75 func DeepEqualSafePodSpec() example.PodSpec {
76 grace := int64(30)
77 return example.PodSpec{
78 RestartPolicy: "Always",
79 TerminationGracePeriodSeconds: &grace,
80 SchedulerName: "default-scheduler",
81 }
82 }
83
84 func computePodKey(obj *example.Pod) string {
85 return fmt.Sprintf("/pods/%s/%s", obj.Namespace, obj.Name)
86 }
87
88 // testPropagateStore helps propagates store with objects, automates key generation, and returns
89 // keys and stored objects.
90 func testPropagateStore(ctx context.Context, t *testing.T, store storage.Interface, obj *example.Pod) (string, *example.Pod) {
91 // Setup store with a key and grab the output for returning.
92 key := computePodKey(obj)
93
94 // Setup store with the specified key and grab the output for returning.
95 err := store.Delete(ctx, key, &example.Pod{}, nil, storage.ValidateAllObjectFunc, nil, storage.DeleteOptions{})
96 if err != nil && !storage.IsNotFound(err) {
97 t.Fatalf("Cleanup failed: %v", err)
98 }
99 setOutput := &example.Pod{}
100 if err := store.Create(ctx, key, obj, setOutput, 0); err != nil {
101 t.Fatalf("Set failed: %v", err)
102 }
103 return key, setOutput
104 }
105
106 func expectNoDiff(t *testing.T, msg string, expected, actual interface{}) {
107 t.Helper()
108 if !reflect.DeepEqual(expected, actual) {
109 if diff := cmp.Diff(expected, actual); diff != "" {
110 t.Errorf("%s: %s", msg, diff)
111 } else {
112 t.Errorf("%s:\nexpected: %#v\ngot: %#v", msg, expected, actual)
113 }
114 }
115 }
116
117 func ExpectContains(t *testing.T, msg string, expectedList []interface{}, got interface{}) {
118 t.Helper()
119 for _, expected := range expectedList {
120 if reflect.DeepEqual(expected, got) {
121 return
122 }
123 }
124 if len(expectedList) == 0 {
125 t.Errorf("%s: empty expectedList", msg)
126 return
127 }
128 if diff := cmp.Diff(expectedList[0], got); diff != "" {
129 t.Errorf("%s: differs from all items, with first: %s", msg, diff)
130 } else {
131 t.Errorf("%s: differs from all items, first: %#v\ngot: %#v", msg, expectedList[0], got)
132 }
133 }
134
135 const dummyPrefix = "adapter"
136
137 func encodeContinueOrDie(key string, resourceVersion int64) string {
138 token, err := storage.EncodeContinue(dummyPrefix+key, dummyPrefix, resourceVersion)
139 if err != nil {
140 panic(err)
141 }
142 return token
143 }
144
145 func testCheckEventType(t *testing.T, w watch.Interface, expectEventType watch.EventType) {
146 select {
147 case res := <-w.ResultChan():
148 if res.Type != expectEventType {
149 t.Errorf("event type want=%v, get=%v", expectEventType, res.Type)
150 }
151 case <-time.After(wait.ForeverTestTimeout):
152 t.Errorf("time out after waiting %v on ResultChan", wait.ForeverTestTimeout)
153 }
154 }
155
156 func testCheckResult(t *testing.T, w watch.Interface, expectEvent watch.Event) {
157 testCheckResultFunc(t, w, func(actualEvent watch.Event) {
158 expectNoDiff(t, "incorrect event", expectEvent, actualEvent)
159 })
160 }
161
162 func testCheckResultFunc(t *testing.T, w watch.Interface, check func(actualEvent watch.Event)) {
163 select {
164 case res := <-w.ResultChan():
165 obj := res.Object
166 if co, ok := obj.(runtime.CacheableObject); ok {
167 res.Object = co.GetObject()
168 }
169 check(res)
170 case <-time.After(wait.ForeverTestTimeout):
171 t.Errorf("time out after waiting %v on ResultChan", wait.ForeverTestTimeout)
172 }
173 }
174
175 func testCheckResultWithIgnoreFunc(t *testing.T, w watch.Interface, expectedEvents []watch.Event, ignore func(watch.Event) bool) {
176 checkIndex := 0
177 for {
178 select {
179 case event := <-w.ResultChan():
180 obj := event.Object
181 if co, ok := obj.(runtime.CacheableObject); ok {
182 event.Object = co.GetObject()
183 }
184 if ignore != nil && ignore(event) {
185 continue
186 }
187 if checkIndex < len(expectedEvents) {
188 expectNoDiff(t, "incorrect event", expectedEvents[checkIndex], event)
189 checkIndex++
190 } else {
191 t.Fatalf("cannot receive correct event, expect no event, but get a event: %+v", event)
192 }
193 case <-time.After(150 * time.Millisecond):
194 // wait 150ms forcibly in order to receive watchEvents including bookmark event.
195 // we cannot guarantee that we will receive all bookmark events within 150ms,
196 // but too large timeout value will lead to exceed the timeout of package test.
197 if checkIndex < len(expectedEvents) {
198 t.Fatalf("cannot receive enough events within specific time, rest expected events: %+v", expectedEvents[checkIndex:])
199 }
200 return
201 }
202 }
203 }
204
205 func testCheckStop(t *testing.T, w watch.Interface) {
206 select {
207 case e, ok := <-w.ResultChan():
208 if ok {
209 var obj string
210 switch e.Object.(type) {
211 case *example.Pod:
212 obj = e.Object.(*example.Pod).Name
213 case *v1.Status:
214 obj = e.Object.(*v1.Status).Message
215 }
216 t.Errorf("ResultChan should have been closed. Event: %s. Object: %s", e.Type, obj)
217 }
218 case <-time.After(wait.ForeverTestTimeout):
219 t.Errorf("time out after waiting 1s on ResultChan")
220 }
221 }
222
223 func TestCheckResultsInStrictOrder(t *testing.T, w watch.Interface, expectedEvents []watch.Event) {
224 for _, expectedEvent := range expectedEvents {
225 testCheckResult(t, w, expectedEvent)
226 }
227 }
228
229 func TestCheckNoMoreResultsWithIgnoreFunc(t *testing.T, w watch.Interface, ignore func(watch.Event) bool) {
230 select {
231 case e := <-w.ResultChan():
232 if ignore == nil || !ignore(e) {
233 t.Errorf("Unexpected: %#v event received, expected no events", e)
234 }
235 // We consciously make the timeout short here to speed up tests.
236 case <-time.After(100 * time.Millisecond):
237 return
238 }
239 }
240
241 func toInterfaceSlice[T any](s []T) []interface{} {
242 result := make([]interface{}, len(s))
243 for i, v := range s {
244 result[i] = v
245 }
246 return result
247 }
248
249 // resourceVersionNotOlderThan returns a function to validate resource versions. Resource versions
250 // referring to points in logical time before the sentinel generate an error. All logical times as
251 // new as the sentinel or newer generate no error.
252 func resourceVersionNotOlderThan(sentinel string) func(string) error {
253 return func(resourceVersion string) error {
254 objectVersioner := storage.APIObjectVersioner{}
255 actualRV, err := objectVersioner.ParseResourceVersion(resourceVersion)
256 if err != nil {
257 return err
258 }
259 expectedRV, err := objectVersioner.ParseResourceVersion(sentinel)
260 if err != nil {
261 return err
262 }
263 if actualRV < expectedRV {
264 return fmt.Errorf("expected a resourceVersion no smaller than than %d, but got %d", expectedRV, actualRV)
265 }
266 return nil
267 }
268 }
269
270 // StorageInjectingListErrors injects a dummy error for first N GetList calls.
271 type StorageInjectingListErrors struct {
272 storage.Interface
273
274 lock sync.Mutex
275 Errors int
276 }
277
278 func (s *StorageInjectingListErrors) GetList(ctx context.Context, key string, opts storage.ListOptions, listObj runtime.Object) error {
279 err := func() error {
280 s.lock.Lock()
281 defer s.lock.Unlock()
282 if s.Errors > 0 {
283 s.Errors--
284 return fmt.Errorf("injected error")
285 }
286 return nil
287 }()
288 if err != nil {
289 return err
290 }
291 return s.Interface.GetList(ctx, key, opts, listObj)
292 }
293
294 func (s *StorageInjectingListErrors) ErrorsConsumed() (bool, error) {
295 s.lock.Lock()
296 defer s.lock.Unlock()
297 return s.Errors == 0, nil
298 }
299
300 // PrefixTransformer adds and verifies that all data has the correct prefix on its way in and out.
301 type PrefixTransformer struct {
302 prefix []byte
303 stale bool
304 err error
305 reads uint64
306 }
307
308 func NewPrefixTransformer(prefix []byte, stale bool) *PrefixTransformer {
309 return &PrefixTransformer{
310 prefix: prefix,
311 stale: stale,
312 }
313 }
314
315 func (p *PrefixTransformer) TransformFromStorage(ctx context.Context, data []byte, dataCtx value.Context) ([]byte, bool, error) {
316 atomic.AddUint64(&p.reads, 1)
317 if dataCtx == nil {
318 panic("no context provided")
319 }
320 if !bytes.HasPrefix(data, p.prefix) {
321 return nil, false, fmt.Errorf("value does not have expected prefix %q: %s,", p.prefix, string(data))
322 }
323 return bytes.TrimPrefix(data, p.prefix), p.stale, p.err
324 }
325 func (p *PrefixTransformer) TransformToStorage(ctx context.Context, data []byte, dataCtx value.Context) ([]byte, error) {
326 if dataCtx == nil {
327 panic("no context provided")
328 }
329 if len(data) > 0 {
330 return append(append([]byte{}, p.prefix...), data...), p.err
331 }
332 return data, p.err
333 }
334
335 func (p *PrefixTransformer) GetReadsAndReset() uint64 {
336 return atomic.SwapUint64(&p.reads, 0)
337 }
338
339 // reproducingTransformer is a custom test-only transformer used purely
340 // for testing consistency.
341 // It allows for creating predefined objects on TransformFromStorage operations,
342 // which allows for precise in time injection of new objects in the middle of
343 // read operations.
344 type reproducingTransformer struct {
345 wrapped value.Transformer
346 store storage.Interface
347
348 index uint32
349 nextObject func(uint32) (string, *example.Pod)
350 }
351
352 func (rt *reproducingTransformer) TransformFromStorage(ctx context.Context, data []byte, dataCtx value.Context) ([]byte, bool, error) {
353 if err := rt.createObject(ctx); err != nil {
354 return nil, false, err
355 }
356 return rt.wrapped.TransformFromStorage(ctx, data, dataCtx)
357 }
358
359 func (rt *reproducingTransformer) TransformToStorage(ctx context.Context, data []byte, dataCtx value.Context) ([]byte, error) {
360 return rt.wrapped.TransformToStorage(ctx, data, dataCtx)
361 }
362
363 func (rt *reproducingTransformer) createObject(ctx context.Context) error {
364 key, obj := rt.nextObject(atomic.AddUint32(&rt.index, 1))
365 out := &example.Pod{}
366 return rt.store.Create(ctx, key, obj, out, 0)
367 }
368
369 // failingTransformer is a custom test-only transformer that always returns
370 // an error on transforming data from storage.
371 type failingTransformer struct {
372 }
373
374 func (ft *failingTransformer) TransformFromStorage(ctx context.Context, data []byte, dataCtx value.Context) ([]byte, bool, error) {
375 return nil, false, fmt.Errorf("failed transformation")
376 }
377
378 func (ft *failingTransformer) TransformToStorage(ctx context.Context, data []byte, dataCtx value.Context) ([]byte, error) {
379 return data, nil
380 }
381
382 type sortablePodList []example.Pod
383
384 func (s sortablePodList) Len() int {
385 return len(s)
386 }
387
388 func (s sortablePodList) Less(i, j int) bool {
389 return computePodKey(&s[i]) < computePodKey(&s[j])
390 }
391
392 func (s sortablePodList) Swap(i, j int) {
393 s[i], s[j] = s[j], s[i]
394 }
395
396 func CreatePodPredicate(field fields.Selector, namespaceScoped bool, indexField []string) storage.SelectionPredicate {
397 return storage.SelectionPredicate{
398 Label: labels.Everything(),
399 Field: field,
400 GetAttrs: determinePodGetAttrFunc(namespaceScoped, indexField),
401 IndexFields: indexField,
402 }
403 }
404
405 func determinePodGetAttrFunc(namespaceScoped bool, indexField []string) storage.AttrFunc {
406 if indexField != nil {
407 if namespaceScoped {
408 return namespacedScopedNodeNameAttrFunc
409 }
410 return clusterScopedNodeNameAttrFunc
411 }
412 if namespaceScoped {
413 return storage.DefaultNamespaceScopedAttr
414 }
415 return storage.DefaultClusterScopedAttr
416 }
417
418 func namespacedScopedNodeNameAttrFunc(obj runtime.Object) (labels.Set, fields.Set, error) {
419 pod := obj.(*example.Pod)
420 return nil, fields.Set{
421 "spec.nodeName": pod.Spec.NodeName,
422 "metadata.name": pod.ObjectMeta.Name,
423 "metadata.namespace": pod.ObjectMeta.Namespace,
424 }, nil
425 }
426
427 func clusterScopedNodeNameAttrFunc(obj runtime.Object) (labels.Set, fields.Set, error) {
428 pod := obj.(*example.Pod)
429 return nil, fields.Set{
430 "spec.nodeName": pod.Spec.NodeName,
431 "metadata.name": pod.ObjectMeta.Name,
432 }, nil
433 }
434
435 func mustAtoi(str string) int {
436 result, err := strconv.Atoi(str)
437 if err != nil {
438 panic(err)
439 }
440 return result
441 }