From a77a6586fc0ddaa95353af2f0771baa83844f1e3 Mon Sep 17 00:00:00 2001 From: Ian Howell Date: Wed, 27 Feb 2019 11:50:15 -0600 Subject: [PATCH] Clean up implementation in chartutil Signed-off-by: Ian Howell --- .../testdata/test-values-negative.schema.yaml | 45 ------------------- pkg/chartutil/values.go | 41 +++++------------ pkg/chartutil/values_test.go | 37 ++++++++++----- 3 files changed, 39 insertions(+), 84 deletions(-) delete mode 100644 pkg/chartutil/testdata/test-values-negative.schema.yaml diff --git a/pkg/chartutil/testdata/test-values-negative.schema.yaml b/pkg/chartutil/testdata/test-values-negative.schema.yaml deleted file mode 100644 index 2c8345c35..000000000 --- a/pkg/chartutil/testdata/test-values-negative.schema.yaml +++ /dev/null @@ -1,45 +0,0 @@ -title: Values -type: object -properties: - firstname: - description: First name - type: string - lastname: - type: string - likesCoffee: - type: boolean - age: - description: Age - type: integer - minimum: 0 - employmentInfo: - type: object - properties: - salary: - type: number - minimum: 0 - title: - type: string - required: - - salary - addresses: - description: List of addresses - type: array - items: - type: object - properties: - city: - type: string - street: - type: string - number: - type: number - phoneNumbers: - type: array - items: - type: string -required: - - firstname - - lastname - - addresses - - employmentInfo diff --git a/pkg/chartutil/values.go b/pkg/chartutil/values.go index 62d5f2dfc..fcea4ac94 100644 --- a/pkg/chartutil/values.go +++ b/pkg/chartutil/values.go @@ -22,7 +22,6 @@ import ( "io" "io/ioutil" "log" - "os" "strings" "github.com/ghodss/yaml" @@ -145,18 +144,6 @@ func ReadValuesFile(filename string) (Values, error) { if err != nil { return map[string]interface{}{}, err } - - schemaPath := strings.Replace(filename, ".yaml", ".schema.yaml", 1) - _, err = os.Stat(schemaPath) - if err == nil { - schemaData, err := ioutil.ReadFile(schemaPath) - if err != nil { - return map[string]interface{}{}, err - } - - return ReadSchematizedValues(data, schemaData) - } - return ReadValues(data) } @@ -169,38 +156,28 @@ func ReadSchemaFile(filename string) (Schema, error) { return ReadSchema(data) } -// ReadSchematizedValues parses a YAML file and asserts that it matches the schema -func ReadSchematizedValues(data, schemaData []byte) (Values, error) { - values, err := ReadValues(data) - if err != nil { - return Values{}, err - } +// ValidateAgainstSchema checks that values does not violate the structure laid out in schema +func ValidateAgainstSchema(values Values, schema Schema) error { valuesJSON := convertToJSON(values) - - schema, err := ReadSchema(schemaData) - if err != nil { - return Values{}, err - } schemaJSON := convertToJSON(schema) - schemaLoader := gojsonschema.NewStringLoader(string(schemaJSON)) valuesLoader := gojsonschema.NewStringLoader(string(valuesJSON)) result, err := gojsonschema.Validate(schemaLoader, valuesLoader) if err != nil { - return Values{}, err + return err } if !result.Valid() { var sb strings.Builder - sb.WriteString("The values.yaml is not valid. see errors :\n") + sb.WriteString(".Values does not meet the specification of values.schema.yaml . see errors :\n") for _, desc := range result.Errors() { sb.WriteString(fmt.Sprintf("- %s\n", desc)) } - return Values{}, errors.New(sb.String()) + return errors.New(sb.String()) } - return values, nil + return nil } // GenerateSchema will create a JSON Schema (in YAML format) for the given values @@ -468,6 +445,12 @@ func ToRenderValues(chrt *chart.Chart, chrtVals map[string]interface{}, options return top, err } + if chrt.Schema != nil { + if err := ValidateAgainstSchema(vals, chrt.Schema); err != nil { + return top, err + } + } + top["Values"] = vals return top, nil } diff --git a/pkg/chartutil/values_test.go b/pkg/chartutil/values_test.go index 18dcd82e5..119a83658 100644 --- a/pkg/chartutil/values_test.go +++ b/pkg/chartutil/values_test.go @@ -169,23 +169,40 @@ func TestReadSchemaFile(t *testing.T) { matchSchema(t, data) } -func TestReadSchematizedValues(t *testing.T) { - _, err := ReadValuesFile("./testdata/test-values.yaml") +func TestValidateAgainstSchema(t *testing.T) { + values, err := ReadValuesFile("./testdata/test-values.yaml") if err != nil { - t.Errorf("Got the following unexpected error while reading schematized values:\n%v", err) + t.Fatalf("Error reading YAML file: %s", err) + } + schema, err := ReadSchemaFile("./testdata/test-values.schema.yaml") + if err != nil { + t.Fatalf("Error reading YAML file: %s", err) + } + + if err := ValidateAgainstSchema(values, schema); err != nil { + t.Errorf("Error validating Values against Schema: %s", err) } } -func TestReadSchematizedValuesNegative(t *testing.T) { - _, err := ReadValuesFile("./testdata/test-values-negative.yaml") +func TestValidateAgainstSchemaNegative(t *testing.T) { + values, err := ReadValuesFile("./testdata/test-values-negative.yaml") + if err != nil { + t.Fatalf("Error reading YAML file: %s", err) + } + schema, err := ReadSchemaFile("./testdata/test-values.schema.yaml") + if err != nil { + t.Fatalf("Error reading YAML file: %s", err) + } - if err == nil { - t.Errorf("Expected an error, but got none") + var errString string + if err := ValidateAgainstSchema(values, schema); err == nil { + t.Errorf("Expected an error, but got nil") + } else { + errString = err.Error() } - errString := err.Error() - if !strings.Contains(errString, "The values.yaml is not valid. see errors :") { - t.Errorf("Error string does not contain expected string: \"The values.yaml is not valid. see errors :\"") + if !strings.Contains(errString, ".Values does not meet the specification of values.schema.yaml . see errors :") { + t.Errorf("Error string does not contain expected string: \".Values does not meet the specification of values.schema.yaml . see errors :\"") } if !strings.Contains(errString, "- (root): employmentInfo is required") { t.Errorf("Error string does not contain expected string: \"- (root): employmentInfo is required\"")