From c84fb11a68b00cb24f75bd4c16f536b93c484172 Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Wed, 3 May 2017 16:20:28 -0700 Subject: [PATCH] Errors out in case requested plugin exists Partially fixes issues/2385 - helm install silently updates the plugin, if it pre-existed --- pkg/plugin/installer/installer.go | 4 ++++ pkg/plugin/installer/local_installer_test.go | 4 ++-- pkg/plugin/installer/vcs_installer_test.go | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/pkg/plugin/installer/installer.go b/pkg/plugin/installer/installer.go index 31ef9ae53..8da6e24e5 100644 --- a/pkg/plugin/installer/installer.go +++ b/pkg/plugin/installer/installer.go @@ -40,6 +40,10 @@ type Installer interface { // Install installs a plugin to $HELM_HOME. func Install(i Installer) error { + if _, pathErr := os.Stat(i.Path()); !os.IsNotExist(pathErr) { + return errors.New("plugin already exists") + } + return i.Install() } diff --git a/pkg/plugin/installer/local_installer_test.go b/pkg/plugin/installer/local_installer_test.go index a2a1b541c..6a7c957d6 100644 --- a/pkg/plugin/installer/local_installer_test.go +++ b/pkg/plugin/installer/local_installer_test.go @@ -31,7 +31,7 @@ func TestLocalInstaller(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Remove(hh) + defer os.RemoveAll(hh) home := helmpath.Home(hh) if err := os.MkdirAll(home.Plugins(), 0755); err != nil { @@ -43,7 +43,7 @@ func TestLocalInstaller(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Remove(tdir) + defer os.RemoveAll(tdir) if err := ioutil.WriteFile(filepath.Join(tdir, "plugin.yaml"), []byte{}, 0644); err != nil { t.Fatal(err) } diff --git a/pkg/plugin/installer/vcs_installer_test.go b/pkg/plugin/installer/vcs_installer_test.go index 53da07ad4..31d9278bb 100644 --- a/pkg/plugin/installer/vcs_installer_test.go +++ b/pkg/plugin/installer/vcs_installer_test.go @@ -18,6 +18,7 @@ package installer // import "k8s.io/helm/pkg/plugin/installer" import ( "io/ioutil" "os" + "path/filepath" "testing" "k8s.io/helm/pkg/helm/helmpath" @@ -53,7 +54,7 @@ func TestVCSInstaller(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Remove(hh) + defer os.RemoveAll(hh) home := helmpath.Home(hh) if err := os.MkdirAll(home.Plugins(), 0755); err != nil { @@ -61,8 +62,9 @@ func TestVCSInstaller(t *testing.T) { } source := "https://github.com/adamreese/helm-env" + testRepoPath, _ := filepath.Abs("../testdata/plugdir/echo") repo := &testRepo{ - local: "../testdata/plugdir/echo", + local: testRepoPath, tags: []string{"0.1.0", "0.1.1"}, } @@ -89,6 +91,13 @@ func TestVCSInstaller(t *testing.T) { if i.Path() != home.Path("plugins", "helm-env") { t.Errorf("expected path '$HELM_HOME/plugins/helm-env', got %q", i.Path()) } + + // Install again to test plugin exists error + if err := Install(i); err == nil { + t.Error("expected error for plugin exists, got none") + } else if err.Error() != "plugin already exists" { + t.Errorf("expected error for plugin exists, got (%v)", err) + } } func TestVCSInstallerNonExistentVersion(t *testing.T) { @@ -96,7 +105,7 @@ func TestVCSInstallerNonExistentVersion(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.Remove(hh) + defer os.RemoveAll(hh) home := helmpath.Home(hh) if err := os.MkdirAll(home.Plugins(), 0755); err != nil {