Commit 893c3b61 authored by Axel Köhler's avatar Axel Köhler Committed by Matthew Fisher

Add chart name check to lint (#3773)

Signed-off-by: 's avatarAxel Köhler <koehler.ax3l@gmail.com>
parent 8015fc35
...@@ -11,3 +11,4 @@ rootfs/rudder ...@@ -11,3 +11,4 @@ rootfs/rudder
vendor/ vendor/
*.exe *.exe
.idea/ .idea/
*.iml
\ No newline at end of file
...@@ -46,7 +46,8 @@ func Chartfile(linter *support.Linter) { ...@@ -46,7 +46,8 @@ func Chartfile(linter *support.Linter) {
return 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)) linter.RunLinterRule(support.ErrorSev, chartFileName, validateChartNameDirMatch(linter.ChartDir, chartFile))
// Chart metadata // Chart metadata
...@@ -74,13 +75,20 @@ func validateChartYamlFormat(chartFileError error) error { ...@@ -74,13 +75,20 @@ func validateChartYamlFormat(chartFileError error) error {
return nil return nil
} }
func validateChartName(cf *chart.Metadata) error { func validateChartNamePresence(cf *chart.Metadata) error {
if cf.Name == "" { if cf.Name == "" {
return errors.New("name is required") return errors.New("name is required")
} }
return nil 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 { func validateChartNameDirMatch(chartDir string, cf *chart.Metadata) error {
if cf.Name != filepath.Base(chartDir) { 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) return fmt.Errorf("directory name (%s) and chart name (%s) must be the same", filepath.Base(chartDir), cf.Name)
......
...@@ -30,16 +30,19 @@ import ( ...@@ -30,16 +30,19 @@ import (
const ( const (
badChartDir = "testdata/badchartfile" badChartDir = "testdata/badchartfile"
badNameChartDir = "testdata/badnamechart"
goodChartDir = "testdata/goodone" goodChartDir = "testdata/goodone"
) )
var ( var (
badChartFilePath = filepath.Join(badChartDir, "Chart.yaml") badChartFilePath = filepath.Join(badChartDir, "Chart.yaml")
badNameChartFilePath = filepath.Join(badNameChartDir, "Chart.yaml")
goodChartFilePath = filepath.Join(goodChartDir, "Chart.yaml") goodChartFilePath = filepath.Join(goodChartDir, "Chart.yaml")
nonExistingChartFilePath = filepath.Join(os.TempDir(), "Chart.yaml") nonExistingChartFilePath = filepath.Join(os.TempDir(), "Chart.yaml")
) )
var badChart, chatLoadRrr = chartutil.LoadChartfile(badChartFilePath) var badChart, chatLoadRrr = chartutil.LoadChartfile(badChartFilePath)
var badNameChart, _ = chartutil.LoadChartfile(badNameChartFilePath)
var goodChart, _ = chartutil.LoadChartfile(goodChartFilePath) var goodChart, _ = chartutil.LoadChartfile(goodChartFilePath)
// Validation functions Test // Validation functions Test
...@@ -66,12 +69,19 @@ func TestValidateChartYamlFormat(t *testing.T) { ...@@ -66,12 +69,19 @@ func TestValidateChartYamlFormat(t *testing.T) {
} }
func TestValidateChartName(t *testing.T) { func TestValidateChartName(t *testing.T) {
err := validateChartName(badChart) err := validateChartNamePresence(badChart)
if err == nil { if err == nil {
t.Errorf("validateChartName to return a linter error, got no error") 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) { func TestValidateChartNameDirMatch(t *testing.T) {
err := validateChartNameDirMatch(goodChartDir, goodChart) err := validateChartNameDirMatch(goodChartDir, goodChart)
if err != nil { if err != nil {
......
name: bad.chart.name
description: A Helm chart for Kubernetes
version: 0.1.0
icon: http://riverrun.io
# Default values for badchartname.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment