From 0afec2dba19ddcd7527bd2a4279d6ac6559ae29f Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Thu, 25 Apr 2019 13:48:41 -0500 Subject: [PATCH] Add unit tests for ValidateAgainstSchema Signed-off-by: Ian Howell --- pkg/chartutil/values_test.go | 82 +++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/pkg/chartutil/values_test.go b/pkg/chartutil/values_test.go index e8433b362..089afc8ed 100644 --- a/pkg/chartutil/values_test.go +++ b/pkg/chartutil/values_test.go @@ -193,8 +193,7 @@ func TestValidateAgainstSingleSchemaNegative(t *testing.T) { errString = err.Error() } - expectedErrString := `values don't meet the specification of the schema: -- (root): employmentInfo is required + expectedErrString := `- (root): employmentInfo is required - age: Must be greater than or equal to 0/1 ` if errString != expectedErrString { @@ -202,6 +201,85 @@ func TestValidateAgainstSingleSchemaNegative(t *testing.T) { } } +const subchrtSchema = `{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Values", + "type": "object", + "properties": { + "age": { + "description": "Age", + "minimum": 0, + "type": "integer" + } + }, + "required": [ + "age" + ] +} +` + +func TestValidateAgainstSchema(t *testing.T) { + subchrtJSON := []byte(subchrtSchema) + subchrt := &chart.Chart{ + Metadata: &chart.Metadata{ + Name: "subchrt", + }, + Schema: subchrtJSON, + } + chrt := &chart.Chart{ + Metadata: &chart.Metadata{ + Name: "chrt", + }, + } + chrt.AddDependency(subchrt) + + vals := map[string]interface{}{ + "name": "John", + "subchrt": map[string]interface{}{ + "age": 25, + }, + } + + if err := ValidateAgainstSchema(chrt, vals); err != nil { + t.Errorf("Error validating Values against Schema: %s", err) + } +} + +func TestValidateAgainstSchemaNegative(t *testing.T) { + subchrtJSON := []byte(subchrtSchema) + subchrt := &chart.Chart{ + Metadata: &chart.Metadata{ + Name: "subchrt", + }, + Schema: subchrtJSON, + } + chrt := &chart.Chart{ + Metadata: &chart.Metadata{ + Name: "chrt", + }, + } + chrt.AddDependency(subchrt) + + vals := map[string]interface{}{ + "name": "John", + "subchrt": map[string]interface{}{}, + } + + var errString string + if err := ValidateAgainstSchema(chrt, vals); err == nil { + t.Fatalf("Expected an error, but got nil") + } else { + errString = err.Error() + } + + expectedErrString := `subchrt: +- (root): age is required +` + if errString != expectedErrString { + t.Errorf("Error string :\n`%s`\ndoes not match expected\n`%s`", errString, expectedErrString) + } +} + func ExampleValues() { doc := ` title: "Moby Dick"