Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly improve the coverage of the validator test #270

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 36 additions & 58 deletions pkg/validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func TestValidate(t *testing.T) {
schemaRegistry2 []byte
ignoreMissingSchema bool
strict bool
expect Status
expectStatus Status
expectErrors []ValidationError
}{
{
"valid resource",
Expand Down Expand Up @@ -67,6 +68,7 @@ lastName: bar
false,
false,
Valid,
[]ValidationError{},
},
{
"invalid resource",
Expand Down Expand Up @@ -101,6 +103,12 @@ lastName: bar
false,
false,
Invalid,
[]ValidationError{
{
Path: "/firstName",
Msg: "expected number, but got string",
},
},
},
{
"missing required field",
Expand Down Expand Up @@ -134,6 +142,12 @@ firstName: foo
false,
false,
Invalid,
[]ValidationError{
{
Path: "",
Msg: "missing properties: 'lastName'",
},
},
},
{
"key \"firstName\" already set in map",
Expand All @@ -160,6 +174,7 @@ firstName: bar
false,
true,
Error,
[]ValidationError{},
},
{
"key firstname already set in map in non-strict mode",
Expand All @@ -186,6 +201,7 @@ firstName: bar
false,
false,
Valid,
[]ValidationError{},
},
{
"resource has invalid yaml",
Expand Down Expand Up @@ -223,6 +239,7 @@ lastName: bar
false,
false,
Error,
[]ValidationError{},
},
{
"missing schema in 1st registry",
Expand Down Expand Up @@ -260,6 +277,7 @@ lastName: bar
false,
false,
Valid,
[]ValidationError{},
},
{
"non-json response in 1st registry",
Expand Down Expand Up @@ -297,6 +315,7 @@ lastName: bar
false,
false,
Valid,
[]ValidationError{},
},
{
"missing schema in both registries, ignore missing",
Expand All @@ -311,6 +330,7 @@ lastName: bar
true,
false,
Skipped,
[]ValidationError{},
},
{
"missing schema in both registries, do not ignore missing",
Expand All @@ -325,6 +345,7 @@ lastName: bar
false,
false,
Error,
[]ValidationError{},
},
{
"non-json response in both registries, ignore missing",
Expand All @@ -339,6 +360,7 @@ lastName: bar
true,
false,
Skipped,
[]ValidationError{},
},
{
"non-json response in both registries, do not ignore missing",
Expand All @@ -353,6 +375,7 @@ lastName: bar
false,
false,
Error,
[]ValidationError{},
},
} {
val := v{
Expand All @@ -373,68 +396,23 @@ 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 {
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 {
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)
}
}
}
}

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)
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)
}
}
}
}

Expand Down