mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-22 19:27:01 +00:00
Slightly improve the coverage of the validator test
This commit is contained in:
parent
9627dd185b
commit
466f4d8b53
1 changed files with 37 additions and 60 deletions
|
|
@ -1,7 +1,6 @@
|
||||||
package validator
|
package validator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/yannh/kubeconform/pkg/registry"
|
"github.com/yannh/kubeconform/pkg/registry"
|
||||||
|
|
@ -30,7 +29,8 @@ func TestValidate(t *testing.T) {
|
||||||
schemaRegistry2 []byte
|
schemaRegistry2 []byte
|
||||||
ignoreMissingSchema bool
|
ignoreMissingSchema bool
|
||||||
strict bool
|
strict bool
|
||||||
expect Status
|
expectStatus Status
|
||||||
|
expectErrors []ValidationError
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"valid resource",
|
"valid resource",
|
||||||
|
|
@ -65,6 +65,7 @@ lastName: bar
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
Valid,
|
Valid,
|
||||||
|
[]ValidationError{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"invalid resource",
|
"invalid resource",
|
||||||
|
|
@ -99,6 +100,12 @@ lastName: bar
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
Invalid,
|
Invalid,
|
||||||
|
[]ValidationError{
|
||||||
|
{
|
||||||
|
Path: "/firstName",
|
||||||
|
Msg: "expected number, but got string",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"missing required field",
|
"missing required field",
|
||||||
|
|
@ -132,6 +139,12 @@ firstName: foo
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
Invalid,
|
Invalid,
|
||||||
|
[]ValidationError{
|
||||||
|
{
|
||||||
|
Path: "",
|
||||||
|
Msg: "missing properties: 'lastName'",
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"key \"firstName\" already set in map",
|
"key \"firstName\" already set in map",
|
||||||
|
|
@ -158,6 +171,7 @@ firstName: bar
|
||||||
false,
|
false,
|
||||||
true,
|
true,
|
||||||
Error,
|
Error,
|
||||||
|
[]ValidationError{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"key firstname already set in map in non-strict mode",
|
"key firstname already set in map in non-strict mode",
|
||||||
|
|
@ -184,6 +198,7 @@ firstName: bar
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
Valid,
|
Valid,
|
||||||
|
[]ValidationError{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"resource has invalid yaml",
|
"resource has invalid yaml",
|
||||||
|
|
@ -221,6 +236,7 @@ lastName: bar
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
Error,
|
Error,
|
||||||
|
[]ValidationError{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"missing schema in 1st registry",
|
"missing schema in 1st registry",
|
||||||
|
|
@ -258,6 +274,7 @@ lastName: bar
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
Valid,
|
Valid,
|
||||||
|
[]ValidationError{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"non-json response in 1st registry",
|
"non-json response in 1st registry",
|
||||||
|
|
@ -295,6 +312,7 @@ lastName: bar
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
Valid,
|
Valid,
|
||||||
|
[]ValidationError{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"missing schema in both registries, ignore missing",
|
"missing schema in both registries, ignore missing",
|
||||||
|
|
@ -309,6 +327,7 @@ lastName: bar
|
||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
Skipped,
|
Skipped,
|
||||||
|
[]ValidationError{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"missing schema in both registries, do not ignore missing",
|
"missing schema in both registries, do not ignore missing",
|
||||||
|
|
@ -323,6 +342,7 @@ lastName: bar
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
Error,
|
Error,
|
||||||
|
[]ValidationError{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"non-json response in both registries, ignore missing",
|
"non-json response in both registries, ignore missing",
|
||||||
|
|
@ -337,6 +357,7 @@ lastName: bar
|
||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
Skipped,
|
Skipped,
|
||||||
|
[]ValidationError{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"non-json response in both registries, do not ignore missing",
|
"non-json response in both registries, do not ignore missing",
|
||||||
|
|
@ -351,6 +372,7 @@ lastName: bar
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
Error,
|
Error,
|
||||||
|
[]ValidationError{},
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
val := v{
|
val := v{
|
||||||
|
|
@ -371,67 +393,22 @@ lastName: bar
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if got := val.ValidateResource(resource.Resource{Bytes: testCase.rawResource}); got.Status != testCase.expect {
|
got := val.ValidateResource(resource.Resource{Bytes: testCase.rawResource})
|
||||||
|
if got.Status != testCase.expectStatus {
|
||||||
if got.Err != nil {
|
if got.Err != nil {
|
||||||
t.Errorf("Test '%s' - expected %d, got %d: %s", testCase.name, testCase.expect, got.Status, got.Err.Error())
|
t.Errorf("Test '%s' - expected %d, got %d: %s", testCase.name, testCase.expectStatus, got.Status, got.Err.Error())
|
||||||
} else {
|
} else {
|
||||||
t.Errorf("%d - expected %d, got %d", i, testCase.expect, got.Status)
|
t.Errorf("Test '%s' - %d - expected %d, got %d", testCase.name, i, testCase.expectStatus, got.Status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(got.ValidationErrors) != len(testCase.expectErrors) {
|
||||||
|
t.Errorf("Test '%s': expected ValidationErrors: %+v, got: % v", testCase.name, testCase.expectErrors, got.ValidationErrors)
|
||||||
|
}
|
||||||
|
for i, _ := range testCase.expectErrors {
|
||||||
|
if testCase.expectErrors[i] != got.ValidationErrors[i] {
|
||||||
|
t.Errorf("Test '%s': expected ValidationErrors: %+v, got: % v", testCase.name, testCase.expectErrors, got.ValidationErrors)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidationErrors(t *testing.T) {
|
|
||||||
rawResource := []byte(`
|
|
||||||
kind: name
|
|
||||||
apiVersion: v1
|
|
||||||
firstName: foo
|
|
||||||
age: not a number
|
|
||||||
`)
|
|
||||||
|
|
||||||
schema := []byte(`{
|
|
||||||
"title": "Example Schema",
|
|
||||||
"type": "object",
|
|
||||||
"properties": {
|
|
||||||
"kind": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"firstName": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"lastName": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
|
||||||
"age": {
|
|
||||||
"description": "Age in years",
|
|
||||||
"type": "integer",
|
|
||||||
"minimum": 0
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"required": ["firstName", "lastName"]
|
|
||||||
}`)
|
|
||||||
|
|
||||||
expectedErrors := []ValidationError{
|
|
||||||
{Path: "", Msg: "missing properties: 'lastName'"},
|
|
||||||
{Path: "/age", Msg: "expected integer, but got string"},
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
got := val.ValidateResource(resource.Resource{Bytes: rawResource})
|
|
||||||
if !reflect.DeepEqual(expectedErrors, got.ValidationErrors) {
|
|
||||||
t.Errorf("Expected %+v, got %+v", expectedErrors, got.ValidationErrors)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue