Debian Codegraph @

pkg/storage/util_test.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 storage_test
18
19 import (
20 "math/rand"
21 "sync"
22 "testing"
23
24 "github.com/stretchr/testify/require"
25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26 "k8s.io/apimachinery/pkg/runtime"
27 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
28 "k8s.io/apiserver/pkg/apis/example"
29 examplev1 "k8s.io/apiserver/pkg/apis/example/v1"
30 example2v1 "k8s.io/apiserver/pkg/apis/example2/v1"
31 "k8s.io/apiserver/pkg/storage"
32 )
33
34 var (
35 scheme = runtime.NewScheme()
36 )
37
38 func init() {
39 metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion)
40 utilruntime.Must(example.AddToScheme(scheme))
41 utilruntime.Must(examplev1.AddToScheme(scheme))
42 utilruntime.Must(example2v1.AddToScheme(scheme))
43 }
44
45 func TestHighWaterMark(t *testing.T) {
46 var h storage.HighWaterMark
47
48 for i := int64(10); i < 20; i++ {
49 if !h.Update(i) {
50 t.Errorf("unexpected false for %v", i)
51 }
52 if h.Update(i - 1) {
53 t.Errorf("unexpected true for %v", i-1)
54 }
55 }
56
57 m := int64(0)
58 wg := sync.WaitGroup{}
59 for i := 0; i < 300; i++ {
60 wg.Add(1)
61 v := rand.Int63()
62 go func(v int64) {
63 defer wg.Done()
64 h.Update(v)
65 }(v)
66 if v > m {
67 m = v
68 }
69 }
70 wg.Wait()
71 if m != int64(h) {
72 t.Errorf("unexpected value, wanted %v, got %v", m, int64(h))
73 }
74 }
75
76 func TestHasInitialEventsEndBookmarkAnnotation(t *testing.T) {
77 createPod := func(name string) *example.Pod {
78 return &example.Pod{ObjectMeta: metav1.ObjectMeta{Name: name}}
79 }
80 createAnnotatedPod := func(name, value string) *example.Pod {
81 p := createPod(name)
82 p.Annotations = map[string]string{}
83 p.Annotations[metav1.InitialEventsAnnotationKey] = value
84 return p
85 }
86 scenarios := []struct {
87 name string
88 object runtime.Object
89 expectAnnotation bool
90 }{
91 {
92 name: "a standard obj with the initial-events-end annotation set to true",
93 object: createAnnotatedPod("p1", "true"),
94 expectAnnotation: true,
95 },
96 {
97 name: "a standard obj with the initial-events-end annotation set to false",
98 object: createAnnotatedPod("p1", "false"),
99 },
100 {
101 name: "a standard obj without the annotation",
102 object: createPod("p1"),
103 },
104 }
105
106 for _, scenario := range scenarios {
107 t.Run(scenario.name, func(t *testing.T) {
108 hasAnnotation, err := storage.HasInitialEventsEndBookmarkAnnotation(scenario.object)
109 require.NoError(t, err)
110 require.Equal(t, scenario.expectAnnotation, hasAnnotation)
111 })
112 }
113 }