2019-12-05 22:35:25 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
2019-12-10 00:25:47 +00:00
|
|
|
"fmt"
|
2022-08-16 18:42:02 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-12-05 22:35:25 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2020-01-16 21:50:04 +00:00
|
|
|
"github.com/pelotech/drone-helm3/internal/env"
|
2019-12-10 00:25:47 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2019-12-05 22:35:25 +00:00
|
|
|
)
|
|
|
|
|
2019-12-10 00:25:47 +00:00
|
|
|
type UpgradeTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
ctrl *gomock.Controller
|
|
|
|
mockCmd *Mockcmd
|
|
|
|
originalCommand func(string, ...string) cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *UpgradeTestSuite) BeforeTest(_, _ string) {
|
|
|
|
suite.ctrl = gomock.NewController(suite.T())
|
|
|
|
suite.mockCmd = NewMockcmd(suite.ctrl)
|
|
|
|
|
|
|
|
suite.originalCommand = command
|
|
|
|
command = func(path string, args ...string) cmd { return suite.mockCmd }
|
|
|
|
}
|
2019-12-05 22:35:25 +00:00
|
|
|
|
2019-12-10 00:25:47 +00:00
|
|
|
func (suite *UpgradeTestSuite) AfterTest(_, _ string) {
|
|
|
|
command = suite.originalCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpgradeTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(UpgradeTestSuite))
|
|
|
|
}
|
|
|
|
|
2020-01-16 21:50:04 +00:00
|
|
|
func (suite *UpgradeTestSuite) TestNewUpgrade() {
|
2021-05-19 17:16:43 +00:00
|
|
|
cfg := env.NewTestConfig(suite.T())
|
|
|
|
cfg.ChartVersion = "seventeen"
|
|
|
|
cfg.DryRun = true
|
|
|
|
cfg.Wait = true
|
|
|
|
cfg.Values = "steadfastness,forthrightness"
|
|
|
|
cfg.StringValues = "tensile_strength,flexibility"
|
|
|
|
cfg.ValuesFiles = []string{"/root/price_inventory.yml"}
|
|
|
|
cfg.ReuseValues = true
|
|
|
|
cfg.Timeout = "go sit in the corner"
|
|
|
|
cfg.Chart = "billboard_top_100"
|
|
|
|
cfg.Release = "post_malone_circles"
|
|
|
|
cfg.Force = true
|
|
|
|
cfg.AtomicUpgrade = true
|
|
|
|
cfg.CleanupOnFail = true
|
|
|
|
|
|
|
|
up := NewUpgrade(*cfg)
|
2020-01-16 21:50:04 +00:00
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
suite.Equal(cfg.Chart, up.chart)
|
|
|
|
suite.Equal(cfg.Release, up.release)
|
|
|
|
suite.Equal(cfg.ChartVersion, up.chartVersion)
|
|
|
|
suite.Equal(true, up.dryRun)
|
|
|
|
suite.Equal(cfg.Wait, up.wait)
|
|
|
|
suite.Equal("steadfastness,forthrightness", up.values)
|
|
|
|
suite.Equal("tensile_strength,flexibility", up.stringValues)
|
|
|
|
suite.Equal([]string{"/root/price_inventory.yml"}, up.valuesFiles)
|
|
|
|
suite.Equal(cfg.ReuseValues, up.reuseValues)
|
|
|
|
suite.Equal(cfg.Timeout, up.timeout)
|
|
|
|
suite.Equal(cfg.Force, up.force)
|
|
|
|
suite.Equal(true, up.atomic)
|
|
|
|
suite.Equal(true, up.cleanupOnFail)
|
|
|
|
suite.NotNil(up.config)
|
2020-01-20 23:40:36 +00:00
|
|
|
suite.NotNil(up.certs)
|
2020-01-16 21:50:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:55:05 +00:00
|
|
|
func (suite *UpgradeTestSuite) TestPrepareAndExecute() {
|
2019-12-10 00:25:47 +00:00
|
|
|
defer suite.ctrl.Finish()
|
|
|
|
|
2021-05-19 17:16:43 +00:00
|
|
|
cfg := env.NewTestConfig(suite.T())
|
|
|
|
cfg.Chart = "at40"
|
|
|
|
cfg.Release = "jonas_brothers_only_human"
|
|
|
|
|
|
|
|
u := NewUpgrade(*cfg)
|
2019-12-05 22:35:25 +00:00
|
|
|
|
2019-12-09 18:52:41 +00:00
|
|
|
command = func(path string, args ...string) cmd {
|
2019-12-10 00:25:47 +00:00
|
|
|
suite.Equal(helmBin, path)
|
2021-05-19 17:16:43 +00:00
|
|
|
suite.Equal([]string{"upgrade", "--install", "--history-max=10", "jonas_brothers_only_human", "at40"}, args)
|
2019-12-05 22:35:25 +00:00
|
|
|
|
2019-12-10 00:25:47 +00:00
|
|
|
return suite.mockCmd
|
2019-12-05 22:35:25 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 00:25:47 +00:00
|
|
|
suite.mockCmd.EXPECT().
|
2019-12-05 22:35:25 +00:00
|
|
|
Stdout(gomock.Any())
|
2019-12-10 00:25:47 +00:00
|
|
|
suite.mockCmd.EXPECT().
|
2019-12-05 22:35:25 +00:00
|
|
|
Stderr(gomock.Any())
|
2019-12-10 00:25:47 +00:00
|
|
|
suite.mockCmd.EXPECT().
|
2019-12-05 22:35:25 +00:00
|
|
|
Run().
|
|
|
|
Times(1)
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
err := u.Prepare()
|
2019-12-12 18:20:11 +00:00
|
|
|
suite.Require().Nil(err)
|
2020-01-16 23:30:21 +00:00
|
|
|
u.Execute()
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *UpgradeTestSuite) TestPrepareNamespaceFlag() {
|
|
|
|
defer suite.ctrl.Finish()
|
|
|
|
|
2021-05-19 17:16:43 +00:00
|
|
|
cfg := env.NewTestConfig(suite.T())
|
|
|
|
cfg.Namespace = "melt"
|
|
|
|
cfg.Chart = "at40"
|
|
|
|
cfg.Release = "shaed_trampoline"
|
|
|
|
|
|
|
|
u := NewUpgrade(*cfg)
|
2019-12-12 18:20:11 +00:00
|
|
|
|
|
|
|
command = func(path string, args ...string) cmd {
|
|
|
|
suite.Equal(helmBin, path)
|
2021-05-19 17:16:43 +00:00
|
|
|
suite.Equal([]string{"--namespace", "melt", "upgrade", "--install", "--history-max=10", "shaed_trampoline", "at40"}, args)
|
2019-12-12 18:20:11 +00:00
|
|
|
|
|
|
|
return suite.mockCmd
|
|
|
|
}
|
|
|
|
|
2019-12-17 00:55:05 +00:00
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any())
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any())
|
2019-12-12 18:20:11 +00:00
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
err := u.Prepare()
|
2019-12-10 00:25:47 +00:00
|
|
|
suite.Require().Nil(err)
|
2019-12-17 00:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *UpgradeTestSuite) TestPrepareWithUpgradeFlags() {
|
|
|
|
defer suite.ctrl.Finish()
|
|
|
|
|
2021-05-19 17:16:43 +00:00
|
|
|
cfg := env.NewTestConfig(suite.T())
|
|
|
|
cfg.Chart = "hot_ac"
|
|
|
|
cfg.Release = "maroon_5_memories"
|
|
|
|
cfg.ChartVersion = "radio_edit"
|
|
|
|
cfg.DryRun = true
|
|
|
|
cfg.Wait = true
|
|
|
|
cfg.Values = "age=35"
|
|
|
|
cfg.StringValues = "height=5ft10in"
|
|
|
|
cfg.ValuesFiles = []string{"/usr/local/stats", "/usr/local/grades"}
|
|
|
|
cfg.ReuseValues = true
|
|
|
|
cfg.Timeout = "sit_in_the_corner"
|
|
|
|
cfg.Force = true
|
|
|
|
cfg.AtomicUpgrade = true
|
|
|
|
cfg.CleanupOnFail = true
|
|
|
|
|
|
|
|
u := NewUpgrade(*cfg)
|
2020-01-20 23:40:36 +00:00
|
|
|
// inject a ca cert filename so repoCerts won't create any files that we'd have to clean up
|
|
|
|
u.certs.caCertFilename = "local_ca.cert"
|
2020-01-02 19:38:41 +00:00
|
|
|
|
2019-12-17 00:55:05 +00:00
|
|
|
command = func(path string, args ...string) cmd {
|
|
|
|
suite.Equal(helmBin, path)
|
2019-12-23 20:43:17 +00:00
|
|
|
suite.Equal([]string{"upgrade", "--install",
|
2019-12-17 00:55:05 +00:00
|
|
|
"--version", "radio_edit",
|
|
|
|
"--dry-run",
|
|
|
|
"--wait",
|
|
|
|
"--reuse-values",
|
|
|
|
"--timeout", "sit_in_the_corner",
|
|
|
|
"--force",
|
2020-01-07 20:17:54 +00:00
|
|
|
"--atomic",
|
2020-01-07 20:53:55 +00:00
|
|
|
"--cleanup-on-fail",
|
2019-12-17 00:55:05 +00:00
|
|
|
"--set", "age=35",
|
|
|
|
"--set-string", "height=5ft10in",
|
|
|
|
"--values", "/usr/local/stats",
|
|
|
|
"--values", "/usr/local/grades",
|
2020-01-20 18:47:49 +00:00
|
|
|
"--ca-file", "local_ca.cert",
|
2021-05-19 17:16:43 +00:00
|
|
|
"--history-max=10",
|
2019-12-17 00:55:05 +00:00
|
|
|
"maroon_5_memories", "hot_ac"}, args)
|
|
|
|
|
|
|
|
return suite.mockCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any())
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any())
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
err := u.Prepare()
|
2019-12-17 00:55:05 +00:00
|
|
|
suite.Require().Nil(err)
|
2019-12-10 00:25:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:55:54 +00:00
|
|
|
func (suite *UpgradeTestSuite) TestRequiresChartAndRelease() {
|
|
|
|
// 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()
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
u := NewUpgrade(env.Config{})
|
|
|
|
u.release = "seth_everman_unskippable_cutscene"
|
2019-12-17 00:55:54 +00:00
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
err := u.Prepare()
|
2019-12-17 00:55:54 +00:00
|
|
|
suite.EqualError(err, "chart is required", "Chart should be mandatory")
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
u.release = ""
|
|
|
|
u.chart = "billboard_top_zero"
|
2019-12-17 00:55:54 +00:00
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
err = u.Prepare()
|
2019-12-17 00:55:54 +00:00
|
|
|
suite.EqualError(err, "release is required", "Release should be mandatory")
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:25:47 +00:00
|
|
|
func (suite *UpgradeTestSuite) TestPrepareDebugFlag() {
|
|
|
|
stdout := strings.Builder{}
|
|
|
|
stderr := strings.Builder{}
|
2021-05-19 17:16:43 +00:00
|
|
|
|
|
|
|
cfg := env.NewTestConfig(suite.T())
|
|
|
|
cfg.Chart = "at40"
|
|
|
|
cfg.Release = "lewis_capaldi_someone_you_loved"
|
|
|
|
cfg.Debug = true
|
|
|
|
cfg.Stdout = &stdout
|
|
|
|
cfg.Stderr = &stderr
|
|
|
|
|
|
|
|
u := NewUpgrade(*cfg)
|
2019-12-10 00:25:47 +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(&stdout)
|
|
|
|
suite.mockCmd.EXPECT().
|
|
|
|
Stderr(&stderr)
|
|
|
|
|
2020-01-17 18:13:53 +00:00
|
|
|
u.Prepare()
|
2019-12-10 00:25:47 +00:00
|
|
|
|
2021-05-19 17:16:43 +00:00
|
|
|
want := fmt.Sprintf(
|
|
|
|
"Generated command: '%s --debug upgrade --install --history-max=10 lewis_capaldi_someone_you_loved at40'\n",
|
|
|
|
helmBin,
|
|
|
|
)
|
2019-12-10 00:25:47 +00:00
|
|
|
suite.Equal(want, stderr.String())
|
|
|
|
suite.Equal("", stdout.String())
|
2019-12-05 22:35:25 +00:00
|
|
|
}
|
2021-07-20 18:21:44 +00:00
|
|
|
|
|
|
|
func (suite *UpgradeTestSuite) TestPrepareSkipCrdsFlag() {
|
|
|
|
defer suite.ctrl.Finish()
|
|
|
|
|
2022-08-16 18:42:02 +00:00
|
|
|
cfg := env.NewTestConfig(suite.T())
|
|
|
|
cfg.Chart = "at40"
|
|
|
|
cfg.Release = "cabbages_smell_great"
|
|
|
|
cfg.SkipCrds = true
|
|
|
|
|
|
|
|
u := NewUpgrade(*cfg)
|
2021-07-20 18:21:44 +00:00
|
|
|
|
|
|
|
command = func(path string, args ...string) cmd {
|
|
|
|
suite.Equal(helmBin, path)
|
2022-08-16 18:42:02 +00:00
|
|
|
suite.Equal([]string{"upgrade", "--install", "--skip-crds", "--history-max=10", "cabbages_smell_great", "at40"}, args)
|
2021-07-20 18:21:44 +00:00
|
|
|
|
|
|
|
return suite.mockCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any())
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any())
|
|
|
|
|
|
|
|
err := u.Prepare()
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
}
|