From 908f8316ed5e8cd5a042f72aeae506bf8cf8a365 Mon Sep 17 00:00:00 2001 From: zwwhdls Date: Wed, 11 Dec 2019 15:28:39 +0800 Subject: [PATCH] add testcase Signed-off-by: zwwhdls --- cmd/helm/fetch_test.go | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/cmd/helm/fetch_test.go b/cmd/helm/fetch_test.go index d6ffc17a1..f92a4f827 100644 --- a/cmd/helm/fetch_test.go +++ b/cmd/helm/fetch_test.go @@ -22,6 +22,7 @@ import ( "os" "path/filepath" "regexp" + "strings" "testing" "k8s.io/helm/pkg/repo/repotest" @@ -48,6 +49,9 @@ func TestFetchCmd(t *testing.T) { chart string flags []string fail bool + wantErrorMsg string + existFile string + existDir string failExpect string expectFile string expectDir bool @@ -98,11 +102,25 @@ func TestFetchCmd(t *testing.T) { expectFile: "./signtest", expectDir: true, }, + { + name: "Fetch untar when file with same name existed", + flags: []string{"test/test1 --untar --untardir test1"}, + existFile: "test1", + fail: true, + wantErrorMsg: "failed to untar", + }, + { + name: "Fetch untar when dir with same name existed", + flags: []string{"test/test2 --untar --untardir test2"}, + existDir: "test2", + fail: true, + wantErrorMsg: "failed to untar", + }, { name: "Fetch, verify, untar", chart: "test/signtest", - flags: []string{"--verify", "--keyring", "testdata/helm-test-key.pub", "--untar", "--untardir", "signtest"}, - expectFile: "./signtest", + flags: []string{"--verify", "--keyring", "testdata/helm-test-key.pub", "--untar", "--untardir", "signtest2"}, + expectFile: "./signtest2", expectDir: true, expectVerify: true, failExpect: "Failed to untar signtest", @@ -148,13 +166,31 @@ func TestFetchCmd(t *testing.T) { os.RemoveAll(outdir) os.Mkdir(outdir, 0755) + // Create file or Dir before helm pull --untar, see: https://github.com/helm/helm/issues/7182 + if tt.existFile != "" { + file := filepath.Join(outdir, tt.existFile) + _, err := os.Create(file) + if err != nil { + t.Fatal("err") + } + } + if tt.existDir != "" { + file := filepath.Join(outdir, tt.existDir) + err := os.Mkdir(file, 0755) + if err != nil { + t.Fatal(err) + } + } buf := bytes.NewBuffer(nil) cmd := newFetchCmd(buf) tt.flags = append(tt.flags, "-d", outdir) cmd.ParseFlags(tt.flags) if err := cmd.RunE(cmd, []string{tt.chart}); err != nil { if tt.fail { - continue + if tt.wantErrorMsg != "" && strings.Contains(tt.wantErrorMsg, err.Error()) { + t.Fatalf("%q reported error not equel wantErr, reported: %s, wanted: %s", tt.name, err, tt.wantErrorMsg) + } + return } t.Errorf("%q reported error: %s", tt.name, err) continue