2019-12-09 17:56:02 +00:00
|
|
|
package helm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-12 18:20:11 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2019-12-09 17:56:02 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2019-12-24 19:08:09 +00:00
|
|
|
"strings"
|
2019-12-09 17:56:02 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-01-14 18:32:20 +00:00
|
|
|
"github.com/pelotech/drone-helm3/internal/env"
|
2019-12-09 17:56:02 +00:00
|
|
|
"github.com/pelotech/drone-helm3/internal/run"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PlanTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPlanTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(PlanTestSuite))
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
func (suite *PlanTestSuite) TestNewPlan() {
|
|
|
|
ctrl := gomock.NewController(suite.T())
|
2019-12-24 23:33:50 +00:00
|
|
|
defer ctrl.Finish()
|
2019-12-12 18:20:11 +00:00
|
|
|
stepOne := NewMockStep(ctrl)
|
|
|
|
stepTwo := NewMockStep(ctrl)
|
|
|
|
|
|
|
|
origHelp := help
|
2020-01-14 18:32:20 +00:00
|
|
|
help = func(cfg env.Config) []Step {
|
2019-12-12 18:20:11 +00:00
|
|
|
return []Step{stepOne, stepTwo}
|
|
|
|
}
|
|
|
|
defer func() { help = origHelp }()
|
|
|
|
|
2019-12-24 19:08:09 +00:00
|
|
|
stdout := strings.Builder{}
|
|
|
|
stderr := strings.Builder{}
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2020-01-02 19:38:41 +00:00
|
|
|
Command: "help",
|
|
|
|
Debug: false,
|
|
|
|
Namespace: "outer",
|
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
stepOne.EXPECT().
|
2020-01-17 18:13:53 +00:00
|
|
|
Prepare()
|
2019-12-12 18:20:11 +00:00
|
|
|
stepTwo.EXPECT().
|
2020-01-17 18:13:53 +00:00
|
|
|
Prepare()
|
2019-12-12 18:20:11 +00:00
|
|
|
|
2019-12-09 17:56:02 +00:00
|
|
|
plan, err := NewPlan(cfg)
|
|
|
|
suite.Require().Nil(err)
|
2019-12-12 18:20:11 +00:00
|
|
|
suite.Equal(cfg, plan.cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *PlanTestSuite) TestNewPlanAbortsOnError() {
|
|
|
|
ctrl := gomock.NewController(suite.T())
|
2019-12-24 23:33:50 +00:00
|
|
|
defer ctrl.Finish()
|
2019-12-12 18:20:11 +00:00
|
|
|
stepOne := NewMockStep(ctrl)
|
|
|
|
stepTwo := NewMockStep(ctrl)
|
2019-12-09 17:56:02 +00:00
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
origHelp := help
|
2020-01-14 18:32:20 +00:00
|
|
|
help = func(cfg env.Config) []Step {
|
2019-12-12 18:20:11 +00:00
|
|
|
return []Step{stepOne, stepTwo}
|
|
|
|
}
|
|
|
|
defer func() { help = origHelp }()
|
|
|
|
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2019-12-12 18:20:11 +00:00
|
|
|
Command: "help",
|
|
|
|
}
|
2019-12-10 00:25:47 +00:00
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
stepOne.EXPECT().
|
2020-01-17 18:13:53 +00:00
|
|
|
Prepare().
|
2019-12-12 18:20:11 +00:00
|
|
|
Return(fmt.Errorf("I'm starry Dave, aye, cat blew that"))
|
|
|
|
|
|
|
|
_, err := NewPlan(cfg)
|
|
|
|
suite.Require().NotNil(err)
|
|
|
|
suite.EqualError(err, "while preparing *helm.MockStep step: I'm starry Dave, aye, cat blew that")
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 23:47:26 +00:00
|
|
|
func (suite *PlanTestSuite) TestExecute() {
|
|
|
|
ctrl := gomock.NewController(suite.T())
|
|
|
|
defer ctrl.Finish()
|
|
|
|
stepOne := NewMockStep(ctrl)
|
|
|
|
stepTwo := NewMockStep(ctrl)
|
|
|
|
|
|
|
|
plan := Plan{
|
2020-01-17 18:13:53 +00:00
|
|
|
steps: []Step{stepOne, stepTwo},
|
2019-12-24 23:47:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stepOne.EXPECT().
|
2020-01-16 23:30:21 +00:00
|
|
|
Execute().
|
2019-12-24 23:47:26 +00:00
|
|
|
Times(1)
|
|
|
|
stepTwo.EXPECT().
|
2020-01-16 23:30:21 +00:00
|
|
|
Execute().
|
2019-12-24 23:47:26 +00:00
|
|
|
Times(1)
|
|
|
|
|
|
|
|
suite.NoError(plan.Execute())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *PlanTestSuite) TestExecuteAbortsOnError() {
|
|
|
|
ctrl := gomock.NewController(suite.T())
|
|
|
|
defer ctrl.Finish()
|
|
|
|
stepOne := NewMockStep(ctrl)
|
|
|
|
stepTwo := NewMockStep(ctrl)
|
|
|
|
|
|
|
|
plan := Plan{
|
2020-01-17 18:13:53 +00:00
|
|
|
steps: []Step{stepOne, stepTwo},
|
2019-12-24 23:47:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stepOne.EXPECT().
|
2020-01-16 23:30:21 +00:00
|
|
|
Execute().
|
2019-12-24 23:47:26 +00:00
|
|
|
Times(1).
|
|
|
|
Return(fmt.Errorf("oh, he'll gnaw"))
|
|
|
|
|
|
|
|
err := plan.Execute()
|
2019-12-24 23:49:47 +00:00
|
|
|
suite.EqualError(err, "while executing *helm.MockStep step: oh, he'll gnaw")
|
2019-12-24 23:47:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
func (suite *PlanTestSuite) TestUpgrade() {
|
2020-01-16 21:50:04 +00:00
|
|
|
steps := upgrade(env.Config{})
|
2019-12-19 01:13:17 +00:00
|
|
|
suite.Require().Equal(2, len(steps), "upgrade should return 2 steps")
|
2020-01-16 21:50:04 +00:00
|
|
|
suite.IsType(&run.InitKube{}, steps[0])
|
|
|
|
suite.IsType(&run.Upgrade{}, steps[1])
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 23:06:32 +00:00
|
|
|
func (suite *PlanTestSuite) TestUpgradeWithUpdateDependencies() {
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2019-12-27 23:06:32 +00:00
|
|
|
UpdateDependencies: true,
|
|
|
|
}
|
|
|
|
steps := upgrade(cfg)
|
|
|
|
suite.Require().Equal(3, len(steps), "upgrade should have a third step when DepUpdate is true")
|
|
|
|
suite.IsType(&run.InitKube{}, steps[0])
|
|
|
|
suite.IsType(&run.DepUpdate{}, steps[1])
|
|
|
|
}
|
|
|
|
|
2019-12-30 19:57:19 +00:00
|
|
|
func (suite *PlanTestSuite) TestUpgradeWithAddRepos() {
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2019-12-30 19:57:19 +00:00
|
|
|
AddRepos: []string{
|
|
|
|
"machine=https://github.com/harold_finch/themachine",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
steps := upgrade(cfg)
|
|
|
|
suite.Require().True(len(steps) > 1, "upgrade should generate at least two steps")
|
|
|
|
suite.IsType(&run.AddRepo{}, steps[1])
|
|
|
|
}
|
|
|
|
|
2019-12-27 23:06:32 +00:00
|
|
|
func (suite *PlanTestSuite) TestUninstall() {
|
2020-01-16 21:50:04 +00:00
|
|
|
steps := uninstall(env.Config{})
|
2019-12-19 23:02:49 +00:00
|
|
|
suite.Require().Equal(2, len(steps), "uninstall should return 2 steps")
|
2019-12-19 00:58:31 +00:00
|
|
|
|
2020-01-16 21:50:04 +00:00
|
|
|
suite.IsType(&run.InitKube{}, steps[0])
|
|
|
|
suite.IsType(&run.Uninstall{}, steps[1])
|
2019-12-19 00:58:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 23:06:32 +00:00
|
|
|
func (suite *PlanTestSuite) TestUninstallWithUpdateDependencies() {
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2019-12-27 23:06:32 +00:00
|
|
|
UpdateDependencies: true,
|
|
|
|
}
|
|
|
|
steps := uninstall(cfg)
|
|
|
|
suite.Require().Equal(3, len(steps), "uninstall should have a third step when DepUpdate is true")
|
|
|
|
suite.IsType(&run.InitKube{}, steps[0])
|
|
|
|
suite.IsType(&run.DepUpdate{}, steps[1])
|
|
|
|
}
|
|
|
|
|
2019-12-18 01:14:39 +00:00
|
|
|
func (suite *PlanTestSuite) TestLint() {
|
2020-01-16 21:50:04 +00:00
|
|
|
steps := lint(env.Config{})
|
|
|
|
suite.Require().Equal(1, len(steps))
|
|
|
|
suite.IsType(&run.Lint{}, steps[0])
|
2019-12-18 01:14:39 +00:00
|
|
|
}
|
|
|
|
|
2019-12-27 23:06:32 +00:00
|
|
|
func (suite *PlanTestSuite) TestLintWithUpdateDependencies() {
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2019-12-27 23:06:32 +00:00
|
|
|
UpdateDependencies: true,
|
|
|
|
}
|
|
|
|
steps := lint(cfg)
|
|
|
|
suite.Require().Equal(2, len(steps), "lint should have a second step when DepUpdate is true")
|
|
|
|
suite.IsType(&run.DepUpdate{}, steps[0])
|
|
|
|
}
|
|
|
|
|
2019-12-30 19:57:19 +00:00
|
|
|
func (suite *PlanTestSuite) TestLintWithAddRepos() {
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2019-12-30 19:57:19 +00:00
|
|
|
AddRepos: []string{"friendczar=https://github.com/logan_pierce/friendczar"},
|
|
|
|
}
|
|
|
|
steps := lint(cfg)
|
|
|
|
suite.Require().True(len(steps) > 0, "lint should return at least one step")
|
|
|
|
suite.IsType(&run.AddRepo{}, steps[0])
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() {
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2019-12-12 18:20:11 +00:00
|
|
|
Command: "upgrade",
|
|
|
|
}
|
|
|
|
stepsMaker := determineSteps(cfg)
|
|
|
|
suite.Same(&upgrade, stepsMaker)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *PlanTestSuite) TestDeterminePlanUpgradeFromDroneEvent() {
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{}
|
2019-12-12 18:20:11 +00:00
|
|
|
|
2019-12-09 17:56:02 +00:00
|
|
|
upgradeEvents := []string{"push", "tag", "deployment", "pull_request", "promote", "rollback"}
|
|
|
|
for _, event := range upgradeEvents {
|
|
|
|
cfg.DroneEvent = event
|
2019-12-12 18:20:11 +00:00
|
|
|
stepsMaker := determineSteps(cfg)
|
|
|
|
suite.Same(&upgrade, stepsMaker, fmt.Sprintf("for event type '%s'", event))
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-10 00:25:47 +00:00
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
func (suite *PlanTestSuite) TestDeterminePlanUninstallCommand() {
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2019-12-19 23:02:49 +00:00
|
|
|
Command: "uninstall",
|
|
|
|
}
|
|
|
|
stepsMaker := determineSteps(cfg)
|
|
|
|
suite.Same(&uninstall, stepsMaker)
|
|
|
|
}
|
|
|
|
|
2020-01-08 19:04:30 +00:00
|
|
|
// helm_command = delete is provided as an alias for backward-compatibility with drone-helm
|
2019-12-19 00:58:31 +00:00
|
|
|
func (suite *PlanTestSuite) TestDeterminePlanDeleteCommand() {
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2019-12-19 00:58:31 +00:00
|
|
|
Command: "delete",
|
|
|
|
}
|
|
|
|
stepsMaker := determineSteps(cfg)
|
2019-12-19 23:02:49 +00:00
|
|
|
suite.Same(&uninstall, stepsMaker)
|
2019-12-19 00:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *PlanTestSuite) TestDeterminePlanDeleteFromDroneEvent() {
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2019-12-19 00:58:31 +00:00
|
|
|
DroneEvent: "delete",
|
|
|
|
}
|
|
|
|
stepsMaker := determineSteps(cfg)
|
2019-12-19 23:02:49 +00:00
|
|
|
suite.Same(&uninstall, stepsMaker)
|
2019-12-19 00:58:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-18 01:14:39 +00:00
|
|
|
func (suite *PlanTestSuite) TestDeterminePlanLintCommand() {
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2019-12-18 01:14:39 +00:00
|
|
|
Command: "lint",
|
|
|
|
}
|
|
|
|
|
|
|
|
stepsMaker := determineSteps(cfg)
|
|
|
|
suite.Same(&lint, stepsMaker)
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() {
|
2020-01-14 18:32:20 +00:00
|
|
|
cfg := env.Config{
|
2019-12-10 00:25:47 +00:00
|
|
|
Command: "help",
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
stepsMaker := determineSteps(cfg)
|
|
|
|
suite.Same(&help, stepsMaker)
|
2019-12-10 00:25:47 +00:00
|
|
|
}
|