2019-12-05 22:35:25 +00:00
|
|
|
package run
|
|
|
|
|
|
|
|
import (
|
2019-12-10 00:25:47 +00:00
|
|
|
"fmt"
|
2019-12-05 22:35:25 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2019-12-10 00:25:47 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"strings"
|
2019-12-05 22:35:25 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2019-12-17 00:55:05 +00:00
|
|
|
func (suite *UpgradeTestSuite) TestPrepareAndExecute() {
|
2019-12-10 00:25:47 +00:00
|
|
|
defer suite.ctrl.Finish()
|
|
|
|
|
|
|
|
u := Upgrade{
|
|
|
|
Chart: "at40",
|
|
|
|
Release: "jonas_brothers_only_human",
|
|
|
|
}
|
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)
|
2019-12-23 20:43:17 +00:00
|
|
|
suite.Equal([]string{"upgrade", "--install", "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)
|
|
|
|
|
2019-12-23 20:43:17 +00:00
|
|
|
cfg := Config{}
|
2019-12-12 18:20:11 +00:00
|
|
|
err := u.Prepare(cfg)
|
|
|
|
suite.Require().Nil(err)
|
|
|
|
u.Execute(cfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *UpgradeTestSuite) TestPrepareNamespaceFlag() {
|
|
|
|
defer suite.ctrl.Finish()
|
|
|
|
|
|
|
|
u := Upgrade{
|
|
|
|
Chart: "at40",
|
|
|
|
Release: "shaed_trampoline",
|
|
|
|
}
|
|
|
|
|
|
|
|
command = func(path string, args ...string) cmd {
|
|
|
|
suite.Equal(helmBin, path)
|
2019-12-23 20:43:17 +00:00
|
|
|
suite.Equal([]string{"--namespace", "melt", "upgrade", "--install", "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
|
|
|
|
|
|
|
cfg := Config{
|
2019-12-23 20:43:17 +00:00
|
|
|
Namespace: "melt",
|
2019-12-12 18:20:11 +00:00
|
|
|
}
|
|
|
|
err := u.Prepare(cfg)
|
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()
|
|
|
|
|
|
|
|
u := Upgrade{
|
|
|
|
Chart: "hot_ac",
|
|
|
|
Release: "maroon_5_memories",
|
|
|
|
ChartVersion: "radio_edit", //-version
|
|
|
|
DryRun: true, //-run
|
|
|
|
Wait: true, //-wait
|
|
|
|
ReuseValues: true, //-values
|
|
|
|
Timeout: "sit_in_the_corner", //-timeout
|
|
|
|
Force: true, //-force
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := Config{
|
|
|
|
Values: "age=35",
|
|
|
|
StringValues: "height=5ft10in",
|
|
|
|
ValuesFiles: []string{"/usr/local/stats", "/usr/local/grades"},
|
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
|
|
|
"--set", "age=35",
|
|
|
|
"--set-string", "height=5ft10in",
|
|
|
|
"--values", "/usr/local/stats",
|
|
|
|
"--values", "/usr/local/grades",
|
|
|
|
"maroon_5_memories", "hot_ac"}, args)
|
|
|
|
|
|
|
|
return suite.mockCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.mockCmd.EXPECT().Stdout(gomock.Any())
|
|
|
|
suite.mockCmd.EXPECT().Stderr(gomock.Any())
|
|
|
|
|
|
|
|
err := u.Prepare(cfg)
|
|
|
|
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()
|
|
|
|
|
|
|
|
u := Upgrade{
|
|
|
|
Release: "seth_everman_unskippable_cutscene",
|
|
|
|
}
|
|
|
|
|
|
|
|
err := u.Prepare(Config{})
|
|
|
|
suite.EqualError(err, "chart is required", "Chart should be mandatory")
|
|
|
|
|
|
|
|
u = Upgrade{
|
|
|
|
Chart: "billboard_top_zero",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = u.Prepare(Config{})
|
|
|
|
suite.EqualError(err, "release is required", "Release should be mandatory")
|
|
|
|
}
|
|
|
|
|
2019-12-10 00:25:47 +00:00
|
|
|
func (suite *UpgradeTestSuite) TestPrepareDebugFlag() {
|
|
|
|
u := Upgrade{
|
|
|
|
Chart: "at40",
|
|
|
|
Release: "lewis_capaldi_someone_you_loved",
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout := strings.Builder{}
|
|
|
|
stderr := strings.Builder{}
|
|
|
|
cfg := Config{
|
2019-12-23 20:43:17 +00:00
|
|
|
Debug: true,
|
|
|
|
Stdout: &stdout,
|
|
|
|
Stderr: &stderr,
|
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)
|
|
|
|
|
|
|
|
u.Prepare(cfg)
|
|
|
|
|
2019-12-23 20:43:17 +00:00
|
|
|
want := fmt.Sprintf("Generated command: '%s --debug upgrade "+
|
2019-12-12 18:20:11 +00:00
|
|
|
"--install 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
|
|
|
}
|