github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/action/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 action
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  var (
    24  	values                  = make(map[string]interface{})
    25  	namespace               = "testNamespace"
    26  	strict                  = false
    27  	chart1MultipleChartLint = "testdata/charts/multiplecharts-lint-chart-1"
    28  	chart2MultipleChartLint = "testdata/charts/multiplecharts-lint-chart-2"
    29  	corruptedTgzChart       = "testdata/charts/corrupted-compressed-chart.tgz"
    30  	chartWithNoTemplatesDir = "testdata/charts/chart-with-no-templates-dir"
    31  )
    32  
    33  func TestLintChart(t *testing.T) {
    34  	tests := []struct {
    35  		name      string
    36  		chartPath string
    37  		err       bool
    38  	}{
    39  		{
    40  			name:      "decompressed-chart",
    41  			chartPath: "testdata/charts/decompressedchart/",
    42  		},
    43  		{
    44  			name:      "archived-chart-path",
    45  			chartPath: "testdata/charts/compressedchart-0.1.0.tgz",
    46  		},
    47  		{
    48  			name:      "archived-chart-path-with-hyphens",
    49  			chartPath: "testdata/charts/compressedchart-with-hyphens-0.1.0.tgz",
    50  		},
    51  		{
    52  			name:      "archived-tar-gz-chart-path",
    53  			chartPath: "testdata/charts/compressedchart-0.1.0.tar.gz",
    54  		},
    55  		{
    56  			name:      "invalid-archived-chart-path",
    57  			chartPath: "testdata/charts/invalidcompressedchart0.1.0.tgz",
    58  			err:       true,
    59  		},
    60  		{
    61  			name:      "chart-missing-manifest",
    62  			chartPath: "testdata/charts/chart-missing-manifest",
    63  			err:       true,
    64  		},
    65  		{
    66  			name:      "chart-with-schema",
    67  			chartPath: "testdata/charts/chart-with-schema",
    68  		},
    69  		{
    70  			name:      "chart-with-schema-negative",
    71  			chartPath: "testdata/charts/chart-with-schema-negative",
    72  		},
    73  		{
    74  			name:      "pre-release-chart",
    75  			chartPath: "testdata/charts/pre-release-chart-0.1.0-alpha.tgz",
    76  		},
    77  	}
    78  
    79  	for _, tt := range tests {
    80  		t.Run(tt.name, func(t *testing.T) {
    81  			_, err := lintChart(tt.chartPath, map[string]interface{}{}, namespace, strict)
    82  			switch {
    83  			case err != nil && !tt.err:
    84  				t.Errorf("%s", err)
    85  			case err == nil && tt.err:
    86  				t.Errorf("Expected a chart parsing error")
    87  			}
    88  		})
    89  	}
    90  }
    91  
    92  func TestNonExistentChart(t *testing.T) {
    93  	t.Run("should error out for non existent tgz chart", func(t *testing.T) {
    94  		testCharts := []string{"non-existent-chart.tgz"}
    95  		expectedError := "unable to open tarball: open non-existent-chart.tgz: no such file or directory"
    96  		testLint := NewLint()
    97  
    98  		result := testLint.Run(testCharts, values)
    99  		if len(result.Errors) != 1 {
   100  			t.Error("expected one error, but got", len(result.Errors))
   101  		}
   102  
   103  		actual := result.Errors[0].Error()
   104  		if actual != expectedError {
   105  			t.Errorf("expected '%s', but got '%s'", expectedError, actual)
   106  		}
   107  	})
   108  
   109  	t.Run("should error out for corrupted tgz chart", func(t *testing.T) {
   110  		testCharts := []string{corruptedTgzChart}
   111  		expectedEOFError := "unable to extract tarball: EOF"
   112  		testLint := NewLint()
   113  
   114  		result := testLint.Run(testCharts, values)
   115  		if len(result.Errors) != 1 {
   116  			t.Error("expected one error, but got", len(result.Errors))
   117  		}
   118  
   119  		actual := result.Errors[0].Error()
   120  		if actual != expectedEOFError {
   121  			t.Errorf("expected '%s', but got '%s'", expectedEOFError, actual)
   122  		}
   123  	})
   124  }
   125  
   126  func TestLint_MultipleCharts(t *testing.T) {
   127  	testCharts := []string{chart2MultipleChartLint, chart1MultipleChartLint}
   128  	testLint := NewLint()
   129  	if result := testLint.Run(testCharts, values); len(result.Errors) > 0 {
   130  		t.Error(result.Errors)
   131  	}
   132  }
   133  
   134  func TestLint_EmptyResultErrors(t *testing.T) {
   135  	testCharts := []string{chart2MultipleChartLint}
   136  	testLint := NewLint()
   137  	if result := testLint.Run(testCharts, values); len(result.Errors) > 0 {
   138  		t.Error("Expected no error, got more")
   139  	}
   140  }
   141  
   142  func TestLint_ChartWithWarnings(t *testing.T) {
   143  	t.Run("should pass when not strict", func(t *testing.T) {
   144  		testCharts := []string{chartWithNoTemplatesDir}
   145  		testLint := NewLint()
   146  		testLint.Strict = false
   147  		if result := testLint.Run(testCharts, values); len(result.Errors) > 0 {
   148  			t.Error("Expected no error, got more")
   149  		}
   150  	})
   151  
   152  	t.Run("should fail with errors when strict", func(t *testing.T) {
   153  		testCharts := []string{chartWithNoTemplatesDir}
   154  		testLint := NewLint()
   155  		testLint.Strict = true
   156  		if result := testLint.Run(testCharts, values); len(result.Errors) != 1 {
   157  			t.Error("expected one error, but got", len(result.Errors))
   158  		}
   159  	})
   160  }