Extract Plan's InitKube creation [#4]

It's `func initKube` rather than `var initKube = func` because initKube
is not meant to be returned by determineSteps.
This commit is contained in:
Erin Call 2019-12-18 17:13:17 -08:00
parent f398ee5724
commit 5de156f823
No known key found for this signature in database
GPG key ID: 4071FF6C15B8DAD1
2 changed files with 51 additions and 48 deletions

View file

@ -93,16 +93,7 @@ func (p *Plan) Execute() error {
} }
var upgrade = func(cfg Config) []Step { var upgrade = func(cfg Config) []Step {
steps := make([]Step, 0) steps := initKube(cfg)
steps = append(steps, &run.InitKube{
SkipTLSVerify: cfg.SkipTLSVerify,
Certificate: cfg.Certificate,
APIServer: cfg.APIServer,
ServiceAccount: cfg.ServiceAccount,
Token: cfg.KubeToken,
TemplateFile: kubeConfigTemplate,
})
steps = append(steps, &run.Upgrade{ steps = append(steps, &run.Upgrade{
Chart: cfg.Chart, Chart: cfg.Chart,
@ -119,6 +110,21 @@ var upgrade = func(cfg Config) []Step {
} }
var del = func(cfg Config) []Step { var del = func(cfg Config) []Step {
steps := initKube(cfg)
steps = append(steps, &run.Delete{
Release: cfg.Release,
DryRun: cfg.DryRun,
})
return steps
}
var help = func(cfg Config) []Step {
help := &run.Help{}
return []Step{help}
}
func initKube(cfg Config) []Step {
return []Step{ return []Step{
&run.InitKube{ &run.InitKube{
SkipTLSVerify: cfg.SkipTLSVerify, SkipTLSVerify: cfg.SkipTLSVerify,
@ -128,14 +134,5 @@ var del = func(cfg Config) []Step {
Token: cfg.KubeToken, Token: cfg.KubeToken,
TemplateFile: kubeConfigTemplate, TemplateFile: kubeConfigTemplate,
}, },
&run.Delete{
Release: cfg.Release,
DryRun: cfg.DryRun,
},
} }
} }
var help = func(cfg Config) []Step {
help := &run.Help{}
return []Step{help}
}

View file

@ -87,11 +87,6 @@ func (suite *PlanTestSuite) TestNewPlanAbortsOnError() {
func (suite *PlanTestSuite) TestUpgrade() { func (suite *PlanTestSuite) TestUpgrade() {
cfg := Config{ cfg := Config{
KubeToken: "cXVlZXIgY2hhcmFjdGVyCg==",
SkipTLSVerify: true,
Certificate: "b2Ygd29rZW5lc3MK",
APIServer: "123.456.78.9",
ServiceAccount: "helmet",
ChartVersion: "seventeen", ChartVersion: "seventeen",
DryRun: true, DryRun: true,
Wait: true, Wait: true,
@ -103,27 +98,13 @@ func (suite *PlanTestSuite) TestUpgrade() {
} }
steps := upgrade(cfg) steps := upgrade(cfg)
suite.Require().Equal(2, len(steps), "upgrade should return 2 steps")
suite.Equal(2, len(steps))
suite.Require().IsType(&run.InitKube{}, steps[0]) suite.Require().IsType(&run.InitKube{}, steps[0])
init, _ := steps[0].(*run.InitKube)
var expected Step = &run.InitKube{
SkipTLSVerify: cfg.SkipTLSVerify,
Certificate: cfg.Certificate,
APIServer: cfg.APIServer,
ServiceAccount: cfg.ServiceAccount,
Token: cfg.KubeToken,
TemplateFile: kubeConfigTemplate,
}
suite.Equal(expected, init)
suite.Require().IsType(&run.Upgrade{}, steps[1]) suite.Require().IsType(&run.Upgrade{}, steps[1])
upgrade, _ := steps[1].(*run.Upgrade) upgrade, _ := steps[1].(*run.Upgrade)
expected = &run.Upgrade{ expected := &run.Upgrade{
Chart: cfg.Chart, Chart: cfg.Chart,
Release: cfg.Release, Release: cfg.Release,
ChartVersion: cfg.ChartVersion, ChartVersion: cfg.ChartVersion,
@ -174,6 +155,31 @@ func (suite *PlanTestSuite) TestDel() {
suite.Equal(expected, actual) suite.Equal(expected, actual)
} }
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,
}
suite.Equal(expected, init)
}
func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() { func (suite *PlanTestSuite) TestDeterminePlanUpgradeCommand() {
cfg := Config{ cfg := Config{
Command: "upgrade", Command: "upgrade",