2019-12-18 19:47:15 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/golang/mock/gomock"
|
2020-01-16 21:50:04 +00:00
|
|
|
"github.com/pelotech/drone-helm3/internal/env"
|
2019-12-18 19:47:15 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
type UninstallTestSuite struct {
|
2019-12-18 19:47:15 +00:00
|
|
|
suite.Suite
|
|
|
|
ctrl *gomock.Controller
|
|
|
|
mockCmd *Mockcmd
|
|
|
|
actualArgs []string
|
|
|
|
originalCommand func(string, ...string) cmd
|
|
|
|
}
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
func (suite *UninstallTestSuite) BeforeTest(_, _ string) {
|
2019-12-18 19:47:15 +00:00
|
|
|
suite.ctrl = gomock.NewController(suite.T())
|
|
|
|
suite.mockCmd = NewMockcmd(suite.ctrl)
|
|
|
|
|
|
|
|
suite.originalCommand = command
|
|
|
|
command = func(path string, args ...string) cmd {
|
|
|
|
suite.actualArgs = args
|
|
|
|
return suite.mockCmd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
func (suite *UninstallTestSuite) AfterTest(_, _ string) {
|
2019-12-18 19:47:15 +00:00
|
|
|
command = suite.originalCommand
|
|
|
|
}
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
func TestUninstallTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(UninstallTestSuite))
|
2019-12-18 19:47:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 21:50:04 +00:00
|
|
|
func (suite *UninstallTestSuite) TestNewUninstall() {
|
|
|
|
cfg := env.Config{
|
|
|
|
DryRun: true,
|
|
|
|
Release: "jetta_id_love_to_change_the_world",
|
|
|
|
KeepHistory: true,
|
|
|
|
}
|
|
|
|
u := NewUninstall(cfg)
|
2020-01-17 18:13:53 +00:00
|
|
|
suite.Equal("jetta_id_love_to_change_the_world", u.release)
|
|
|
|
suite.Equal(true, u.dryRun)
|
|
|
|
suite.Equal(true, u.keepHistory)
|
|
|
|
suite.NotNil(u.config)
|
2020-01-16 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
func (suite *UninstallTestSuite) TestPrepareAndExecute() {
|
2019-12-18 19:47:15 +00:00
|
|
|
defer suite.ctrl.Finish()
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
cfg := env.Config{
|
2019-12-18 19:47:15 +00:00
|
|
|
Release: "zayde_wølf_king",
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
u := NewUninstall(cfg)
|
2019-12-18 19:47:15 +00:00
|
|
|
|
|
|
|
actual := []string{}
|
|
|
|
command = func(path string, args ...string) cmd {
|
|
|
|
suite.Equal(helmBin, path)
|
|
|
|
actual = args
|
|
|
|
|
|
|
|
return suite.mockCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.mockCmd.EXPECT().
|
|
|
|
Stdout(gomock.Any())
|
|
|
|
suite.mockCmd.EXPECT().
|
|
|
|
Stderr(gomock.Any())
|
|
|
|
suite.mockCmd.EXPECT().
|
|
|
|
Run().
|
|
|
|
Times(1)
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
suite.NoError(u.Prepare())
|
2019-12-23 20:43:17 +00:00
|
|
|
expected := []string{"uninstall", "zayde_wølf_king"}
|
2019-12-18 19:47:15 +00:00
|
|
|
suite.Equal(expected, actual)
|
|
|
|
|
2020-01-16 23:30:21 +00:00
|
|
|
u.Execute()
|
2019-12-18 19:47:15 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
func (suite *UninstallTestSuite) TestPrepareDryRunFlag() {
|
2020-01-17 18:13:53 +00:00
|
|
|
cfg := env.Config{
|
2019-12-18 19:47:15 +00:00
|
|
|
Release: "firefox_ak_wildfire",
|
|
|
|
DryRun: true,
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
u := NewUninstall(cfg)
|
2019-12-18 19:47:15 +00:00
|
|
|
|
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
suite.NoError(u.Prepare())
|
2019-12-23 20:43:17 +00:00
|
|
|
expected := []string{"uninstall", "--dry-run", "firefox_ak_wildfire"}
|
2019-12-18 19:47:15 +00:00
|
|
|
suite.Equal(expected, suite.actualArgs)
|
|
|
|
}
|
|
|
|
|
2020-01-02 18:58:58 +00:00
|
|
|
func (suite *UninstallTestSuite) TestPrepareKeepHistoryFlag() {
|
2020-01-17 18:13:53 +00:00
|
|
|
cfg := env.Config{
|
2020-01-02 18:58:58 +00:00
|
|
|
Release: "perturbator_sentient",
|
|
|
|
KeepHistory: true,
|
|
|
|
}
|
2020-01-17 18:13:53 +00:00
|
|
|
u := NewUninstall(cfg)
|
2020-01-02 18:58:58 +00:00
|
|
|
|
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
suite.NoError(u.Prepare())
|
2020-01-02 18:58:58 +00:00
|
|
|
expected := []string{"uninstall", "--keep-history", "perturbator_sentient"}
|
|
|
|
suite.Equal(expected, suite.actualArgs)
|
|
|
|
}
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
func (suite *UninstallTestSuite) TestPrepareRequiresRelease() {
|
2019-12-18 19:47:15 +00:00
|
|
|
// 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()
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
u := NewUninstall(env.Config{})
|
|
|
|
err := u.Prepare()
|
2019-12-19 23:02:49 +00:00
|
|
|
suite.EqualError(err, "release is required", "Uninstall.Release should be mandatory")
|
2019-12-18 19:47:15 +00:00
|
|
|
}
|