fix: throw warning when chart version is not semverv2

Signed-off-by: Kamil Swiechowski <kamil.swiechowski@iprally.com>
pull/31251/head
Kamil Swiechowski 3 months ago
parent eab6c55646
commit 81d7761f65

@ -9,6 +9,7 @@
[ERROR] Chart.yaml: apiVersion is required. The value must be either "v1" or "v2"
[ERROR] Chart.yaml: version is required
[INFO] Chart.yaml: icon is recommended
[WARNING] Chart.yaml: version '' is not a valid SemVerV2
[ERROR] templates/: validation: chart.metadata.name is required
[ERROR] : unable to load chart
validation: chart.metadata.name is required

@ -39,12 +39,12 @@ const malformedTemplate = "rules/testdata/malformed-template"
func TestBadChart(t *testing.T) {
m := All(badChartDir, values, namespace, strict).Messages
if len(m) != 8 {
if len(m) != 9 {
t.Errorf("Number of errors %v", len(m))
t.Errorf("All didn't fail with expected errors, got %#v", m)
}
// There should be one INFO, and 2 ERROR messages, check for them
var i, e, e2, e3, e4, e5, e6 bool
// There should be one INFO, one WARNING and 2 ERROR messages, check for them
var i, w, e, e2, e3, e4, e5, e6 bool
for _, msg := range m {
if msg.Severity == support.InfoSev {
if strings.Contains(msg.Err.Error(), "icon is recommended") {
@ -75,8 +75,13 @@ func TestBadChart(t *testing.T) {
e6 = true
}
}
if msg.Severity == support.WarningSev {
if strings.Contains(msg.Err.Error(), "version '0.0.0.0' is not a valid SemVerV2") {
w = true
}
}
}
if !e || !e2 || !e3 || !e4 || !e5 || !i || !e6 {
if !e || !e2 || !e3 || !e4 || !e5 || !i || !e6 || !w {
t.Errorf("Didn't find all the expected errors, got %#v", m)
}
}

@ -64,6 +64,7 @@ func Chartfile(linter *support.Linter) {
linter.RunLinterRule(support.ErrorSev, chartFileName, validateChartIconURL(chartFile))
linter.RunLinterRule(support.ErrorSev, chartFileName, validateChartType(chartFile))
linter.RunLinterRule(support.ErrorSev, chartFileName, validateChartDependencies(chartFile))
linter.RunLinterRule(support.WarningSev, chartFileName, validateChartVersionStrictSemVerV2(chartFile))
}
func validateChartVersionType(data map[string]interface{}) error {
@ -149,6 +150,16 @@ func validateChartVersion(cf *chart.Metadata) error {
return nil
}
func validateChartVersionStrictSemVerV2(cf *chart.Metadata) error {
_, err := semver.StrictNewVersion(cf.Version)
if err != nil {
return errors.Errorf("version '%s' is not a valid SemVerV2", cf.Version)
}
return nil
}
func validateChartMaintainer(cf *chart.Metadata) error {
for _, maintainer := range cf.Maintainers {
if maintainer == nil {

@ -109,6 +109,35 @@ func TestValidateChartVersion(t *testing.T) {
}
}
func TestValidateChartVersionStrictSemVerV2(t *testing.T) {
var failTest = []struct {
Version string
ErrorMsg string
}{
{"", "version '' is not a valid SemVerV2"},
{"1", "version '1' is not a valid SemVerV2"},
{"1.1", "version '1.1' is not a valid SemVerV2"},
}
var successTest = []string{"1.1.1", "0.0.1+build", "0.0.1-beta"}
for _, test := range failTest {
badChart.Version = test.Version
err := validateChartVersionStrictSemVerV2(badChart)
if err == nil || !strings.Contains(err.Error(), test.ErrorMsg) {
t.Errorf("validateChartVersionStrictSemVerV2(%s) to return \"%s\", got no error", test.Version, test.ErrorMsg)
}
}
for _, version := range successTest {
badChart.Version = version
err := validateChartVersionStrictSemVerV2(badChart)
if err != nil {
t.Errorf("validateChartVersionStrictSemVerV2(%s) to return no error, got a linter error", version)
}
}
}
func TestValidateChartMaintainer(t *testing.T) {
var failTest = []struct {
Name string
@ -207,7 +236,7 @@ func TestChartfile(t *testing.T) {
linter := support.Linter{ChartDir: badChartDir}
Chartfile(&linter)
msgs := linter.Messages
expectedNumberOfErrorMessages := 6
expectedNumberOfErrorMessages := 7
if len(msgs) != expectedNumberOfErrorMessages {
t.Errorf("Expected %d errors, got %d", expectedNumberOfErrorMessages, len(msgs))
@ -237,13 +266,16 @@ func TestChartfile(t *testing.T) {
if !strings.Contains(msgs[5].Err.Error(), "dependencies are not valid in the Chart file with apiVersion") {
t.Errorf("Unexpected message 5: %s", msgs[5].Err)
}
if !strings.Contains(msgs[6].Err.Error(), "version '0.0.0.0' is not a valid SemVerV2") {
t.Errorf("Unexpected message 6: %s", msgs[6].Err)
}
})
t.Run("Chart.yaml validity issues due to type mismatch", func(t *testing.T) {
linter := support.Linter{ChartDir: anotherBadChartDir}
Chartfile(&linter)
msgs := linter.Messages
expectedNumberOfErrorMessages := 3
expectedNumberOfErrorMessages := 4
if len(msgs) != expectedNumberOfErrorMessages {
t.Errorf("Expected %d errors, got %d", expectedNumberOfErrorMessages, len(msgs))
@ -261,5 +293,8 @@ func TestChartfile(t *testing.T) {
if !strings.Contains(msgs[2].Err.Error(), "appVersion should be of type string") {
t.Errorf("Unexpected message 2: %s", msgs[2].Err)
}
if !strings.Contains(msgs[3].Err.Error(), "version '7.2445e+06' is not a valid SemVerV2") {
t.Errorf("Unexpected message 3: %s", msgs[3].Err)
}
})
}

Loading…
Cancel
Save