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