fix(helm): refactor tiller release update tests to reduce duplication

Signed-off-by: Arash Deshmeh <adeshmeh@ca.ibm.com>
pull/3888/head
Arash Deshmeh 8 years ago
parent ad714fe704
commit 2456ccce76

@ -160,6 +160,39 @@ func withNotes(notes string) chartOption {
} }
} }
func withChartValues(raw string) chartOption {
return func(opts *chartOptions) {
opts.Values = &chart.Config{Raw: raw}
}
}
func withHooks(hooks string) chartOption {
return func(opts *chartOptions) {
found := false
for _, t := range opts.Templates {
if t.Name == "templates/hooks" {
t.Data = []byte(hooks)
found = true
}
}
if !found {
opts.Templates = append(opts.Templates, &chart.Template{
Name: "templates/hooks",
Data: []byte(hooks),
})
}
}
}
func withTemplate(name, data string) chartOption {
return func(opts *chartOptions) {
opts.Templates = []*chart.Template{
{Name: name, Data: []byte(data)},
}
}
}
func withSampleTemplates() chartOption { func withSampleTemplates() chartOption {
return func(opts *chartOptions) { return func(opts *chartOptions) {
sampleTemplates := []*chart.Template{ sampleTemplates := []*chart.Template{
@ -209,6 +242,12 @@ func withChart(chartOpts ...chartOption) installOption {
} }
} }
func withValues(raw string) installOption {
return func(opts *installOptions) {
opts.Values = &chart.Config{Raw: raw}
}
}
func installRequest(opts ...installOption) *services.InstallReleaseRequest { func installRequest(opts ...installOption) *services.InstallReleaseRequest {
reqOpts := &installOptions{ reqOpts := &installOptions{
&services.InstallReleaseRequest{ &services.InstallReleaseRequest{

@ -29,22 +29,70 @@ import (
"k8s.io/helm/pkg/proto/hapi/services" "k8s.io/helm/pkg/proto/hapi/services"
) )
type updateOptions struct {
*services.UpdateReleaseRequest
}
type updateOption func(*updateOptions)
func updateRequest(name string, opts ...updateOption) *services.UpdateReleaseRequest {
reqOpts := &updateOptions{
&services.UpdateReleaseRequest{
Name: name,
Chart: buildChart(withHooks(manifestWithUpgradeHooks)),
},
}
for _, opt := range opts {
opt(reqOpts)
}
return reqOpts.UpdateReleaseRequest
}
func withReuseValues() updateOption {
return func(opts *updateOptions) {
opts.ReuseValues = true
}
}
func withResetValues() updateOption {
return func(opts *updateOptions) {
opts.ResetValues = true
}
}
func withUpdateDisabledHooks() updateOption {
return func(opts *updateOptions) {
opts.DisableHooks = true
}
}
func withForce() updateOption {
return func(opts *updateOptions) {
opts.Force = true
}
}
func withUpdateValues(raw string) updateOption {
return func(opts *updateOptions) {
opts.Values = &chart.Config{Raw: raw}
}
}
func withUpdateChart(chartOpts ...chartOption) updateOption {
return func(opts *updateOptions) {
opts.Chart = buildChart(chartOpts...)
}
}
func TestUpdateRelease(t *testing.T) { func TestUpdateRelease(t *testing.T) {
c := helm.NewContext() c := helm.NewContext()
rs := rsFixture() rs := rsFixture()
rel := releaseStub() rel := releaseStub()
rs.env.Releases.Create(rel) rs.env.Releases.Create(rel)
req := &services.UpdateReleaseRequest{ req := updateRequest(rel.Name)
Name: rel.Name,
Chart: &chart.Chart{
Metadata: &chart.Metadata{Name: "hello"},
Templates: []*chart.Template{
{Name: "templates/hello", Data: []byte("hello: world")},
{Name: "templates/hooks", Data: []byte(manifestWithUpgradeHooks)},
},
},
}
res, err := rs.UpdateRelease(c, req) res, err := rs.UpdateRelease(c, req)
if err != nil { if err != nil {
t.Fatalf("Failed updated: %s", err) t.Fatalf("Failed updated: %s", err)
@ -108,17 +156,7 @@ func TestUpdateRelease_ResetValues(t *testing.T) {
rel := releaseStub() rel := releaseStub()
rs.env.Releases.Create(rel) rs.env.Releases.Create(rel)
req := &services.UpdateReleaseRequest{ req := updateRequest(rel.Name, withResetValues())
Name: rel.Name,
Chart: &chart.Chart{
Metadata: &chart.Metadata{Name: "hello"},
Templates: []*chart.Template{
{Name: "templates/hello", Data: []byte("hello: world")},
{Name: "templates/hooks", Data: []byte(manifestWithUpgradeHooks)},
},
},
ResetValues: true,
}
res, err := rs.UpdateRelease(c, req) res, err := rs.UpdateRelease(c, req)
if err != nil { if err != nil {
t.Fatalf("Failed updated: %s", err) t.Fatalf("Failed updated: %s", err)
@ -134,18 +172,10 @@ func TestUpdateRelease_ComplexReuseValues(t *testing.T) {
c := helm.NewContext() c := helm.NewContext()
rs := rsFixture() rs := rsFixture()
installReq := &services.InstallReleaseRequest{ installReq := installRequest(
Namespace: "spaced", withChart(withChartValues("defaultFoo: defaultBar")),
Chart: &chart.Chart{ withValues("foo: bar"),
Metadata: &chart.Metadata{Name: "hello"}, )
Templates: []*chart.Template{
{Name: "templates/hello", Data: []byte("hello: world")},
{Name: "templates/hooks", Data: []byte(manifestWithHook)},
},
Values: &chart.Config{Raw: "defaultFoo: defaultBar"},
},
Values: &chart.Config{Raw: "foo: bar"},
}
fmt.Println("Running Install release with foo: bar override") fmt.Println("Running Install release with foo: bar override")
installResp, err := rs.InstallRelease(c, installReq) installResp, err := rs.InstallRelease(c, installReq)
@ -154,17 +184,7 @@ func TestUpdateRelease_ComplexReuseValues(t *testing.T) {
} }
rel := installResp.Release rel := installResp.Release
req := &services.UpdateReleaseRequest{ req := updateRequest(rel.Name, withUpdateChart(withChartValues("defaultFoo: defaultBar")))
Name: rel.Name,
Chart: &chart.Chart{
Metadata: &chart.Metadata{Name: "hello"},
Templates: []*chart.Template{
{Name: "templates/hello", Data: []byte("hello: world")},
{Name: "templates/hooks", Data: []byte(manifestWithUpgradeHooks)},
},
Values: &chart.Config{Raw: "defaultFoo: defaultBar"},
},
}
fmt.Println("Running Update release with no overrides and no reuse-values flag") fmt.Println("Running Update release with no overrides and no reuse-values flag")
res, err := rs.UpdateRelease(c, req) res, err := rs.UpdateRelease(c, req)
@ -178,19 +198,11 @@ func TestUpdateRelease_ComplexReuseValues(t *testing.T) {
} }
rel = res.Release rel = res.Release
req = &services.UpdateReleaseRequest{ req = updateRequest(rel.Name,
Name: rel.Name, withUpdateChart(withChartValues("defaultFoo: defaultBar")),
Chart: &chart.Chart{ withUpdateValues("foo2: bar2"),
Metadata: &chart.Metadata{Name: "hello"}, withReuseValues(),
Templates: []*chart.Template{ )
{Name: "templates/hello", Data: []byte("hello: world")},
{Name: "templates/hooks", Data: []byte(manifestWithUpgradeHooks)},
},
Values: &chart.Config{Raw: "defaultFoo: defaultBar"},
},
Values: &chart.Config{Raw: "foo2: bar2"},
ReuseValues: true,
}
fmt.Println("Running Update release with foo2: bar2 override and reuse-values") fmt.Println("Running Update release with foo2: bar2 override and reuse-values")
res, err = rs.UpdateRelease(c, req) res, err = rs.UpdateRelease(c, req)
@ -205,19 +217,11 @@ func TestUpdateRelease_ComplexReuseValues(t *testing.T) {
} }
rel = res.Release rel = res.Release
req = &services.UpdateReleaseRequest{ req = updateRequest(rel.Name,
Name: rel.Name, withUpdateChart(withChartValues("defaultFoo: defaultBar")),
Chart: &chart.Chart{ withUpdateValues("foo: baz"),
Metadata: &chart.Metadata{Name: "hello"}, withReuseValues(),
Templates: []*chart.Template{ )
{Name: "templates/hello", Data: []byte("hello: world")},
{Name: "templates/hooks", Data: []byte(manifestWithUpgradeHooks)},
},
Values: &chart.Config{Raw: "defaultFoo: defaultBar"},
},
Values: &chart.Config{Raw: "foo: baz"},
ReuseValues: true,
}
fmt.Println("Running Update release with foo=baz override with reuse-values flag") fmt.Println("Running Update release with foo=baz override with reuse-values flag")
res, err = rs.UpdateRelease(c, req) res, err = rs.UpdateRelease(c, req)
@ -236,20 +240,12 @@ func TestUpdateRelease_ReuseValues(t *testing.T) {
rel := releaseStub() rel := releaseStub()
rs.env.Releases.Create(rel) rs.env.Releases.Create(rel)
req := &services.UpdateReleaseRequest{ req := updateRequest(rel.Name,
Name: rel.Name,
Chart: &chart.Chart{
Metadata: &chart.Metadata{Name: "hello"},
Templates: []*chart.Template{
{Name: "templates/hello", Data: []byte("hello: world")},
{Name: "templates/hooks", Data: []byte(manifestWithUpgradeHooks)},
},
// Since reuseValues is set, this should get ignored. // Since reuseValues is set, this should get ignored.
Values: &chart.Config{Raw: "foo: bar\n"}, withUpdateChart(withChartValues("foo: bar\n")),
}, withUpdateValues("name2: val2"),
Values: &chart.Config{Raw: "name2: val2"}, withReuseValues(),
ReuseValues: true, )
}
res, err := rs.UpdateRelease(c, req) res, err := rs.UpdateRelease(c, req)
if err != nil { if err != nil {
t.Fatalf("Failed updated: %s", err) t.Fatalf("Failed updated: %s", err)
@ -274,18 +270,7 @@ func TestUpdateRelease_ResetReuseValues(t *testing.T) {
rel := releaseStub() rel := releaseStub()
rs.env.Releases.Create(rel) rs.env.Releases.Create(rel)
req := &services.UpdateReleaseRequest{ req := updateRequest(rel.Name, withResetValues(), withReuseValues())
Name: rel.Name,
Chart: &chart.Chart{
Metadata: &chart.Metadata{Name: "hello"},
Templates: []*chart.Template{
{Name: "templates/hello", Data: []byte("hello: world")},
{Name: "templates/hooks", Data: []byte(manifestWithUpgradeHooks)},
},
},
ResetValues: true,
ReuseValues: true,
}
res, err := rs.UpdateRelease(c, req) res, err := rs.UpdateRelease(c, req)
if err != nil { if err != nil {
t.Fatalf("Failed updated: %s", err) t.Fatalf("Failed updated: %s", err)
@ -305,16 +290,10 @@ func TestUpdateReleaseFailure(t *testing.T) {
rs.env.KubeClient = newUpdateFailingKubeClient() rs.env.KubeClient = newUpdateFailingKubeClient()
rs.Log = t.Logf rs.Log = t.Logf
req := &services.UpdateReleaseRequest{ req := updateRequest(rel.Name,
Name: rel.Name, withUpdateDisabledHooks(),
DisableHooks: true, withUpdateChart(withTemplate("templates/something", "hello: world")),
Chart: &chart.Chart{ )
Metadata: &chart.Metadata{Name: "hello"},
Templates: []*chart.Template{
{Name: "templates/something", Data: []byte("hello: world")},
},
},
}
res, err := rs.UpdateRelease(c, req) res, err := rs.UpdateRelease(c, req)
if err == nil { if err == nil {
@ -348,17 +327,11 @@ func TestUpdateReleaseFailure_Force(t *testing.T) {
rs.env.Releases.Create(rel) rs.env.Releases.Create(rel)
rs.Log = t.Logf rs.Log = t.Logf
req := &services.UpdateReleaseRequest{ req := updateRequest(rel.Name,
Name: rel.Name, withUpdateDisabledHooks(),
DisableHooks: true, withUpdateChart(withTemplate("templates/something", "text: 'Did you ever hear the tragedy of Darth Plagueis the Wise? I thought not. Its not a story the Jedi would tell you. Its a Sith legend. Darth Plagueis was a Dark Lord of the Sith, so powerful and so wise he could use the Force to influence the Midichlorians to create life... He had such a knowledge of the Dark Side that he could even keep the ones he cared about from dying. The Dark Side of the Force is a pathway to many abilities some consider to be unnatural. He became so powerful... The only thing he was afraid of was losing his power, which eventually, of course, he did. Unfortunately, he taught his apprentice everything he knew, then his apprentice killed him in his sleep. Ironic. He could save others from death, but not himself.'")),
Chart: &chart.Chart{ withForce(),
Metadata: &chart.Metadata{Name: "hello"}, )
Templates: []*chart.Template{
{Name: "templates/something", Data: []byte("text: 'Did you ever hear the tragedy of Darth Plagueis the Wise? I thought not. Its not a story the Jedi would tell you. Its a Sith legend. Darth Plagueis was a Dark Lord of the Sith, so powerful and so wise he could use the Force to influence the Midichlorians to create life... He had such a knowledge of the Dark Side that he could even keep the ones he cared about from dying. The Dark Side of the Force is a pathway to many abilities some consider to be unnatural. He became so powerful... The only thing he was afraid of was losing his power, which eventually, of course, he did. Unfortunately, he taught his apprentice everything he knew, then his apprentice killed him in his sleep. Ironic. He could save others from death, but not himself.'")},
},
},
Force: true,
}
res, err := rs.UpdateRelease(c, req) res, err := rs.UpdateRelease(c, req)
if err != nil { if err != nil {
@ -391,17 +364,7 @@ func TestUpdateReleaseNoHooks(t *testing.T) {
rel := releaseStub() rel := releaseStub()
rs.env.Releases.Create(rel) rs.env.Releases.Create(rel)
req := &services.UpdateReleaseRequest{ req := updateRequest(rel.Name, withUpdateDisabledHooks())
Name: rel.Name,
DisableHooks: true,
Chart: &chart.Chart{
Metadata: &chart.Metadata{Name: "hello"},
Templates: []*chart.Template{
{Name: "templates/hello", Data: []byte("hello: world")},
{Name: "templates/hooks", Data: []byte(manifestWithUpgradeHooks)},
},
},
}
res, err := rs.UpdateRelease(c, req) res, err := rs.UpdateRelease(c, req)
if err != nil { if err != nil {
@ -420,11 +383,8 @@ func TestUpdateReleaseNoChanges(t *testing.T) {
rel := releaseStub() rel := releaseStub()
rs.env.Releases.Create(rel) rs.env.Releases.Create(rel)
req := &services.UpdateReleaseRequest{ req := updateRequest(rel.Name, withUpdateDisabledHooks())
Name: rel.Name, req.Chart = rel.GetChart()
DisableHooks: true,
Chart: rel.GetChart(),
}
_, err := rs.UpdateRelease(c, req) _, err := rs.UpdateRelease(c, req)
if err != nil { if err != nil {

Loading…
Cancel
Save