mirror of
https://github.com/yannh/kubeconform.git
synced 2026-04-14 16:34:46 +00:00
avoid double unmarshalling
This commit is contained in:
parent
4afe9b1977
commit
29a8f4c09e
7 changed files with 72 additions and 17 deletions
|
|
@ -109,7 +109,12 @@ func (val *v) ValidateResource(res resource.Resource) Result {
|
|||
return Result{Resource: res, Err: nil, Status: Empty}
|
||||
}
|
||||
|
||||
sig, err := res.Signature()
|
||||
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)}
|
||||
}
|
||||
|
||||
sig, err := res.SignatureFromMap(r)
|
||||
if err != nil {
|
||||
return Result{Resource: res, Err: fmt.Errorf("error while parsing: %s", err), Status: Error}
|
||||
}
|
||||
|
|
@ -122,11 +127,6 @@ 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}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ func TestValidate(t *testing.T) {
|
|||
{
|
||||
"valid resource",
|
||||
[]byte(`
|
||||
Kind: name
|
||||
kind: name
|
||||
firstName: foo
|
||||
lastName: bar
|
||||
`),
|
||||
|
|
@ -26,7 +26,7 @@ lastName: bar
|
|||
"title": "Example Schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Kind": {
|
||||
"kind": {
|
||||
"type": "string"
|
||||
},
|
||||
"firstName": {
|
||||
|
|
@ -48,7 +48,7 @@ lastName: bar
|
|||
{
|
||||
"invalid resource",
|
||||
[]byte(`
|
||||
Kind: name
|
||||
kind: name
|
||||
firstName: foo
|
||||
lastName: bar
|
||||
`),
|
||||
|
|
@ -56,7 +56,7 @@ lastName: bar
|
|||
"title": "Example Schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Kind": {
|
||||
"kind": {
|
||||
"type": "string"
|
||||
},
|
||||
"firstName": {
|
||||
|
|
@ -78,14 +78,14 @@ lastName: bar
|
|||
{
|
||||
"missing required field",
|
||||
[]byte(`
|
||||
Kind: name
|
||||
kind: name
|
||||
firstName: foo
|
||||
`),
|
||||
[]byte(`{
|
||||
"title": "Example Schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Kind": {
|
||||
"kind": {
|
||||
"type": "string"
|
||||
},
|
||||
"firstName": {
|
||||
|
|
@ -107,7 +107,7 @@ firstName: foo
|
|||
{
|
||||
"resource has invalid yaml",
|
||||
[]byte(`
|
||||
Kind: name
|
||||
kind: name
|
||||
firstName foo
|
||||
lastName: bar
|
||||
`),
|
||||
|
|
@ -115,7 +115,7 @@ lastName: bar
|
|||
"title": "Example Schema",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"Kind": {
|
||||
"kind": {
|
||||
"type": "string"
|
||||
},
|
||||
"firstName": {
|
||||
|
|
@ -151,7 +151,7 @@ lastName: bar
|
|||
regs: nil,
|
||||
}
|
||||
if got := val.ValidateResource(resource.Resource{Bytes: testCase.rawResource}); got.Status != testCase.expect {
|
||||
t.Errorf("%d - expected %d, got %d", i, testCase.expect, got.Status)
|
||||
t.Errorf("%d - expected %d, got %d: %s", i, testCase.expect, got.Status, got.Err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue