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