update for v3 and update tests

Signed-off-by: Janit Sriganeshaelankovan <janits_27@hotmail.com>
pull/31062/head
Janit Sriganeshaelankovan 12 months ago
parent 51f369725d
commit eaae99f7b2
No known key found for this signature in database
GPG Key ID: 82CD06AA03D8CEF1

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"github.com/Masterminds/semver/v3" "github.com/Masterminds/semver/v3"
"github.com/asaskevich/govalidator" "github.com/asaskevich/govalidator"
@ -31,6 +32,13 @@ import (
chartutil "helm.sh/helm/v4/internal/chart/v3/util" chartutil "helm.sh/helm/v4/internal/chart/v3/util"
) )
// chartName is a regular expression for testing the supplied name of a chart.
var chartName = regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*$`)
// maxChartNameLength is lower than the limits we know of with certain file systems,
// and with certain Kubernetes fields.
const maxChartNameLength = 250
// Chartfile runs a set of linter rules related to Chart.yaml file // Chartfile runs a set of linter rules related to Chart.yaml file
func Chartfile(linter *support.Linter) { func Chartfile(linter *support.Linter) {
chartFileName := "Chart.yaml" chartFileName := "Chart.yaml"
@ -113,12 +121,11 @@ func validateChartYamlStrictFormat(chartFileError error) error {
} }
func validateChartName(cf *chart.Metadata) error { func validateChartName(cf *chart.Metadata) error {
if cf.Name == "" { if cf.Name == "" || len(cf.Name) > maxChartNameLength {
return errors.New("name is required") return fmt.Errorf("chart name must be between 1 and %d characters", maxChartNameLength)
} }
name := filepath.Base(cf.Name) if !chartName.MatchString(cf.Name) {
if name != cf.Name { return fmt.Errorf("chart name must use only lowercase letters or digits optionally with dash separators")
return fmt.Errorf("chart name %q is invalid", cf.Name)
} }
return nil return nil
} }

@ -272,7 +272,7 @@ func TestV3Chartfile(t *testing.T) {
return return
} }
if !strings.Contains(msgs[0].Err.Error(), "name is required") { if !strings.Contains(msgs[0].Err.Error(), "chart name must be between 1 and 250 characters") {
t.Errorf("Unexpected message 0: %s", msgs[0].Err) t.Errorf("Unexpected message 0: %s", msgs[0].Err)
} }

@ -35,7 +35,7 @@ import (
// This regular expression is probably stricter than it needs to be. We can relax it // This regular expression is probably stricter than it needs to be. We can relax it
// somewhat. Newline characters, as well as $, quotes, +, parens, and % are known to be // somewhat. Newline characters, as well as $, quotes, +, parens, and % are known to be
// problematic. // problematic.
var chartName = regexp.MustCompile("^[a-zA-Z0-9._-]+$") var chartName = regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*$`)
const ( const (
// ChartfileName is the default Chart file name. // ChartfileName is the default Chart file name.
@ -828,7 +828,7 @@ func validateChartName(name string) error {
return fmt.Errorf("chart name must be between 1 and %d characters", maxChartNameLength) return fmt.Errorf("chart name must be between 1 and %d characters", maxChartNameLength)
} }
if !chartName.MatchString(name) { if !chartName.MatchString(name) {
return fmt.Errorf("chart name must match the regular expression %q", chartName.String()) return fmt.Errorf("chart name must use only lowercase letters or digits optionally with dash separators")
} }
return nil return nil
} }

@ -147,8 +147,10 @@ func TestCreate_Overwrite(t *testing.T) {
func TestValidateChartName(t *testing.T) { func TestValidateChartName(t *testing.T) {
for name, shouldPass := range map[string]bool{ for name, shouldPass := range map[string]bool{
"": false, "": false,
"abcdefghijklmnopqrstuvwxyz-_.": true, "abcdefghijklmnopqrstuvwxyz-_.": false,
"ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.": true, "abcdefghijklmnopqrstuvwxyz": true,
"abcdefghijklm-nopqrstuvwxyz": true,
"ABCDEFGHIJKLMNOPQRSTUVWXYZ-_.": false,
"$hello": false, "$hello": false,
"Hellô": false, "Hellô": false,
"he%%o": false, "he%%o": false,

@ -299,7 +299,7 @@ func TestChartfile(t *testing.T) {
return return
} }
if !strings.Contains(msgs[0].Err.Error(), "name is required") { if !strings.Contains(msgs[0].Err.Error(), "chart name must be between 1 and 250 characters") {
t.Errorf("Unexpected message 0: %s", msgs[0].Err) t.Errorf("Unexpected message 0: %s", msgs[0].Err)
} }

Loading…
Cancel
Save