github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/pkg/lint/rules/template_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 "os" 21 "path/filepath" 22 "strings" 23 "testing" 24 25 "k8s.io/helm/pkg/lint/support" 26 ) 27 28 const templateTestBasedir = "./testdata/albatross" 29 30 func TestValidateAllowedExtension(t *testing.T) { 31 var failTest = []string{"/foo", "/test.yml", "/test.toml", "test.yml"} 32 for _, test := range failTest { 33 err := validateAllowedExtension(test) 34 if err == nil || !strings.Contains(err.Error(), "Valid extensions are .yaml, .tpl, or .txt") { 35 t.Errorf("validateAllowedExtension('%s') to return \"Valid extensions are .yaml, .tpl, or .txt\", got no error", test) 36 } 37 } 38 var successTest = []string{"/foo.yaml", "foo.yaml", "foo.tpl", "/foo/bar/baz.yaml", "NOTES.txt"} 39 for _, test := range successTest { 40 err := validateAllowedExtension(test) 41 if err != nil { 42 t.Errorf("validateAllowedExtension('%s') to return no error but got \"%s\"", test, err.Error()) 43 } 44 } 45 } 46 47 func TestValidateQuotes(t *testing.T) { 48 // add `| quote` lint error 49 var failTest = []string{"foo: {{.Release.Service }}", "foo: {{.Release.Service }}", "- {{.Release.Service }}", "foo: {{default 'Never' .restart_policy}}", "- {{.Release.Service }} "} 50 51 for _, test := range failTest { 52 err := validateQuotes(test) 53 if err == nil || !strings.Contains(err.Error(), "use the sprig \"quote\" function") { 54 t.Errorf("validateQuotes('%s') to return \"use the sprig \"quote\" function:\", got no error.", test) 55 } 56 } 57 58 var successTest = []string{"foo: {{.Release.Service | quote }}", "foo: {{.Release.Service | quote }}", "- {{.Release.Service | quote }}", "foo: {{default 'Never' .restart_policy | quote }}", "foo: \"{{ .Release.Service }}\"", "foo: \"{{ .Release.Service }} {{ .Foo.Bar }}\"", "foo: \"{{ default 'Never' .Release.Service }} {{ .Foo.Bar }}\"", "foo: {{.Release.Service | squote }}"} 59 60 for _, test := range successTest { 61 err := validateQuotes(test) 62 if err != nil { 63 t.Errorf("validateQuotes('%s') to return not error and got \"%s\"", test, err.Error()) 64 } 65 } 66 67 // Surrounding quotes 68 failTest = []string{"foo: {{.Release.Service }}-{{ .Release.Bar }}", "foo: {{.Release.Service }} {{ .Release.Bar }}", "- {{.Release.Service }}-{{ .Release.Bar }}", "- {{.Release.Service }}-{{ .Release.Bar }} {{ .Release.Baz }}", "foo: {{.Release.Service | default }}-{{ .Release.Bar }}"} 69 70 for _, test := range failTest { 71 err := validateQuotes(test) 72 if err == nil || !strings.Contains(err.Error(), "wrap substitution functions in quotes") { 73 t.Errorf("validateQuotes('%s') to return \"wrap substitution functions in quotes\", got no error", test) 74 } 75 } 76 77 } 78 79 func TestTemplateParsing(t *testing.T) { 80 linter := support.Linter{ChartDir: templateTestBasedir} 81 Templates(&linter) 82 res := linter.Messages 83 84 if len(res) != 1 { 85 t.Fatalf("Expected one error, got %d, %v", len(res), res) 86 } 87 88 if !strings.Contains(res[0].Err.Error(), "deliberateSyntaxError") { 89 t.Errorf("Unexpected error: %s", res[0]) 90 } 91 } 92 93 var wrongTemplatePath = filepath.Join(templateTestBasedir, "templates", "fail.yaml") 94 var ignoredTemplatePath = filepath.Join(templateTestBasedir, "fail.yaml.ignored") 95 96 // Test a template with all the existing features: 97 // namespaces, partial templates 98 func TestTemplateIntegrationHappyPath(t *testing.T) { 99 // Rename file so it gets ignored by the linter 100 os.Rename(wrongTemplatePath, ignoredTemplatePath) 101 defer os.Rename(ignoredTemplatePath, wrongTemplatePath) 102 103 linter := support.Linter{ChartDir: templateTestBasedir} 104 Templates(&linter) 105 res := linter.Messages 106 107 if len(res) != 0 { 108 t.Fatalf("Expected no error, got %d, %v", len(res), res) 109 } 110 }