From a7a11173271e5721078994647caf856489dfd929 Mon Sep 17 00:00:00 2001 From: wujunwei Date: Wed, 15 Jun 2022 22:01:31 +0800 Subject: [PATCH 1/2] add nil judge for dependency , maintainers validate and some testcase. Signed-off-by: wujunwei --- pkg/chart/dependency.go | 3 +++ pkg/chart/metadata.go | 3 +++ pkg/chart/metadata_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/pkg/chart/dependency.go b/pkg/chart/dependency.go index b2819f373..17d7603a3 100644 --- a/pkg/chart/dependency.go +++ b/pkg/chart/dependency.go @@ -53,6 +53,9 @@ type Dependency struct { // the chart. This check must be done at load time before the dependency's charts are // loaded. func (d *Dependency) Validate() error { + if d == nil { + return ValidationError("the dependency's item can not be nil") + } d.Name = sanitizeString(d.Name) d.Version = sanitizeString(d.Version) d.Repository = sanitizeString(d.Repository) diff --git a/pkg/chart/metadata.go b/pkg/chart/metadata.go index 1925e45ac..7249e1ba0 100644 --- a/pkg/chart/metadata.go +++ b/pkg/chart/metadata.go @@ -34,6 +34,9 @@ type Maintainer struct { // Validate checks valid data and sanitizes string characters. func (m *Maintainer) Validate() error { + if m == nil { + return ValidationError("the maintainer's item can not be nil") + } m.Name = sanitizeString(m.Name) m.Email = sanitizeString(m.Email) m.URL = sanitizeString(m.URL) diff --git a/pkg/chart/metadata_test.go b/pkg/chart/metadata_test.go index 9f881a4e1..b89dc9cf2 100644 --- a/pkg/chart/metadata_test.go +++ b/pkg/chart/metadata_test.go @@ -72,6 +72,30 @@ func TestValidate(t *testing.T) { }, ValidationError("dependency \"bad\" has disallowed characters in the alias"), }, + { + &Metadata{ + Name: "test", + APIVersion: "v2", + Version: "1.0", + Type: "application", + Dependencies: []*Dependency{ + nil, + }, + }, + ValidationError("the dependency's item can not be nil"), + }, + { + &Metadata{ + Name: "test", + APIVersion: "v2", + Version: "1.0", + Type: "application", + Maintainers: []*Maintainer{ + nil, + }, + }, + ValidationError("the maintainer's item can not be nil"), + }, { &Metadata{APIVersion: "v2", Name: "test", Version: "1.2.3.4"}, ValidationError("chart.metadata.version \"1.2.3.4\" is invalid"), From 4fcec24d15c616011fb2d7c22c3dd0024bb9e41b Mon Sep 17 00:00:00 2001 From: wujunwei Date: Wed, 24 Aug 2022 13:51:37 +0800 Subject: [PATCH 2/2] update: Optimize the error message Signed-off-by: wujunwei --- pkg/chart/dependency.go | 2 +- pkg/chart/metadata.go | 2 +- pkg/chart/metadata_test.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/chart/dependency.go b/pkg/chart/dependency.go index 17d7603a3..d9d4ee981 100644 --- a/pkg/chart/dependency.go +++ b/pkg/chart/dependency.go @@ -54,7 +54,7 @@ type Dependency struct { // loaded. func (d *Dependency) Validate() error { if d == nil { - return ValidationError("the dependency's item can not be nil") + return ValidationError("dependency cannot be an empty list") } d.Name = sanitizeString(d.Name) d.Version = sanitizeString(d.Version) diff --git a/pkg/chart/metadata.go b/pkg/chart/metadata.go index 7249e1ba0..7d16ecd1b 100644 --- a/pkg/chart/metadata.go +++ b/pkg/chart/metadata.go @@ -35,7 +35,7 @@ type Maintainer struct { // Validate checks valid data and sanitizes string characters. func (m *Maintainer) Validate() error { if m == nil { - return ValidationError("the maintainer's item can not be nil") + return ValidationError("maintainer cannot be an empty list") } m.Name = sanitizeString(m.Name) m.Email = sanitizeString(m.Email) diff --git a/pkg/chart/metadata_test.go b/pkg/chart/metadata_test.go index b89dc9cf2..98354d13f 100644 --- a/pkg/chart/metadata_test.go +++ b/pkg/chart/metadata_test.go @@ -82,7 +82,7 @@ func TestValidate(t *testing.T) { nil, }, }, - ValidationError("the dependency's item can not be nil"), + ValidationError("dependency cannot be an empty list"), }, { &Metadata{ @@ -94,7 +94,7 @@ func TestValidate(t *testing.T) { nil, }, }, - ValidationError("the maintainer's item can not be nil"), + ValidationError("maintainer cannot be an empty list"), }, { &Metadata{APIVersion: "v2", Name: "test", Version: "1.2.3.4"},