-
Notifications
You must be signed in to change notification settings - Fork 63
/
validate_test.go
403 lines (384 loc) · 9.28 KB
/
validate_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
/*
Copyright 2018 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 typed_test
import (
"fmt"
"strings"
"testing"
"sigs.k8s.io/structured-merge-diff/v4/schema"
"sigs.k8s.io/structured-merge-diff/v4/typed"
)
type validationTestCase struct {
name string
rootTypeName string
schema typed.YAMLObject
validObjects []typed.YAMLObject
invalidObjects []typed.YAMLObject
// duplicatesObjects are valid with AllowDuplicates validation, invalid otherwise.
duplicatesObjects []typed.YAMLObject
}
var validationCases = []validationTestCase{{
name: "simple pair",
rootTypeName: "stringPair",
schema: `types:
- name: stringPair
map:
fields:
- name: key
type:
scalar: string
- name: value
type:
namedType: __untyped_atomic_
- name: __untyped_atomic_
scalar: untyped
list:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
map:
elementType:
namedType: __untyped_atomic_
elementRelationship: atomic
`,
validObjects: []typed.YAMLObject{
`{"key":"foo","value":1}`,
`{"key":"foo","value":{}}`,
`{"key":"foo","value":null}`,
`{"key":"foo"}`,
`{"key":"foo","value":true}`,
`{"key":null}`,
},
invalidObjects: []typed.YAMLObject{
`{"key":true,"value":1}`,
`{"key":1,"value":{}}`,
`{"key":false,"value":null}`,
`{"key":[1, 2]}`,
`{"key":{"foo":true}}`,
},
}, {
name: "struct grab bag",
rootTypeName: "myStruct",
schema: `types:
- name: myStruct
map:
fields:
- name: numeric
type:
scalar: numeric
- name: string
type:
scalar: string
- name: bool
type:
scalar: boolean
- name: setStr
type:
list:
elementType:
scalar: string
elementRelationship: associative
- name: setBool
type:
list:
elementType:
scalar: boolean
elementRelationship: associative
- name: setNumeric
type:
list:
elementType:
scalar: numeric
elementRelationship: associative
`,
validObjects: []typed.YAMLObject{
`{"numeric":null}`,
`{"numeric":1}`,
`{"numeric":3.14159}`,
`{"string":null}`,
`{"string":"aoeu"}`,
`{"bool":null}`,
`{"bool":true}`,
`{"bool":false}`,
`{"setStr":["a","b","c"]}`,
`{"setBool":[true,false]}`,
`{"setNumeric":[1,2,3,3.14159]}`,
},
invalidObjects: []typed.YAMLObject{
`{"numeric":["foo"]}`,
`{"numeric":{"a":1}}`,
`{"numeric":"foo"}`,
`{"numeric":true}`,
`{"string":1}`,
`{"string":3.5}`,
`{"string":true}`,
`{"string":{"a":1}}`,
`{"string":["foo"]}`,
`{"bool":1}`,
`{"bool":3.5}`,
`{"bool":"aoeu"}`,
`{"bool":{"a":1}}`,
`{"bool":["foo"]}`,
`{"setStr":[1]}`,
`{"setStr":[true]}`,
`{"setStr":[1.5]}`,
`{"setStr":[null]}`,
`{"setStr":[{}]}`,
`{"setStr":[[]]}`,
`{"setBool":[1]}`,
`{"setBool":[1.5]}`,
`{"setBool":[null]}`,
`{"setBool":[{}]}`,
`{"setBool":[[]]}`,
`{"setBool":["a"]}`,
`{"setNumeric":[null]}`,
`{"setNumeric":[true]}`,
`{"setNumeric":["a"]}`,
`{"setNumeric":[[]]}`,
`{"setNumeric":[{}]}`,
}, duplicatesObjects: []typed.YAMLObject{
`{"setStr":["a","a"]}`,
`{"setBool":[true,false,true]}`,
`{"setNumeric":[1,2,3,3.14159,1]}`,
},
}, {
name: "associative list",
rootTypeName: "myRoot",
schema: `types:
- name: myRoot
map:
fields:
- name: list
type:
namedType: myList
- name: atomicList
type:
namedType: mySequence
- name: myList
list:
elementType:
namedType: myElement
elementRelationship: associative
keys:
- key
- id
- name: mySequence
list:
elementType:
scalar: string
elementRelationship: atomic
- name: myElement
map:
fields:
- name: key
type:
scalar: string
- name: id
type:
scalar: numeric
- name: value
type:
namedType: myValue
- name: bv
type:
scalar: boolean
- name: nv
type:
scalar: numeric
- name: myValue
map:
elementType:
scalar: string
`,
validObjects: []typed.YAMLObject{
`{"list":[]}`,
`{"list":[{"key":"a","id":1,"value":{"a":"a"}}]}`,
`{"list":[{"key":"a","id":1},{"key":"a","id":2},{"key":"b","id":1}]}`,
`{"atomicList":["a","a","a"]}`,
},
invalidObjects: []typed.YAMLObject{
`{"key":true,"value":1}`,
`{"list":{"key":true,"value":1}}`,
`{"list":true}`,
`true`,
`{"list":[{"key":true,"value":1}]}`,
`{"list":[{"key":[],"value":1}]}`,
`{"list":[{"key":{},"value":1}]}`,
`{"list":[{"key":1.5,"value":1}]}`,
`{"list":[{"key":1,"value":1}]}`,
`{"list":[{"key":null,"value":1}]}`,
`{"list":[{},{}]}`,
`{"list":[{},null]}`,
`{"list":[[]]}`,
`{"list":[null]}`,
`{"list":[{}]}`,
`{"list":[{"value":{"a":"a"},"bv":true,"nv":3.14}]}`,
`{"list":[{"key":"a","id":1,"value":{"a":1}}]}`,
`{"list":[{"key":"a","id":1,"value":{"a":"a"},"bv":"true","nv":3.14}]}`,
`{"list":[{"key":"a","id":1,"value":{"a":"a"},"bv":true,"nv":false}]}`,
}, duplicatesObjects: []typed.YAMLObject{
`{"list":[{"key":"a","id":1},{"key":"a","id":1}]}`,
},
}}
func (tt validationTestCase) test(t *testing.T) {
parser, err := typed.NewParser(tt.schema)
if err != nil {
t.Fatalf("failed to create schema: %v", err)
}
pt := parser.Type(tt.rootTypeName)
for i, v := range tt.validObjects {
v := v
t.Run(fmt.Sprintf("%v-valid-%v", tt.name, i), func(t *testing.T) {
t.Parallel()
_, err := pt.FromYAML(v)
if err != nil {
t.Errorf("failed to parse/validate yaml: %v\n%v", err, v)
}
})
}
for i, iv := range tt.invalidObjects {
iv := iv
t.Run(fmt.Sprintf("%v-invalid-%v", tt.name, i), func(t *testing.T) {
t.Parallel()
_, err := pt.FromYAML(iv)
if err == nil {
t.Fatalf("Object should fail:\n%v", iv)
}
if strings.Contains(err.Error(), "invalid atom") {
t.Errorf("Error should be useful, but got: %v\n%v", err, iv)
}
})
}
for i, iv := range tt.duplicatesObjects {
iv := iv
t.Run(fmt.Sprintf("%v-duplicates-%v", tt.name, i), func(t *testing.T) {
t.Parallel()
_, err := pt.FromYAML(iv)
if err == nil {
t.Fatalf("Object should fail:\n%v", iv)
}
if strings.Contains(err.Error(), "invalid atom") {
t.Errorf("Error should be useful, but got: %v\n%v", err, iv)
}
_, err = pt.FromYAML(iv, typed.AllowDuplicates)
if err != nil {
t.Errorf("failed to parse/validate yaml: %v\n%v", err, iv)
}
})
}
}
func TestSchemaValidation(t *testing.T) {
for _, tt := range validationCases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
tt.test(t)
})
}
}
func TestSchemaSchema(t *testing.T) {
// Verify that the schema schema validates itself.
_, err := typed.NewParser(typed.YAMLObject(schema.SchemaSchemaYAML))
if err != nil {
t.Fatalf("failed to create schemaschema: %v", err)
}
}
func BenchmarkValidateStructured(b *testing.B) {
type Primitives struct {
s string
i int64
f float64
b bool
}
primitive1 := Primitives{s: "string1"}
primitive2 := Primitives{i: 100}
primitive3 := Primitives{f: 3.14}
primitive4 := Primitives{b: true}
type Example struct {
listOfPrimitives []Primitives
mapOfPrimitives map[string]Primitives
mapOfLists map[string][]Primitives
}
tests := []struct {
name string
rootTypeName string
schema typed.YAMLObject
object interface{}
}{
{
name: "struct",
rootTypeName: "example",
schema: `types:
- name: example
map:
fields:
- name: listOfPrimitives
type:
list:
elementType:
namedType: primitives
- name: mapOfPrimitives
type:
map:
elementType:
namedType: primitives
- name: mapOfLists
type:
map:
elementType:
list:
elementType:
namedType: primitives
- name: primitives
map:
fields:
- name: s
type:
scalar: string
- name: i
type:
scalar: numeric
- name: f
type:
scalar: numeric
- name: b
type:
scalar: boolean
`,
object: &Example{
listOfPrimitives: []Primitives{primitive1, primitive2, primitive3, primitive4},
mapOfPrimitives: map[string]Primitives{"1": primitive1, "2": primitive2, "3": primitive3, "4": primitive4},
mapOfLists: map[string][]Primitives{
"1": {primitive1, primitive2, primitive3, primitive4},
"2": {primitive1, primitive2, primitive3, primitive4},
},
},
},
}
for _, test := range tests {
b.Run(test.name, func(b *testing.B) {
parser, err := typed.NewParser(test.schema)
if err != nil {
b.Fatalf("failed to create schema: %v", err)
}
pt := parser.Type(test.rootTypeName)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
tv, err := pt.FromStructured(test.object)
if err != nil {
b.Errorf("failed to parse/validate yaml: %v\n%v", err, tv)
}
}
})
}
}