From dd17d620d7e9c18b3faaf39322f946c73b21a807 Mon Sep 17 00:00:00 2001 From: pospispa Date: Mon, 13 Mar 2017 11:54:01 +0100 Subject: [PATCH] Added func ValidateZone The zone parameter provided in a Storage Class may erroneously be an empty string or contain only spaces and tab characters. Such situation shall be detected and reported as an error. That's why the func ValidateZone was added. --- pkg/volume/util.go | 10 ++++++++++ pkg/volume/util_test.go | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/pkg/volume/util.go b/pkg/volume/util.go index 0d0c59c7193..aac4c9e543c 100644 --- a/pkg/volume/util.go +++ b/pkg/volume/util.go @@ -424,3 +424,13 @@ func ZonesToSet(zonesString string) (sets.String, error) { } return zonesSet, nil } + +// ValidateZone returns: +// - an error in case zone is an empty string or contains only any combination of spaces and tab characters +// - nil otherwise +func ValidateZone(zone string) error { + if strings.TrimSpace(zone) == "" { + return fmt.Errorf("the provided %q zone is not valid, it's an empty string or contains only spaces and tab characters", zone) + } + return nil +} diff --git a/pkg/volume/util_test.go b/pkg/volume/util_test.go index 656fbf04f3e..67d504b451d 100644 --- a/pkg/volume/util_test.go +++ b/pkg/volume/util_test.go @@ -557,3 +557,23 @@ func TestZonesToSet(t *testing.T) { } } } + +func TestValidateZone(t *testing.T) { + functionUnderTest := "ValidateZone" + + // First part: want an error + errCases := []string{"", " "} + for _, errCase := range errCases { + if got := ValidateZone(errCase); got == nil { + t.Errorf("%v(%v) returned (%v), want (%v)", functionUnderTest, errCase, got, "an error") + } + } + + // Second part: want no error + succCases := []string{" us-east-1a "} + for _, succCase := range succCases { + if got := ValidateZone(succCase); got != nil { + t.Errorf("%v(%v) returned (%v), want (%v)", functionUnderTest, succCase, got, nil) + } + } +}