From d020c64afa6c7ead58553e5dcd2fd0cc8817d1cc Mon Sep 17 00:00:00 2001 From: wxdao Date: Thu, 5 Sep 2019 15:33:02 +0000 Subject: [PATCH] Fix Save misbehavior on nonexistent directory Signed-off-by: wxdao --- pkg/chartutil/save.go | 2 +- pkg/chartutil/save_test.go | 67 ++++++++++++++++++++------------------ 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/pkg/chartutil/save.go b/pkg/chartutil/save.go index 83d95cc5a..5317fc3a3 100644 --- a/pkg/chartutil/save.go +++ b/pkg/chartutil/save.go @@ -93,7 +93,7 @@ func Save(c *chart.Chart, outDir string) (string, error) { filename := fmt.Sprintf("%s-%s.tgz", c.Name(), c.Metadata.Version) filename = filepath.Join(outDir, filename) if stat, err := os.Stat(filepath.Dir(filename)); os.IsNotExist(err) { - if err := os.MkdirAll(filepath.Dir(filename), 0755); !os.IsExist(err) { + if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil { return "", err } } else if !stat.IsDir() { diff --git a/pkg/chartutil/save_test.go b/pkg/chartutil/save_test.go index 54e7fef79..65aaf2169 100644 --- a/pkg/chartutil/save_test.go +++ b/pkg/chartutil/save_test.go @@ -19,6 +19,7 @@ package chartutil import ( "io/ioutil" "os" + "path" "path/filepath" "strings" "testing" @@ -34,37 +35,41 @@ func TestSave(t *testing.T) { } defer os.RemoveAll(tmp) - c := &chart.Chart{ - Metadata: &chart.Metadata{ - APIVersion: chart.APIVersionV1, - Name: "ahab", - Version: "1.2.3", - }, - Files: []*chart.File{ - {Name: "scheherazade/shahryar.txt", Data: []byte("1,001 Nights")}, - }, - } - - where, err := Save(c, tmp) - if err != nil { - t.Fatalf("Failed to save: %s", err) - } - if !strings.HasPrefix(where, tmp) { - t.Fatalf("Expected %q to start with %q", where, tmp) - } - if !strings.HasSuffix(where, ".tgz") { - t.Fatalf("Expected %q to end with .tgz", where) - } - - c2, err := loader.LoadFile(where) - if err != nil { - t.Fatal(err) - } - if c2.Name() != c.Name() { - t.Fatalf("Expected chart archive to have %q, got %q", c.Name(), c2.Name()) - } - if len(c2.Files) != 1 || c2.Files[0].Name != "scheherazade/shahryar.txt" { - t.Fatal("Files data did not match") + for _, dest := range []string{tmp, path.Join(tmp, "newdir")} { + t.Run("outDir="+dest, func(t *testing.T) { + c := &chart.Chart{ + Metadata: &chart.Metadata{ + APIVersion: chart.APIVersionV1, + Name: "ahab", + Version: "1.2.3", + }, + Files: []*chart.File{ + {Name: "scheherazade/shahryar.txt", Data: []byte("1,001 Nights")}, + }, + } + + where, err := Save(c, dest) + if err != nil { + t.Fatalf("Failed to save: %s", err) + } + if !strings.HasPrefix(where, dest) { + t.Fatalf("Expected %q to start with %q", where, dest) + } + if !strings.HasSuffix(where, ".tgz") { + t.Fatalf("Expected %q to end with .tgz", where) + } + + c2, err := loader.LoadFile(where) + if err != nil { + t.Fatal(err) + } + if c2.Name() != c.Name() { + t.Fatalf("Expected chart archive to have %q, got %q", c.Name(), c2.Name()) + } + if len(c2.Files) != 1 || c2.Files[0].Name != "scheherazade/shahryar.txt" { + t.Fatal("Files data did not match") + } + }) } }