From c22492ff016ea85d7b05be013ecd4458b00b02ae Mon Sep 17 00:00:00 2001 From: Arash Deshmeh Date: Mon, 9 Apr 2018 12:18:23 -0400 Subject: [PATCH] fix(helm): refactor tiller release install unit tests using chart and install request stubs Signed-off-by: Arash Deshmeh --- pkg/tiller/release_install_test.go | 150 +++++++---------------------- pkg/tiller/release_server_test.go | 125 ++++++++++++++++++++++-- 2 files changed, 148 insertions(+), 127 deletions(-) diff --git a/pkg/tiller/release_install_test.go b/pkg/tiller/release_install_test.go index 8e2238e74..2f21dc46b 100644 --- a/pkg/tiller/release_install_test.go +++ b/pkg/tiller/release_install_test.go @@ -22,7 +22,6 @@ import ( "testing" "k8s.io/helm/pkg/helm" - "k8s.io/helm/pkg/proto/hapi/chart" "k8s.io/helm/pkg/proto/hapi/release" "k8s.io/helm/pkg/proto/hapi/services" "k8s.io/helm/pkg/version" @@ -32,17 +31,7 @@ func TestInstallRelease(t *testing.T) { c := helm.NewContext() rs := rsFixture() - // TODO: Refactor this into a mock. - req := &services.InstallReleaseRequest{ - Namespace: "spaced", - Chart: &chart.Chart{ - Metadata: &chart.Metadata{Name: "hello"}, - Templates: []*chart.Template{ - {Name: "templates/hello", Data: []byte("hello: world")}, - {Name: "templates/hooks", Data: []byte(manifestWithHook)}, - }, - }, - } + req := installRequest() res, err := rs.InstallRelease(c, req) if err != nil { t.Fatalf("Failed install: %s", err) @@ -96,18 +85,9 @@ func TestInstallRelease_WithNotes(t *testing.T) { c := helm.NewContext() rs := rsFixture() - // TODO: Refactor this into a mock. - req := &services.InstallReleaseRequest{ - Namespace: "spaced", - Chart: &chart.Chart{ - Metadata: &chart.Metadata{Name: "hello"}, - Templates: []*chart.Template{ - {Name: "templates/hello", Data: []byte("hello: world")}, - {Name: "templates/hooks", Data: []byte(manifestWithHook)}, - {Name: "templates/NOTES.txt", Data: []byte(notesText)}, - }, - }, - } + req := installRequest( + withChart(withNotes(notesText)), + ) res, err := rs.InstallRelease(c, req) if err != nil { t.Fatalf("Failed install: %s", err) @@ -165,18 +145,9 @@ func TestInstallRelease_WithNotesRendered(t *testing.T) { c := helm.NewContext() rs := rsFixture() - // TODO: Refactor this into a mock. - req := &services.InstallReleaseRequest{ - Namespace: "spaced", - Chart: &chart.Chart{ - Metadata: &chart.Metadata{Name: "hello"}, - Templates: []*chart.Template{ - {Name: "templates/hello", Data: []byte("hello: world")}, - {Name: "templates/hooks", Data: []byte(manifestWithHook)}, - {Name: "templates/NOTES.txt", Data: []byte(notesText + " {{.Release.Name}}")}, - }, - }, - } + req := installRequest( + withChart(withNotes(notesText + " {{.Release.Name}}")), + ) res, err := rs.InstallRelease(c, req) if err != nil { t.Fatalf("Failed install: %s", err) @@ -236,17 +207,9 @@ func TestInstallRelease_TillerVersion(t *testing.T) { c := helm.NewContext() rs := rsFixture() - // TODO: Refactor this into a mock. - req := &services.InstallReleaseRequest{ - Namespace: "spaced", - Chart: &chart.Chart{ - Metadata: &chart.Metadata{Name: "hello", TillerVersion: ">=2.2.0"}, - Templates: []*chart.Template{ - {Name: "templates/hello", Data: []byte("hello: world")}, - {Name: "templates/hooks", Data: []byte(manifestWithHook)}, - }, - }, - } + req := installRequest( + withChart(withTiller(">=2.2.0")), + ) _, err := rs.InstallRelease(c, req) if err != nil { t.Fatalf("Expected valid range. Got %q", err) @@ -258,17 +221,9 @@ func TestInstallRelease_WrongTillerVersion(t *testing.T) { c := helm.NewContext() rs := rsFixture() - // TODO: Refactor this into a mock. - req := &services.InstallReleaseRequest{ - Namespace: "spaced", - Chart: &chart.Chart{ - Metadata: &chart.Metadata{Name: "hello", TillerVersion: "<2.0.0"}, - Templates: []*chart.Template{ - {Name: "templates/hello", Data: []byte("hello: world")}, - {Name: "templates/hooks", Data: []byte(manifestWithHook)}, - }, - }, - } + req := installRequest( + withChart(withTiller("<2.0.0")), + ) _, err := rs.InstallRelease(c, req) if err == nil { t.Fatalf("Expected to fail because of wrong version") @@ -284,29 +239,10 @@ func TestInstallRelease_WithChartAndDependencyNotes(t *testing.T) { c := helm.NewContext() rs := rsFixture() - // TODO: Refactor this into a mock. - req := &services.InstallReleaseRequest{ - Namespace: "spaced", - Chart: &chart.Chart{ - Metadata: &chart.Metadata{Name: "hello"}, - Templates: []*chart.Template{ - {Name: "templates/hello", Data: []byte("hello: world")}, - {Name: "templates/hooks", Data: []byte(manifestWithHook)}, - {Name: "templates/NOTES.txt", Data: []byte(notesText)}, - }, - Dependencies: []*chart.Chart{ - { - Metadata: &chart.Metadata{Name: "hello"}, - Templates: []*chart.Template{ - {Name: "templates/hello", Data: []byte("hello: world")}, - {Name: "templates/hooks", Data: []byte(manifestWithHook)}, - {Name: "templates/NOTES.txt", Data: []byte(notesText + " child")}, - }, - }, - }, - }, - } - + req := installRequest(withChart( + withNotes(notesText), + withDependency(withNotes(notesText+" child")), + )) res, err := rs.InstallRelease(c, req) if err != nil { t.Fatalf("Failed install: %s", err) @@ -335,10 +271,9 @@ func TestInstallRelease_DryRun(t *testing.T) { c := helm.NewContext() rs := rsFixture() - req := &services.InstallReleaseRequest{ - Chart: chartStub(), - DryRun: true, - } + req := installRequest(withDryRun(), + withChart(withSampleTemplates()), + ) res, err := rs.InstallRelease(c, req) if err != nil { t.Errorf("Failed install: %s", err) @@ -389,10 +324,7 @@ func TestInstallRelease_NoHooks(t *testing.T) { rs := rsFixture() rs.env.Releases.Create(releaseStub()) - req := &services.InstallReleaseRequest{ - Chart: chartStub(), - DisableHooks: true, - } + req := installRequest(withDisabledHooks()) res, err := rs.InstallRelease(c, req) if err != nil { t.Errorf("Failed install: %s", err) @@ -409,9 +341,7 @@ func TestInstallRelease_FailedHooks(t *testing.T) { rs.env.Releases.Create(releaseStub()) rs.env.KubeClient = newHookFailingKubeClient() - req := &services.InstallReleaseRequest{ - Chart: chartStub(), - } + req := installRequest() res, err := rs.InstallRelease(c, req) if err == nil { t.Error("Expected failed install") @@ -429,11 +359,10 @@ func TestInstallRelease_ReuseName(t *testing.T) { rel.Info.Status.Code = release.Status_DELETED rs.env.Releases.Create(rel) - req := &services.InstallReleaseRequest{ - Chart: chartStub(), - ReuseName: true, - Name: rel.Name, - } + req := installRequest( + withReuseName(), + withName(rel.Name), + ) res, err := rs.InstallRelease(c, req) if err != nil { t.Fatalf("Failed install: %s", err) @@ -457,16 +386,9 @@ func TestInstallRelease_KubeVersion(t *testing.T) { c := helm.NewContext() rs := rsFixture() - // TODO: Refactor this into a mock. - req := &services.InstallReleaseRequest{ - Chart: &chart.Chart{ - Metadata: &chart.Metadata{Name: "hello", KubeVersion: ">=0.0.0"}, - Templates: []*chart.Template{ - {Name: "templates/hello", Data: []byte("hello: world")}, - {Name: "templates/hooks", Data: []byte(manifestWithHook)}, - }, - }, - } + req := installRequest( + withChart(withKube(">=0.0.0")), + ) _, err := rs.InstallRelease(c, req) if err != nil { t.Fatalf("Expected valid range. Got %q", err) @@ -477,16 +399,10 @@ func TestInstallRelease_WrongKubeVersion(t *testing.T) { c := helm.NewContext() rs := rsFixture() - // TODO: Refactor this into a mock. - req := &services.InstallReleaseRequest{ - Chart: &chart.Chart{ - Metadata: &chart.Metadata{Name: "hello", KubeVersion: ">=5.0.0"}, - Templates: []*chart.Template{ - {Name: "templates/hello", Data: []byte("hello: world")}, - {Name: "templates/hooks", Data: []byte(manifestWithHook)}, - }, - }, - } + req := installRequest( + withChart(withKube(">=5.0.0")), + ) + _, err := rs.InstallRelease(c, req) if err == nil { t.Fatalf("Expected to fail because of wrong version") diff --git a/pkg/tiller/release_server_test.go b/pkg/tiller/release_server_test.go index 6c4d42e04..73ba08f67 100644 --- a/pkg/tiller/release_server_test.go +++ b/pkg/tiller/release_server_test.go @@ -105,23 +105,128 @@ func rsFixture() *ReleaseServer { } } -// chartStub creates a fully stubbed out chart. -func chartStub() *chart.Chart { - return &chart.Chart{ - // TODO: This should be more complete. - Metadata: &chart.Metadata{ - Name: "hello", +type chartOptions struct { + *chart.Chart +} + +type chartOption func(*chartOptions) + +func buildChart(opts ...chartOption) *chart.Chart { + c := &chartOptions{ + Chart: &chart.Chart{ + // TODO: This should be more complete. + Metadata: &chart.Metadata{ + Name: "hello", + }, + // This adds a basic template and hooks. + Templates: []*chart.Template{ + {Name: "templates/hello", Data: []byte("hello: world")}, + {Name: "templates/hooks", Data: []byte(manifestWithHook)}, + }, }, - // This adds basic templates, partials, and hooks. - Templates: []*chart.Template{ - {Name: "templates/hello", Data: []byte("hello: world")}, + } + + for _, opt := range opts { + opt(c) + } + + return c.Chart +} + +func withKube(version string) chartOption { + return func(opts *chartOptions) { + opts.Metadata.KubeVersion = version + } +} + +func withTiller(version string) chartOption { + return func(opts *chartOptions) { + opts.Metadata.TillerVersion = version + } +} + +func withDependency(dependencyOpts ...chartOption) chartOption { + return func(opts *chartOptions) { + opts.Dependencies = append(opts.Dependencies, buildChart(dependencyOpts...)) + } +} + +func withNotes(notes string) chartOption { + return func(opts *chartOptions) { + opts.Templates = append(opts.Templates, &chart.Template{ + Name: "templates/NOTES.txt", + Data: []byte(notes), + }) + } +} + +func withSampleTemplates() chartOption { + return func(opts *chartOptions) { + sampleTemplates := []*chart.Template{ + // This adds basic templates and partials. {Name: "templates/goodbye", Data: []byte("goodbye: world")}, {Name: "templates/empty", Data: []byte("")}, {Name: "templates/with-partials", Data: []byte(`hello: {{ template "_planet" . }}`)}, {Name: "templates/partials/_planet", Data: []byte(`{{define "_planet"}}Earth{{end}}`)}, - {Name: "templates/hooks", Data: []byte(manifestWithHook)}, + } + opts.Templates = append(opts.Templates, sampleTemplates...) + } +} + +type installOptions struct { + *services.InstallReleaseRequest +} + +type installOption func(*installOptions) + +func withName(name string) installOption { + return func(opts *installOptions) { + opts.Name = name + } +} + +func withDryRun() installOption { + return func(opts *installOptions) { + opts.DryRun = true + } +} + +func withDisabledHooks() installOption { + return func(opts *installOptions) { + opts.DisableHooks = true + } +} + +func withReuseName() installOption { + return func(opts *installOptions) { + opts.ReuseName = true + } +} + +func withChart(chartOpts ...chartOption) installOption { + return func(opts *installOptions) { + opts.Chart = buildChart(chartOpts...) + } +} + +func installRequest(opts ...installOption) *services.InstallReleaseRequest { + reqOpts := &installOptions{ + &services.InstallReleaseRequest{ + Namespace: "spaced", + Chart: buildChart(), }, } + + for _, opt := range opts { + opt(reqOpts) + } + + return reqOpts.InstallReleaseRequest +} + +// chartStub creates a fully stubbed out chart. +func chartStub() *chart.Chart { + return buildChart(withSampleTemplates()) } // releaseStub creates a release stub, complete with the chartStub as its chart.