Skip to content

Commit 46dc82e

Browse files
cpanatounguiculus
authored andcommitted
Add new flags to disable lint/schema validation (#68)
Signed-off-by: cpanato <[email protected]>
1 parent d3c13ac commit 46dc82e

15 files changed

+165
-26
lines changed

Gopkg.lock

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/cmd/lint.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"os"
2020

2121
"github.com/MakeNowJust/heredoc"
22-
2322
"github.com/helm/chart-testing/pkg/chart"
2423
"github.com/helm/chart-testing/pkg/config"
2524
"github.com/spf13/cobra"
@@ -65,9 +64,13 @@ func addLintFlags(flags *flag.FlagSet) {
6564
is searched in the current directory, '$HOME/.ct', and '/etc/ct', in
6665
that order.`))
6766
flags.Bool("validate-maintainers", true, heredoc.Doc(`
68-
Enabled validation of maintainer account names in chart.yml (default: true).
67+
Enable validation of maintainer account names in chart.yml (default: true).
6968
Works for GitHub, GitLab, and Bitbucket`))
7069
flags.Bool("check-version-increment", true, "Activates a check for chart version increments (default: true)")
70+
flags.Bool("validate-chart-schema", true, heredoc.Doc(`
71+
Enable schema validation of 'Chart.yaml' using Yamale (default: true)`))
72+
flags.Bool("validate-yaml", true, heredoc.Doc(`
73+
Enable linting of 'Chart.yaml' and values files (default: true)`))
7174
}
7275

7376
func lint(cmd *cobra.Command, args []string) {
@@ -95,6 +98,6 @@ func lint(cmd *cobra.Command, args []string) {
9598
}
9699

97100
func bindLintFlags(flagSet *flag.FlagSet, v *viper.Viper) error {
98-
options := []string{"lint-conf", "chart-yaml-schema", "validate-maintainers", "check-version-increment"}
101+
options := []string{"lint-conf", "chart-yaml-schema", "validate-maintainers", "check-version-increment", "validate-chart-schema", "validate-yaml"}
99102
return bindFlags(options, flagSet, v)
100103
}

doc/ct.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ in given chart directories.
2525
* [ct lint-and-install](ct_lint-and-install.md) - Lint, install, and test a chart
2626
* [ct version](ct_version.md) - Print version information
2727

28-
###### Auto generated by spf13/cobra on 17-Nov-2018
28+
###### Auto generated by spf13/cobra on 19-Dec-2018

doc/ct_install.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ ct install [flags]
5757

5858
* [ct](ct.md) - The Helm chart testing tool
5959

60-
###### Auto generated by spf13/cobra on 17-Nov-2018
60+
###### Auto generated by spf13/cobra on 19-Dec-2018

doc/ct_lint-and-install.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ ct lint-and-install [flags]
4646
This is only used if namespace is specified. (default "app.kubernetes.io/instance")
4747
--remote string The name of the Git remote used to identify changed charts (default "origin")
4848
--target-branch string The name of the target branch used to identify changed charts (default "master")
49-
--validate-maintainers Enabled validation of maintainer account names in chart.yml (default: true).
49+
--validate-chart-schema Enable schema validation of 'Chart.yaml' using Yamale (default: true) (default true)
50+
--validate-maintainers Enable validation of maintainer account names in chart.yml (default: true).
5051
Works for GitHub, GitLab, and Bitbucket (default true)
52+
--validate-yaml Enable linting of 'Chart.yaml' and values files (default: true) (default true)
5153
```
5254

5355
### SEE ALSO
5456

5557
* [ct](ct.md) - The Helm chart testing tool
5658

57-
###### Auto generated by spf13/cobra on 17-Nov-2018
59+
###### Auto generated by spf13/cobra on 19-Dec-2018

doc/ct_lint.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ ct lint [flags]
5050
that order
5151
--remote string The name of the Git remote used to identify changed charts (default "origin")
5252
--target-branch string The name of the target branch used to identify changed charts (default "master")
53-
--validate-maintainers Enabled validation of maintainer account names in chart.yml (default: true).
53+
--validate-chart-schema Enable schema validation of 'Chart.yaml' using Yamale (default: true) (default true)
54+
--validate-maintainers Enable validation of maintainer account names in chart.yml (default: true).
5455
Works for GitHub, GitLab, and Bitbucket (default true)
56+
--validate-yaml Enable linting of 'Chart.yaml' and values files (default: true) (default true)
5557
```
5658

5759
### SEE ALSO
5860

5961
* [ct](ct.md) - The Helm chart testing tool
6062

61-
###### Auto generated by spf13/cobra on 17-Nov-2018
63+
###### Auto generated by spf13/cobra on 19-Dec-2018

doc/ct_version.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ ct version [flags]
2020

2121
* [ct](ct.md) - The Helm chart testing tool
2222

23-
###### Auto generated by spf13/cobra on 17-Nov-2018
23+
###### Auto generated by spf13/cobra on 19-Dec-2018

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.ValidateChartSchema {
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.ValidateYaml {
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

+105-8
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ import (
1919
"strings"
2020
"testing"
2121

22-
"github.com/pkg/errors"
23-
24-
"github.com/helm/chart-testing/pkg/util"
25-
2622
"github.com/helm/chart-testing/pkg/config"
23+
"github.com/helm/chart-testing/pkg/util"
24+
"github.com/pkg/errors"
2725
"github.com/stretchr/testify/assert"
26+
"github.com/stretchr/testify/mock"
2827
)
2928

3029
type fakeGit struct{}
@@ -94,10 +93,18 @@ func (v fakeAccountValidator) Validate(repoDomain string, account string) error
9493
return errors.New(fmt.Sprintf("Error validating account: %s", account))
9594
}
9695

97-
type fakeLinter struct{}
96+
type fakeLinter struct {
97+
mock.Mock
98+
}
9899

99-
func (l fakeLinter) YamlLint(yamlFile, configFile string) error { return nil }
100-
func (l fakeLinter) Yamale(yamlFile, schemaFile string) error { return nil }
100+
func (l *fakeLinter) YamlLint(yamlFile, configFile string) error {
101+
l.Called(yamlFile, configFile)
102+
return nil
103+
}
104+
func (l *fakeLinter) Yamale(yamlFile, schemaFile string) error {
105+
l.Called(yamlFile, schemaFile)
106+
return nil
107+
}
101108

102109
type fakeHelm struct{}
103110

@@ -120,13 +127,16 @@ func init() {
120127
ExcludedCharts: []string{"excluded"},
121128
ChartDirs: []string{"stable", "incubator"},
122129
}
130+
131+
fakeMockLinter := new(fakeLinter)
132+
123133
ct = Testing{
124134
config: cfg,
125135
directoryLister: fakeDirLister{},
126136
git: fakeGit{},
127137
chartUtils: fakeChartUtils{},
128138
accountValidator: fakeAccountValidator{},
129-
linter: fakeLinter{},
139+
linter: fakeMockLinter,
130140
helm: fakeHelm{},
131141
}
132142
}
@@ -200,3 +210,90 @@ func TestLintChartMaintainerValidation(t *testing.T) {
200210
runTests(true)
201211
runTests(false)
202212
}
213+
214+
func TestLintChartSchemaValidation(t *testing.T) {
215+
type testData struct {
216+
name string
217+
chartDir string
218+
expected bool
219+
}
220+
221+
runTests := func(validate bool, callsYamlLint int, callsYamale int) {
222+
fakeMockLinter := new(fakeLinter)
223+
224+
fakeMockLinter.On("Yamale", mock.Anything, mock.Anything).Return(true)
225+
fakeMockLinter.On("YamlLint", mock.Anything, mock.Anything).Return(true)
226+
227+
ct.linter = fakeMockLinter
228+
ct.config.ValidateChartSchema = validate
229+
ct.config.ValidateMaintainers = false
230+
ct.config.ValidateYaml = false
231+
232+
var suffix string
233+
if validate {
234+
suffix = "with-validation"
235+
} else {
236+
suffix = "without-validation"
237+
}
238+
239+
testCases := []testData{
240+
{fmt.Sprintf("schema-%s", suffix), "testdata/test_lints", true},
241+
}
242+
243+
for _, testData := range testCases {
244+
t.Run(testData.name, func(t *testing.T) {
245+
result := ct.LintChart(testData.chartDir, []string{})
246+
assert.Equal(t, testData.expected, result.Error == nil)
247+
fakeMockLinter.AssertNumberOfCalls(t, "Yamale", callsYamale)
248+
fakeMockLinter.AssertNumberOfCalls(t, "YamlLint", callsYamlLint)
249+
})
250+
}
251+
}
252+
253+
runTests(true, 0, 1)
254+
runTests(false, 0, 0)
255+
256+
}
257+
258+
func TestLintYamlValidation(t *testing.T) {
259+
type testData struct {
260+
name string
261+
chartDir string
262+
expected bool
263+
}
264+
265+
runTests := func(validate bool, callsYamlLint int, callsYamale int) {
266+
fakeMockLinter := new(fakeLinter)
267+
268+
fakeMockLinter.On("Yamale", mock.Anything, mock.Anything).Return(true)
269+
fakeMockLinter.On("YamlLint", mock.Anything, mock.Anything).Return(true)
270+
271+
ct.linter = fakeMockLinter
272+
ct.config.ValidateYaml = validate
273+
ct.config.ValidateChartSchema = false
274+
ct.config.ValidateMaintainers = false
275+
276+
var suffix string
277+
if validate {
278+
suffix = "with-validation"
279+
} else {
280+
suffix = "without-validation"
281+
}
282+
283+
testCases := []testData{
284+
{fmt.Sprintf("lint-%s", suffix), "testdata/test_lints", true},
285+
}
286+
287+
for _, testData := range testCases {
288+
t.Run(testData.name, func(t *testing.T) {
289+
result := ct.LintChart(testData.chartDir, []string{})
290+
assert.Equal(t, testData.expected, result.Error == nil)
291+
fakeMockLinter.AssertNumberOfCalls(t, "Yamale", callsYamale)
292+
fakeMockLinter.AssertNumberOfCalls(t, "YamlLint", callsYamlLint)
293+
})
294+
}
295+
}
296+
297+
runTests(true, 2, 0)
298+
runTests(false, 0, 0)
299+
}
+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.go

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type Configuration struct {
4545
LintConf string `mapstructure:"lint-conf"`
4646
ChartYamlSchema string `mapstructure:"chart-yaml-schema"`
4747
ValidateMaintainers bool `mapstructure:"validate-maintainers"`
48+
ValidateChartSchema bool `mapstructure:"validate-chart-schema"`
49+
ValidateYaml bool `mapstructure:"validate-yaml"`
4850
CheckVersionIncrement bool `mapstructure:"check-version-increment"`
4951
ProcessAllCharts bool `mapstructure:"all"`
5052
Charts []string `mapstructure:"charts"`

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, true, cfg.ValidateChartSchema)
42+
require.Equal(t, true, cfg.ValidateYaml)
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+
"validate-chart-schema": true,
10+
"validate-yaml": true,
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+
validate-chart-schema: true
9+
validate-yaml: true
810
check-version-increment: true
911
all: false
1012
chart-repos:

0 commit comments

Comments
 (0)