@@ -15,67 +15,78 @@ import (
15
15
func TestValidateCRD (t * testing.T ) {
16
16
var table = []struct {
17
17
description string
18
- directory string
18
+ filePath string
19
19
version string
20
20
hasError bool
21
21
errString string
22
22
}{
23
23
{
24
- description : "registryv1 bundle/valid crd " ,
25
- directory : "./testdata/v1beta1.crd.yaml" ,
24
+ description : "registryv1 bundle/valid v1beta1 CRD " ,
25
+ filePath : "./testdata/v1beta1.crd.yaml" ,
26
26
version : "v1beta1" ,
27
27
hasError : false ,
28
28
errString : "" ,
29
29
},
30
30
{
31
- description : "registryv1 bundle/ invalid crd " ,
32
- directory : "./testdata/duplicateVersions.crd.yaml" ,
31
+ description : "registryv1 bundle invalid v1beta1 CRD duplicate version " ,
32
+ filePath : "./testdata/duplicateVersions.crd.yaml" ,
33
33
version : "v1beta1" ,
34
34
hasError : true ,
35
35
errString : "must contain unique version names" ,
36
36
},
37
37
{
38
- description : "registryv1 bundle/invalid crd " ,
39
- directory : "./testdata/v1.crd.yaml" ,
38
+ description : "registryv1 bundle/valid v1 CRD " ,
39
+ filePath : "./testdata/v1.crd.yaml" ,
40
40
version : "v1" ,
41
41
hasError : false ,
42
42
errString : "" ,
43
43
},
44
44
{
45
- description : "registryv1 bundle/ invalid crd " ,
46
- directory : "./testdata/deprecatedVersion.crd.yaml" ,
45
+ description : "registryv1 bundle invalid v1 CRD deprecated .spec.version field " ,
46
+ filePath : "./testdata/deprecatedVersion.crd.yaml" ,
47
47
version : "v1" ,
48
48
hasError : true ,
49
49
errString : "must have exactly one version marked as storage version" ,
50
50
},
51
+ {
52
+ description : "registryv1 bundle invalid CRD no conversionReviewVersions" ,
53
+ filePath : "./testdata/noConversionReviewVersions.crd.yaml" ,
54
+ version : "v1" ,
55
+ hasError : true ,
56
+ errString : "spec.conversion.conversionReviewVersions: Required value" ,
57
+ },
51
58
}
52
-
53
59
for _ , tt := range table {
54
- b , err := ioutil .ReadFile (tt .directory )
60
+ b , err := ioutil .ReadFile (tt .filePath )
55
61
if err != nil {
56
- t .Fatalf ("Error reading CRD path %s: %v" , tt .directory , err )
62
+ t .Fatalf ("Error reading CRD path %s: %v" , tt .filePath , err )
57
63
}
58
64
59
65
results := []errors.ManifestResult {}
60
66
switch tt .version {
61
67
case "v1" :
62
68
crd := & v1.CustomResourceDefinition {}
63
69
if err = yaml .Unmarshal (b , crd ); err != nil {
64
- t .Fatalf ("Error unmarshalling CRD at path %s: %v" , tt .directory , err )
70
+ t .Fatalf ("Error unmarshalling CRD at path %s: %v" , tt .filePath , err )
65
71
}
66
72
results = CRDValidator .Validate (crd )
67
73
default :
68
74
crd := & v1beta1.CustomResourceDefinition {}
69
75
if err = yaml .Unmarshal (b , crd ); err != nil {
70
- t .Fatalf ("Error unmarshalling CRD at path %s: %v" , tt .directory , err )
76
+ t .Fatalf ("Error unmarshalling CRD at path %s: %v" , tt .filePath , err )
71
77
}
72
78
results = CRDValidator .Validate (crd )
73
79
}
74
80
75
81
if len (results ) > 0 {
76
- require .Equal (t , results [0 ].HasError (), tt .hasError )
77
82
if results [0 ].HasError () {
78
- require .Contains (t , results [0 ].Errors [0 ].Error (), tt .errString )
83
+ if ! tt .hasError {
84
+ t .Errorf ("%s: expected no errors, got: %+q" , tt .description , results [0 ].Errors )
85
+ } else {
86
+ require .Contains (t , results [0 ].Errors [0 ].Error (), tt .errString , tt .description )
87
+ }
88
+ } else if tt .hasError {
89
+ t .Errorf ("%s: expected error %q, got none" , tt .description , tt .errString )
79
90
}
80
91
}
81
92
}
0 commit comments