github.com/qsis/helm@v3.0.0-beta.3+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  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"testing"
    24  
    25  	"helm.sh/helm/pkg/lint/support"
    26  )
    27  
    28  const templateTestBasedir = "./testdata/albatross"
    29  
    30  func TestValidateAllowedExtension(t *testing.T) {
    31  	var failTest = []string{"/foo", "/test.toml"}
    32  	for _, test := range failTest {
    33  		err := validateAllowedExtension(test)
    34  		if err == nil || !strings.Contains(err.Error(), "Valid extensions are .yaml, .yml, .tpl, or .txt") {
    35  			t.Errorf("validateAllowedExtension('%s') to return \"Valid extensions are .yaml, .yml, .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  var values = map[string]interface{}{"nameOverride": "", "httpPort": 80}
    48  
    49  const namespace = "testNamespace"
    50  const strict = false
    51  
    52  func TestTemplateParsing(t *testing.T) {
    53  	linter := support.Linter{ChartDir: templateTestBasedir}
    54  	Templates(&linter, values, namespace, strict)
    55  	res := linter.Messages
    56  
    57  	if len(res) != 1 {
    58  		t.Fatalf("Expected one error, got %d, %v", len(res), res)
    59  	}
    60  
    61  	if !strings.Contains(res[0].Err.Error(), "deliberateSyntaxError") {
    62  		t.Errorf("Unexpected error: %s", res[0])
    63  	}
    64  }
    65  
    66  var wrongTemplatePath = filepath.Join(templateTestBasedir, "templates", "fail.yaml")
    67  var ignoredTemplatePath = filepath.Join(templateTestBasedir, "fail.yaml.ignored")
    68  
    69  // Test a template with all the existing features:
    70  // namespaces, partial templates
    71  func TestTemplateIntegrationHappyPath(t *testing.T) {
    72  	// Rename file so it gets ignored by the linter
    73  	os.Rename(wrongTemplatePath, ignoredTemplatePath)
    74  	defer os.Rename(ignoredTemplatePath, wrongTemplatePath)
    75  
    76  	linter := support.Linter{ChartDir: templateTestBasedir}
    77  	Templates(&linter, values, namespace, strict)
    78  	res := linter.Messages
    79  
    80  	if len(res) != 0 {
    81  		t.Fatalf("Expected no error, got %d, %v", len(res), res)
    82  	}
    83  }