Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new flags to disable lint/schema validation #68

Merged
merged 5 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions app/cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"os"

"github.com/MakeNowJust/heredoc"

"github.com/helm/chart-testing/pkg/chart"
"github.com/helm/chart-testing/pkg/config"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -65,9 +64,13 @@ func addLintFlags(flags *flag.FlagSet) {
is searched in the current directory, '$HOME/.ct', and '/etc/ct', in
that order.`))
flags.Bool("validate-maintainers", true, heredoc.Doc(`
Enabled validation of maintainer account names in chart.yml (default: true).
Enable validation of maintainer account names in chart.yml (default: true).
Works for GitHub, GitLab, and Bitbucket`))
flags.Bool("check-version-increment", true, "Activates a check for chart version increments (default: true)")
flags.Bool("validate-chart-schema", true, heredoc.Doc(`
Enable schema validation of 'Chart.yaml' using Yamale (default: true)`))
flags.Bool("validate-yaml", true, heredoc.Doc(`
Enable linting of 'Chart.yaml' and values files (default: true)`))
}

func lint(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -95,6 +98,6 @@ func lint(cmd *cobra.Command, args []string) {
}

func bindLintFlags(flagSet *flag.FlagSet, v *viper.Viper) error {
options := []string{"lint-conf", "chart-yaml-schema", "validate-maintainers", "check-version-increment"}
options := []string{"lint-conf", "chart-yaml-schema", "validate-maintainers", "check-version-increment", "validate-chart-schema", "validate-yaml"}
return bindFlags(options, flagSet, v)
}
2 changes: 1 addition & 1 deletion doc/ct.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ in given chart directories.
* [ct lint-and-install](ct_lint-and-install.md) - Lint, install, and test a chart
* [ct version](ct_version.md) - Print version information

###### Auto generated by spf13/cobra on 17-Nov-2018
###### Auto generated by spf13/cobra on 19-Dec-2018
2 changes: 1 addition & 1 deletion doc/ct_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ ct install [flags]

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

###### Auto generated by spf13/cobra on 17-Nov-2018
###### Auto generated by spf13/cobra on 19-Dec-2018
6 changes: 4 additions & 2 deletions doc/ct_lint-and-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ ct lint-and-install [flags]
This is only used if namespace is specified. (default "app.kubernetes.io/instance")
--remote string The name of the Git remote used to identify changed charts (default "origin")
--target-branch string The name of the target branch used to identify changed charts (default "master")
--validate-maintainers Enabled validation of maintainer account names in chart.yml (default: true).
--validate-chart-schema Enable schema validation of 'Chart.yaml' using Yamale (default: true) (default true)
--validate-maintainers Enable validation of maintainer account names in chart.yml (default: true).
Works for GitHub, GitLab, and Bitbucket (default true)
--validate-yaml Enable linting of 'Chart.yaml' and values files (default: true) (default true)
```

### SEE ALSO

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

###### Auto generated by spf13/cobra on 17-Nov-2018
###### Auto generated by spf13/cobra on 19-Dec-2018
6 changes: 4 additions & 2 deletions doc/ct_lint.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ ct lint [flags]
that order
--remote string The name of the Git remote used to identify changed charts (default "origin")
--target-branch string The name of the target branch used to identify changed charts (default "master")
--validate-maintainers Enabled validation of maintainer account names in chart.yml (default: true).
--validate-chart-schema Enable schema validation of 'Chart.yaml' using Yamale (default: true) (default true)
--validate-maintainers Enable validation of maintainer account names in chart.yml (default: true).
Works for GitHub, GitLab, and Bitbucket (default true)
--validate-yaml Enable linting of 'Chart.yaml' and values files (default: true) (default true)
```

### SEE ALSO

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

###### Auto generated by spf13/cobra on 17-Nov-2018
###### Auto generated by spf13/cobra on 19-Dec-2018
2 changes: 1 addition & 1 deletion doc/ct_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ ct version [flags]

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

###### Auto generated by spf13/cobra on 17-Nov-2018
###### Auto generated by spf13/cobra on 19-Dec-2018
20 changes: 12 additions & 8 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,23 @@ func (t *Testing) LintChart(chart string, valuesFiles []string) TestResult {
chartYaml := path.Join(chart, "Chart.yaml")
valuesYaml := path.Join(chart, "values.yaml")

if err := t.linter.Yamale(chartYaml, t.config.ChartYamlSchema); err != nil {
result.Error = err
return result
}

yamlFiles := append([]string{chartYaml, valuesYaml}, valuesFiles...)
for _, yamlFile := range yamlFiles {
if err := t.linter.YamlLint(yamlFile, t.config.LintConf); err != nil {
if t.config.ValidateChartSchema {
if err := t.linter.Yamale(chartYaml, t.config.ChartYamlSchema); err != nil {
result.Error = err
return result
}
}

if t.config.ValidateYaml {
yamlFiles := append([]string{chartYaml, valuesYaml}, valuesFiles...)
for _, yamlFile := range yamlFiles {
if err := t.linter.YamlLint(yamlFile, t.config.LintConf); err != nil {
result.Error = err
return result
}
}
}

if t.config.ValidateMaintainers {
if err := t.ValidateMaintainers(chart); err != nil {
result.Error = err
Expand Down
113 changes: 105 additions & 8 deletions pkg/chart/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ import (
"strings"
"testing"

"github.com/pkg/errors"

"github.com/helm/chart-testing/pkg/util"

"github.com/helm/chart-testing/pkg/config"
"github.com/helm/chart-testing/pkg/util"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

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

type fakeLinter struct{}
type fakeLinter struct {
mock.Mock
}

func (l fakeLinter) YamlLint(yamlFile, configFile string) error { return nil }
func (l fakeLinter) Yamale(yamlFile, schemaFile string) error { return nil }
func (l *fakeLinter) YamlLint(yamlFile, configFile string) error {
l.Called(yamlFile, configFile)
return nil
}
func (l *fakeLinter) Yamale(yamlFile, schemaFile string) error {
l.Called(yamlFile, schemaFile)
return nil
}

type fakeHelm struct{}

Expand All @@ -120,13 +127,16 @@ func init() {
ExcludedCharts: []string{"excluded"},
ChartDirs: []string{"stable", "incubator"},
}

fakeMockLinter := new(fakeLinter)

ct = Testing{
config: cfg,
directoryLister: fakeDirLister{},
git: fakeGit{},
chartUtils: fakeChartUtils{},
accountValidator: fakeAccountValidator{},
linter: fakeLinter{},
linter: fakeMockLinter,
helm: fakeHelm{},
}
}
Expand Down Expand Up @@ -200,3 +210,90 @@ func TestLintChartMaintainerValidation(t *testing.T) {
runTests(true)
runTests(false)
}

func TestLintChartSchemaValidation(t *testing.T) {
type testData struct {
name string
chartDir string
expected bool
}

runTests := func(validate bool, callsYamlLint int, callsYamale int) {
fakeMockLinter := new(fakeLinter)

fakeMockLinter.On("Yamale", mock.Anything, mock.Anything).Return(true)
fakeMockLinter.On("YamlLint", mock.Anything, mock.Anything).Return(true)

ct.linter = fakeMockLinter
ct.config.ValidateChartSchema = validate
ct.config.ValidateMaintainers = false
ct.config.ValidateYaml = false

var suffix string
if validate {
suffix = "with-validation"
} else {
suffix = "without-validation"
}

testCases := []testData{
{fmt.Sprintf("schema-%s", suffix), "testdata/test_lints", true},
}

for _, testData := range testCases {
t.Run(testData.name, func(t *testing.T) {
result := ct.LintChart(testData.chartDir, []string{})
assert.Equal(t, testData.expected, result.Error == nil)
fakeMockLinter.AssertNumberOfCalls(t, "Yamale", callsYamale)
fakeMockLinter.AssertNumberOfCalls(t, "YamlLint", callsYamlLint)
})
}
}

runTests(true, 0, 1)
runTests(false, 0, 0)

}

func TestLintYamlValidation(t *testing.T) {
type testData struct {
name string
chartDir string
expected bool
}

runTests := func(validate bool, callsYamlLint int, callsYamale int) {
fakeMockLinter := new(fakeLinter)

fakeMockLinter.On("Yamale", mock.Anything, mock.Anything).Return(true)
fakeMockLinter.On("YamlLint", mock.Anything, mock.Anything).Return(true)

ct.linter = fakeMockLinter
ct.config.ValidateYaml = validate
ct.config.ValidateChartSchema = false
ct.config.ValidateMaintainers = false

var suffix string
if validate {
suffix = "with-validation"
} else {
suffix = "without-validation"
}

testCases := []testData{
{fmt.Sprintf("lint-%s", suffix), "testdata/test_lints", true},
}

for _, testData := range testCases {
t.Run(testData.name, func(t *testing.T) {
result := ct.LintChart(testData.chartDir, []string{})
assert.Equal(t, testData.expected, result.Error == nil)
fakeMockLinter.AssertNumberOfCalls(t, "Yamale", callsYamale)
fakeMockLinter.AssertNumberOfCalls(t, "YamlLint", callsYamlLint)
})
}
}

runTests(true, 2, 0)
runTests(false, 0, 0)
}
10 changes: 10 additions & 0 deletions pkg/chart/testdata/test_lints/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
appVersion: xoxo
description: A Helm chart for testing
name: invalid
version: 1.2.3
home: https://github.com/helm/chart-testing
maintainers:
- name: invalid
- email: [email protected]

3 changes: 3 additions & 0 deletions pkg/chart/testdata/test_lints/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test: abc
list:
- abc
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type Configuration struct {
LintConf string `mapstructure:"lint-conf"`
ChartYamlSchema string `mapstructure:"chart-yaml-schema"`
ValidateMaintainers bool `mapstructure:"validate-maintainers"`
ValidateChartSchema bool `mapstructure:"validate-chart-schema"`
ValidateYaml bool `mapstructure:"validate-yaml"`
CheckVersionIncrement bool `mapstructure:"check-version-increment"`
ProcessAllCharts bool `mapstructure:"all"`
Charts []string `mapstructure:"charts"`
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func loadAndAssertConfigFromFile(t *testing.T, configFile string) {
require.Equal(t, "my-lint-conf.yaml", cfg.LintConf)
require.Equal(t, "my-chart-yaml-schema.yaml", cfg.ChartYamlSchema)
require.Equal(t, true, cfg.ValidateMaintainers)
require.Equal(t, true, cfg.ValidateChartSchema)
require.Equal(t, true, cfg.ValidateYaml)
require.Equal(t, true, cfg.CheckVersionIncrement)
require.Equal(t, false, cfg.ProcessAllCharts)
require.Equal(t, []string{"incubator=https://incubator"}, cfg.ChartRepos)
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/test_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"chart-yaml-schema": "my-chart-yaml-schema.yaml",
"github-instance": "https://github.com",
"validate-maintainers": true,
"validate-chart-schema": true,
"validate-yaml": true,
"check-version-increment": true,
"all": false,
"chart-repos": [
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/test_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ lint-conf: my-lint-conf.yaml
chart-yaml-schema: my-chart-yaml-schema.yaml
github-instance: https://github.com
validate-maintainers: true
validate-chart-schema: true
validate-yaml: true
check-version-increment: true
all: false
chart-repos:
Expand Down