Debian Codegraph @

pkg/cel/library/format_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 library_test
18
19 import (
20 "fmt"
21 "testing"
22
23 "github.com/google/cel-go/common/types"
24 "github.com/google/cel-go/common/types/ref"
25
26 "k8s.io/apiserver/pkg/cel"
27 "k8s.io/apiserver/pkg/cel/library"
28 )
29
30 func TestFormat(t *testing.T) {
31 type testcase struct {
32 name string
33 expr string
34 expectValue ref.Val
35 expectedCompileErr []string
36 expectedRuntimeErr string
37 }
38
39 cases := []testcase{
40 {
41 name: "example_usage_dns1123Label",
42 expr: `format.dns1123Label().validate("my-label-name")`,
43 expectValue: types.OptionalNone,
44 },
45 {
46 name: "example_usage_dns1123Subdomain",
47 expr: `format.dns1123Subdomain().validate("apiextensions.k8s.io")`,
48 expectValue: types.OptionalNone,
49 },
50 {
51 name: "example_usage_qualifiedName",
52 expr: `format.qualifiedName().validate("apiextensions.k8s.io/v1beta1")`,
53 expectValue: types.OptionalNone,
54 },
55 {
56 name: "example_usage_dns1123LabelPrefix",
57 expr: `format.dns1123LabelPrefix().validate("my-label-prefix-")`,
58 expectValue: types.OptionalNone,
59 },
60 {
61 name: "example_usage_dns1123SubdomainPrefix",
62 expr: `format.dns1123SubdomainPrefix().validate("mysubdomain.prefix.-")`,
63 expectValue: types.OptionalNone,
64 },
65 {
66 name: "example_usage_dns1035LabelPrefix",
67 expr: `format.dns1035LabelPrefix().validate("my-label-prefix-")`,
68 expectValue: types.OptionalNone,
69 },
70 {
71 name: "example_usage_uri",
72 expr: `format.uri().validate("http://example.com")`,
73 expectValue: types.OptionalNone,
74 },
75 {
76 name: "example_usage_uuid",
77 expr: `format.uuid().validate("123e4567-e89b-12d3-a456-426614174000")`,
78 expectValue: types.OptionalNone,
79 },
80 {
81 name: "example_usage_byte",
82 expr: `format.byte().validate("aGVsbG8=")`,
83 expectValue: types.OptionalNone,
84 },
85 {
86 name: "example_usage_date",
87 expr: `format.date().validate("2021-01-01")`,
88 expectValue: types.OptionalNone,
89 },
90 {
91 name: "example_usage_datetime",
92 expr: `format.datetime().validate("2021-01-01T00:00:00Z")`,
93 expectValue: types.OptionalNone,
94 },
95 {
96 name: "dns1123Label",
97 expr: `format.dns1123Label().validate("contains a space")`,
98 expectValue: types.OptionalOf(types.NewStringList(types.DefaultTypeAdapter, []string{"a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')"})),
99 },
100 {
101 name: "dns1123Subdomain",
102 expr: `format.dns1123Subdomain().validate("contains a space")`,
103 expectValue: types.OptionalOf(types.NewStringList(types.DefaultTypeAdapter, []string{`a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')`})),
104 },
105 {
106 name: "dns1035Label",
107 expr: `format.dns1035Label().validate("contains a space")`,
108 expectValue: types.OptionalOf(types.NewStringList(types.DefaultTypeAdapter, []string{`a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')`})),
109 },
110 {
111 name: "qualifiedName",
112 expr: `format.qualifiedName().validate("contains a space")`,
113 expectValue: types.OptionalOf(types.NewStringList(types.DefaultTypeAdapter, []string{`name part must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyName', or 'my.name', or '123-abc', regex used for validation is '([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9]')`})),
114 },
115 {
116 name: "dns1123LabelPrefix",
117 expr: `format.dns1123LabelPrefix().validate("contains a space-")`,
118 expectValue: types.OptionalOf(types.NewStringList(types.DefaultTypeAdapter, []string{"a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')"})),
119 },
120 {
121 name: "dns1123SubdomainPrefix",
122 expr: `format.dns1123SubdomainPrefix().validate("contains a space-")`,
123 expectValue: types.OptionalOf(types.NewStringList(types.DefaultTypeAdapter, []string{`a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')`})),
124 },
125 {
126 name: "dns1035LabelPrefix",
127 expr: `format.dns1035LabelPrefix().validate("contains a space-")`,
128 expectValue: types.OptionalOf(types.NewStringList(types.DefaultTypeAdapter, []string{`a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')`})),
129 },
130 {
131 name: "dns1123Label_Success",
132 expr: `format.dns1123Label().validate("my-label-name")`,
133 expectValue: types.OptionalNone,
134 },
135 {
136 name: "dns1123Subdomain_Success",
137 expr: `format.dns1123Subdomain().validate("example.com")`,
138 expectValue: types.OptionalNone,
139 },
140 {
141 name: "dns1035Label_Success",
142 expr: `format.dns1035Label().validate("my-label-name")`,
143 expectValue: types.OptionalNone,
144 },
145 {
146 name: "qualifiedName_Success",
147 expr: `format.qualifiedName().validate("my.name")`,
148 expectValue: types.OptionalNone,
149 },
150 {
151 // byte is base64
152 name: "byte_success",
153 expr: `format.byte().validate("aGVsbG8=")`,
154 expectValue: types.OptionalNone,
155 },
156 {
157 // byte is base64
158 name: "byte_failure",
159 expr: `format.byte().validate("aGVsbG8")`,
160 expectValue: types.OptionalOf(types.NewStringList(types.DefaultTypeAdapter, []string{"invalid base64"})),
161 },
162 {
163 name: "date_success",
164 expr: `format.date().validate("2020-01-01")`,
165 // date is a valid date
166 expectValue: types.OptionalNone,
167 },
168 {
169 name: "date_failure",
170 expr: `format.date().validate("2020-01-32")`,
171 expectValue: types.OptionalOf(types.NewStringList(types.DefaultTypeAdapter, []string{"invalid date"})),
172 },
173 {
174 name: "datetime_success",
175 expr: `format.datetime().validate("2020-01-01T00:00:00Z")`,
176 // datetime is a valid date
177 expectValue: types.OptionalNone,
178 },
179 {
180 name: "datetime_failure",
181 expr: `format.datetime().validate("2020-01-32T00:00:00Z")`,
182 expectValue: types.OptionalOf(types.NewStringList(types.DefaultTypeAdapter, []string{"invalid datetime"})),
183 },
184 {
185 name: "unknown_format",
186 expr: `format.named("unknown").hasValue()`,
187 expectValue: types.False,
188 },
189 {
190 name: "labelValue_success",
191 expr: `format.labelValue().validate("my-cool-label-Value")`,
192 expectValue: types.OptionalNone,
193 },
194 {
195 name: "labelValue_failure",
196 expr: `format.labelValue().validate("my-cool-label-Value!!\n\n!!!")`,
197 expectValue: types.OptionalOf(types.NewStringList(types.DefaultTypeAdapter, []string{
198 "a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue', or 'my_value', or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')",
199 })),
200 },
201 }
202
203 // Also test format names and comparisons of all constants
204 for keyLHS := range library.ConstantFormats {
205 cases = append(cases, testcase{
206 name: "lookup and comparison",
207 expr: fmt.Sprintf(`format.named("%s").hasValue()`, keyLHS),
208 expectValue: types.True,
209 }, testcase{
210 name: "comparison with lookup succeeds",
211 expr: fmt.Sprintf(`format.named("%s").value() == format.%s()`, keyLHS, keyLHS),
212 expectValue: types.True,
213 })
214
215 for keyRHS := range library.ConstantFormats {
216 if keyLHS == keyRHS {
217 continue
218 }
219 cases = append(cases, testcase{
220 name: fmt.Sprintf("compare_%s_%s", keyLHS, keyRHS),
221 expr: fmt.Sprintf(`format.%s() == format.%s()`, keyLHS, keyRHS),
222 expectValue: types.False,
223 })
224 }
225 }
226
227 for _, tc := range cases {
228 t.Run(tc.name, func(t *testing.T) {
229 testQuantity(t, tc.expr, tc.expectValue, tc.expectedRuntimeErr, tc.expectedCompileErr)
230 })
231 }
232 }
233
234 func TestSizeLimit(t *testing.T) {
235 for name := range library.ConstantFormats {
236 if len(name) > cel.MaxFormatSize {
237 t.Fatalf("All formats must be <= %d chars in length", cel.MaxFormatSize)
238 }
239 }
240 }