github.com/koderover/helm@v2.17.0+incompatible/pkg/lint/rules/template_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 rules 18 19 import ( 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "strings" 24 "testing" 25 26 "k8s.io/helm/pkg/chartutil" 27 "k8s.io/helm/pkg/lint/support" 28 "k8s.io/helm/pkg/proto/hapi/chart" 29 ) 30 31 const ( 32 strict = false 33 namespace = "testNamespace" 34 templateTestBasedir = "./testdata/albatross" 35 ) 36 37 func TestValidateAllowedExtension(t *testing.T) { 38 var failTest = []string{"/foo", "/test.toml"} 39 for _, test := range failTest { 40 err := validateAllowedExtension(test) 41 if err == nil || !strings.Contains(err.Error(), "Valid extensions are .yaml, .yml, .tpl, or .txt") { 42 t.Errorf("validateAllowedExtension('%s') to return \"Valid extensions are .yaml, .yml, .tpl, or .txt\", got no error", test) 43 } 44 } 45 var successTest = []string{"/foo.yaml", "foo.yaml", "foo.tpl", "/foo/bar/baz.yaml", "NOTES.txt"} 46 for _, test := range successTest { 47 err := validateAllowedExtension(test) 48 if err != nil { 49 t.Errorf("validateAllowedExtension('%s') to return no error but got \"%s\"", test, err.Error()) 50 } 51 } 52 } 53 54 var values = []byte("nameOverride: ''\nhttpPort: 80") 55 56 func TestTemplateParsing(t *testing.T) { 57 linter := support.Linter{ChartDir: templateTestBasedir} 58 Templates(&linter, values, namespace, strict) 59 res := linter.Messages 60 61 if len(res) != 1 { 62 t.Fatalf("Expected one error, got %d, %v", len(res), res) 63 } 64 65 if !strings.Contains(res[0].Err.Error(), "deliberateSyntaxError") { 66 t.Errorf("Unexpected error: %s", res[0]) 67 } 68 } 69 70 var wrongTemplatePath = filepath.Join(templateTestBasedir, "templates", "fail.yaml") 71 var ignoredTemplatePath = filepath.Join(templateTestBasedir, "fail.yaml.ignored") 72 73 // Test a template with all the existing features: 74 // namespaces, partial templates 75 func TestTemplateIntegrationHappyPath(t *testing.T) { 76 // Rename file so it gets ignored by the linter 77 os.Rename(wrongTemplatePath, ignoredTemplatePath) 78 defer os.Rename(ignoredTemplatePath, wrongTemplatePath) 79 80 linter := support.Linter{ChartDir: templateTestBasedir} 81 Templates(&linter, values, namespace, strict) 82 res := linter.Messages 83 84 if len(res) != 0 { 85 t.Fatalf("Expected no error, got %d, %v", len(res), res) 86 } 87 } 88 89 // TestSTrictTemplatePrasingMapError is a regression test. 90 // 91 // The template engine should not produce an error when a map in values.yaml does 92 // not contain all possible keys. 93 // 94 // See https://github.com/helm/helm/issues/7483 (helm v3) 95 // and https://github.com/helm/helm/issues/6705 (helm v2) 96 func TestStrictTemplateParsingMapError(t *testing.T) { 97 dir, err := ioutil.TempDir("", "helm-test-") 98 if err != nil { 99 t.Fatal(err) 100 } 101 defer os.RemoveAll(dir) 102 103 var vals = []byte(` 104 mymap: 105 key1: nestedValue 106 `) 107 var manifest = `apiVersion: v1 108 kind: ConfigMap 109 metadata: 110 name: foo 111 data: 112 myval1: {{default "val" .Values.mymap.key1 }} 113 myval2: {{default "val" .Values.mymap.key2 }} 114 ` 115 ch := chart.Chart{ 116 Metadata: &chart.Metadata{ 117 Name: "regression.6705", 118 ApiVersion: "v1", 119 Version: "0.1.0", 120 }, 121 Templates: []*chart.Template{ 122 { 123 Name: "templates/configmap.yaml", 124 Data: []byte(manifest), 125 }, 126 }, 127 } 128 129 if err := chartutil.SaveDir(&ch, dir); err != nil { 130 t.Fatal(err) 131 } 132 linter := &support.Linter{ 133 ChartDir: filepath.Join(dir, ch.Metadata.Name), 134 } 135 Templates(linter, vals, namespace, true) 136 if len(linter.Messages) != 0 { 137 t.Errorf("expected zero messages, got %d", len(linter.Messages)) 138 for i, msg := range linter.Messages { 139 t.Logf("Message %d: %q", i, msg) 140 } 141 } 142 }