woodpecker-helm3/internal/run/uninstall_test.go

120 lines
2.9 KiB
Go
Raw Permalink Normal View History

2019-12-18 19:47:15 +00:00
package run
import (
"github.com/golang/mock/gomock"
"github.com/pelotech/drone-helm3/internal/env"
2019-12-18 19:47:15 +00:00
"github.com/stretchr/testify/suite"
"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) TestNewUninstall() {
cfg := env.Config{
DryRun: true,
Release: "jetta_id_love_to_change_the_world",
KeepHistory: true,
}
u := NewUninstall(cfg)
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)
}
func (suite *UninstallTestSuite) TestPrepareAndExecute() {
2019-12-18 19:47:15 +00:00
defer suite.ctrl.Finish()
cfg := env.Config{
2019-12-18 19:47:15 +00:00
Release: "zayde_wølf_king",
}
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)
suite.NoError(u.Prepare())
expected := []string{"uninstall", "zayde_wølf_king"}
2019-12-18 19:47:15 +00:00
suite.Equal(expected, actual)
u.Execute()
2019-12-18 19:47:15 +00:00
}
func (suite *UninstallTestSuite) TestPrepareDryRunFlag() {
cfg := env.Config{
2019-12-18 19:47:15 +00:00
Release: "firefox_ak_wildfire",
DryRun: true,
}
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()
suite.NoError(u.Prepare())
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() {
cfg := env.Config{
Release: "perturbator_sentient",
KeepHistory: true,
}
u := NewUninstall(cfg)
suite.mockCmd.EXPECT().Stdout(gomock.Any()).AnyTimes()
suite.mockCmd.EXPECT().Stderr(gomock.Any()).AnyTimes()
suite.NoError(u.Prepare())
expected := []string{"uninstall", "--keep-history", "perturbator_sentient"}
suite.Equal(expected, suite.actualArgs)
}
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 := NewUninstall(env.Config{})
err := u.Prepare()
suite.EqualError(err, "release is required", "Uninstall.Release should be mandatory")
2019-12-18 19:47:15 +00:00
}