Debian Codegraph @

pkg/storage/interfaces_test.go

raw
1 /*
2 Copyright 2024 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
18
19 import (
20 "errors"
21 "strings"
22 "testing"
23
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 )
26
27 func TestPreconditionsCheckWithNilObject(t *testing.T) {
28 p := &Preconditions{}
29 err := p.Check("foo", nil)
30 if err == nil {
31 t.Fatalf("expected an error")
32 }
33
34 var internalErr InternalError
35 if !errors.As(err, &internalErr) {
36 t.Fatalf("expected error to be of type: %T, but got: %#v", InternalError{}, err)
37 }
38 if want := "can't enforce preconditions"; !strings.Contains(internalErr.Error(), want) {
39 t.Errorf("expected error to contain %q", want)
40 }
41 }
42
43 func TestValidateListOptions(t *testing.T) {
44 testCases := []struct {
45 name string
46 opts ListOptions
47 expectedError string
48 expectedRev int64
49 expectedContinueKey string
50 }{
51 {
52 name: "specifying resource version when using continue",
53 opts: ListOptions{
54 Recursive: true,
55 ResourceVersion: "200",
56 Predicate: SelectionPredicate{
57 Continue: encodeContinueOrDie("meta.k8s.io/v1", 100, "continue"),
58 },
59 },
60 expectedError: "specifying resource version is not allowed when using continue",
61 },
62 {
63 name: "invalid resource version",
64 opts: ListOptions{
65 ResourceVersion: "invalid",
66 },
67 expectedError: "invalid resource version",
68 },
69 {
70 name: "unknown ResourceVersionMatch value",
71 opts: ListOptions{
72 ResourceVersion: "200",
73 ResourceVersionMatch: "unknown",
74 },
75 expectedError: "unknown ResourceVersionMatch value",
76 },
77 {
78 name: "use continueRV",
79 opts: ListOptions{
80 ResourceVersion: "0",
81 Recursive: true,
82 Predicate: SelectionPredicate{
83 Continue: encodeContinueOrDie("meta.k8s.io/v1", 100, "continue"),
84 },
85 },
86 expectedRev: 100,
87 expectedContinueKey: "continue",
88 },
89 {
90 name: "use continueRV with empty rv",
91 opts: ListOptions{
92 ResourceVersion: "",
93 Recursive: true,
94 Predicate: SelectionPredicate{
95 Continue: encodeContinueOrDie("meta.k8s.io/v1", 100, "continue"),
96 },
97 },
98 expectedRev: 100,
99 expectedContinueKey: "continue",
100 },
101 {
102 name: "continueRV = 0",
103 opts: ListOptions{
104 ResourceVersion: "",
105 Recursive: true,
106 Predicate: SelectionPredicate{
107 Continue: encodeContinueOrDie("meta.k8s.io/v1", 0, "continue"),
108 },
109 },
110 expectedError: "invalid continue token",
111 },
112 {
113 name: "continueRV < 0",
114 opts: ListOptions{
115 ResourceVersion: "",
116 Recursive: true,
117 Predicate: SelectionPredicate{
118 Continue: encodeContinueOrDie("meta.k8s.io/v1", -1, "continue"),
119 },
120 },
121 expectedRev: 0,
122 expectedContinueKey: "continue",
123 },
124 {
125 name: "default",
126 opts: ListOptions{},
127 expectedRev: 0,
128 },
129 {
130 name: "rev resolve to 0 if ResourceVersionMatchNotOlderThan",
131 opts: ListOptions{
132 ResourceVersion: "200",
133 ResourceVersionMatch: metav1.ResourceVersionMatchNotOlderThan,
134 },
135 expectedRev: 0,
136 },
137 {
138 name: "specified rev if ResourceVersionMatchExact",
139 opts: ListOptions{
140 ResourceVersion: "200",
141 ResourceVersionMatch: metav1.ResourceVersionMatchExact,
142 },
143 expectedRev: 200,
144 },
145 {
146 name: "rev resolve to 0 if not recursive",
147 opts: ListOptions{
148 ResourceVersion: "200",
149 Predicate: SelectionPredicate{
150 Limit: 1,
151 },
152 },
153 expectedRev: 0,
154 },
155 {
156 name: "rev resolve to 0 if limit unspecified",
157 opts: ListOptions{
158 ResourceVersion: "200",
159 Recursive: true,
160 },
161 expectedRev: 0,
162 },
163 {
164 name: "specified rev if recursive with limit",
165 opts: ListOptions{
166 ResourceVersion: "200",
167 Recursive: true,
168 Predicate: SelectionPredicate{
169 Limit: 1,
170 },
171 },
172 expectedRev: 200,
173 },
174 }
175 for _, tt := range testCases {
176 tt := tt
177 t.Run(tt.name, func(t *testing.T) {
178 withRev, continueKey, err := ValidateListOptions("", APIObjectVersioner{}, tt.opts)
179 if len(tt.expectedError) > 0 {
180 if err == nil || !strings.Contains(err.Error(), tt.expectedError) {
181 t.Fatalf("expected error: %s, but got: %v", tt.expectedError, err)
182 }
183 return
184 }
185 if err != nil {
186 t.Fatalf("resolveRevForGetList failed: %v", err)
187 }
188 if withRev != tt.expectedRev {
189 t.Errorf("%s: expecting rev = %d, but get %d", tt.name, tt.expectedRev, withRev)
190 }
191 if continueKey != tt.expectedContinueKey {
192 t.Errorf("%s: expecting continueKey = %q, but get %q", tt.name, tt.expectedContinueKey, continueKey)
193 }
194 })
195 }
196 }