Don't assume that the returned error from the storage driver isn't nil (#3625) (#4627)

Signed-off-by: Colin Panisset <colin.panisset@cevo.com.au>
pull/4707/merge
Colin Panisset 6 years ago committed by Matthew Fisher
parent 8be42bae88
commit 12ace315ea

@ -202,16 +202,29 @@ func (s *ReleaseServer) uniqName(start string, reuse bool) (string, error) {
return "", fmt.Errorf("a release named %s already exists.\nRun: helm ls --all %s; to check the status of the release\nOr run: helm del --purge %s; to delete it", start, start, start) return "", fmt.Errorf("a release named %s already exists.\nRun: helm ls --all %s; to check the status of the release\nOr run: helm del --purge %s; to delete it", start, start, start)
} }
moniker := moniker.New()
newname, err := s.createUniqName(moniker)
if err != nil {
return "ERROR", err
}
s.Log("info: Created new release name %s", newname)
return newname, nil
}
func (s *ReleaseServer) createUniqName(m moniker.Namer) (string, error) {
maxTries := 5 maxTries := 5
for i := 0; i < maxTries; i++ { for i := 0; i < maxTries; i++ {
namer := moniker.New() name := m.NameSep("-")
name := namer.NameSep("-")
if len(name) > releaseNameMaxLen { if len(name) > releaseNameMaxLen {
name = name[:releaseNameMaxLen] name = name[:releaseNameMaxLen]
} }
if _, err := s.env.Releases.Get(name, 1); strings.Contains(err.Error(), "not found") { if _, err := s.env.Releases.Get(name, 1); err != nil {
if strings.Contains(err.Error(), "not found") {
return name, nil return name, nil
} }
}
s.Log("info: generated name %s is taken. Searching again.", name) s.Log("info: generated name %s is taken. Searching again.", name)
} }
s.Log("warning: No available release names found after %d tries", maxTries) s.Log("warning: No available release names found after %d tries", maxTries)

@ -28,6 +28,7 @@ import (
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
"github.com/golang/protobuf/ptypes/timestamp" "github.com/golang/protobuf/ptypes/timestamp"
"github.com/technosophos/moniker"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
"k8s.io/kubernetes/pkg/apis/core" "k8s.io/kubernetes/pkg/apis/core"
@ -380,6 +381,62 @@ func TestUniqName(t *testing.T) {
} }
} }
type fakeNamer struct {
name string
}
func NewFakeNamer(nam string) moniker.Namer {
return &fakeNamer{
name: nam,
}
}
func (f *fakeNamer) Name() string {
return f.NameSep(" ")
}
func (f *fakeNamer) NameSep(sep string) string {
return f.name
}
func TestCreateUniqueName(t *testing.T) {
rs := rsFixture()
rel1 := releaseStub()
rel1.Name = "happy-panda"
rs.env.Releases.Create(rel1)
tests := []struct {
name string
expect string
err bool
}{
{"happy-panda", "ERROR", true},
{"wobbly-octopus", "[a-z]+-[a-z]+", false},
}
for _, tt := range tests {
m := NewFakeNamer(tt.name)
u, err := rs.createUniqName(m)
if err != nil {
if tt.err {
continue
}
t.Fatal(err)
}
if tt.err {
t.Errorf("Expected an error for %q", tt.name)
}
if match, err := regexp.MatchString(tt.expect, u); err != nil {
t.Fatal(err)
} else if !match {
t.Errorf("Expected %q to match %q", u, tt.expect)
}
}
}
func releaseWithKeepStub(rlsName string) *release.Release { func releaseWithKeepStub(rlsName string) *release.Release {
ch := &chart.Chart{ ch := &chart.Chart{
Metadata: &chart.Metadata{ Metadata: &chart.Metadata{

Loading…
Cancel
Save