From 161960e55eba2044a4e578cb36cf344d3c1e90cd Mon Sep 17 00:00:00 2001 From: Erin Call Date: Thu, 19 Dec 2019 15:02:49 -0800 Subject: [PATCH] Rename Delete to Uninstall [#4] Helm3 renamed its delete command to uninstall. We should still accept helm_command=delete for drone-helm compatibility, but the internals should use Helm's preferred name. --- internal/helm/plan.go | 10 ++-- internal/helm/plan_test.go | 23 +++++--- internal/run/delete.go | 51 ------------------ internal/run/uninstall.go | 51 ++++++++++++++++++ .../run/{delete_test.go => uninstall_test.go} | 52 +++++++++---------- 5 files changed, 98 insertions(+), 89 deletions(-) delete mode 100644 internal/run/delete.go create mode 100644 internal/run/uninstall.go rename internal/run/{delete_test.go => uninstall_test.go} (65%) diff --git a/internal/helm/plan.go b/internal/helm/plan.go index e765bbf..1d4ced9 100644 --- a/internal/helm/plan.go +++ b/internal/helm/plan.go @@ -59,8 +59,8 @@ func determineSteps(cfg Config) *func(Config) []Step { switch cfg.Command { case "upgrade": return &upgrade - case "delete": - return &del + case "uninstall", "delete": + return &uninstall case "lint": return &lint case "help": @@ -70,7 +70,7 @@ func determineSteps(cfg Config) *func(Config) []Step { case "push", "tag", "deployment", "pull_request", "promote", "rollback": return &upgrade case "delete": - return &del + return &uninstall default: panic("not implemented") } @@ -109,9 +109,9 @@ var upgrade = func(cfg Config) []Step { return steps } -var del = func(cfg Config) []Step { +var uninstall = func(cfg Config) []Step { steps := initKube(cfg) - steps = append(steps, &run.Delete{ + steps = append(steps, &run.Uninstall{ Release: cfg.Release, DryRun: cfg.DryRun, }) diff --git a/internal/helm/plan_test.go b/internal/helm/plan_test.go index 02ca79e..e5a1a96 100644 --- a/internal/helm/plan_test.go +++ b/internal/helm/plan_test.go @@ -130,8 +130,8 @@ func (suite *PlanTestSuite) TestDel() { Release: "jetta_id_love_to_change_the_world", } - steps := del(cfg) - suite.Require().Equal(2, len(steps), "del should return 2 steps") + steps := uninstall(cfg) + suite.Require().Equal(2, len(steps), "uninstall should return 2 steps") suite.Require().IsType(&run.InitKube{}, steps[0]) init, _ := steps[0].(*run.InitKube) @@ -146,9 +146,9 @@ func (suite *PlanTestSuite) TestDel() { suite.Equal(expected, init) - suite.Require().IsType(&run.Delete{}, steps[1]) - actual, _ := steps[1].(*run.Delete) - expected = &run.Delete{ + suite.Require().IsType(&run.Uninstall{}, steps[1]) + actual, _ := steps[1].(*run.Uninstall) + expected = &run.Uninstall{ Release: "jetta_id_love_to_change_the_world", DryRun: true, } @@ -213,12 +213,21 @@ func (suite *PlanTestSuite) TestDeterminePlanUpgradeFromDroneEvent() { } } +func (suite *PlanTestSuite) TestDeterminePlanUninstallCommand() { + cfg := Config{ + Command: "uninstall", + } + stepsMaker := determineSteps(cfg) + suite.Same(&uninstall, stepsMaker) +} + +// helm_command = delete is provided as an alias for backwards-compatibility with drone-helm func (suite *PlanTestSuite) TestDeterminePlanDeleteCommand() { cfg := Config{ Command: "delete", } stepsMaker := determineSteps(cfg) - suite.Same(&del, stepsMaker) + suite.Same(&uninstall, stepsMaker) } func (suite *PlanTestSuite) TestDeterminePlanDeleteFromDroneEvent() { @@ -226,7 +235,7 @@ func (suite *PlanTestSuite) TestDeterminePlanDeleteFromDroneEvent() { DroneEvent: "delete", } stepsMaker := determineSteps(cfg) - suite.Same(&del, stepsMaker) + suite.Same(&uninstall, stepsMaker) } func (suite *PlanTestSuite) TestDeterminePlanLintCommand() { diff --git a/internal/run/delete.go b/internal/run/delete.go deleted file mode 100644 index d11c2a6..0000000 --- a/internal/run/delete.go +++ /dev/null @@ -1,51 +0,0 @@ -package run - -import ( - "fmt" -) - -// Delete is an execution step that calls `helm upgrade` when executed. -type Delete struct { - Release string - DryRun bool - cmd cmd -} - -// Execute executes the `helm upgrade` command. -func (d *Delete) Execute(_ Config) error { - return d.cmd.Run() -} - -// Prepare gets the Delete ready to execute. -func (d *Delete) Prepare(cfg Config) error { - if d.Release == "" { - return fmt.Errorf("release is required") - } - - args := []string{"--kubeconfig", cfg.KubeConfig} - - if cfg.Namespace != "" { - args = append(args, "--namespace", cfg.Namespace) - } - if cfg.Debug { - args = append(args, "--debug") - } - - args = append(args, "delete") - - if d.DryRun { - args = append(args, "--dry-run") - } - - args = append(args, d.Release) - - d.cmd = command(helmBin, args...) - d.cmd.Stdout(cfg.Stdout) - d.cmd.Stderr(cfg.Stderr) - - if cfg.Debug { - fmt.Fprintf(cfg.Stderr, "Generated command: '%s'\n", d.cmd.String()) - } - - return nil -} diff --git a/internal/run/uninstall.go b/internal/run/uninstall.go new file mode 100644 index 0000000..1b9d0b2 --- /dev/null +++ b/internal/run/uninstall.go @@ -0,0 +1,51 @@ +package run + +import ( + "fmt" +) + +// Uninstall is an execution step that calls `helm uninstall` when executed. +type Uninstall struct { + Release string + DryRun bool + cmd cmd +} + +// Execute executes the `helm uninstall` command. +func (u *Uninstall) Execute(_ Config) error { + return u.cmd.Run() +} + +// Prepare gets the Uninstall ready to execute. +func (u *Uninstall) Prepare(cfg Config) error { + if u.Release == "" { + return fmt.Errorf("release is required") + } + + args := []string{"--kubeconfig", cfg.KubeConfig} + + if cfg.Namespace != "" { + args = append(args, "--namespace", cfg.Namespace) + } + if cfg.Debug { + args = append(args, "--debug") + } + + args = append(args, "uninstall") + + if u.DryRun { + args = append(args, "--dry-run") + } + + args = append(args, u.Release) + + u.cmd = command(helmBin, args...) + u.cmd.Stdout(cfg.Stdout) + u.cmd.Stderr(cfg.Stderr) + + if cfg.Debug { + fmt.Fprintf(cfg.Stderr, "Generated command: '%s'\n", u.cmd.String()) + } + + return nil +} diff --git a/internal/run/delete_test.go b/internal/run/uninstall_test.go similarity index 65% rename from internal/run/delete_test.go rename to internal/run/uninstall_test.go index f9e547d..9e826c9 100644 --- a/internal/run/delete_test.go +++ b/internal/run/uninstall_test.go @@ -8,7 +8,7 @@ import ( "testing" ) -type DeleteTestSuite struct { +type UninstallTestSuite struct { suite.Suite ctrl *gomock.Controller mockCmd *Mockcmd @@ -16,7 +16,7 @@ type DeleteTestSuite struct { originalCommand func(string, ...string) cmd } -func (suite *DeleteTestSuite) BeforeTest(_, _ string) { +func (suite *UninstallTestSuite) BeforeTest(_, _ string) { suite.ctrl = gomock.NewController(suite.T()) suite.mockCmd = NewMockcmd(suite.ctrl) @@ -27,18 +27,18 @@ func (suite *DeleteTestSuite) BeforeTest(_, _ string) { } } -func (suite *DeleteTestSuite) AfterTest(_, _ string) { +func (suite *UninstallTestSuite) AfterTest(_, _ string) { command = suite.originalCommand } -func TestDeleteTestSuite(t *testing.T) { - suite.Run(t, new(DeleteTestSuite)) +func TestUninstallTestSuite(t *testing.T) { + suite.Run(t, new(UninstallTestSuite)) } -func (suite *DeleteTestSuite) TestPrepareAndExecute() { +func (suite *UninstallTestSuite) TestPrepareAndExecute() { defer suite.ctrl.Finish() - d := Delete{ + u := Uninstall{ Release: "zayde_wølf_king", } @@ -61,15 +61,15 @@ func (suite *DeleteTestSuite) TestPrepareAndExecute() { cfg := Config{ KubeConfig: "/root/.kube/config", } - suite.NoError(d.Prepare(cfg)) - expected := []string{"--kubeconfig", "/root/.kube/config", "delete", "zayde_wølf_king"} + suite.NoError(u.Prepare(cfg)) + expected := []string{"--kubeconfig", "/root/.kube/config", "uninstall", "zayde_wølf_king"} suite.Equal(expected, actual) - d.Execute(cfg) + u.Execute(cfg) } -func (suite *DeleteTestSuite) TestPrepareDryRunFlag() { - d := Delete{ +func (suite *UninstallTestSuite) TestPrepareDryRunFlag() { + u := Uninstall{ Release: "firefox_ak_wildfire", DryRun: true, } @@ -80,13 +80,13 @@ func (suite *DeleteTestSuite) TestPrepareDryRunFlag() { suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes() suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes() - suite.NoError(d.Prepare(cfg)) - expected := []string{"--kubeconfig", "/root/.kube/config", "delete", "--dry-run", "firefox_ak_wildfire"} + suite.NoError(u.Prepare(cfg)) + expected := []string{"--kubeconfig", "/root/.kube/config", "uninstall", "--dry-run", "firefox_ak_wildfire"} suite.Equal(expected, suite.actualArgs) } -func (suite *DeleteTestSuite) TestPrepareNamespaceFlag() { - d := Delete{ +func (suite *UninstallTestSuite) TestPrepareNamespaceFlag() { + u := Uninstall{ Release: "carly_simon_run_away_with_me", } cfg := Config{ @@ -97,14 +97,14 @@ func (suite *DeleteTestSuite) TestPrepareNamespaceFlag() { suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes() suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes() - suite.NoError(d.Prepare(cfg)) + suite.NoError(u.Prepare(cfg)) expected := []string{"--kubeconfig", "/root/.kube/config", - "--namespace", "emotion", "delete", "carly_simon_run_away_with_me"} + "--namespace", "emotion", "uninstall", "carly_simon_run_away_with_me"} suite.Equal(expected, suite.actualArgs) } -func (suite *DeleteTestSuite) TestPrepareDebugFlag() { - d := Delete{ +func (suite *UninstallTestSuite) TestPrepareDebugFlag() { + u := Uninstall{ Release: "just_a_band_huff_and_puff", } stderr := strings.Builder{} @@ -125,17 +125,17 @@ func (suite *DeleteTestSuite) TestPrepareDebugFlag() { suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes() suite.mockCmd.EXPECT().Stderr(&stderr).AnyTimes() - suite.NoError(d.Prepare(cfg)) + suite.NoError(u.Prepare(cfg)) suite.Equal(fmt.Sprintf("Generated command: '%s --kubeconfig /root/.kube/config "+ - "--debug delete just_a_band_huff_and_puff'\n", helmBin), stderr.String()) + "--debug uninstall just_a_band_huff_and_puff'\n", helmBin), stderr.String()) } -func (suite *DeleteTestSuite) TestPrepareRequiresRelease() { +func (suite *UninstallTestSuite) TestPrepareRequiresRelease() { // These aren't really expected, but allowing them gives clearer test-failure messages suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes() suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes() - d := Delete{} - err := d.Prepare(Config{}) - suite.EqualError(err, "release is required", "Delete.Release should be mandatory") + u := Uninstall{} + err := u.Prepare(Config{}) + suite.EqualError(err, "release is required", "Uninstall.Release should be mandatory") }