From 7a4cfc8692efbb923ea8531e6eb5952c437e1f0a Mon Sep 17 00:00:00 2001 From: Karuppiah Natarajan Date: Tue, 30 Apr 2019 00:02:31 +0530 Subject: [PATCH] Make errors regarding chart.yaml metadata clear Signed-off-by: Karuppiah Natarajan --- .../output/install-chart-bad-type.txt | 2 +- .../testdata/output/template-lib-chart.txt | 2 +- pkg/chart/errors.go | 20 +++-- pkg/chart/loader/load_test.go | 4 +- pkg/chart/metadata.go | 11 +-- pkg/chart/metadata_test.go | 87 +++++++++++++++++++ 6 files changed, 112 insertions(+), 14 deletions(-) create mode 100644 pkg/chart/metadata_test.go diff --git a/cmd/helm/testdata/output/install-chart-bad-type.txt b/cmd/helm/testdata/output/install-chart-bad-type.txt index d8a3bf275..1e18fb72d 100644 --- a/cmd/helm/testdata/output/install-chart-bad-type.txt +++ b/cmd/helm/testdata/output/install-chart-bad-type.txt @@ -1 +1 @@ -Error: validation: chart.metadata.type must be application or library +Error: validation: chart.metadata.type must be 'application' or 'library' in Chart.yaml diff --git a/cmd/helm/testdata/output/template-lib-chart.txt b/cmd/helm/testdata/output/template-lib-chart.txt index d8a3bf275..1e18fb72d 100644 --- a/cmd/helm/testdata/output/template-lib-chart.txt +++ b/cmd/helm/testdata/output/template-lib-chart.txt @@ -1 +1 @@ -Error: validation: chart.metadata.type must be application or library +Error: validation: chart.metadata.type must be 'application' or 'library' in Chart.yaml diff --git a/pkg/chart/errors.go b/pkg/chart/errors.go index 4cb4189e6..e06f6fc50 100644 --- a/pkg/chart/errors.go +++ b/pkg/chart/errors.go @@ -15,9 +15,19 @@ limitations under the License. package chart -// ValidationError represents a data validation error. -type ValidationError string +import "github.com/pkg/errors" -func (v ValidationError) Error() string { - return "validation: " + string(v) -} +// ErrMissingMetadata indicates that Chart.yaml is missing +var ErrMissingMetadata = errors.New("validation: chart.metadata (Chart.yaml) is required") + +// ErrMissingAPIVersion indicates that chart apiVersion is missing in Chart.yaml +var ErrMissingAPIVersion = errors.New("validation: chart.metadata.apiVersion is required in Chart.yaml") + +// ErrMissingName indicates that chart name is missing in Chart.yaml +var ErrMissingName = errors.New("validation: chart.metadata.name is required in Chart.yaml") + +// ErrMissingVersion indicates that chart version is missing in Chart.yaml +var ErrMissingVersion = errors.New("validation: chart.metadata.version is required in Chart.yaml") + +// ErrInvalidType indicates that chart type is invalid in Chart.yaml +var ErrInvalidType = errors.New("validation: chart.metadata.type must be 'application' or 'library' in Chart.yaml") diff --git a/pkg/chart/loader/load_test.go b/pkg/chart/loader/load_test.go index 9e6697c40..1c82b63ff 100644 --- a/pkg/chart/loader/load_test.go +++ b/pkg/chart/loader/load_test.go @@ -130,8 +130,8 @@ icon: https://example.com/64x64.png if _, err = LoadFiles([]*BufferedFile{}); err == nil { t.Fatal("Expected err to be non-nil") } - if err.Error() != "validation: chart.metadata is required" { - t.Errorf("Expected chart metadata missing error, got '%s'", err.Error()) + if err != chart.ErrMissingMetadata { + t.Errorf("Expected '%s', got '%s'", chart.ErrMissingMetadata.Error(), err.Error()) } } diff --git a/pkg/chart/metadata.go b/pkg/chart/metadata.go index 627ee19f3..3eb36fde7 100644 --- a/pkg/chart/metadata.go +++ b/pkg/chart/metadata.go @@ -64,21 +64,22 @@ type Metadata struct { Type string `json:"type,omitempty"` } +// Validate validates the Chart.yaml contents func (md *Metadata) Validate() error { if md == nil { - return ValidationError("chart.metadata is required") + return ErrMissingMetadata } if md.APIVersion == "" { - return ValidationError("chart.metadata.apiVersion is required") + return ErrMissingAPIVersion } if md.Name == "" { - return ValidationError("chart.metadata.name is required") + return ErrMissingName } if md.Version == "" { - return ValidationError("chart.metadata.version is required") + return ErrMissingVersion } if !isValidChartType(md.Type) { - return ValidationError("chart.metadata.type must be application or library") + return ErrInvalidType } // TODO validate valid semver here? return nil diff --git a/pkg/chart/metadata_test.go b/pkg/chart/metadata_test.go new file mode 100644 index 000000000..46d874443 --- /dev/null +++ b/pkg/chart/metadata_test.go @@ -0,0 +1,87 @@ +/* +Copyright The Helm Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package chart + +import ( + "testing" +) + +func TestMetadata_Validate(t *testing.T) { + t.Run("return error when metadata is missing", func(t *testing.T) { + var metadata *Metadata + err := metadata.Validate() + if err == nil { + t.Fatal("Expected err to be non-nil") + } + if err != ErrMissingMetadata { + t.Errorf("Expected '%s', got '%s'", ErrMissingMetadata.Error(), err.Error()) + } + }) + + t.Run("return error when api version is missing", func(t *testing.T) { + metadata := &Metadata{} + err := metadata.Validate() + if err == nil { + t.Fatal("Expected err to be non-nil") + } + if err != ErrMissingAPIVersion { + t.Errorf("Expected '%s', got '%s'", ErrMissingAPIVersion.Error(), err.Error()) + } + }) + + t.Run("return error when name is missing", func(t *testing.T) { + metadata := &Metadata{ + APIVersion: APIVersionV1, + } + err := metadata.Validate() + if err == nil { + t.Fatal("Expected err to be non-nil") + } + if err != ErrMissingName { + t.Errorf("Expected %s, got '%s'", ErrMissingName, err.Error()) + } + }) + + t.Run("return error when version is missing", func(t *testing.T) { + metadata := &Metadata{ + APIVersion: APIVersionV1, + Name: "testChart", + } + err := metadata.Validate() + if err == nil { + t.Fatal("Expected err to be non-nil") + } + if err != ErrMissingVersion { + t.Errorf("Expected %s, got '%s'", ErrMissingVersion, err.Error()) + } + }) + + t.Run("return error when type is invalid", func(t *testing.T) { + metadata := &Metadata{ + APIVersion: APIVersionV1, + Name: "testChart", + Version: "0.1.0", + Type: "dummy", + } + err := metadata.Validate() + if err == nil { + t.Fatal("Expected err to be non-nil") + } + if err != ErrInvalidType { + t.Errorf("Expected '%s', got '%s'", ErrInvalidType.Error(), err.Error()) + } + }) +}