mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-19 01:47:02 +00:00
Merge branch 'master' into improve-validator-test
This commit is contained in:
commit
222545d29e
2 changed files with 68 additions and 2 deletions
|
|
@ -223,8 +223,9 @@ func (val *v) ValidateWithContext(ctx context.Context, filename string, r io.Rea
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case res, ok := <-resourcesChan:
|
case res, ok := <-resourcesChan:
|
||||||
validationResults = append(validationResults, val.ValidateResource(res))
|
if ok {
|
||||||
if !ok {
|
validationResults = append(validationResults, val.ValidateResource(res))
|
||||||
|
} else {
|
||||||
resourcesChan = nil
|
resourcesChan = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
package validator
|
package validator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/yannh/kubeconform/pkg/registry"
|
"github.com/yannh/kubeconform/pkg/registry"
|
||||||
|
|
@ -412,3 +415,65 @@ lastName: bar
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateFile(t *testing.T) {
|
||||||
|
inputData := []byte(`
|
||||||
|
kind: name
|
||||||
|
apiVersion: v1
|
||||||
|
firstName: bar
|
||||||
|
lastName: qux
|
||||||
|
---
|
||||||
|
kind: name
|
||||||
|
apiVersion: v1
|
||||||
|
firstName: foo
|
||||||
|
`)
|
||||||
|
|
||||||
|
schema := []byte(`{
|
||||||
|
"title": "Example Schema",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"kind": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"firstName": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"lastName": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["firstName", "lastName"]
|
||||||
|
}`)
|
||||||
|
|
||||||
|
val := v{
|
||||||
|
opts: Opts{
|
||||||
|
SkipKinds: map[string]struct{}{},
|
||||||
|
RejectKinds: map[string]struct{}{},
|
||||||
|
},
|
||||||
|
schemaCache: nil,
|
||||||
|
schemaDownload: downloadSchema,
|
||||||
|
regs: []registry.Registry{
|
||||||
|
newMockRegistry(func() (string, []byte, error) {
|
||||||
|
return "", schema, nil
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
gotStatuses := []Status{}
|
||||||
|
gotValidationErrors := []ValidationError{}
|
||||||
|
for _, got := range val.Validate("test-file", io.NopCloser(bytes.NewReader(inputData))) {
|
||||||
|
gotStatuses = append(gotStatuses, got.Status)
|
||||||
|
gotValidationErrors = append(gotValidationErrors, got.ValidationErrors...)
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedStatuses := []Status{Valid, Invalid}
|
||||||
|
expectedValidationErrors := []ValidationError{
|
||||||
|
{Path: "", Msg: "missing properties: 'lastName'"},
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(expectedStatuses, gotStatuses) {
|
||||||
|
t.Errorf("Expected %+v, got %+v", expectedStatuses, gotStatuses)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(expectedValidationErrors, gotValidationErrors) {
|
||||||
|
t.Errorf("Expected %+v, got %+v", expectedValidationErrors, gotValidationErrors)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue