throw error when file with key/values is missing a Kind, add acc test

This commit is contained in:
Yann Hamon 2020-11-15 23:44:15 +01:00
parent d0ac45222c
commit f63d329742
2 changed files with 23 additions and 8 deletions

View file

@ -82,6 +82,12 @@
[ "$output" = "Summary: 0 resource found in 1 file - Valid: 0, Invalid: 0, Errors: 0, Skipped: 0" ]
}
@test "Fail when parsing a config that is missing a Kind" {
run bin/kubeconform -summary fixtures/missing_kind.yaml
[ "$status" -eq 1 ]
[[ "$output" = *resource\ missing\ a\ Kind* ]]
}
@test "Fail when parsing a config with CRD" {
run bin/kubeconform fixtures/test_crd.yaml
[ "$status" -eq 1 ]

View file

@ -105,15 +105,15 @@ func (val *v) ValidateResource(res resource.Resource) Result {
return ok
}
if len(res.Bytes) == 0 {
return Result{Resource: res, Err: nil, Status: Empty}
}
sig, err := res.Signature()
if err != nil {
return Result{Resource: res, Err: fmt.Errorf("error while parsing: %s", err), Status: Error}
}
if sig.Kind == "" {
return Result{Resource: res, Err: nil, Status: Empty}
}
if skip(*sig) {
return Result{Resource: res, Err: nil, Status: Skipped}
}
@ -122,6 +122,19 @@ func (val *v) ValidateResource(res resource.Resource) Result {
return Result{Resource: res, Err: fmt.Errorf("prohibited resource kind %s", sig.Kind), Status: Error}
}
var r map[string]interface{}
if err := yaml.Unmarshal(res.Bytes, &r); err != nil {
return Result{Resource: res, Status: Error, Err: fmt.Errorf("error unmarshalling resource: %s", err)}
}
if r == nil { // Resource is empty
return Result{Resource: res, Err: nil, Status: Empty}
}
if sig.Kind == "" && r != nil { // Resource contains key/values but no Kind
return Result{Resource: res, Err: fmt.Errorf("resource missing a Kind"), Status: Error}
}
cached := false
var schema *gojsonschema.Schema
cacheKey := ""
@ -149,10 +162,6 @@ func (val *v) ValidateResource(res resource.Resource) Result {
}
}
var r map[string]interface{}
if err := yaml.Unmarshal(res.Bytes, &r); err != nil {
return Result{Resource: res, Status: Error, Err: fmt.Errorf("error unmarshalling resource: %s", err)}
}
resourceLoader := gojsonschema.NewGoLoader(r)
results, err := schema.Validate(resourceLoader)