Debian Codegraph @

pkg/storage/cacher/delegator_test.go

raw
1 /*
2 Copyright 2025 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 cacher
18
19 import (
20 "context"
21 "testing"
22
23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24 "k8s.io/apimachinery/pkg/runtime"
25 "k8s.io/apiserver/pkg/apis/example"
26 "k8s.io/apiserver/pkg/storage"
27 )
28
29 func TestCalculateDigest(t *testing.T) {
30 newListFunc := func() runtime.Object { return &example.PodList{} }
31 testCases := []struct {
32 desc string
33 resourceVersion string
34 cacherReady bool
35 cacherItems []example.Pod
36 etcdItems []example.Pod
37 resourcePrefix string
38
39 expectListKey string
40 expectDigest storageDigest
41 expectErr bool
42 }{
43 {
44 desc: "not ready",
45 cacherReady: false,
46 resourceVersion: "1",
47 expectErr: true,
48 },
49 {
50 desc: "empty",
51 resourceVersion: "1",
52 cacherReady: true,
53 expectDigest: storageDigest{
54 ResourceVersion: "1",
55 CacheDigest: "cbf29ce484222325",
56 EtcdDigest: "cbf29ce484222325",
57 },
58 },
59 {
60 desc: "with one element equal",
61 resourceVersion: "2",
62 cacherReady: true,
63 cacherItems: []example.Pod{
64 {ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "pod", ResourceVersion: "2"}},
65 },
66 etcdItems: []example.Pod{
67 {ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "pod", ResourceVersion: "2"}},
68 },
69 expectDigest: storageDigest{
70 ResourceVersion: "2",
71 CacheDigest: "86bf3a5e80d1c5cb",
72 EtcdDigest: "86bf3a5e80d1c5cb",
73 },
74 },
75 {
76 desc: "namespace changes digest",
77 resourceVersion: "2",
78 cacherReady: true,
79 cacherItems: []example.Pod{
80 {ObjectMeta: metav1.ObjectMeta{Namespace: "kube-system", Name: "pod", ResourceVersion: "2"}},
81 },
82 etcdItems: []example.Pod{
83 {ObjectMeta: metav1.ObjectMeta{Namespace: "kube-public", Name: "pod", ResourceVersion: "2"}},
84 },
85 expectDigest: storageDigest{
86 ResourceVersion: "2",
87 CacheDigest: "4ae4e750bd825b17",
88 EtcdDigest: "f940a60af965b03",
89 },
90 },
91 {
92 desc: "name changes digest",
93 resourceVersion: "2",
94 cacherReady: true,
95 cacherItems: []example.Pod{
96 {ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "pod2", ResourceVersion: "2"}},
97 },
98 etcdItems: []example.Pod{
99 {ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "pod3", ResourceVersion: "2"}},
100 },
101 expectDigest: storageDigest{
102 ResourceVersion: "2",
103 CacheDigest: "c9120494e4c1897d",
104 EtcdDigest: "c9156494e4c46274",
105 },
106 },
107 {
108 desc: "resourceVersion changes digest",
109 resourceVersion: "2",
110 cacherReady: true,
111 cacherItems: []example.Pod{
112 {ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "pod", ResourceVersion: "3"}},
113 },
114 etcdItems: []example.Pod{
115 {ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "pod", ResourceVersion: "4"}},
116 },
117 expectDigest: storageDigest{
118 ResourceVersion: "2",
119 CacheDigest: "86bf3a5e80d1c5ca",
120 EtcdDigest: "86bf3a5e80d1c5cd",
121 },
122 },
123 {
124 desc: "watch missed write event",
125 resourceVersion: "3",
126 cacherReady: true,
127 cacherItems: []example.Pod{
128 {ObjectMeta: metav1.ObjectMeta{Namespace: "Default", Name: "pod", ResourceVersion: "2"}},
129 },
130 etcdItems: []example.Pod{
131 {ObjectMeta: metav1.ObjectMeta{Namespace: "Default", Name: "pod", ResourceVersion: "2"}},
132 {ObjectMeta: metav1.ObjectMeta{Namespace: "Default", Name: "pod", ResourceVersion: "3"}},
133 },
134 expectDigest: storageDigest{
135 ResourceVersion: "3",
136 CacheDigest: "1859bac707c2cb2b",
137 EtcdDigest: "11d147fc800df0e0",
138 },
139 },
140 }
141
142 for _, tc := range testCases {
143 t.Run(tc.desc, func(t *testing.T) {
144 etcd := &dummyStorage{
145 getListFn: func(_ context.Context, key string, opts storage.ListOptions, listObj runtime.Object) error {
146 if key != tc.expectListKey {
147 t.Fatalf("Expect GetList key %q, got %q", tc.expectListKey, key)
148 }
149 if opts.ResourceVersion != tc.resourceVersion {
150 t.Fatalf("Expect GetList resourceVersion %q, got %q", tc.resourceVersion, opts.ResourceVersion)
151 }
152 if opts.ResourceVersionMatch != metav1.ResourceVersionMatchExact {
153 t.Fatalf("Expect GetList match exact, got %q", opts.ResourceVersionMatch)
154 }
155 podList := listObj.(*example.PodList)
156 podList.Items = tc.etcdItems
157 podList.ResourceVersion = tc.resourceVersion
158 return nil
159 },
160 }
161 cacher := &dummyCacher{
162 ready: tc.cacherReady,
163 dummyStorage: dummyStorage{
164 getListFn: func(_ context.Context, key string, opts storage.ListOptions, listObj runtime.Object) error {
165 if key != tc.expectListKey {
166 t.Fatalf("Expect GetList key %q, got %q", tc.expectListKey, key)
167 }
168 if opts.ResourceVersion != "0" {
169 t.Fatalf("Expect GetList resourceVersion 0, got %q", opts.ResourceVersion)
170 }
171 if opts.ResourceVersionMatch != metav1.ResourceVersionMatchNotOlderThan {
172 t.Fatalf("Expect GetList match not older than, got %q", opts.ResourceVersionMatch)
173 }
174 podList := listObj.(*example.PodList)
175 podList.Items = tc.cacherItems
176 podList.ResourceVersion = tc.resourceVersion
177 return nil
178 },
179 },
180 }
181 checker := newConsistencyChecker(tc.resourcePrefix, newListFunc, cacher, etcd)
182 digest, err := checker.calculateDigests(context.Background())
183 if (err != nil) != tc.expectErr {
184 t.Fatalf("Expect error: %v, got: %v", tc.expectErr, err)
185 }
186 if err != nil {
187 return
188 }
189 if *digest != tc.expectDigest {
190 t.Errorf("Expect: %+v Got: %+v", &tc.expectDigest, *digest)
191 }
192 })
193 }
194 }