github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/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  	"strings"
    21  	"testing"
    22  
    23  	"github.com/stefanmcshane/helm/pkg/chartutil"
    24  	"github.com/stefanmcshane/helm/pkg/lint/support"
    25  )
    26  
    27  var values map[string]interface{}
    28  
    29  const namespace = "testNamespace"
    30  const strict = false
    31  
    32  const badChartDir = "rules/testdata/badchartfile"
    33  const badValuesFileDir = "rules/testdata/badvaluesfile"
    34  const badYamlFileDir = "rules/testdata/albatross"
    35  const goodChartDir = "rules/testdata/goodone"
    36  const subChartValuesDir = "rules/testdata/withsubchart"
    37  
    38  func TestBadChart(t *testing.T) {
    39  	m := All(badChartDir, values, namespace, strict).Messages
    40  	if len(m) != 8 {
    41  		t.Errorf("Number of errors %v", len(m))
    42  		t.Errorf("All didn't fail with expected errors, got %#v", m)
    43  	}
    44  	// There should be one INFO, 2 WARNINGs and 2 ERROR messages, check for them
    45  	var i, w, e, e2, e3, e4, e5, e6 bool
    46  	for _, msg := range m {
    47  		if msg.Severity == support.InfoSev {
    48  			if strings.Contains(msg.Err.Error(), "icon is recommended") {
    49  				i = true
    50  			}
    51  		}
    52  		if msg.Severity == support.WarningSev {
    53  			if strings.Contains(msg.Err.Error(), "directory not found") {
    54  				w = true
    55  			}
    56  		}
    57  		if msg.Severity == support.ErrorSev {
    58  			if strings.Contains(msg.Err.Error(), "version '0.0.0.0' is not a valid SemVer") {
    59  				e = true
    60  			}
    61  			if strings.Contains(msg.Err.Error(), "name is required") {
    62  				e2 = true
    63  			}
    64  
    65  			if strings.Contains(msg.Err.Error(), "apiVersion is required. The value must be either \"v1\" or \"v2\"") {
    66  				e3 = true
    67  			}
    68  
    69  			if strings.Contains(msg.Err.Error(), "chart type is not valid in apiVersion") {
    70  				e4 = true
    71  			}
    72  
    73  			if strings.Contains(msg.Err.Error(), "dependencies are not valid in the Chart file with apiVersion") {
    74  				e5 = true
    75  			}
    76  			// This comes from the dependency check, which loads dependency info from the Chart.yaml
    77  			if strings.Contains(msg.Err.Error(), "unable to load chart") {
    78  				e6 = true
    79  			}
    80  		}
    81  	}
    82  	if !e || !e2 || !e3 || !e4 || !e5 || !w || !i || !e6 {
    83  		t.Errorf("Didn't find all the expected errors, got %#v", m)
    84  	}
    85  }
    86  
    87  func TestInvalidYaml(t *testing.T) {
    88  	m := All(badYamlFileDir, values, namespace, strict).Messages
    89  	if len(m) != 1 {
    90  		t.Fatalf("All didn't fail with expected errors, got %#v", m)
    91  	}
    92  	if !strings.Contains(m[0].Err.Error(), "deliberateSyntaxError") {
    93  		t.Errorf("All didn't have the error for deliberateSyntaxError")
    94  	}
    95  }
    96  
    97  func TestBadValues(t *testing.T) {
    98  	m := All(badValuesFileDir, values, namespace, strict).Messages
    99  	if len(m) < 1 {
   100  		t.Fatalf("All didn't fail with expected errors, got %#v", m)
   101  	}
   102  	if !strings.Contains(m[0].Err.Error(), "unable to parse YAML") {
   103  		t.Errorf("All didn't have the error for invalid key format: %s", m[0].Err)
   104  	}
   105  }
   106  
   107  func TestGoodChart(t *testing.T) {
   108  	m := All(goodChartDir, values, namespace, strict).Messages
   109  	if len(m) != 0 {
   110  		t.Error("All returned linter messages when it shouldn't have")
   111  		for i, msg := range m {
   112  			t.Logf("Message %d: %s", i, msg)
   113  		}
   114  	}
   115  }
   116  
   117  // TestHelmCreateChart tests that a `helm create` always passes a `helm lint` test.
   118  //
   119  // See https://github.com/helm/helm/issues/7923
   120  func TestHelmCreateChart(t *testing.T) {
   121  	dir := t.TempDir()
   122  
   123  	createdChart, err := chartutil.Create("testhelmcreatepasseslint", dir)
   124  	if err != nil {
   125  		t.Error(err)
   126  		// Fatal is bad because of the defer.
   127  		return
   128  	}
   129  
   130  	// Note: we test with strict=true here, even though others have
   131  	// strict = false.
   132  	m := All(createdChart, values, namespace, true).Messages
   133  	if ll := len(m); ll != 1 {
   134  		t.Errorf("All should have had exactly 1 error. Got %d", ll)
   135  		for i, msg := range m {
   136  			t.Logf("Message %d: %s", i, msg.Error())
   137  		}
   138  	} else if msg := m[0].Err.Error(); !strings.Contains(msg, "icon is recommended") {
   139  		t.Errorf("Unexpected lint error: %s", msg)
   140  	}
   141  }
   142  
   143  // lint ignores import-values
   144  // See https://github.com/helm/helm/issues/9658
   145  func TestSubChartValuesChart(t *testing.T) {
   146  	m := All(subChartValuesDir, values, namespace, strict).Messages
   147  	if len(m) != 0 {
   148  		t.Error("All returned linter messages when it shouldn't have")
   149  		for i, msg := range m {
   150  			t.Logf("Message %d: %s", i, msg)
   151  		}
   152  	}
   153  }