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