2019-12-18 19:47:15 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"strings"
|
|
|
|
"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
|
|
|
}
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
func (suite *UninstallTestSuite) TestPrepareAndExecute() {
|
2019-12-18 19:47:15 +00:00
|
|
|
defer suite.ctrl.Finish()
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
u := Uninstall{
|
2019-12-18 19:47:15 +00:00
|
|
|
Release: "zayde_wølf_king",
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2019-12-23 20:43:17 +00:00
|
|
|
cfg := Config{}
|
2019-12-19 23:02:49 +00:00
|
|
|
suite.NoError(u.Prepare(cfg))
|
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)
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
u.Execute(cfg)
|
2019-12-18 19:47:15 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
func (suite *UninstallTestSuite) TestPrepareDryRunFlag() {
|
|
|
|
u := Uninstall{
|
2019-12-18 19:47:15 +00:00
|
|
|
Release: "firefox_ak_wildfire",
|
|
|
|
DryRun: true,
|
|
|
|
}
|
2019-12-23 20:43:17 +00:00
|
|
|
cfg := Config{}
|
2019-12-18 19:47:15 +00:00
|
|
|
|
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
suite.NoError(u.Prepare(cfg))
|
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() {
|
|
|
|
u := Uninstall{
|
|
|
|
Release: "perturbator_sentient",
|
|
|
|
KeepHistory: true,
|
|
|
|
}
|
|
|
|
cfg := Config{}
|
|
|
|
|
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
|
|
|
|
|
|
|
|
suite.NoError(u.Prepare(cfg))
|
|
|
|
expected := []string{"uninstall", "--keep-history", "perturbator_sentient"}
|
|
|
|
suite.Equal(expected, suite.actualArgs)
|
|
|
|
}
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
func (suite *UninstallTestSuite) TestPrepareNamespaceFlag() {
|
|
|
|
u := Uninstall{
|
2019-12-18 19:47:15 +00:00
|
|
|
Release: "carly_simon_run_away_with_me",
|
|
|
|
}
|
|
|
|
cfg := Config{
|
2019-12-23 20:43:17 +00:00
|
|
|
Namespace: "emotion",
|
2019-12-18 19:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
suite.NoError(u.Prepare(cfg))
|
2019-12-23 20:43:17 +00:00
|
|
|
expected := []string{"--namespace", "emotion", "uninstall", "carly_simon_run_away_with_me"}
|
2019-12-18 19:47:15 +00:00
|
|
|
suite.Equal(expected, suite.actualArgs)
|
|
|
|
}
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
func (suite *UninstallTestSuite) TestPrepareDebugFlag() {
|
|
|
|
u := Uninstall{
|
2019-12-18 19:47:15 +00:00
|
|
|
Release: "just_a_band_huff_and_puff",
|
|
|
|
}
|
|
|
|
stderr := strings.Builder{}
|
|
|
|
cfg := Config{
|
2019-12-23 20:43:17 +00:00
|
|
|
Debug: true,
|
|
|
|
Stderr: &stderr,
|
2019-12-18 19:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
command = func(path string, args ...string) cmd {
|
|
|
|
suite.mockCmd.EXPECT().
|
|
|
|
String().
|
|
|
|
Return(fmt.Sprintf("%s %s", path, strings.Join(args, " ")))
|
|
|
|
|
|
|
|
return suite.mockCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
|
|
|
|
suite.mockCmd.EXPECT().Stderr(&stderr).AnyTimes()
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
suite.NoError(u.Prepare(cfg))
|
2019-12-23 20:43:17 +00:00
|
|
|
suite.Equal(fmt.Sprintf("Generated command: '%s --debug "+
|
|
|
|
"uninstall just_a_band_huff_and_puff'\n", helmBin), stderr.String())
|
2019-12-18 19:47:15 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
u := Uninstall{}
|
|
|
|
err := u.Prepare(Config{})
|
|
|
|
suite.EqualError(err, "release is required", "Uninstall.Release should be mandatory")
|
2019-12-18 19:47:15 +00:00
|
|
|
}
|