Add chart name check to lint (#3773)

Signed-off-by: Axel Köhler <koehler.ax3l@gmail.com>
pull/5154/head
Axel Köhler 6 years ago committed by Matthew Fisher
parent 8015fc3570
commit 893c3b61f6

1
.gitignore vendored

@ -11,3 +11,4 @@ rootfs/rudder
vendor/
*.exe
.idea/
*.iml

@ -46,7 +46,8 @@ func Chartfile(linter *support.Linter) {
return
}
linter.RunLinterRule(support.ErrorSev, chartFileName, validateChartName(chartFile))
linter.RunLinterRule(support.ErrorSev, chartFileName, validateChartNamePresence(chartFile))
linter.RunLinterRule(support.WarningSev, chartFileName, validateChartNameFormat(chartFile))
linter.RunLinterRule(support.ErrorSev, chartFileName, validateChartNameDirMatch(linter.ChartDir, chartFile))
// Chart metadata
@ -74,13 +75,20 @@ func validateChartYamlFormat(chartFileError error) error {
return nil
}
func validateChartName(cf *chart.Metadata) error {
func validateChartNamePresence(cf *chart.Metadata) error {
if cf.Name == "" {
return errors.New("name is required")
}
return nil
}
func validateChartNameFormat(cf *chart.Metadata) error {
if strings.Contains(cf.Name, ".") {
return errors.New("name should be lower case letters and numbers. Words may be separated with dashes")
}
return nil
}
func validateChartNameDirMatch(chartDir string, cf *chart.Metadata) error {
if cf.Name != filepath.Base(chartDir) {
return fmt.Errorf("directory name (%s) and chart name (%s) must be the same", filepath.Base(chartDir), cf.Name)

@ -29,17 +29,20 @@ import (
)
const (
badChartDir = "testdata/badchartfile"
goodChartDir = "testdata/goodone"
badChartDir = "testdata/badchartfile"
badNameChartDir = "testdata/badnamechart"
goodChartDir = "testdata/goodone"
)
var (
badChartFilePath = filepath.Join(badChartDir, "Chart.yaml")
badNameChartFilePath = filepath.Join(badNameChartDir, "Chart.yaml")
goodChartFilePath = filepath.Join(goodChartDir, "Chart.yaml")
nonExistingChartFilePath = filepath.Join(os.TempDir(), "Chart.yaml")
)
var badChart, chatLoadRrr = chartutil.LoadChartfile(badChartFilePath)
var badNameChart, _ = chartutil.LoadChartfile(badNameChartFilePath)
var goodChart, _ = chartutil.LoadChartfile(goodChartFilePath)
// Validation functions Test
@ -66,12 +69,19 @@ func TestValidateChartYamlFormat(t *testing.T) {
}
func TestValidateChartName(t *testing.T) {
err := validateChartName(badChart)
err := validateChartNamePresence(badChart)
if err == nil {
t.Errorf("validateChartName to return a linter error, got no error")
}
}
func TestValidateChartNameFormat(t *testing.T) {
err := validateChartNameFormat(badNameChart)
if err == nil {
t.Errorf("validateChartNameFormat to return a linter error, got no error")
}
}
func TestValidateChartNameDirMatch(t *testing.T) {
err := validateChartNameDirMatch(goodChartDir, goodChart)
if err != nil {

@ -0,0 +1,4 @@
name: bad.chart.name
description: A Helm chart for Kubernetes
version: 0.1.0
icon: http://riverrun.io

@ -0,0 +1 @@
# Default values for badchartname.
Loading…
Cancel
Save