From 23926365a0654a0d12cd66022b0b54f67fdd79ef Mon Sep 17 00:00:00 2001 From: Karuppiah Natarajan Date: Tue, 1 Oct 2019 00:06:53 +0530 Subject: [PATCH] fix silent lint ignore for non existing packaged charts Signed-off-by: Karuppiah Natarajan --- cmd/helm/lint.go | 41 +++++++++---------- cmd/helm/lint_test.go | 37 +++++++++++++++++ .../testcharts/corrupted-compressed-chart.tgz | 0 3 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 cmd/helm/testdata/testcharts/corrupted-compressed-chart.tgz diff --git a/cmd/helm/lint.go b/cmd/helm/lint.go index 342cc22ea..c52b23c78 100644 --- a/cmd/helm/lint.go +++ b/cmd/helm/lint.go @@ -17,7 +17,6 @@ limitations under the License. package main import ( - "errors" "fmt" "io" "io/ioutil" @@ -81,8 +80,6 @@ func newLintCmd(out io.Writer) *cobra.Command { return cmd } -var errLintNoChart = errors.New("No chart found for linting (missing Chart.yaml)") - func (l *lintCmd) run() error { var lowestTolerance int if l.strict { @@ -100,27 +97,27 @@ func (l *lintCmd) run() error { var total int var failures int for _, path := range l.paths { - if linter, err := lintChart(path, rvals, l.namespace, l.strict); err != nil { + linter, err := lintChart(path, rvals, l.namespace, l.strict) + if err != nil { + failures = failures + 1 fmt.Println("==> Skipping", path) fmt.Println(err) - if err == errLintNoChart { - failures = failures + 1 - } - } else { - fmt.Println("==> Linting", path) + fmt.Println("") + continue + } - if len(linter.Messages) == 0 { - fmt.Println("Lint OK") - } + fmt.Println("==> Linting", path) + if len(linter.Messages) == 0 { + fmt.Println("Lint OK") + } - for _, msg := range linter.Messages { - fmt.Println(msg) - } + for _, msg := range linter.Messages { + fmt.Println(msg) + } - total = total + 1 - if linter.HighestSeverity >= lowestTolerance { - failures = failures + 1 - } + total = total + 1 + if linter.HighestSeverity >= lowestTolerance { + failures = failures + 1 } fmt.Println("") } @@ -148,12 +145,12 @@ func lintChart(path string, vals []byte, namespace string, strict bool) (support file, err := os.Open(path) if err != nil { - return linter, err + return linter, fmt.Errorf("unable to open tar ball %s: %s", path, err.Error()) } defer file.Close() if err = chartutil.Expand(tempDir, file); err != nil { - return linter, err + return linter, fmt.Errorf("unable to extract tar ball: %s", err.Error()) } files, err := ioutil.ReadDir(tempDir) @@ -171,7 +168,7 @@ func lintChart(path string, vals []byte, namespace string, strict bool) (support // Guard: Error out if this is not a chart. if _, err := os.Stat(filepath.Join(chartPath, "Chart.yaml")); err != nil { - return linter, errLintNoChart + return linter, fmt.Errorf("unable to check Chart.yaml file in chart: %s", err.Error()) } return lint.All(chartPath, vals, namespace, strict), nil diff --git a/cmd/helm/lint_test.go b/cmd/helm/lint_test.go index fc40151b8..42487a34f 100644 --- a/cmd/helm/lint_test.go +++ b/cmd/helm/lint_test.go @@ -17,6 +17,8 @@ limitations under the License. package main import ( + "bytes" + "fmt" "testing" ) @@ -70,3 +72,38 @@ func TestLintChart(t *testing.T) { }) } } + +func TestLinRunForNonExistentChart(t *testing.T) { + t.Run("should error out for non existent tgz chart", func(t *testing.T) { + testCharts := []string{"non-existent-chart.tgz"} + testLint := &lintCmd{ + paths: testCharts, + out: bytes.NewBufferString(""), + } + expectedErr := fmt.Errorf("0 chart(s) linted, 1 chart(s) failed") + err := testLint.run() + if err == nil { + t.Errorf("expected error but got no error") + } + if err != nil && (err.Error() != expectedErr.Error()) { + t.Errorf("expected: \"%v\" , but got: \"%v\"", expectedErr, err) + } + }) + + t.Run("should error out for corrupted tgz chart", func(t *testing.T) { + var corruptedTgzChart = "testdata/testcharts/corrupted-compressed-chart.tgz" + testCharts := []string{corruptedTgzChart} + testLint := &lintCmd{ + paths: testCharts, + out: bytes.NewBufferString(""), + } + expectedErr := fmt.Errorf("0 chart(s) linted, 1 chart(s) failed") + err := testLint.run() + if err == nil { + t.Errorf("expected error but got no error") + } + if err != nil && (err.Error() != expectedErr.Error()) { + t.Errorf("expected: \"%v\" , but got: \"%v\"", expectedErr, err) + } + }) +} diff --git a/cmd/helm/testdata/testcharts/corrupted-compressed-chart.tgz b/cmd/helm/testdata/testcharts/corrupted-compressed-chart.tgz new file mode 100644 index 000000000..e69de29bb