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