Debian Codegraph @

pkg/cel/library/semver_test.go

raw
1 /*
2 Copyright 2023 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 library_test
18
19 import (
20 "regexp"
21 "testing"
22
23 "github.com/blang/semver/v4"
24 "github.com/google/cel-go/cel"
25 "github.com/google/cel-go/common/types"
26 "github.com/google/cel-go/common/types/ref"
27 "github.com/stretchr/testify/require"
28
29 "k8s.io/apimachinery/pkg/util/sets"
30 apiservercel "k8s.io/apiserver/pkg/cel"
31 library "k8s.io/apiserver/pkg/cel/library"
32 )
33
34 func testSemver(t *testing.T, expr string, expectResult ref.Val, expectRuntimeErrPattern string, expectCompileErrs []string, version uint32) {
35 env, err := cel.NewEnv(
36 library.SemverLib(library.SemverVersion(version)),
37 )
38 if err != nil {
39 t.Fatalf("%v", err)
40 }
41 compiled, issues := env.Compile(expr)
42
43 if len(expectCompileErrs) > 0 {
44 missingCompileErrs := []string{}
45 matchedCompileErrs := sets.New[int]()
46 for _, expectedCompileErr := range expectCompileErrs {
47 compiledPattern, err := regexp.Compile(expectedCompileErr)
48 if err != nil {
49 t.Fatalf("failed to compile expected err regex: %v", err)
50 }
51
52 didMatch := false
53
54 for i, compileError := range issues.Errors() {
55 if compiledPattern.Match([]byte(compileError.Message)) {
56 didMatch = true
57 matchedCompileErrs.Insert(i)
58 }
59 }
60
61 if !didMatch {
62 missingCompileErrs = append(missingCompileErrs, expectedCompileErr)
63 } else if len(matchedCompileErrs) != len(issues.Errors()) {
64 unmatchedErrs := []cel.Error{}
65 for i, issue := range issues.Errors() {
66 if !matchedCompileErrs.Has(i) {
67 unmatchedErrs = append(unmatchedErrs, *issue)
68 }
69 }
70 require.Empty(t, unmatchedErrs, "unexpected compilation errors")
71 }
72 }
73
74 require.Empty(t, missingCompileErrs, "expected compilation errors")
75 return
76 } else if len(issues.Errors()) > 0 {
77 for _, err := range issues.Errors() {
78 t.Errorf("unexpected compile error: %v", err)
79 }
80 t.FailNow()
81 }
82
83 prog, err := env.Program(compiled)
84 if err != nil {
85 t.Fatalf("%v", err)
86 }
87 res, _, err := prog.Eval(map[string]interface{}{})
88 if len(expectRuntimeErrPattern) > 0 {
89 if err == nil {
90 t.Fatalf("no runtime error thrown. Expected: %v", expectRuntimeErrPattern)
91 } else if matched, regexErr := regexp.MatchString(expectRuntimeErrPattern, err.Error()); regexErr != nil {
92 t.Fatalf("failed to compile expected err regex: %v", regexErr)
93 } else if !matched {
94 t.Fatalf("unexpected err: %v", err)
95 }
96 } else if err != nil {
97 t.Fatalf("%v", err)
98 } else if expectResult != nil {
99 converted := res.Equal(expectResult).Value().(bool)
100 require.True(t, converted, "expectation not equal to output")
101 } else {
102 t.Fatal("expected result must not be nil")
103 }
104
105 }
106
107 func TestSemver(t *testing.T) {
108 trueVal := types.Bool(true)
109 falseVal := types.Bool(false)
110
111 cases := []struct {
112 name string
113 expr string
114 expectValue ref.Val
115 expectedCompileErr []string
116 expectedRuntimeErr string
117 version uint32
118 }{
119 {
120 name: "parse",
121 expr: `semver("1.2.3")`,
122 expectValue: apiservercel.Semver{Version: semver.MustParse("1.2.3")},
123 },
124 {
125 name: "parseInvalidVersion",
126 expr: `semver("v1.0")`,
127 expectedRuntimeErr: "No Major.Minor.Patch elements found",
128 },
129 {
130 name: "isSemver",
131 expr: `isSemver("1.2.3-beta.1+build.1")`,
132 expectValue: trueVal,
133 },
134 {
135 name: "isSemver_empty_false",
136 expr: `isSemver("")`,
137 expectValue: falseVal,
138 },
139 {
140 name: "isSemver_v_prefix_false",
141 expr: `isSemver("v1.0.0")`,
142 expectValue: falseVal,
143 },
144 {
145 name: "isSemver_v_leading_whitespace_false",
146 expr: `isSemver(" 1.0.0")`,
147 expectValue: falseVal,
148 },
149 {
150 name: "isSemver_v_contains_whitespace_false",
151 expr: `isSemver("1. 0.0")`,
152 expectValue: falseVal,
153 },
154 {
155 name: "isSemver_v_trailing_whitespace_false",
156 expr: `isSemver("1.0.0 ")`,
157 expectValue: falseVal,
158 },
159 {
160 name: "isSemver_leading_zeros_false",
161 expr: `isSemver("01.01.01")`,
162 expectValue: falseVal,
163 },
164 {
165 name: "isSemver_major_only_false",
166 expr: `isSemver("1")`,
167 expectValue: falseVal,
168 },
169 {
170 name: "isSemver_major_minor_only_false",
171 expr: `isSemver("1.1")`,
172 expectValue: falseVal,
173 },
174 {
175 name: "isSemver_empty_normalize_false",
176 expr: `isSemver("", true)`,
177 expectValue: falseVal,
178 version: 1,
179 },
180 {
181 name: "isSemver_v_leading_whitespace_normalize_false",
182 expr: `isSemver(" 1.0.0", true)`,
183 expectValue: falseVal,
184 version: 1,
185 },
186 {
187 name: "isSemver_v_contains_whitespace_normalize_false",
188 expr: `isSemver("1. 0.0", true)`,
189 expectValue: falseVal,
190 version: 1,
191 },
192 {
193 name: "isSemver_v_trailing_whitespace_normalize_false",
194 expr: `isSemver("1.0.0 ", true)`,
195 expectValue: falseVal,
196 version: 1,
197 },
198 {
199 name: "isSemver_v_prefix_normalize_true",
200 expr: `isSemver("v1.0.0", true)`,
201 expectValue: trueVal,
202 version: 1,
203 },
204 {
205 name: "isSemver_leading_zeros_normalize_true",
206 expr: `isSemver("01.01.01", true)`,
207 expectValue: trueVal,
208 version: 1,
209 },
210 {
211 name: "isSemver_major_only_normalize_true",
212 expr: `isSemver("1", true)`,
213 expectValue: trueVal,
214 version: 1,
215 },
216 {
217 name: "isSemver_major_minor_only_normalize_true",
218 expr: `isSemver("1.1", true)`,
219 expectValue: trueVal,
220 version: 1,
221 },
222 {
223 name: "isSemver_noOverload",
224 expr: `isSemver([1, 2, 3])`,
225 expectedCompileErr: []string{"found no matching overload for 'isSemver' applied to.*"},
226 },
227 {
228 name: "equality_normalize",
229 expr: `semver("v01.01", true) == semver("1.1.0")`,
230 expectValue: trueVal,
231 version: 1,
232 },
233 {
234 name: "equality_reflexivity",
235 expr: `semver("1.2.3") == semver("1.2.3")`,
236 expectValue: trueVal,
237 },
238 {
239 name: "inequality",
240 expr: `semver("1.2.3") == semver("1.0.0")`,
241 expectValue: falseVal,
242 },
243 {
244 name: "semver_less",
245 expr: `semver("1.0.0").isLessThan(semver("1.2.3"))`,
246 expectValue: trueVal,
247 },
248 {
249 name: "semver_less_false",
250 expr: `semver("1.0.0").isLessThan(semver("1.0.0"))`,
251 expectValue: falseVal,
252 },
253 {
254 name: "semver_greater",
255 expr: `semver("1.2.3").isGreaterThan(semver("1.0.0"))`,
256 expectValue: trueVal,
257 },
258 {
259 name: "semver_greater_false",
260 expr: `semver("1.0.0").isGreaterThan(semver("1.0.0"))`,
261 expectValue: falseVal,
262 },
263 {
264 name: "compare_equal",
265 expr: `semver("1.2.3").compareTo(semver("1.2.3"))`,
266 expectValue: types.Int(0),
267 },
268 {
269 name: "compare_less",
270 expr: `semver("1.0.0").compareTo(semver("1.2.3"))`,
271 expectValue: types.Int(-1),
272 },
273 {
274 name: "compare_greater",
275 expr: `semver("1.2.3").compareTo(semver("1.0.0"))`,
276 expectValue: types.Int(1),
277 },
278 {
279 name: "major",
280 expr: `semver("1.2.3").major()`,
281 expectValue: types.Int(1),
282 },
283 {
284 name: "minor",
285 expr: `semver("1.2.3").minor()`,
286 expectValue: types.Int(2),
287 },
288 {
289 name: "patch",
290 expr: `semver("1.2.3").patch()`,
291 expectValue: types.Int(3),
292 },
293 }
294
295 for _, c := range cases {
296 t.Run(c.name, func(t *testing.T) {
297 testSemver(t, c.expr, c.expectValue, c.expectedRuntimeErr, c.expectedCompileErr, c.version)
298 })
299 }
300 }