github.com/koderover/helm@v2.17.0+incompatible/pkg/lint/lint_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 lint
    18  
    19  import (
    20  	"io/ioutil"
    21  	"os"
    22  	"strings"
    23  	"testing"
    24  
    25  	"k8s.io/helm/pkg/chartutil"
    26  	"k8s.io/helm/pkg/lint/support"
    27  	"k8s.io/helm/pkg/proto/hapi/chart"
    28  )
    29  
    30  var values = []byte{}
    31  
    32  const (
    33  	namespace        = "testNamespace"
    34  	strict           = false
    35  	badChartDir      = "rules/testdata/badchartfile"
    36  	badValuesFileDir = "rules/testdata/badvaluesfile"
    37  	badYamlFileDir   = "rules/testdata/albatross"
    38  	goodChartDir     = "rules/testdata/goodone"
    39  )
    40  
    41  func TestBadChart(t *testing.T) {
    42  	m := All(badChartDir, values, namespace, strict).Messages
    43  	if len(m) != 6 {
    44  		t.Errorf("Number of errors %v", len(m))
    45  		t.Errorf("All didn't fail with expected errors, got %#v", m)
    46  	}
    47  	// There should be one INFO, 2 WARNINGs and one ERROR messages, check for them
    48  	var i, w, e, e2, e3, e4 bool
    49  	for _, msg := range m {
    50  		if msg.Severity == support.InfoSev {
    51  			if strings.Contains(msg.Err.Error(), "icon is recommended") {
    52  				i = true
    53  			}
    54  		}
    55  		if msg.Severity == support.WarningSev {
    56  			if strings.Contains(msg.Err.Error(), "directory not found") {
    57  				w = true
    58  			}
    59  		}
    60  		if msg.Severity == support.ErrorSev {
    61  			if strings.Contains(msg.Err.Error(), "version '0.0.0.0' is not a valid SemVer") {
    62  				e = true
    63  			}
    64  			if strings.Contains(msg.Err.Error(), "name is required") {
    65  				e2 = true
    66  			}
    67  			if strings.Contains(msg.Err.Error(), "directory name (badchartfile) and chart name () must be the same") {
    68  				e3 = true
    69  			}
    70  
    71  			if strings.Contains(msg.Err.Error(), "apiVersion is required") {
    72  				e4 = true
    73  			}
    74  		}
    75  	}
    76  	if !e || !e2 || !e3 || !e4 || !w || !i {
    77  		t.Errorf("Didn't find all the expected errors, got %#v", m)
    78  	}
    79  }
    80  
    81  func TestInvalidYaml(t *testing.T) {
    82  	m := All(badYamlFileDir, values, namespace, strict).Messages
    83  	if len(m) != 1 {
    84  		t.Fatalf("All didn't fail with expected errors, got %#v", m)
    85  	}
    86  	if !strings.Contains(m[0].Err.Error(), "deliberateSyntaxError") {
    87  		t.Errorf("All didn't have the error for deliberateSyntaxError")
    88  	}
    89  }
    90  
    91  func TestBadValues(t *testing.T) {
    92  	m := All(badValuesFileDir, values, namespace, strict).Messages
    93  	if len(m) != 1 {
    94  		t.Fatalf("All didn't fail with expected errors, got %#v", m)
    95  	}
    96  	if !strings.Contains(m[0].Err.Error(), "cannot unmarshal") {
    97  		t.Errorf("All didn't have the error for invalid key format: %s", m[0].Err)
    98  	}
    99  }
   100  
   101  func TestGoodChart(t *testing.T) {
   102  	m := All(goodChartDir, values, namespace, strict).Messages
   103  	if len(m) != 0 {
   104  		t.Errorf("All failed but shouldn't have: %#v", m)
   105  	}
   106  }
   107  
   108  // TestHelmCreateChart tests that a `helm create` always passes a `helm lint` test.
   109  //
   110  // See https://github.com/helm/helm/issues/7923
   111  func TestHelmCreateChart(t *testing.T) {
   112  	dir, err := ioutil.TempDir("", "-helm-test")
   113  	if err != nil {
   114  		t.Fatal(err)
   115  	}
   116  	defer os.RemoveAll(dir)
   117  
   118  	cfile := &chart.Metadata{
   119  		Name:        "testhelmcreatepasseslint",
   120  		Description: "See lint_test.go",
   121  		Version:     "0.1.0",
   122  		AppVersion:  "1.0",
   123  		ApiVersion:  chartutil.ApiVersionV1,
   124  	}
   125  
   126  	createdChart, err := chartutil.Create(cfile, dir)
   127  	if err != nil {
   128  		t.Error(err)
   129  		// Fatal is bad because of the defer.
   130  		return
   131  	}
   132  
   133  	m := All(createdChart, values, namespace, true).Messages
   134  	if ll := len(m); ll != 1 {
   135  		t.Errorf("All should have had exactly 1 error. Got %d", ll)
   136  	} else if msg := m[0].Err.Error(); !strings.Contains(msg, "icon is recommended") {
   137  		t.Errorf("Unexpected lint error: %s", msg)
   138  	}
   139  }