2016-06-23 02:28:45 +08:00
|
|
|
/*
|
2018-08-25 03:03:55 +08:00
|
|
|
Copyright The Helm Authors.
|
2016-06-23 02:28:45 +08:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-06-10 07:51:00 +08:00
|
|
|
package rules
|
2016-04-27 07:20:37 +08:00
|
|
|
|
|
|
|
import (
|
2016-06-28 08:47:18 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-04-27 07:20:37 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-06-28 08:47:18 +08:00
|
|
|
|
2019-10-04 02:27:05 +08:00
|
|
|
"helm.sh/helm/v3/pkg/lint/support"
|
2016-04-27 07:20:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const templateTestBasedir = "./testdata/albatross"
|
|
|
|
|
2016-06-24 07:05:27 +08:00
|
|
|
func TestValidateAllowedExtension(t *testing.T) {
|
2018-03-09 21:10:10 +08:00
|
|
|
var failTest = []string{"/foo", "/test.toml"}
|
2016-06-24 07:05:27 +08:00
|
|
|
for _, test := range failTest {
|
|
|
|
err := validateAllowedExtension(test)
|
2018-03-09 21:10:10 +08:00
|
|
|
if err == nil || !strings.Contains(err.Error(), "Valid extensions are .yaml, .yml, .tpl, or .txt") {
|
|
|
|
t.Errorf("validateAllowedExtension('%s') to return \"Valid extensions are .yaml, .yml, .tpl, or .txt\", got no error", test)
|
2016-06-24 07:05:27 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-09 04:23:22 +08:00
|
|
|
var successTest = []string{"/foo.yaml", "foo.yaml", "foo.tpl", "/foo/bar/baz.yaml", "NOTES.txt"}
|
2016-06-24 07:05:27 +08:00
|
|
|
for _, test := range successTest {
|
|
|
|
err := validateAllowedExtension(test)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("validateAllowedExtension('%s') to return no error but got \"%s\"", test, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-05 07:57:24 +08:00
|
|
|
var values = map[string]interface{}{"nameOverride": "", "httpPort": 80}
|
2017-12-14 06:43:12 +08:00
|
|
|
|
|
|
|
const namespace = "testNamespace"
|
|
|
|
const strict = false
|
|
|
|
|
2016-06-28 08:47:18 +08:00
|
|
|
func TestTemplateParsing(t *testing.T) {
|
2016-06-10 07:51:00 +08:00
|
|
|
linter := support.Linter{ChartDir: templateTestBasedir}
|
2017-12-14 06:43:12 +08:00
|
|
|
Templates(&linter, values, namespace, strict)
|
2016-06-10 07:51:00 +08:00
|
|
|
res := linter.Messages
|
2016-04-27 07:20:37 +08:00
|
|
|
|
|
|
|
if len(res) != 1 {
|
2016-06-10 07:51:00 +08:00
|
|
|
t.Fatalf("Expected one error, got %d, %v", len(res), res)
|
2016-04-27 07:20:37 +08:00
|
|
|
}
|
|
|
|
|
2016-07-01 10:07:56 +08:00
|
|
|
if !strings.Contains(res[0].Err.Error(), "deliberateSyntaxError") {
|
2016-04-27 07:20:37 +08:00
|
|
|
t.Errorf("Unexpected error: %s", res[0])
|
|
|
|
}
|
|
|
|
}
|
2016-06-28 08:47:18 +08:00
|
|
|
|
2016-07-02 06:20:13 +08:00
|
|
|
var wrongTemplatePath = filepath.Join(templateTestBasedir, "templates", "fail.yaml")
|
|
|
|
var ignoredTemplatePath = filepath.Join(templateTestBasedir, "fail.yaml.ignored")
|
2016-06-28 08:47:18 +08:00
|
|
|
|
|
|
|
// Test a template with all the existing features:
|
|
|
|
// namespaces, partial templates
|
|
|
|
func TestTemplateIntegrationHappyPath(t *testing.T) {
|
|
|
|
// Rename file so it gets ignored by the linter
|
|
|
|
os.Rename(wrongTemplatePath, ignoredTemplatePath)
|
|
|
|
defer os.Rename(ignoredTemplatePath, wrongTemplatePath)
|
|
|
|
|
|
|
|
linter := support.Linter{ChartDir: templateTestBasedir}
|
2017-12-14 06:43:12 +08:00
|
|
|
Templates(&linter, values, namespace, strict)
|
2016-06-28 08:47:18 +08:00
|
|
|
res := linter.Messages
|
|
|
|
|
|
|
|
if len(res) != 0 {
|
|
|
|
t.Fatalf("Expected no error, got %d, %v", len(res), res)
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 01:20:47 +08:00
|
|
|
|
|
|
|
func TestV3Fail(t *testing.T) {
|
|
|
|
linter := support.Linter{ChartDir: "./testdata/v3-fail"}
|
|
|
|
Templates(&linter, values, namespace, strict)
|
|
|
|
res := linter.Messages
|
|
|
|
|
|
|
|
if len(res) != 3 {
|
|
|
|
t.Fatalf("Expected 3 errors, got %d, %v", len(res), res)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(res[0].Err.Error(), ".Release.Time has been removed in v3") {
|
|
|
|
t.Errorf("Unexpected error: %s", res[0].Err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(res[1].Err.Error(), "manifest is a crd-install hook") {
|
|
|
|
t.Errorf("Unexpected error: %s", res[1].Err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(res[2].Err.Error(), "manifest is a crd-install hook") {
|
|
|
|
t.Errorf("Unexpected error: %s", res[2].Err)
|
|
|
|
}
|
|
|
|
}
|