| 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 | package generators_test |
| 17 | |
| 18 | import ( |
| 19 | "testing" |
| 20 | |
| 21 | "github.com/stretchr/testify/require" |
| 22 | |
| 23 | "k8s.io/gengo/v2/types" |
| 24 | "k8s.io/kube-openapi/pkg/generators" |
| 25 | "k8s.io/kube-openapi/pkg/validation/spec" |
| 26 | "k8s.io/utils/ptr" |
| 27 | ) |
| 28 | |
| 29 | var structKind *types.Type = &types.Type{Kind: types.Struct, Name: types.Name{Name: "struct"}} |
| 30 | var mapType *types.Type = &types.Type{Kind: types.Map, Name: types.Name{Name: "map[string]int"}} |
| 31 | var arrayType *types.Type = &types.Type{Kind: types.Slice, Name: types.Name{Name: "[]int"}} |
| 32 | |
| 33 | func TestParseCommentTags(t *testing.T) { |
| 34 | |
| 35 | cases := []struct { |
| 36 | t *types.Type |
| 37 | name string |
| 38 | comments []string |
| 39 | expected *spec.Schema |
| 40 | |
| 41 | // regex pattern matching the error, or empty string/unset if no error |
| 42 | // is expected |
| 43 | expectedError string |
| 44 | }{ |
| 45 | { |
| 46 | t: structKind, |
| 47 | name: "basic example", |
| 48 | comments: []string{ |
| 49 | "comment", |
| 50 | "another + comment", |
| 51 | "+k8s:validation:minimum=10.0", |
| 52 | "+k8s:validation:maximum=20.0", |
| 53 | "+k8s:validation:minLength=20", |
| 54 | "+k8s:validation:maxLength=30", |
| 55 | `+k8s:validation:pattern="asdf"`, |
| 56 | "+k8s:validation:multipleOf=1.0", |
| 57 | "+k8s:validation:minItems=1", |
| 58 | "+k8s:validation:maxItems=2", |
| 59 | "+k8s:validation:uniqueItems=true", |
| 60 | "exclusiveMaximum=true", |
| 61 | "not+k8s:validation:Minimum=0.0", |
| 62 | }, |
| 63 | expectedError: `invalid marker comments: maxItems can only be used on array types |
| 64 | minItems can only be used on array types |
| 65 | uniqueItems can only be used on array types |
| 66 | minLength can only be used on string types |
| 67 | maxLength can only be used on string types |
| 68 | pattern can only be used on string types |
| 69 | minimum can only be used on numeric types |
| 70 | maximum can only be used on numeric types |
| 71 | multipleOf can only be used on numeric types`, |
| 72 | }, |
| 73 | { |
| 74 | t: arrayType, |
| 75 | name: "basic array example", |
| 76 | comments: []string{ |
| 77 | "comment", |
| 78 | "another + comment", |
| 79 | "+k8s:validation:minItems=1", |
| 80 | "+k8s:validation:maxItems=2", |
| 81 | "+k8s:validation:uniqueItems=true", |
| 82 | }, |
| 83 | expected: &spec.Schema{ |
| 84 | SchemaProps: spec.SchemaProps{ |
| 85 | MinItems: ptr.To[int64](1), |
| 86 | MaxItems: ptr.To[int64](2), |
| 87 | UniqueItems: true, |
| 88 | }, |
| 89 | }, |
| 90 | }, |
| 91 | { |
| 92 | t: mapType, |
| 93 | name: "basic map example", |
| 94 | comments: []string{ |
| 95 | "comment", |
| 96 | "another + comment", |
| 97 | "+k8s:validation:minProperties=1", |
| 98 | "+k8s:validation:maxProperties=2", |
| 99 | }, |
| 100 | expected: &spec.Schema{ |
| 101 | SchemaProps: spec.SchemaProps{ |
| 102 | MinProperties: ptr.To[int64](1), |
| 103 | MaxProperties: ptr.To[int64](2), |
| 104 | }, |
| 105 | }, |
| 106 | }, |
| 107 | { |
| 108 | t: types.String, |
| 109 | name: "basic string example", |
| 110 | comments: []string{ |
| 111 | "comment", |
| 112 | "another + comment", |
| 113 | "+k8s:validation:minLength=20", |
| 114 | "+k8s:validation:maxLength=30", |
| 115 | `+k8s:validation:pattern="asdf"`, |
| 116 | }, |
| 117 | expected: &spec.Schema{ |
| 118 | SchemaProps: spec.SchemaProps{ |
| 119 | MinLength: ptr.To[int64](20), |
| 120 | MaxLength: ptr.To[int64](30), |
| 121 | Pattern: "asdf", |
| 122 | }, |
| 123 | }, |
| 124 | }, |
| 125 | { |
| 126 | t: types.Int, |
| 127 | name: "basic int example", |
| 128 | comments: []string{ |
| 129 | "comment", |
| 130 | "another + comment", |
| 131 | "+k8s:validation:minimum=10.0", |
| 132 | "+k8s:validation:maximum=20.0", |
| 133 | "+k8s:validation:multipleOf=1.0", |
| 134 | "exclusiveMaximum=true", |
| 135 | "not+k8s:validation:Minimum=0.0", |
| 136 | }, |
| 137 | expected: &spec.Schema{ |
| 138 | SchemaProps: spec.SchemaProps{ |
| 139 | Maximum: ptr.To(20.0), |
| 140 | Minimum: ptr.To(10.0), |
| 141 | MultipleOf: ptr.To(1.0), |
| 142 | }, |
| 143 | }, |
| 144 | }, |
| 145 | { |
| 146 | t: structKind, |
| 147 | name: "empty", |
| 148 | expected: &spec.Schema{}, |
| 149 | }, |
| 150 | { |
| 151 | t: types.Float64, |
| 152 | name: "single", |
| 153 | comments: []string{ |
| 154 | "+k8s:validation:minimum=10.0", |
| 155 | }, |
| 156 | expected: &spec.Schema{ |
| 157 | SchemaProps: spec.SchemaProps{ |
| 158 | Minimum: ptr.To(10.0), |
| 159 | }, |
| 160 | }, |
| 161 | }, |
| 162 | { |
| 163 | t: types.Float64, |
| 164 | name: "multiple", |
| 165 | comments: []string{ |
| 166 | "+k8s:validation:minimum=10.0", |
| 167 | "+k8s:validation:maximum=20.0", |
| 168 | }, |
| 169 | expected: &spec.Schema{ |
| 170 | SchemaProps: spec.SchemaProps{ |
| 171 | Maximum: ptr.To(20.0), |
| 172 | Minimum: ptr.To(10.0), |
| 173 | }, |
| 174 | }, |
| 175 | }, |
| 176 | { |
| 177 | t: types.Float64, |
| 178 | name: "invalid duplicate key", |
| 179 | comments: []string{ |
| 180 | "+k8s:validation:minimum=10.0", |
| 181 | "+k8s:validation:maximum=20.0", |
| 182 | "+k8s:validation:minimum=30.0", |
| 183 | }, |
| 184 | expectedError: `failed to parse marker comments: cannot have multiple values for key 'minimum'`, |
| 185 | }, |
| 186 | { |
| 187 | t: structKind, |
| 188 | name: "unrecognized key is ignored", |
| 189 | comments: []string{ |
| 190 | "+ignored=30.0", |
| 191 | }, |
| 192 | expected: &spec.Schema{}, |
| 193 | }, |
| 194 | { |
| 195 | t: types.Float64, |
| 196 | name: "invalid: non-JSON value", |
| 197 | comments: []string{ |
| 198 | `+k8s:validation:minimum=asdf`, |
| 199 | }, |
| 200 | expectedError: `failed to parse marker comments: failed to parse value for key minimum as JSON: invalid character 'a' looking for beginning of value`, |
| 201 | }, |
| 202 | { |
| 203 | t: types.Float64, |
| 204 | name: "invalid: invalid value type", |
| 205 | comments: []string{ |
| 206 | `+k8s:validation:minimum="asdf"`, |
| 207 | }, |
| 208 | expectedError: `failed to unmarshal marker comments: json: cannot unmarshal string into Go struct field commentTags.minimum of type float64`, |
| 209 | }, |
| 210 | { |
| 211 | |
| 212 | t: structKind, |
| 213 | name: "invalid: empty key", |
| 214 | comments: []string{ |
| 215 | "+k8s:validation:", |
| 216 | }, |
| 217 | expectedError: `failed to parse marker comments: cannot have empty key for marker comment`, |
| 218 | }, |
| 219 | { |
| 220 | t: types.Float64, |
| 221 | // temporary test. ref support may be added in the future |
| 222 | name: "ignore refs", |
| 223 | comments: []string{ |
| 224 | "+k8s:validation:pattern=ref(asdf)", |
| 225 | }, |
| 226 | expected: &spec.Schema{}, |
| 227 | }, |
| 228 | { |
| 229 | t: types.Float64, |
| 230 | name: "cel rule", |
| 231 | comments: []string{ |
| 232 | `+k8s:validation:cel[0]:rule="oldSelf == self"`, |
| 233 | `+k8s:validation:cel[0]:message="immutable field"`, |
| 234 | }, |
| 235 | expected: &spec.Schema{ |
| 236 | VendorExtensible: spec.VendorExtensible{ |
| 237 | Extensions: map[string]interface{}{ |
| 238 | "x-kubernetes-validations": []interface{}{ |
| 239 | map[string]interface{}{ |
| 240 | "rule": "oldSelf == self", |
| 241 | "message": "immutable field", |
| 242 | }, |
| 243 | }, |
| 244 | }, |
| 245 | }, |
| 246 | }, |
| 247 | }, |
| 248 | { |
| 249 | t: types.Float64, |
| 250 | name: "skipped CEL rule", |
| 251 | comments: []string{ |
| 252 | // This should parse, but return an error in validation since |
| 253 | // index 1 is missing |
| 254 | `+k8s:validation:cel[0]:rule="oldSelf == self"`, |
| 255 | `+k8s:validation:cel[0]:message="immutable field"`, |
| 256 | `+k8s:validation:cel[2]:rule="self > 5"`, |
| 257 | `+k8s:validation:cel[2]:message="must be greater than 5"`, |
| 258 | }, |
| 259 | expectedError: `failed to parse marker comments: error parsing cel[2]:rule="self > 5": non-consecutive index 2 for key 'cel'`, |
| 260 | }, |
| 261 | { |
| 262 | t: types.Float64, |
| 263 | name: "multiple CEL params", |
| 264 | comments: []string{ |
| 265 | `+k8s:validation:cel[0]:rule="oldSelf == self"`, |
| 266 | `+k8s:validation:cel[0]:message="immutable field"`, |
| 267 | `+k8s:validation:cel[1]:rule="self > 5"`, |
| 268 | `+k8s:validation:cel[1]:optionalOldSelf=true`, |
| 269 | `+k8s:validation:cel[1]:message="must be greater than 5"`, |
| 270 | }, |
| 271 | expected: &spec.Schema{ |
| 272 | VendorExtensible: spec.VendorExtensible{ |
| 273 | Extensions: map[string]interface{}{ |
| 274 | "x-kubernetes-validations": []interface{}{ |
| 275 | map[string]interface{}{ |
| 276 | "rule": "oldSelf == self", |
| 277 | "message": "immutable field", |
| 278 | }, |
| 279 | map[string]interface{}{ |
| 280 | "rule": "self > 5", |
| 281 | "optionalOldSelf": true, |
| 282 | "message": "must be greater than 5", |
| 283 | }, |
| 284 | }, |
| 285 | }, |
| 286 | }, |
| 287 | }, |
| 288 | }, |
| 289 | { |
| 290 | t: types.Float64, |
| 291 | name: "multiple rules with multiple params", |
| 292 | comments: []string{ |
| 293 | `+k8s:validation:cel[0]:rule="oldSelf == self"`, |
| 294 | `+k8s:validation:cel[0]:optionalOldSelf`, |
| 295 | `+k8s:validation:cel[0]:messageExpression="self + ' must be equal to old value'"`, |
| 296 | `+k8s:validation:cel[1]:rule="self > 5"`, |
| 297 | `+k8s:validation:cel[1]:optionalOldSelf=true`, |
| 298 | `+k8s:validation:cel[1]:message="must be greater than 5"`, |
| 299 | }, |
| 300 | expected: &spec.Schema{ |
| 301 | VendorExtensible: spec.VendorExtensible{ |
| 302 | Extensions: map[string]interface{}{ |
| 303 | "x-kubernetes-validations": []interface{}{ |
| 304 | map[string]interface{}{ |
| 305 | "rule": "oldSelf == self", |
| 306 | "optionalOldSelf": true, |
| 307 | "messageExpression": "self + ' must be equal to old value'", |
| 308 | }, |
| 309 | map[string]interface{}{ |
| 310 | "rule": "self > 5", |
| 311 | "optionalOldSelf": true, |
| 312 | "message": "must be greater than 5", |
| 313 | }, |
| 314 | }, |
| 315 | }, |
| 316 | }, |
| 317 | }, |
| 318 | }, |
| 319 | { |
| 320 | t: types.Float64, |
| 321 | name: "skipped array index", |
| 322 | comments: []string{ |
| 323 | `+k8s:validation:cel[0]:rule="oldSelf == self"`, |
| 324 | `+k8s:validation:cel[0]:optionalOldSelf`, |
| 325 | `+k8s:validation:cel[0]:messageExpression="self + ' must be equal to old value'"`, |
| 326 | `+k8s:validation:cel[2]:rule="self > 5"`, |
| 327 | `+k8s:validation:cel[2]:optionalOldSelf=true`, |
| 328 | `+k8s:validation:cel[2]:message="must be greater than 5"`, |
| 329 | }, |
| 330 | expectedError: `failed to parse marker comments: error parsing cel[2]:rule="self > 5": non-consecutive index 2 for key 'cel'`, |
| 331 | }, |
| 332 | { |
| 333 | t: types.Float64, |
| 334 | name: "non-consecutive array index", |
| 335 | comments: []string{ |
| 336 | `+k8s:validation:cel[0]:rule="oldSelf == self"`, |
| 337 | `+k8s:validation:cel[1]:rule="self > 5"`, |
| 338 | `+k8s:validation:cel[1]:message="self > 5"`, |
| 339 | `+k8s:validation:cel[0]:optionalOldSelf=true`, |
| 340 | `+k8s:validation:cel[0]:message="must be greater than 5"`, |
| 341 | }, |
| 342 | expectedError: "failed to parse marker comments: error parsing cel[0]:optionalOldSelf=true: non-consecutive index 0 for key 'cel'", |
| 343 | }, |
| 344 | { |
| 345 | t: types.Float64, |
| 346 | name: "interjected array index", |
| 347 | comments: []string{ |
| 348 | `+k8s:validation:cel[0]:rule="oldSelf == self"`, |
| 349 | `+k8s:validation:cel[0]:message="cant change"`, |
| 350 | `+k8s:validation:cel[1]:rule="self > 5"`, |
| 351 | `+k8s:validation:cel[1]:message="must be greater than 5"`, |
| 352 | `+k8s:validation:minimum=5`, |
| 353 | `+k8s:validation:cel[2]:rule="a rule"`, |
| 354 | `+k8s:validation:cel[2]:message="message 2"`, |
| 355 | }, |
| 356 | expectedError: "failed to parse marker comments: error parsing cel[2]:rule=\"a rule\": non-consecutive index 2 for key 'cel'", |
| 357 | }, |
| 358 | { |
| 359 | t: types.Float64, |
| 360 | name: "interjected array index with non-prefixed comment", |
| 361 | comments: []string{ |
| 362 | `+k8s:validation:cel[0]:rule="oldSelf == self"`, |
| 363 | `+k8s:validation:cel[0]:message="cant change"`, |
| 364 | `+k8s:validation:cel[1]:rule="self > 5"`, |
| 365 | `+k8s:validation:cel[1]:message="must be greater than 5"`, |
| 366 | `+minimum=5`, |
| 367 | `+k8s:validation:cel[2]:rule="a rule"`, |
| 368 | `+k8s:validation:cel[2]:message="message 2"`, |
| 369 | }, |
| 370 | expectedError: "failed to parse marker comments: error parsing cel[2]:rule=\"a rule\": non-consecutive index 2 for key 'cel'", |
| 371 | }, |
| 372 | { |
| 373 | t: types.Float64, |
| 374 | name: "non-consecutive raw string indexing", |
| 375 | comments: []string{ |
| 376 | `+k8s:validation:cel[0]:rule> raw string rule`, |
| 377 | `+k8s:validation:cel[1]:rule="self > 5"`, |
| 378 | `+k8s:validation:cel[1]:message="must be greater than 5"`, |
| 379 | `+k8s:validation:cel[0]:message>another raw string message`, |
| 380 | }, |
| 381 | expectedError: "failed to parse marker comments: error parsing cel[0]:message>another raw string message: non-consecutive index 0 for key 'cel'", |
| 382 | }, |
| 383 | { |
| 384 | t: types.String, |
| 385 | name: "non-consecutive string indexing false positive", |
| 386 | comments: []string{ |
| 387 | `+k8s:validation:cel[0]:message>[3]string rule [1]`, |
| 388 | `+k8s:validation:cel[0]:rule="string rule [1]"`, |
| 389 | `+k8s:validation:pattern="self[3] == 'hi'"`, |
| 390 | }, |
| 391 | expected: &spec.Schema{ |
| 392 | SchemaProps: spec.SchemaProps{ |
| 393 | Pattern: `self[3] == 'hi'`, |
| 394 | }, |
| 395 | VendorExtensible: spec.VendorExtensible{ |
| 396 | Extensions: map[string]interface{}{ |
| 397 | "x-kubernetes-validations": []interface{}{ |
| 398 | map[string]interface{}{ |
| 399 | "rule": "string rule [1]", |
| 400 | "message": "[3]string rule [1]", |
| 401 | }, |
| 402 | }, |
| 403 | }, |
| 404 | }, |
| 405 | }, |
| 406 | }, |
| 407 | { |
| 408 | t: types.String, |
| 409 | name: "non-consecutive raw string indexing false positive", |
| 410 | comments: []string{ |
| 411 | `+k8s:validation:cel[0]:message>[3]raw string message with subscirpt [3]"`, |
| 412 | `+k8s:validation:cel[0]:rule> raw string rule [1]`, |
| 413 | `+k8s:validation:pattern>"self[3] == 'hi'"`, |
| 414 | }, |
| 415 | expected: &spec.Schema{ |
| 416 | SchemaProps: spec.SchemaProps{ |
| 417 | Pattern: `"self[3] == 'hi'"`, |
| 418 | }, |
| 419 | VendorExtensible: spec.VendorExtensible{ |
| 420 | Extensions: map[string]interface{}{ |
| 421 | "x-kubernetes-validations": []interface{}{ |
| 422 | map[string]interface{}{ |
| 423 | "rule": "raw string rule [1]", |
| 424 | "message": "[3]raw string message with subscirpt [3]\"", |
| 425 | }, |
| 426 | }, |
| 427 | }, |
| 428 | }, |
| 429 | }, |
| 430 | }, |
| 431 | { |
| 432 | t: types.Float64, |
| 433 | name: "boolean key at invalid index", |
| 434 | comments: []string{ |
| 435 | `+k8s:validation:cel[0]:rule="oldSelf == self"`, |
| 436 | `+k8s:validation:cel[0]:message="cant change"`, |
| 437 | `+k8s:validation:cel[2]:optionalOldSelf`, |
| 438 | }, |
| 439 | expectedError: `failed to parse marker comments: error parsing cel[2]:optionalOldSelf: non-consecutive index 2 for key 'cel'`, |
| 440 | }, |
| 441 | { |
| 442 | t: types.Float64, |
| 443 | name: "boolean key after non-prefixed comment", |
| 444 | comments: []string{ |
| 445 | `+k8s:validation:cel[0]:rule="oldSelf == self"`, |
| 446 | `+k8s:validation:cel[0]:message="cant change"`, |
| 447 | `+k8s:validation:cel[1]:rule="self > 5"`, |
| 448 | `+k8s:validation:cel[1]:message="must be greater than 5"`, |
| 449 | `+minimum=5`, |
| 450 | `+k8s:validation:cel[1]:optionalOldSelf`, |
| 451 | }, |
| 452 | expectedError: `failed to parse marker comments: error parsing cel[1]:optionalOldSelf: non-consecutive index 1 for key 'cel'`, |
| 453 | }, |
| 454 | { |
| 455 | t: types.Float64, |
| 456 | name: "boolean key at index allowed", |
| 457 | comments: []string{ |
| 458 | `+k8s:validation:cel[0]:rule="oldSelf == self"`, |
| 459 | `+k8s:validation:cel[0]:message="cant change"`, |
| 460 | `+k8s:validation:cel[1]:rule="self > 5"`, |
| 461 | `+k8s:validation:cel[1]:message="must be greater than 5"`, |
| 462 | `+k8s:validation:cel[1]:optionalOldSelf`, |
| 463 | }, |
| 464 | expected: &spec.Schema{ |
| 465 | VendorExtensible: spec.VendorExtensible{ |
| 466 | Extensions: map[string]interface{}{ |
| 467 | "x-kubernetes-validations": []interface{}{ |
| 468 | map[string]interface{}{ |
| 469 | "rule": "oldSelf == self", |
| 470 | "message": "cant change", |
| 471 | }, |
| 472 | map[string]interface{}{ |
| 473 | "rule": "self > 5", |
| 474 | "message": "must be greater than 5", |
| 475 | "optionalOldSelf": true, |
| 476 | }, |
| 477 | }, |
| 478 | }, |
| 479 | }, |
| 480 | }, |
| 481 | }, |
| 482 | { |
| 483 | t: types.Float64, |
| 484 | name: "raw string rule", |
| 485 | comments: []string{ |
| 486 | `+k8s:validation:cel[0]:rule> raw string rule`, |
| 487 | `+k8s:validation:cel[0]:message="raw string message"`, |
| 488 | }, |
| 489 | expected: &spec.Schema{ |
| 490 | VendorExtensible: spec.VendorExtensible{ |
| 491 | Extensions: map[string]interface{}{ |
| 492 | "x-kubernetes-validations": []interface{}{ |
| 493 | map[string]interface{}{ |
| 494 | "rule": "raw string rule", |
| 495 | "message": "raw string message", |
| 496 | }, |
| 497 | }, |
| 498 | }, |
| 499 | }, |
| 500 | }, |
| 501 | }, |
| 502 | { |
| 503 | t: types.Float64, |
| 504 | name: "multiline string rule", |
| 505 | comments: []string{ |
| 506 | `+k8s:validation:cel[0]:rule> self.length() % 2 == 0`, |
| 507 | `+k8s:validation:cel[0]:rule> ? self.field == self.name + ' is even'`, |
| 508 | `+k8s:validation:cel[0]:rule> : self.field == self.name + ' is odd'`, |
| 509 | `+k8s:validation:cel[0]:message>raw string message`, |
| 510 | }, |
| 511 | expected: &spec.Schema{ |
| 512 | VendorExtensible: spec.VendorExtensible{ |
| 513 | Extensions: map[string]interface{}{ |
| 514 | "x-kubernetes-validations": []interface{}{ |
| 515 | map[string]interface{}{ |
| 516 | "rule": "self.length() % 2 == 0\n? self.field == self.name + ' is even'\n: self.field == self.name + ' is odd'", |
| 517 | "message": "raw string message", |
| 518 | }, |
| 519 | }, |
| 520 | }, |
| 521 | }, |
| 522 | }, |
| 523 | }, |
| 524 | { |
| 525 | t: types.Float64, |
| 526 | name: "mix raw and non-raw string marker", |
| 527 | comments: []string{ |
| 528 | `+k8s:validation:cel[0]:message>raw string message`, |
| 529 | `+k8s:validation:cel[0]:rule="self.length() % 2 == 0"`, |
| 530 | `+k8s:validation:cel[0]:rule> ? self.field == self.name + ' is even'`, |
| 531 | `+k8s:validation:cel[0]:rule> : self.field == self.name + ' is odd'`, |
| 532 | }, |
| 533 | expected: &spec.Schema{ |
| 534 | VendorExtensible: spec.VendorExtensible{ |
| 535 | Extensions: map[string]interface{}{ |
| 536 | "x-kubernetes-validations": []interface{}{ |
| 537 | map[string]interface{}{ |
| 538 | "rule": "self.length() % 2 == 0\n? self.field == self.name + ' is even'\n: self.field == self.name + ' is odd'", |
| 539 | "message": "raw string message", |
| 540 | }, |
| 541 | }, |
| 542 | }, |
| 543 | }, |
| 544 | }, |
| 545 | }, |
| 546 | { |
| 547 | name: "raw string with different key in between", |
| 548 | t: types.Float64, |
| 549 | comments: []string{ |
| 550 | `+k8s:validation:cel[0]:message>raw string message`, |
| 551 | `+k8s:validation:cel[0]:rule="self.length() % 2 == 0"`, |
| 552 | `+k8s:validation:cel[0]:message>raw string message 2`, |
| 553 | }, |
| 554 | expectedError: `failed to parse marker comments: concatenations to key 'cel[0]:message' must be consecutive with its assignment`, |
| 555 | }, |
| 556 | { |
| 557 | name: "raw string with different raw string key in between", |
| 558 | t: types.Float64, |
| 559 | comments: []string{ |
| 560 | `+k8s:validation:cel[0]:message>raw string message`, |
| 561 | `+k8s:validation:cel[0]:rule>self.length() % 2 == 0`, |
| 562 | `+k8s:validation:cel[0]:message>raw string message 2`, |
| 563 | }, |
| 564 | expectedError: `failed to parse marker comments: concatenations to key 'cel[0]:message' must be consecutive with its assignment`, |
| 565 | }, |
| 566 | { |
| 567 | name: "nested cel", |
| 568 | comments: []string{ |
| 569 | `+k8s:validation:items:cel[0]:rule="self.length() % 2 == 0"`, |
| 570 | `+k8s:validation:items:cel[0]:message="must be even"`, |
| 571 | }, |
| 572 | t: &types.Type{ |
| 573 | Kind: types.Alias, |
| 574 | Underlying: &types.Type{ |
| 575 | Kind: types.Slice, |
| 576 | Elem: types.String, |
| 577 | }, |
| 578 | }, |
| 579 | expected: &spec.Schema{ |
| 580 | SchemaProps: spec.SchemaProps{ |
| 581 | AllOf: []spec.Schema{ |
| 582 | { |
| 583 | SchemaProps: spec.SchemaProps{ |
| 584 | Items: &spec.SchemaOrArray{ |
| 585 | Schema: &spec.Schema{ |
| 586 | VendorExtensible: spec.VendorExtensible{ |
| 587 | Extensions: map[string]interface{}{ |
| 588 | "x-kubernetes-validations": []interface{}{ |
| 589 | map[string]interface{}{ |
| 590 | "rule": "self.length() % 2 == 0", |
| 591 | "message": "must be even", |
| 592 | }, |
| 593 | }, |
| 594 | }, |
| 595 | }, |
| 596 | }, |
| 597 | }, |
| 598 | }, |
| 599 | }, |
| 600 | }, |
| 601 | }, |
| 602 | }, |
| 603 | }, |
| 604 | } |
| 605 | |
| 606 | for _, tc := range cases { |
| 607 | t.Run(tc.name, func(t *testing.T) { |
| 608 | actual, err := generators.ParseCommentTags(tc.t, tc.comments, "+k8s:validation:") |
| 609 | if tc.expectedError != "" { |
| 610 | require.Error(t, err) |
| 611 | require.EqualError(t, err, tc.expectedError) |
| 612 | return |
| 613 | } else { |
| 614 | require.NoError(t, err) |
| 615 | } |
| 616 | |
| 617 | require.Equal(t, tc.expected, actual) |
| 618 | }) |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | // Test comment tag validation function |
| 623 | func TestCommentTags_Validate(t *testing.T) { |
| 624 | |
| 625 | testCases := []struct { |
| 626 | name string |
| 627 | comments []string |
| 628 | t *types.Type |
| 629 | errorMessage string |
| 630 | }{ |
| 631 | { |
| 632 | name: "invalid minimum type", |
| 633 | comments: []string{ |
| 634 | `+k8s:validation:minimum=10.5`, |
| 635 | }, |
| 636 | t: types.String, |
| 637 | errorMessage: "minimum can only be used on numeric types", |
| 638 | }, |
| 639 | { |
| 640 | name: "invalid minLength type", |
| 641 | comments: []string{ |
| 642 | `+k8s:validation:minLength=10`, |
| 643 | }, |
| 644 | t: types.Bool, |
| 645 | errorMessage: "minLength can only be used on string types", |
| 646 | }, |
| 647 | { |
| 648 | name: "invalid minItems type", |
| 649 | comments: []string{ |
| 650 | `+k8s:validation:minItems=10`, |
| 651 | }, |
| 652 | t: types.String, |
| 653 | errorMessage: "minItems can only be used on array types", |
| 654 | }, |
| 655 | { |
| 656 | name: "invalid minProperties type", |
| 657 | comments: []string{ |
| 658 | `+k8s:validation:minProperties=10`, |
| 659 | }, |
| 660 | t: types.String, |
| 661 | errorMessage: "minProperties can only be used on map types", |
| 662 | }, |
| 663 | { |
| 664 | name: "invalid exclusiveMinimum type", |
| 665 | comments: []string{ |
| 666 | `+k8s:validation:exclusiveMinimum=true`, |
| 667 | }, |
| 668 | t: arrayType, |
| 669 | errorMessage: "exclusiveMinimum can only be used on numeric types", |
| 670 | }, |
| 671 | { |
| 672 | name: "invalid maximum type", |
| 673 | comments: []string{ |
| 674 | `+k8s:validation:maximum=10.5`, |
| 675 | }, |
| 676 | t: arrayType, |
| 677 | errorMessage: "maximum can only be used on numeric types", |
| 678 | }, |
| 679 | { |
| 680 | name: "invalid maxLength type", |
| 681 | comments: []string{ |
| 682 | `+k8s:validation:maxLength=10`, |
| 683 | }, |
| 684 | t: mapType, |
| 685 | errorMessage: "maxLength can only be used on string types", |
| 686 | }, |
| 687 | { |
| 688 | name: "invalid maxItems type", |
| 689 | comments: []string{ |
| 690 | `+k8s:validation:maxItems=10`, |
| 691 | }, |
| 692 | t: types.Bool, |
| 693 | errorMessage: "maxItems can only be used on array types", |
| 694 | }, |
| 695 | { |
| 696 | name: "invalid maxProperties type", |
| 697 | comments: []string{ |
| 698 | `+k8s:validation:maxProperties=10`, |
| 699 | }, |
| 700 | t: types.Bool, |
| 701 | errorMessage: "maxProperties can only be used on map types", |
| 702 | }, |
| 703 | { |
| 704 | name: "invalid exclusiveMaximum type", |
| 705 | comments: []string{ |
| 706 | `+k8s:validation:exclusiveMaximum=true`, |
| 707 | }, |
| 708 | t: mapType, |
| 709 | errorMessage: "exclusiveMaximum can only be used on numeric types", |
| 710 | }, |
| 711 | { |
| 712 | name: "invalid pattern type", |
| 713 | comments: []string{ |
| 714 | `+k8s:validation:pattern=".*"`, |
| 715 | }, |
| 716 | t: types.Int, |
| 717 | errorMessage: "pattern can only be used on string types", |
| 718 | }, |
| 719 | { |
| 720 | name: "invalid multipleOf type", |
| 721 | comments: []string{ |
| 722 | `+k8s:validation:multipleOf=10.5`, |
| 723 | }, |
| 724 | t: types.String, |
| 725 | errorMessage: "multipleOf can only be used on numeric types", |
| 726 | }, |
| 727 | { |
| 728 | name: "invalid uniqueItems type", |
| 729 | comments: []string{ |
| 730 | `+k8s:validation:uniqueItems=true`, |
| 731 | }, |
| 732 | t: types.Int, |
| 733 | errorMessage: "uniqueItems can only be used on array types", |
| 734 | }, |
| 735 | { |
| 736 | name: "negative minLength", |
| 737 | comments: []string{ |
| 738 | `+k8s:validation:minLength=-10`, |
| 739 | }, |
| 740 | t: types.String, |
| 741 | errorMessage: "minLength cannot be negative", |
| 742 | }, |
| 743 | { |
| 744 | name: "negative minItems", |
| 745 | comments: []string{ |
| 746 | `+k8s:validation:minItems=-10`, |
| 747 | }, |
| 748 | t: arrayType, |
| 749 | errorMessage: "minItems cannot be negative", |
| 750 | }, |
| 751 | { |
| 752 | name: "negative minProperties", |
| 753 | comments: []string{ |
| 754 | `+k8s:validation:minProperties=-10`, |
| 755 | }, |
| 756 | t: mapType, |
| 757 | errorMessage: "minProperties cannot be negative", |
| 758 | }, |
| 759 | { |
| 760 | name: "negative maxLength", |
| 761 | comments: []string{ |
| 762 | `+k8s:validation:maxLength=-10`, |
| 763 | }, |
| 764 | t: types.String, |
| 765 | errorMessage: "maxLength cannot be negative", |
| 766 | }, |
| 767 | { |
| 768 | name: "negative maxItems", |
| 769 | comments: []string{ |
| 770 | `+k8s:validation:maxItems=-10`, |
| 771 | }, |
| 772 | t: arrayType, |
| 773 | errorMessage: "maxItems cannot be negative", |
| 774 | }, |
| 775 | { |
| 776 | name: "negative maxProperties", |
| 777 | comments: []string{ |
| 778 | `+k8s:validation:maxProperties=-10`, |
| 779 | }, |
| 780 | t: mapType, |
| 781 | errorMessage: "maxProperties cannot be negative", |
| 782 | }, |
| 783 | { |
| 784 | name: "minimum > maximum", |
| 785 | comments: []string{ |
| 786 | `+k8s:validation:minimum=10.5`, |
| 787 | `+k8s:validation:maximum=5.5`, |
| 788 | }, |
| 789 | t: types.Float64, |
| 790 | errorMessage: "minimum 10.500000 is greater than maximum 5.500000", |
| 791 | }, |
| 792 | { |
| 793 | name: "exclusiveMinimum when minimum == maximum", |
| 794 | comments: []string{ |
| 795 | `+k8s:validation:minimum=10.5`, |
| 796 | `+k8s:validation:maximum=10.5`, |
| 797 | `+k8s:validation:exclusiveMinimum=true`, |
| 798 | }, |
| 799 | t: types.Float64, |
| 800 | errorMessage: "exclusiveMinimum/Maximum cannot be set when minimum == maximum", |
| 801 | }, |
| 802 | { |
| 803 | name: "exclusiveMaximum when minimum == maximum", |
| 804 | comments: []string{ |
| 805 | `+k8s:validation:minimum=10.5`, |
| 806 | `+k8s:validation:maximum=10.5`, |
| 807 | `+k8s:validation:exclusiveMaximum=true`, |
| 808 | }, |
| 809 | t: types.Float64, |
| 810 | errorMessage: "exclusiveMinimum/Maximum cannot be set when minimum == maximum", |
| 811 | }, |
| 812 | { |
| 813 | name: "minLength > maxLength", |
| 814 | comments: []string{ |
| 815 | `+k8s:validation:minLength=10`, |
| 816 | `+k8s:validation:maxLength=5`, |
| 817 | }, |
| 818 | t: types.String, |
| 819 | errorMessage: "minLength 10 is greater than maxLength 5", |
| 820 | }, |
| 821 | { |
| 822 | name: "minItems > maxItems", |
| 823 | comments: []string{ |
| 824 | `+k8s:validation:minItems=10`, |
| 825 | `+k8s:validation:maxItems=5`, |
| 826 | }, |
| 827 | t: arrayType, |
| 828 | errorMessage: "minItems 10 is greater than maxItems 5", |
| 829 | }, |
| 830 | { |
| 831 | name: "minProperties > maxProperties", |
| 832 | comments: []string{ |
| 833 | `+k8s:validation:minProperties=10`, |
| 834 | `+k8s:validation:maxProperties=5`, |
| 835 | }, |
| 836 | t: mapType, |
| 837 | errorMessage: "minProperties 10 is greater than maxProperties 5", |
| 838 | }, |
| 839 | { |
| 840 | name: "invalid pattern", |
| 841 | comments: []string{ |
| 842 | `+k8s:validation:pattern="([a-z]+"`, |
| 843 | }, |
| 844 | t: types.String, |
| 845 | errorMessage: "invalid pattern \"([a-z]+\": error parsing regexp: missing closing ): `([a-z]+`", |
| 846 | }, |
| 847 | { |
| 848 | name: "multipleOf = 0", |
| 849 | comments: []string{ |
| 850 | `+k8s:validation:multipleOf=0.0`, |
| 851 | }, |
| 852 | t: types.Int, |
| 853 | errorMessage: "multipleOf cannot be 0", |
| 854 | }, |
| 855 | { |
| 856 | name: "valid comment tags with no invalid validations", |
| 857 | comments: []string{ |
| 858 | `+k8s:validation:pattern=".*"`, |
| 859 | }, |
| 860 | t: types.String, |
| 861 | errorMessage: "", |
| 862 | }, |
| 863 | { |
| 864 | name: "additionalProperties on non-map", |
| 865 | comments: []string{ |
| 866 | `+k8s:validation:additionalProperties:pattern=".*"`, |
| 867 | }, |
| 868 | t: types.String, |
| 869 | errorMessage: "additionalProperties can only be used on map types", |
| 870 | }, |
| 871 | { |
| 872 | name: "properties on non-struct", |
| 873 | comments: []string{ |
| 874 | `+k8s:validation:properties:name:pattern=".*"`, |
| 875 | }, |
| 876 | t: types.String, |
| 877 | errorMessage: "properties can only be used on struct types", |
| 878 | }, |
| 879 | { |
| 880 | name: "items on non-array", |
| 881 | comments: []string{ |
| 882 | `+k8s:validation:items:pattern=".*"`, |
| 883 | }, |
| 884 | t: types.String, |
| 885 | errorMessage: "items can only be used on array types", |
| 886 | }, |
| 887 | { |
| 888 | name: "property missing from struct", |
| 889 | comments: []string{ |
| 890 | `+k8s:validation:properties:name:pattern=".*"`, |
| 891 | }, |
| 892 | t: &types.Type{ |
| 893 | Kind: types.Struct, |
| 894 | Name: types.Name{Name: "struct"}, |
| 895 | Members: []types.Member{ |
| 896 | { |
| 897 | Name: "notname", |
| 898 | Type: types.String, |
| 899 | Tags: `json:"notname"`, |
| 900 | }, |
| 901 | }, |
| 902 | }, |
| 903 | errorMessage: `property used in comment tag "name" not found in struct struct`, |
| 904 | }, |
| 905 | { |
| 906 | name: "nested comments also type checked", |
| 907 | comments: []string{ |
| 908 | `+k8s:validation:properties:name:items:pattern=".*"`, |
| 909 | }, |
| 910 | t: &types.Type{ |
| 911 | Kind: types.Struct, |
| 912 | Name: types.Name{Name: "struct"}, |
| 913 | Members: []types.Member{ |
| 914 | { |
| 915 | Name: "name", |
| 916 | Type: types.String, |
| 917 | Tags: `json:"name"`, |
| 918 | }, |
| 919 | }, |
| 920 | }, |
| 921 | errorMessage: `failed to validate property "name": items can only be used on array types`, |
| 922 | }, |
| 923 | { |
| 924 | name: "nested comments also type checked - passing", |
| 925 | comments: []string{ |
| 926 | `+k8s:validation:properties:name:pattern=".*"`, |
| 927 | }, |
| 928 | t: &types.Type{ |
| 929 | Kind: types.Struct, |
| 930 | Name: types.Name{Name: "struct"}, |
| 931 | Members: []types.Member{ |
| 932 | { |
| 933 | Name: "name", |
| 934 | Type: types.String, |
| 935 | Tags: `json:"name"`, |
| 936 | }, |
| 937 | }, |
| 938 | }, |
| 939 | }, |
| 940 | { |
| 941 | name: "nested marker type checking through alias", |
| 942 | comments: []string{ |
| 943 | `+k8s:validation:properties:name:pattern=".*"`, |
| 944 | }, |
| 945 | t: &types.Type{ |
| 946 | Kind: types.Struct, |
| 947 | Name: types.Name{Name: "struct"}, |
| 948 | Members: []types.Member{ |
| 949 | { |
| 950 | Name: "name", |
| 951 | Tags: `json:"name"`, |
| 952 | Type: &types.Type{ |
| 953 | Kind: types.Alias, |
| 954 | Underlying: &types.Type{ |
| 955 | Kind: types.Slice, |
| 956 | Elem: types.String, |
| 957 | }, |
| 958 | }, |
| 959 | }, |
| 960 | }, |
| 961 | }, |
| 962 | errorMessage: `failed to validate property "name": pattern can only be used on string types`, |
| 963 | }, |
| 964 | { |
| 965 | name: "ignore unknown field with unparsable value", |
| 966 | comments: []string{ |
| 967 | `+k8s:validation:xyz=a=b`, // a=b is not a valid value |
| 968 | }, |
| 969 | t: &types.Type{ |
| 970 | Kind: types.Struct, |
| 971 | Name: types.Name{Name: "struct"}, |
| 972 | Members: []types.Member{ |
| 973 | { |
| 974 | Name: "name", |
| 975 | Type: types.String, |
| 976 | Tags: `json:"name"`, |
| 977 | }, |
| 978 | }, |
| 979 | }, |
| 980 | }, |
| 981 | } |
| 982 | |
| 983 | for _, tc := range testCases { |
| 984 | t.Run(tc.name, func(t *testing.T) { |
| 985 | _, err := generators.ParseCommentTags(tc.t, tc.comments, "+k8s:validation:") |
| 986 | if tc.errorMessage != "" { |
| 987 | require.Error(t, err) |
| 988 | require.Equal(t, "invalid marker comments: "+tc.errorMessage, err.Error()) |
| 989 | } else { |
| 990 | require.NoError(t, err) |
| 991 | } |
| 992 | }) |
| 993 | } |
| 994 | } |