github.com/x-helm/helm@v3.0.0-beta.3+incompatible/pkg/chartutil/jsonschema_test.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package chartutil 18 19 import ( 20 "io/ioutil" 21 "testing" 22 23 "helm.sh/helm/pkg/chart" 24 ) 25 26 func TestValidateAgainstSingleSchema(t *testing.T) { 27 values, err := ReadValuesFile("./testdata/test-values.yaml") 28 if err != nil { 29 t.Fatalf("Error reading YAML file: %s", err) 30 } 31 schema, err := ioutil.ReadFile("./testdata/test-values.schema.json") 32 if err != nil { 33 t.Fatalf("Error reading YAML file: %s", err) 34 } 35 36 if err := ValidateAgainstSingleSchema(values, schema); err != nil { 37 t.Errorf("Error validating Values against Schema: %s", err) 38 } 39 } 40 41 func TestValidateAgainstSingleSchemaNegative(t *testing.T) { 42 values, err := ReadValuesFile("./testdata/test-values-negative.yaml") 43 if err != nil { 44 t.Fatalf("Error reading YAML file: %s", err) 45 } 46 schema, err := ioutil.ReadFile("./testdata/test-values.schema.json") 47 if err != nil { 48 t.Fatalf("Error reading YAML file: %s", err) 49 } 50 51 var errString string 52 if err := ValidateAgainstSingleSchema(values, schema); err == nil { 53 t.Fatalf("Expected an error, but got nil") 54 } else { 55 errString = err.Error() 56 } 57 58 expectedErrString := `- (root): employmentInfo is required 59 - age: Must be greater than or equal to 0/1 60 ` 61 if errString != expectedErrString { 62 t.Errorf("Error string :\n`%s`\ndoes not match expected\n`%s`", errString, expectedErrString) 63 } 64 } 65 66 const subchrtSchema = `{ 67 "$schema": "http://json-schema.org/draft-07/schema#", 68 "title": "Values", 69 "type": "object", 70 "properties": { 71 "age": { 72 "description": "Age", 73 "minimum": 0, 74 "type": "integer" 75 } 76 }, 77 "required": [ 78 "age" 79 ] 80 } 81 ` 82 83 func TestValidateAgainstSchema(t *testing.T) { 84 subchrtJSON := []byte(subchrtSchema) 85 subchrt := &chart.Chart{ 86 Metadata: &chart.Metadata{ 87 Name: "subchrt", 88 }, 89 Schema: subchrtJSON, 90 } 91 chrt := &chart.Chart{ 92 Metadata: &chart.Metadata{ 93 Name: "chrt", 94 }, 95 } 96 chrt.AddDependency(subchrt) 97 98 vals := map[string]interface{}{ 99 "name": "John", 100 "subchrt": map[string]interface{}{ 101 "age": 25, 102 }, 103 } 104 105 if err := ValidateAgainstSchema(chrt, vals); err != nil { 106 t.Errorf("Error validating Values against Schema: %s", err) 107 } 108 } 109 110 func TestValidateAgainstSchemaNegative(t *testing.T) { 111 subchrtJSON := []byte(subchrtSchema) 112 subchrt := &chart.Chart{ 113 Metadata: &chart.Metadata{ 114 Name: "subchrt", 115 }, 116 Schema: subchrtJSON, 117 } 118 chrt := &chart.Chart{ 119 Metadata: &chart.Metadata{ 120 Name: "chrt", 121 }, 122 } 123 chrt.AddDependency(subchrt) 124 125 vals := map[string]interface{}{ 126 "name": "John", 127 "subchrt": map[string]interface{}{}, 128 } 129 130 var errString string 131 if err := ValidateAgainstSchema(chrt, vals); err == nil { 132 t.Fatalf("Expected an error, but got nil") 133 } else { 134 errString = err.Error() 135 } 136 137 expectedErrString := `subchrt: 138 - (root): age is required 139 ` 140 if errString != expectedErrString { 141 t.Errorf("Error string :\n`%s`\ndoes not match expected\n`%s`", errString, expectedErrString) 142 } 143 }