github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+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 TestTemplateParsing(t *testing.T) {
    48  	linter := support.Linter{ChartDir: templateTestBasedir}
    49  	Templates(&linter)
    50  	res := linter.Messages
    51  
    52  	if len(res) != 1 {
    53  		t.Fatalf("Expected one error, got %d, %v", len(res), res)
    54  	}
    55  
    56  	if !strings.Contains(res[0].Err.Error(), "deliberateSyntaxError") {
    57  		t.Errorf("Unexpected error: %s", res[0])
    58  	}
    59  }
    60  
    61  var wrongTemplatePath = filepath.Join(templateTestBasedir, "templates", "fail.yaml")
    62  var ignoredTemplatePath = filepath.Join(templateTestBasedir, "fail.yaml.ignored")
    63  
    64  // Test a template with all the existing features:
    65  // namespaces, partial templates
    66  func TestTemplateIntegrationHappyPath(t *testing.T) {
    67  	// Rename file so it gets ignored by the linter
    68  	os.Rename(wrongTemplatePath, ignoredTemplatePath)
    69  	defer os.Rename(ignoredTemplatePath, wrongTemplatePath)
    70  
    71  	linter := support.Linter{ChartDir: templateTestBasedir}
    72  	Templates(&linter)
    73  	res := linter.Messages
    74  
    75  	if len(res) != 0 {
    76  		t.Fatalf("Expected no error, got %d, %v", len(res), res)
    77  	}
    78  }