github.com/koderover/helm@v2.17.0+incompatible/cmd/helm/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 main
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"testing"
    23  )
    24  
    25  func TestLintChart(t *testing.T) {
    26  	tests := []struct {
    27  		name      string
    28  		chartPath string
    29  		err       bool
    30  	}{
    31  		{
    32  			name:      "decompressed-chart",
    33  			chartPath: "testdata/testcharts/decompressedchart/",
    34  		},
    35  		{
    36  			name:      "archived-chart-path",
    37  			chartPath: "testdata/testcharts/compressedchart-0.1.0.tgz",
    38  		},
    39  		{
    40  			name:      "archived-chart-path-with-hyphens",
    41  			chartPath: "testdata/testcharts/compressedchart-with-hyphens-0.1.0.tgz",
    42  		},
    43  		{
    44  			name:      "pre-release-chart",
    45  			chartPath: "testdata/testcharts/pre-release-chart-0.1.0-alpha.tgz",
    46  		},
    47  		{
    48  			name:      "invalid-archived-chart-path",
    49  			chartPath: "testdata/testcharts/invalidcompressedchart0.1.0.tgz",
    50  			err:       true,
    51  		},
    52  		{
    53  			name:      "chart-missing-manifest",
    54  			chartPath: "testdata/testcharts/chart-missing-manifest",
    55  			err:       true,
    56  		},
    57  	}
    58  
    59  	values := []byte{}
    60  	namespace := "testNamespace"
    61  	strict := false
    62  
    63  	for _, tt := range tests {
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			_, err := lintChart(tt.chartPath, values, namespace, strict)
    66  			switch {
    67  			case err != nil && !tt.err:
    68  				t.Errorf("%s", err)
    69  			case err == nil && tt.err:
    70  				t.Errorf("Expected a chart parsing error")
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestLinRunForNonExistentChart(t *testing.T) {
    77  	t.Run("should error out for non existent tgz chart", func(t *testing.T) {
    78  		testCharts := []string{"non-existent-chart.tgz"}
    79  		testLint := &lintCmd{
    80  			paths: testCharts,
    81  			out:   bytes.NewBufferString(""),
    82  		}
    83  		expectedErr := fmt.Errorf("0 chart(s) linted, 1 chart(s) failed")
    84  		err := testLint.run()
    85  		if err == nil {
    86  			t.Errorf("expected error but got no error")
    87  		}
    88  		if err != nil && (err.Error() != expectedErr.Error()) {
    89  			t.Errorf("expected: \"%v\" , but got: \"%v\"", expectedErr, err)
    90  		}
    91  	})
    92  
    93  	t.Run("should error out for corrupted tgz chart", func(t *testing.T) {
    94  		var corruptedTgzChart = "testdata/testcharts/corrupted-compressed-chart.tgz"
    95  		testCharts := []string{corruptedTgzChart}
    96  		testLint := &lintCmd{
    97  			paths: testCharts,
    98  			out:   bytes.NewBufferString(""),
    99  		}
   100  		expectedErr := fmt.Errorf("0 chart(s) linted, 1 chart(s) failed")
   101  		err := testLint.run()
   102  		if err == nil {
   103  			t.Errorf("expected error but got no error")
   104  		}
   105  		if err != nil && (err.Error() != expectedErr.Error()) {
   106  			t.Errorf("expected: \"%v\" , but got: \"%v\"", expectedErr, err)
   107  		}
   108  	})
   109  }
   110  
   111  func TestLintRunForStrictErrors(t *testing.T) {
   112  	out := bytes.NewBufferString("")
   113  	tests := []struct {
   114  		name      string
   115  		chartPath string
   116  		strict    bool
   117  		errStr    string
   118  		wantErr   bool
   119  	}{
   120  		{
   121  			name:      "non-strict should not error",
   122  			chartPath: "testdata/testcharts/bad.name",
   123  			strict:    false,
   124  			wantErr:   false,
   125  		},
   126  		{
   127  			name:      "strict should error",
   128  			chartPath: "testdata/testcharts/bad.name",
   129  			strict:    true,
   130  			errStr:    "1 chart(s) linted, 1 chart(s) failed",
   131  			wantErr:   true,
   132  		},
   133  	}
   134  	for _, tt := range tests {
   135  		t.Run(tt.name, func(t *testing.T) {
   136  			testLint := &lintCmd{
   137  				paths:  []string{tt.chartPath},
   138  				strict: tt.strict,
   139  				out:    out,
   140  			}
   141  			expectedErr := fmt.Errorf(tt.errStr)
   142  			err := testLint.run()
   143  			if tt.wantErr && err == nil {
   144  				t.Errorf("expected error but got no error")
   145  			}
   146  			if err != nil && (err.Error() != expectedErr.Error()) {
   147  				t.Errorf("expected: \"%v\" , but got: \"%v\"", expectedErr, err)
   148  			}
   149  		})
   150  	}
   151  }