@@ -19,6 +19,7 @@ import (
19
19
"strings"
20
20
"testing"
21
21
22
+ "github.com/stretchr/testify/require"
22
23
"knative.dev/pkg/apis"
23
24
)
24
25
@@ -30,9 +31,9 @@ func TestImagePatternValidation(t *testing.T) {
30
31
policy ClusterImagePolicy
31
32
}{
32
33
{
33
- name : "Should fail when both regex and glob are present: %v " ,
34
+ name : "Should fail when both regex and glob are present" ,
34
35
expectErr : true ,
35
- errorString : "expected exactly one, got both: spec.images[0].glob, spec.images[0].regex" ,
36
+ errorString : "expected exactly one, got both: spec.images[0].glob, spec.images[0].regex\n invalid value: **: spec.images[0].glob \n glob match supports only a single * as a trailing character \n missing field(s): spec.authorities \n must not set the field(s): spec.images[0].regex " ,
36
37
policy : ClusterImagePolicy {
37
38
Spec : ClusterImagePolicySpec {
38
39
Images : []ImagePattern {
@@ -45,9 +46,9 @@ func TestImagePatternValidation(t *testing.T) {
45
46
},
46
47
},
47
48
{
48
- name : "Should fail when neither regex nor glob are present: %v " ,
49
+ name : "Should fail when neither regex nor glob are present" ,
49
50
expectErr : true ,
50
- errorString : "expected exactly one, got neither: spec.images[0].glob, spec.images[0].regex" ,
51
+ errorString : "expected exactly one, got neither: spec.images[0].glob, spec.images[0].regex\n missing field(s): spec.authorities " ,
51
52
policy : ClusterImagePolicy {
52
53
Spec : ClusterImagePolicySpec {
53
54
Images : []ImagePattern {
@@ -57,9 +58,9 @@ func TestImagePatternValidation(t *testing.T) {
57
58
},
58
59
},
59
60
{
60
- name : "Glob should fail with multiple *: %v " ,
61
+ name : "Glob should fail with multiple *" ,
61
62
expectErr : true ,
62
- errorString : "glob match supports only a single * as a trailing character" ,
63
+ errorString : "invalid value: **: spec.images[0]. glob\n glob match supports only a single * as a trailing character\n missing field(s): spec.authorities " ,
63
64
policy : ClusterImagePolicy {
64
65
Spec : ClusterImagePolicySpec {
65
66
Images : []ImagePattern {
@@ -71,9 +72,9 @@ func TestImagePatternValidation(t *testing.T) {
71
72
},
72
73
},
73
74
{
74
- name : "Glob should fail with non-trailing *: %v " ,
75
+ name : "Glob should fail with non-trailing *" ,
75
76
expectErr : true ,
76
- errorString : "glob match supports only * as a trailing character" ,
77
+ errorString : "invalid value: foo*bar: spec.images[0]. glob\n glob match supports only * as a trailing character\n missing field(s): spec.authorities " ,
77
78
policy : ClusterImagePolicy {
78
79
Spec : ClusterImagePolicySpec {
79
80
Images : []ImagePattern {
@@ -84,13 +85,26 @@ func TestImagePatternValidation(t *testing.T) {
84
85
},
85
86
},
86
87
},
88
+ {
89
+ name : "missing image and authorities in the spec" ,
90
+ expectErr : true ,
91
+ errorString : "missing field(s): spec.authorities, spec.images" ,
92
+ policy : ClusterImagePolicy {
93
+ Spec : ClusterImagePolicySpec {},
94
+ },
95
+ },
87
96
}
88
97
89
98
for _ , test := range tests {
90
- err := test .policy .Validate (context .TODO ())
91
- if test .expectErr && ! strings .Contains (err .Error (), test .errorString ) {
92
- t .Errorf (test .name , err )
93
- }
99
+ t .Run (test .name , func (t * testing.T ) {
100
+ err := test .policy .Validate (context .TODO ())
101
+ if test .expectErr {
102
+ require .NotNil (t , err )
103
+ require .EqualError (t , err , test .errorString )
104
+ } else {
105
+ require .Nil (t , err )
106
+ }
107
+ })
94
108
}
95
109
}
96
110
@@ -102,9 +116,9 @@ func TestKeyValidation(t *testing.T) {
102
116
policy ClusterImagePolicy
103
117
}{
104
118
{
105
- name : "Should fail when key has multiple properties: %v " ,
119
+ name : "Should fail when key has multiple properties" ,
106
120
expectErr : true ,
107
- errorString : "expected exactly one, got both" ,
121
+ errorString : "expected exactly one, got both: spec.authorities[0].key.data, spec.authorities[0].key.kms, spec.authorities[0].key.secretref " ,
108
122
policy : ClusterImagePolicy {
109
123
Spec : ClusterImagePolicySpec {
110
124
Images : []ImagePattern {
@@ -124,9 +138,9 @@ func TestKeyValidation(t *testing.T) {
124
138
},
125
139
},
126
140
{
127
- name : "Should fail when key has mixed valid and invalid data: %v " ,
141
+ name : "Should fail when key has mixed valid and invalid data" ,
128
142
expectErr : true ,
129
- errorString : "invalid value: -----BEGIN PUBLIC KEY-----\n MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEaEOVJCFtduYr3xqTxeRWSW32CY/s\n TBNZj4oIUPl8JvhVPJ1TKDPlNcuT4YphSt6t3yOmMvkdQbCj8broX6vijw==\n -----END PUBLIC KEY-----\n ---somedata---" ,
143
+ errorString : "invalid value: -----BEGIN PUBLIC KEY-----\n MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEaEOVJCFtduYr3xqTxeRWSW32CY/s\n TBNZj4oIUPl8JvhVPJ1TKDPlNcuT4YphSt6t3yOmMvkdQbCj8broX6vijw==\n -----END PUBLIC KEY-----\n ---somedata---: spec.authorities[0].key.data " ,
130
144
policy : ClusterImagePolicy {
131
145
Spec : ClusterImagePolicySpec {
132
146
Images : []ImagePattern {
@@ -145,9 +159,9 @@ func TestKeyValidation(t *testing.T) {
145
159
},
146
160
},
147
161
{
148
- name : "Should fail when key has malformed pubkey data: %v " ,
162
+ name : "Should fail when key has malformed pubkey data" ,
149
163
expectErr : true ,
150
- errorString : "invalid value: ---some key data----" ,
164
+ errorString : "invalid value: ---some key data----: spec.authorities[0].key.data " ,
151
165
policy : ClusterImagePolicy {
152
166
Spec : ClusterImagePolicySpec {
153
167
Images : []ImagePattern {
@@ -166,9 +180,9 @@ func TestKeyValidation(t *testing.T) {
166
180
},
167
181
},
168
182
{
169
- name : "Should fail when key is empty: %v " ,
183
+ name : "Should fail when key is empty" ,
170
184
expectErr : true ,
171
- errorString : "expected exactly one, got neither" ,
185
+ errorString : "expected exactly one, got neither: spec.authorities[0].key.data, spec.authorities[0].key.kms, spec.authorities[0].key.secretref " ,
172
186
policy : ClusterImagePolicy {
173
187
Spec : ClusterImagePolicySpec {
174
188
Images : []ImagePattern {
@@ -185,7 +199,7 @@ func TestKeyValidation(t *testing.T) {
185
199
},
186
200
},
187
201
{
188
- name : "Should fail when regex is given: %v " ,
202
+ name : "Should fail when regex is given" ,
189
203
expectErr : true ,
190
204
errorString : "must not set the field(s): spec.images[0].regex" ,
191
205
policy : ClusterImagePolicy {
@@ -206,8 +220,8 @@ func TestKeyValidation(t *testing.T) {
206
220
},
207
221
},
208
222
{
209
- name : "Should pass when key has only one property: %v " ,
210
- errorString : "" ,
223
+ name : "Should pass when key has only one property" ,
224
+ expectErr : false ,
211
225
policy : ClusterImagePolicy {
212
226
Spec : ClusterImagePolicySpec {
213
227
Images : []ImagePattern {
@@ -228,13 +242,15 @@ func TestKeyValidation(t *testing.T) {
228
242
}
229
243
230
244
for _ , test := range tests {
231
- err := test .policy .Validate (context .TODO ())
232
- if test .expectErr && ! strings .Contains (err .Error (), test .errorString ) {
233
- t .Errorf (test .name , err )
234
- }
235
- if ! test .expectErr && err != nil {
236
- t .Errorf (test .name , err )
237
- }
245
+ t .Run (test .name , func (t * testing.T ) {
246
+ err := test .policy .Validate (context .TODO ())
247
+ if test .expectErr {
248
+ require .NotNil (t , err )
249
+ require .EqualError (t , err , test .errorString )
250
+ } else {
251
+ require .Nil (t , err )
252
+ }
253
+ })
238
254
}
239
255
}
240
256
@@ -246,7 +262,7 @@ func TestKeylessValidation(t *testing.T) {
246
262
policy ClusterImagePolicy
247
263
}{
248
264
{
249
- name : "Should fail when keyless is empty: %v " ,
265
+ name : "Should fail when keyless is empty" ,
250
266
expectErr : true ,
251
267
errorString : "expected exactly one, got neither" ,
252
268
policy : ClusterImagePolicy {
@@ -265,7 +281,7 @@ func TestKeylessValidation(t *testing.T) {
265
281
},
266
282
},
267
283
{
268
- name : "Should fail when keyless has multiple properties: %v " ,
284
+ name : "Should fail when keyless has multiple properties" ,
269
285
expectErr : true ,
270
286
errorString : "expected exactly one, got both" ,
271
287
policy : ClusterImagePolicy {
@@ -291,8 +307,8 @@ func TestKeylessValidation(t *testing.T) {
291
307
},
292
308
},
293
309
{
294
- name : "Should pass when a valid keyless ref is specified: %v " ,
295
- errorString : "" ,
310
+ name : "Should pass when a valid keyless ref is specified" ,
311
+ expectErr : false ,
296
312
policy : ClusterImagePolicy {
297
313
Spec : ClusterImagePolicySpec {
298
314
Images : []ImagePattern {
@@ -313,14 +329,17 @@ func TestKeylessValidation(t *testing.T) {
313
329
},
314
330
},
315
331
}
332
+
316
333
for _ , test := range tests {
317
- err := test .policy .Validate (context .TODO ())
318
- if test .expectErr && ! strings .Contains (err .Error (), test .errorString ) {
319
- t .Errorf (test .name , err )
320
- }
321
- if ! test .expectErr && err != nil {
322
- t .Errorf (test .name , err )
323
- }
334
+ t .Run (test .name , func (t * testing.T ) {
335
+ err := test .policy .Validate (context .TODO ())
336
+ if test .expectErr {
337
+ require .NotNil (t , err )
338
+ require .True (t , strings .Contains (err .Error (), test .errorString ))
339
+ } else {
340
+ require .Nil (t , err )
341
+ }
342
+ })
324
343
}
325
344
}
326
345
@@ -332,9 +351,9 @@ func TestAuthoritiesValidation(t *testing.T) {
332
351
policy ClusterImagePolicy
333
352
}{
334
353
{
335
- name : "Should fail when keyless is empty: %v " ,
354
+ name : "Should fail when keyless is empty" ,
336
355
expectErr : true ,
337
- errorString : "expected exactly one, got both" ,
356
+ errorString : "expected exactly one, got both: spec.authorities[0].key, spec.authorities[0].keyless \n expected exactly one, got neither: spec.authorities[0].key.data, spec.authorities[0].key.kms, spec.authorities[0].key.secretref, spec.authorities[0].keyless.ca-key, spec.authorities[0].keyless.identities, spec.authorities[0].keyless.url " ,
338
357
policy : ClusterImagePolicy {
339
358
Spec : ClusterImagePolicySpec {
340
359
Images : []ImagePattern {
@@ -351,11 +370,10 @@ func TestAuthoritiesValidation(t *testing.T) {
351
370
},
352
371
},
353
372
},
354
-
355
373
{
356
- name : "Should fail when keyless is empty: %v " ,
374
+ name : "Should fail when keyless is empty" ,
357
375
expectErr : true ,
358
- errorString : "At least one authority should be defined " ,
376
+ errorString : "missing field(s): spec.authorities " ,
359
377
policy : ClusterImagePolicy {
360
378
Spec : ClusterImagePolicySpec {
361
379
Images : []ImagePattern {
@@ -369,13 +387,15 @@ func TestAuthoritiesValidation(t *testing.T) {
369
387
},
370
388
}
371
389
for _ , test := range tests {
372
- err := test .policy .Validate (context .TODO ())
373
- if test .expectErr && ! strings .Contains (err .Error (), test .errorString ) {
374
- t .Errorf (test .name , err )
375
- }
376
- if ! test .expectErr && err != nil {
377
- t .Errorf (test .name , err )
378
- }
390
+ t .Run (test .name , func (t * testing.T ) {
391
+ err := test .policy .Validate (context .TODO ())
392
+ if test .expectErr {
393
+ require .NotNil (t , err )
394
+ require .EqualError (t , err , test .errorString )
395
+ } else {
396
+ require .Nil (t , err )
397
+ }
398
+ })
379
399
}
380
400
}
381
401
@@ -387,9 +407,9 @@ func TestIdentitiesValidation(t *testing.T) {
387
407
policy ClusterImagePolicy
388
408
}{
389
409
{
390
- name : "Should fail when identities is empty: %v " ,
410
+ name : "Should fail when identities is empty" ,
391
411
expectErr : true ,
392
- errorString : "At least one identity must be provided " ,
412
+ errorString : "missing field(s): spec.authorities[0].keyless.identities " ,
393
413
policy : ClusterImagePolicy {
394
414
Spec : ClusterImagePolicySpec {
395
415
Images : []ImagePattern {
@@ -408,8 +428,8 @@ func TestIdentitiesValidation(t *testing.T) {
408
428
},
409
429
},
410
430
{
411
- name : "Should pass when identities is valid: %v " ,
412
- errorString : "" ,
431
+ name : "Should pass when identities is valid" ,
432
+ expectErr : false ,
413
433
policy : ClusterImagePolicy {
414
434
Spec : ClusterImagePolicySpec {
415
435
Images : []ImagePattern {
@@ -433,12 +453,14 @@ func TestIdentitiesValidation(t *testing.T) {
433
453
},
434
454
}
435
455
for _ , test := range tests {
436
- err := test .policy .Validate (context .TODO ())
437
- if test .expectErr && ! strings .Contains (err .Error (), test .errorString ) {
438
- t .Errorf (test .name , err )
439
- }
440
- if ! test .expectErr && err != nil {
441
- t .Errorf (test .name , err )
442
- }
456
+ t .Run (test .name , func (t * testing.T ) {
457
+ err := test .policy .Validate (context .TODO ())
458
+ if test .expectErr {
459
+ require .NotNil (t , err )
460
+ require .EqualError (t , err , test .errorString )
461
+ } else {
462
+ require .Nil (t , err )
463
+ }
464
+ })
443
465
}
444
466
}
0 commit comments