Skip to content

Commit b1b0917

Browse files
committed
add tests
Signed-off-by: cpanato <[email protected]>
1 parent f6ebd0e commit b1b0917

File tree

7 files changed

+137
-8
lines changed

7 files changed

+137
-8
lines changed

pkg/chart/chart.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -280,19 +280,23 @@ func (t *Testing) LintChart(chart string, valuesFiles []string) TestResult {
280280
chartYaml := path.Join(chart, "Chart.yaml")
281281
valuesYaml := path.Join(chart, "values.yaml")
282282

283-
if err := t.linter.Yamale(chartYaml, t.config.ChartYamlSchema); err != nil {
284-
result.Error = err
285-
return result
286-
}
287-
288-
yamlFiles := append([]string{chartYaml, valuesYaml}, valuesFiles...)
289-
for _, yamlFile := range yamlFiles {
290-
if err := t.linter.YamlLint(yamlFile, t.config.LintConf); err != nil {
283+
if !t.config.NoChartSchemaValidation {
284+
if err := t.linter.Yamale(chartYaml, t.config.ChartYamlSchema); err != nil {
291285
result.Error = err
292286
return result
293287
}
294288
}
295289

290+
if !t.config.NoYamlLint {
291+
yamlFiles := append([]string{chartYaml, valuesYaml}, valuesFiles...)
292+
for _, yamlFile := range yamlFiles {
293+
if err := t.linter.YamlLint(yamlFile, t.config.LintConf); err != nil {
294+
result.Error = err
295+
return result
296+
}
297+
}
298+
}
299+
296300
if t.config.ValidateMaintainers {
297301
if err := t.ValidateMaintainers(chart); err != nil {
298302
result.Error = err

pkg/chart/chart_test.go

+106
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/helm/chart-testing/pkg/config"
2727
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/mock"
2829
)
2930

3031
type fakeGit struct{}
@@ -99,6 +100,19 @@ type fakeLinter struct{}
99100
func (l fakeLinter) YamlLint(yamlFile, configFile string) error { return nil }
100101
func (l fakeLinter) Yamale(yamlFile, schemaFile string) error { return nil }
101102

103+
type fakeLinter2 struct {
104+
mock.Mock
105+
}
106+
107+
func (l *fakeLinter2) YamlLint(yamlFile, configFile string) error {
108+
l.Called(yamlFile, configFile)
109+
return nil
110+
}
111+
func (l *fakeLinter2) Yamale(yamlFile, schemaFile string) error {
112+
l.Called(yamlFile, schemaFile)
113+
return nil
114+
}
115+
102116
type fakeHelm struct{}
103117

104118
func (h fakeHelm) Init() error { return nil }
@@ -200,3 +214,95 @@ func TestLintChartMaintainerValidation(t *testing.T) {
200214
runTests(true)
201215
runTests(false)
202216
}
217+
218+
func TestLintNoChartSchemaValidation(t *testing.T) {
219+
type testData struct {
220+
name string
221+
chartDir string
222+
expected bool
223+
}
224+
225+
runTests := func(noValidation bool, callsYamlLint, callsYamale int) {
226+
var fakeMockLinter = new(fakeLinter2)
227+
228+
fakeMockLinter.On("Yamale", mock.Anything, mock.Anything).Return(true)
229+
fakeMockLinter.On("YamlLint", mock.Anything, mock.Anything).Return(true)
230+
231+
ct.linter = fakeMockLinter
232+
ct.config.NoChartSchemaValidation = noValidation
233+
ct.config.ValidateMaintainers = false
234+
ct.config.NoYamlLint = false
235+
236+
var suffix string
237+
if noValidation {
238+
suffix = "without-validation"
239+
} else {
240+
suffix = "with-validation"
241+
}
242+
243+
testCases := []testData{
244+
{fmt.Sprintf("schema-%s", suffix), "testdata/test_lints", true},
245+
}
246+
247+
for _, testData := range testCases {
248+
t.Run(testData.name, func(t *testing.T) {
249+
result := ct.LintChart(testData.chartDir, []string{})
250+
assert.Equal(t, testData.expected, result.Error == nil)
251+
fakeMockLinter.AssertNumberOfCalls(t, "Yamale", callsYamale)
252+
fakeMockLinter.AssertNumberOfCalls(t, "YamlLint", callsYamlLint)
253+
})
254+
}
255+
}
256+
257+
// will run the schema validation
258+
runTests(false, 2, 1)
259+
// will not run the schema validation
260+
runTests(true, 2, 0)
261+
262+
}
263+
264+
func TestLintNoYamlLintValidation(t *testing.T) {
265+
type testData struct {
266+
name string
267+
chartDir string
268+
expected bool
269+
}
270+
271+
runTests := func(noValidation bool, callsYamlLint, callsYamale int) {
272+
273+
var fakeMockLinter = new(fakeLinter2)
274+
275+
fakeMockLinter.On("Yamale", mock.Anything, mock.Anything).Return(true)
276+
fakeMockLinter.On("YamlLint", mock.Anything, mock.Anything).Return(true)
277+
278+
ct.linter = fakeMockLinter
279+
ct.config.NoYamlLint = noValidation
280+
ct.config.NoChartSchemaValidation = false
281+
ct.config.ValidateMaintainers = false
282+
283+
var suffix string
284+
if noValidation {
285+
suffix = "without-yaml-validation"
286+
} else {
287+
suffix = "with-yaml-validation"
288+
}
289+
290+
testCases := []testData{
291+
{fmt.Sprintf("lint-%s", suffix), "testdata/test_lints", true},
292+
}
293+
294+
for _, testData := range testCases {
295+
t.Run(testData.name, func(t *testing.T) {
296+
result := ct.LintChart(testData.chartDir, []string{})
297+
assert.Equal(t, testData.expected, result.Error == nil)
298+
fakeMockLinter.AssertNumberOfCalls(t, "Yamale", callsYamale)
299+
fakeMockLinter.AssertNumberOfCalls(t, "YamlLint", callsYamlLint)
300+
})
301+
}
302+
}
303+
304+
// will run the lint validation
305+
runTests(false, 2, 1)
306+
// will not run the lint validation
307+
runTests(true, 0, 1)
308+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: v1
2+
appVersion: xoxo
3+
description: A Helm chart for testing
4+
name: invalid
5+
version: 1.2.3
6+
home: https://github.com/helm/chart-testing
7+
maintainers:
8+
- name: invalid
9+
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test: abc
2+
list:
3+
- abc

pkg/config/config_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func loadAndAssertConfigFromFile(t *testing.T, configFile string) {
3838
require.Equal(t, "my-lint-conf.yaml", cfg.LintConf)
3939
require.Equal(t, "my-chart-yaml-schema.yaml", cfg.ChartYamlSchema)
4040
require.Equal(t, true, cfg.ValidateMaintainers)
41+
require.Equal(t, false, cfg.NoChartSchemaValidation)
42+
require.Equal(t, false, cfg.NoYamlLint)
4143
require.Equal(t, true, cfg.CheckVersionIncrement)
4244
require.Equal(t, false, cfg.ProcessAllCharts)
4345
require.Equal(t, []string{"incubator=https://incubator"}, cfg.ChartRepos)

pkg/config/test_config.json

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"chart-yaml-schema": "my-chart-yaml-schema.yaml",
77
"github-instance": "https://github.com",
88
"validate-maintainers": true,
9+
"no-chart-schema-validation": false,
10+
"no-yaml-lint": false,
911
"check-version-increment": true,
1012
"all": false,
1113
"chart-repos": [

pkg/config/test_config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ lint-conf: my-lint-conf.yaml
55
chart-yaml-schema: my-chart-yaml-schema.yaml
66
github-instance: https://github.com
77
validate-maintainers: true
8+
no-chart-schema-validation: false
9+
no-yaml-lint: false
810
check-version-increment: true
911
all: false
1012
chart-repos:

0 commit comments

Comments
 (0)