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"
|
|
|
|
|
|
|
|
"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
|
|
|
|
help = func(cfg Config) []Step {
|
|
|
|
return []Step{stepOne, stepTwo}
|
|
|
|
}
|
|
|
|
defer func() { help = origHelp }()
|
|
|
|
|
2019-12-24 19:08:09 +00:00
|
|
|
stdout := strings.Builder{}
|
|
|
|
stderr := strings.Builder{}
|
2019-12-09 17:56:02 +00:00
|
|
|
cfg := Config{
|
2019-12-12 18:20:11 +00:00
|
|
|
Command: "help",
|
|
|
|
Debug: false,
|
|
|
|
Values: "steadfastness,forthrightness",
|
|
|
|
StringValues: "tensile_strength,flexibility",
|
|
|
|
ValuesFiles: []string{"/root/price_inventory.yml"},
|
|
|
|
Namespace: "outer",
|
2019-12-24 19:08:09 +00:00
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
runCfg := run.Config{
|
|
|
|
Debug: false,
|
|
|
|
Values: "steadfastness,forthrightness",
|
|
|
|
StringValues: "tensile_strength,flexibility",
|
|
|
|
ValuesFiles: []string{"/root/price_inventory.yml"},
|
|
|
|
Namespace: "outer",
|
2019-12-24 19:08:09 +00:00
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stepOne.EXPECT().
|
|
|
|
Prepare(runCfg)
|
|
|
|
stepTwo.EXPECT().
|
|
|
|
Prepare(runCfg)
|
|
|
|
|
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)
|
|
|
|
suite.Equal(runCfg, plan.runCfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
help = func(cfg Config) []Step {
|
|
|
|
return []Step{stepOne, stepTwo}
|
|
|
|
}
|
|
|
|
defer func() { help = origHelp }()
|
|
|
|
|
|
|
|
cfg := Config{
|
|
|
|
Command: "help",
|
|
|
|
}
|
2019-12-10 00:25:47 +00:00
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
stepOne.EXPECT().
|
|
|
|
Prepare(gomock.Any()).
|
|
|
|
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)
|
|
|
|
|
|
|
|
runCfg := run.Config{}
|
|
|
|
|
|
|
|
plan := Plan{
|
|
|
|
steps: []Step{stepOne, stepTwo},
|
|
|
|
runCfg: runCfg,
|
|
|
|
}
|
|
|
|
|
|
|
|
stepOne.EXPECT().
|
|
|
|
Execute(runCfg).
|
|
|
|
Times(1)
|
|
|
|
stepTwo.EXPECT().
|
|
|
|
Execute(runCfg).
|
|
|
|
Times(1)
|
|
|
|
|
|
|
|
suite.NoError(plan.Execute())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *PlanTestSuite) TestExecuteAbortsOnError() {
|
|
|
|
ctrl := gomock.NewController(suite.T())
|
|
|
|
defer ctrl.Finish()
|
|
|
|
stepOne := NewMockStep(ctrl)
|
|
|
|
stepTwo := NewMockStep(ctrl)
|
|
|
|
|
|
|
|
runCfg := run.Config{}
|
|
|
|
|
|
|
|
plan := Plan{
|
|
|
|
steps: []Step{stepOne, stepTwo},
|
|
|
|
runCfg: runCfg,
|
|
|
|
}
|
|
|
|
|
|
|
|
stepOne.EXPECT().
|
|
|
|
Execute(runCfg).
|
|
|
|
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() {
|
2019-12-09 17:56:02 +00:00
|
|
|
cfg := Config{
|
2019-12-19 01:13:17 +00:00
|
|
|
ChartVersion: "seventeen",
|
|
|
|
DryRun: true,
|
|
|
|
Wait: true,
|
|
|
|
ReuseValues: true,
|
|
|
|
Timeout: "go sit in the corner",
|
|
|
|
Chart: "billboard_top_100",
|
|
|
|
Release: "post_malone_circles",
|
|
|
|
Force: true,
|
2019-12-09 17:56:02 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
steps := upgrade(cfg)
|
2019-12-19 01:13:17 +00:00
|
|
|
suite.Require().Equal(2, len(steps), "upgrade should return 2 steps")
|
2019-12-12 18:20:11 +00:00
|
|
|
suite.Require().IsType(&run.InitKube{}, steps[0])
|
|
|
|
|
|
|
|
suite.Require().IsType(&run.Upgrade{}, steps[1])
|
|
|
|
upgrade, _ := steps[1].(*run.Upgrade)
|
|
|
|
|
2019-12-19 01:13:17 +00:00
|
|
|
expected := &run.Upgrade{
|
2019-12-12 18:20:11 +00:00
|
|
|
Chart: cfg.Chart,
|
|
|
|
Release: cfg.Release,
|
|
|
|
ChartVersion: cfg.ChartVersion,
|
2019-12-17 17:23:44 +00:00
|
|
|
DryRun: true,
|
2019-12-12 18:20:11 +00:00
|
|
|
Wait: cfg.Wait,
|
|
|
|
ReuseValues: cfg.ReuseValues,
|
|
|
|
Timeout: cfg.Timeout,
|
|
|
|
Force: cfg.Force,
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.Equal(expected, upgrade)
|
|
|
|
}
|
|
|
|
|
2019-12-19 00:58:31 +00:00
|
|
|
func (suite *PlanTestSuite) TestDel() {
|
|
|
|
cfg := Config{
|
|
|
|
KubeToken: "b2YgbXkgYWZmZWN0aW9u",
|
|
|
|
SkipTLSVerify: true,
|
|
|
|
Certificate: "cHJvY2xhaW1zIHdvbmRlcmZ1bCBmcmllbmRzaGlw",
|
|
|
|
APIServer: "98.765.43.21",
|
|
|
|
ServiceAccount: "greathelm",
|
|
|
|
DryRun: true,
|
|
|
|
Timeout: "think about what you did",
|
|
|
|
Release: "jetta_id_love_to_change_the_world",
|
|
|
|
}
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
steps := uninstall(cfg)
|
|
|
|
suite.Require().Equal(2, len(steps), "uninstall should return 2 steps")
|
2019-12-19 00:58:31 +00:00
|
|
|
|
|
|
|
suite.Require().IsType(&run.InitKube{}, steps[0])
|
|
|
|
init, _ := steps[0].(*run.InitKube)
|
|
|
|
var expected Step = &run.InitKube{
|
|
|
|
SkipTLSVerify: true,
|
|
|
|
Certificate: "cHJvY2xhaW1zIHdvbmRlcmZ1bCBmcmllbmRzaGlw",
|
|
|
|
APIServer: "98.765.43.21",
|
|
|
|
ServiceAccount: "greathelm",
|
|
|
|
Token: "b2YgbXkgYWZmZWN0aW9u",
|
|
|
|
TemplateFile: kubeConfigTemplate,
|
2019-12-23 20:43:17 +00:00
|
|
|
ConfigFile: kubeConfigFile,
|
2019-12-19 00:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
suite.Equal(expected, init)
|
|
|
|
|
2019-12-19 23:02:49 +00:00
|
|
|
suite.Require().IsType(&run.Uninstall{}, steps[1])
|
|
|
|
actual, _ := steps[1].(*run.Uninstall)
|
|
|
|
expected = &run.Uninstall{
|
2019-12-19 00:58:31 +00:00
|
|
|
Release: "jetta_id_love_to_change_the_world",
|
|
|
|
DryRun: true,
|
|
|
|
}
|
|
|
|
suite.Equal(expected, actual)
|
|
|
|
}
|
|
|
|
|
2019-12-19 01:13:17 +00:00
|
|
|
func (suite *PlanTestSuite) TestInitKube() {
|
|
|
|
cfg := Config{
|
|
|
|
KubeToken: "cXVlZXIgY2hhcmFjdGVyCg==",
|
|
|
|
SkipTLSVerify: true,
|
|
|
|
Certificate: "b2Ygd29rZW5lc3MK",
|
|
|
|
APIServer: "123.456.78.9",
|
|
|
|
ServiceAccount: "helmet",
|
|
|
|
}
|
|
|
|
|
|
|
|
steps := initKube(cfg)
|
|
|
|
suite.Require().Equal(1, len(steps), "initKube should return one step")
|
|
|
|
suite.Require().IsType(&run.InitKube{}, steps[0])
|
|
|
|
init, _ := steps[0].(*run.InitKube)
|
|
|
|
|
|
|
|
expected := &run.InitKube{
|
|
|
|
SkipTLSVerify: true,
|
|
|
|
Certificate: "b2Ygd29rZW5lc3MK",
|
|
|
|
APIServer: "123.456.78.9",
|
|
|
|
ServiceAccount: "helmet",
|
|
|
|
Token: "cXVlZXIgY2hhcmFjdGVyCg==",
|
|
|
|
TemplateFile: kubeConfigTemplate,
|
2019-12-23 20:43:17 +00:00
|
|
|
ConfigFile: kubeConfigFile,
|
2019-12-19 01:13:17 +00:00
|
|
|
}
|
|
|
|
suite.Equal(expected, init)
|
|
|
|
}
|
|
|
|
|
2019-12-18 01:14:39 +00:00
|
|
|
func (suite *PlanTestSuite) TestLint() {
|
|
|
|
cfg := Config{
|
|
|
|
Chart: "./flow",
|
|
|
|
}
|
|
|
|
|
|
|
|
steps := lint(cfg)
|
|
|
|
suite.Equal(1, len(steps))
|
|
|
|
|
|
|
|
want := &run.Lint{
|
|
|
|
Chart: "./flow",
|
|
|
|
}
|
|
|
|
suite.Equal(want, steps[0])
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() {
|
|
|
|
cfg := Config{
|
|
|
|
Command: "upgrade",
|
|
|
|
}
|
|
|
|
stepsMaker := determineSteps(cfg)
|
|
|
|
suite.Same(&upgrade, stepsMaker)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *PlanTestSuite) TestDeterminePlanUpgradeFromDroneEvent() {
|
|
|
|
cfg := Config{}
|
|
|
|
|
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() {
|
|
|
|
cfg := Config{
|
|
|
|
Command: "uninstall",
|
|
|
|
}
|
|
|
|
stepsMaker := determineSteps(cfg)
|
|
|
|
suite.Same(&uninstall, stepsMaker)
|
|
|
|
}
|
|
|
|
|
|
|
|
// helm_command = delete is provided as an alias for backwards-compatibility with drone-helm
|
2019-12-19 00:58:31 +00:00
|
|
|
func (suite *PlanTestSuite) TestDeterminePlanDeleteCommand() {
|
|
|
|
cfg := Config{
|
|
|
|
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() {
|
|
|
|
cfg := Config{
|
|
|
|
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() {
|
|
|
|
cfg := Config{
|
|
|
|
Command: "lint",
|
|
|
|
}
|
|
|
|
|
|
|
|
stepsMaker := determineSteps(cfg)
|
|
|
|
suite.Same(&lint, stepsMaker)
|
|
|
|
}
|
|
|
|
|
2019-12-12 18:20:11 +00:00
|
|
|
func (suite *PlanTestSuite) TestDeterminePlanHelpCommand() {
|
2019-12-10 00:25:47 +00:00
|
|
|
cfg := Config{
|
|
|
|
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
|
|
|
}
|