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
|
|
|
|
2020-05-02 04:01:15 +08:00
|
|
|
"helm.sh/helm/v3/internal/test/ensure"
|
|
|
|
"helm.sh/helm/v3/pkg/chart"
|
|
|
|
"helm.sh/helm/v3/pkg/chartutil"
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-04-29 07:12:14 +08:00
|
|
|
|
|
|
|
func TestValidateMetadataName(t *testing.T) {
|
|
|
|
names := map[string]bool{
|
|
|
|
"": false,
|
|
|
|
"foo": true,
|
|
|
|
"foo.bar1234baz.seventyone": true,
|
|
|
|
"FOO": false,
|
|
|
|
"123baz": true,
|
|
|
|
"foo.BAR.baz": false,
|
|
|
|
"one-two": true,
|
|
|
|
"-two": false,
|
|
|
|
"one_two": false,
|
|
|
|
"a..b": false,
|
|
|
|
"%^&#$%*@^*@&#^": false,
|
|
|
|
}
|
|
|
|
for input, expectPass := range names {
|
|
|
|
obj := K8sYamlStruct{Metadata: k8sYamlMetadata{Name: input}}
|
|
|
|
if err := validateMetadataName(&obj); (err == nil) != expectPass {
|
|
|
|
st := "fail"
|
|
|
|
if expectPass {
|
|
|
|
st = "succeed"
|
|
|
|
}
|
|
|
|
t.Errorf("Expected %q to %s", input, st)
|
|
|
|
if err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-02 04:01:15 +08:00
|
|
|
|
|
|
|
func TestDeprecatedAPIFails(t *testing.T) {
|
|
|
|
mychart := chart.Chart{
|
|
|
|
Metadata: &chart.Metadata{
|
|
|
|
APIVersion: "v2",
|
|
|
|
Name: "failapi",
|
|
|
|
Version: "0.1.0",
|
|
|
|
Icon: "satisfy-the-linting-gods.gif",
|
|
|
|
},
|
|
|
|
Templates: []*chart.File{
|
|
|
|
{
|
|
|
|
Name: "templates/baddeployment.yaml",
|
|
|
|
Data: []byte("apiVersion: apps/v1beta1\nkind: Deployment\nmetadata:\n name: baddep"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "templates/goodsecret.yaml",
|
|
|
|
Data: []byte("apiVersion: v1\nkind: Secret\nmetadata:\n name: goodsecret"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
tmpdir := ensure.TempDir(t)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
if err := chartutil.SaveDir(&mychart, tmpdir); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
linter := support.Linter{ChartDir: filepath.Join(tmpdir, mychart.Name())}
|
|
|
|
Templates(&linter, values, namespace, strict)
|
|
|
|
if l := len(linter.Messages); l != 1 {
|
|
|
|
for i, msg := range linter.Messages {
|
|
|
|
t.Logf("Message %d: %s", i, msg)
|
|
|
|
}
|
|
|
|
t.Fatalf("Expected 1 lint error, got %d", l)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := linter.Messages[0].Err.(deprecatedAPIError)
|
|
|
|
if err.Deprecated != "apps/v1beta1 Deployment" {
|
|
|
|
t.Errorf("Surprised to learn that %q is deprecated", err.Deprecated)
|
|
|
|
}
|
|
|
|
}
|