woodpecker-helm3/internal/run/uninstall_test.go

150 lines
3.6 KiB
Go
Raw Normal View History

2019-12-18 19:47:15 +00:00
package run
import (
"fmt"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/suite"
"strings"
"testing"
)
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
}
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
}
}
func (suite *UninstallTestSuite) AfterTest(_, _ string) {
2019-12-18 19:47:15 +00:00
command = suite.originalCommand
}
func TestUninstallTestSuite(t *testing.T) {
suite.Run(t, new(UninstallTestSuite))
2019-12-18 19:47:15 +00:00
}
func (suite *UninstallTestSuite) TestPrepareAndExecute() {
2019-12-18 19:47:15 +00:00
defer suite.ctrl.Finish()
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)
cfg := Config{}
suite.NoError(u.Prepare(cfg))
expected := []string{"uninstall", "zayde_wølf_king"}
2019-12-18 19:47:15 +00:00
suite.Equal(expected, actual)
u.Execute(cfg)
2019-12-18 19:47:15 +00:00
}
func (suite *UninstallTestSuite) TestPrepareDryRunFlag() {
u := Uninstall{
2019-12-18 19:47:15 +00:00
Release: "firefox_ak_wildfire",
DryRun: true,
}
cfg := Config{}
2019-12-18 19:47:15 +00:00
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
suite.NoError(u.Prepare(cfg))
expected := []string{"uninstall", "--dry-run", "firefox_ak_wildfire"}
2019-12-18 19:47:15 +00:00
suite.Equal(expected, suite.actualArgs)
}
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)
}
func (suite *UninstallTestSuite) TestPrepareNamespaceFlag() {
u := Uninstall{
2019-12-18 19:47:15 +00:00
Release: "carly_simon_run_away_with_me",
}
cfg := Config{
Namespace: "emotion",
2019-12-18 19:47:15 +00:00
}
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
suite.NoError(u.Prepare(cfg))
expected := []string{"--namespace", "emotion", "uninstall", "carly_simon_run_away_with_me"}
2019-12-18 19:47:15 +00:00
suite.Equal(expected, suite.actualArgs)
}
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{
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()
suite.NoError(u.Prepare(cfg))
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
}
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()
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
}