Commit c22492ff authored by Arash Deshmeh's avatar Arash Deshmeh

fix(helm): refactor tiller release install unit tests using chart and install request stubs

Signed-off-by: 's avatarArash Deshmeh <adeshmeh@ca.ibm.com>
parent 4694195d
...@@ -22,7 +22,6 @@ import ( ...@@ -22,7 +22,6 @@ import (
"testing" "testing"
"k8s.io/helm/pkg/helm" "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/release"
"k8s.io/helm/pkg/proto/hapi/services" "k8s.io/helm/pkg/proto/hapi/services"
"k8s.io/helm/pkg/version" "k8s.io/helm/pkg/version"
...@@ -32,17 +31,7 @@ func TestInstallRelease(t *testing.T) { ...@@ -32,17 +31,7 @@ func TestInstallRelease(t *testing.T) {
c := helm.NewContext() c := helm.NewContext()
rs := rsFixture() rs := rsFixture()
// TODO: Refactor this into a mock. req := installRequest()
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)},
},
},
}
res, err := rs.InstallRelease(c, req) res, err := rs.InstallRelease(c, req)
if err != nil { if err != nil {
t.Fatalf("Failed install: %s", err) t.Fatalf("Failed install: %s", err)
...@@ -96,18 +85,9 @@ func TestInstallRelease_WithNotes(t *testing.T) { ...@@ -96,18 +85,9 @@ func TestInstallRelease_WithNotes(t *testing.T) {
c := helm.NewContext() c := helm.NewContext()
rs := rsFixture() rs := rsFixture()
// TODO: Refactor this into a mock. req := installRequest(
req := &services.InstallReleaseRequest{ withChart(withNotes(notesText)),
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)},
},
},
}
res, err := rs.InstallRelease(c, req) res, err := rs.InstallRelease(c, req)
if err != nil { if err != nil {
t.Fatalf("Failed install: %s", err) t.Fatalf("Failed install: %s", err)
...@@ -165,18 +145,9 @@ func TestInstallRelease_WithNotesRendered(t *testing.T) { ...@@ -165,18 +145,9 @@ func TestInstallRelease_WithNotesRendered(t *testing.T) {
c := helm.NewContext() c := helm.NewContext()
rs := rsFixture() rs := rsFixture()
// TODO: Refactor this into a mock. req := installRequest(
req := &services.InstallReleaseRequest{ withChart(withNotes(notesText + " {{.Release.Name}}")),
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}}")},
},
},
}
res, err := rs.InstallRelease(c, req) res, err := rs.InstallRelease(c, req)
if err != nil { if err != nil {
t.Fatalf("Failed install: %s", err) t.Fatalf("Failed install: %s", err)
...@@ -236,17 +207,9 @@ func TestInstallRelease_TillerVersion(t *testing.T) { ...@@ -236,17 +207,9 @@ func TestInstallRelease_TillerVersion(t *testing.T) {
c := helm.NewContext() c := helm.NewContext()
rs := rsFixture() rs := rsFixture()
// TODO: Refactor this into a mock. req := installRequest(
req := &services.InstallReleaseRequest{ withChart(withTiller(">=2.2.0")),
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)},
},
},
}
_, err := rs.InstallRelease(c, req) _, err := rs.InstallRelease(c, req)
if err != nil { if err != nil {
t.Fatalf("Expected valid range. Got %q", err) t.Fatalf("Expected valid range. Got %q", err)
...@@ -258,17 +221,9 @@ func TestInstallRelease_WrongTillerVersion(t *testing.T) { ...@@ -258,17 +221,9 @@ func TestInstallRelease_WrongTillerVersion(t *testing.T) {
c := helm.NewContext() c := helm.NewContext()
rs := rsFixture() rs := rsFixture()
// TODO: Refactor this into a mock. req := installRequest(
req := &services.InstallReleaseRequest{ withChart(withTiller("<2.0.0")),
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)},
},
},
}
_, err := rs.InstallRelease(c, req) _, err := rs.InstallRelease(c, req)
if err == nil { if err == nil {
t.Fatalf("Expected to fail because of wrong version") t.Fatalf("Expected to fail because of wrong version")
...@@ -284,29 +239,10 @@ func TestInstallRelease_WithChartAndDependencyNotes(t *testing.T) { ...@@ -284,29 +239,10 @@ func TestInstallRelease_WithChartAndDependencyNotes(t *testing.T) {
c := helm.NewContext() c := helm.NewContext()
rs := rsFixture() rs := rsFixture()
// TODO: Refactor this into a mock. req := installRequest(withChart(
req := &services.InstallReleaseRequest{ withNotes(notesText),
Namespace: "spaced", withDependency(withNotes(notesText+" child")),
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")},
},
},
},
},
}
res, err := rs.InstallRelease(c, req) res, err := rs.InstallRelease(c, req)
if err != nil { if err != nil {
t.Fatalf("Failed install: %s", err) t.Fatalf("Failed install: %s", err)
...@@ -335,10 +271,9 @@ func TestInstallRelease_DryRun(t *testing.T) { ...@@ -335,10 +271,9 @@ func TestInstallRelease_DryRun(t *testing.T) {
c := helm.NewContext() c := helm.NewContext()
rs := rsFixture() rs := rsFixture()
req := &services.InstallReleaseRequest{ req := installRequest(withDryRun(),
Chart: chartStub(), withChart(withSampleTemplates()),
DryRun: true, )
}
res, err := rs.InstallRelease(c, req) res, err := rs.InstallRelease(c, req)
if err != nil { if err != nil {
t.Errorf("Failed install: %s", err) t.Errorf("Failed install: %s", err)
...@@ -389,10 +324,7 @@ func TestInstallRelease_NoHooks(t *testing.T) { ...@@ -389,10 +324,7 @@ func TestInstallRelease_NoHooks(t *testing.T) {
rs := rsFixture() rs := rsFixture()
rs.env.Releases.Create(releaseStub()) rs.env.Releases.Create(releaseStub())
req := &services.InstallReleaseRequest{ req := installRequest(withDisabledHooks())
Chart: chartStub(),
DisableHooks: true,
}
res, err := rs.InstallRelease(c, req) res, err := rs.InstallRelease(c, req)
if err != nil { if err != nil {
t.Errorf("Failed install: %s", err) t.Errorf("Failed install: %s", err)
...@@ -409,9 +341,7 @@ func TestInstallRelease_FailedHooks(t *testing.T) { ...@@ -409,9 +341,7 @@ func TestInstallRelease_FailedHooks(t *testing.T) {
rs.env.Releases.Create(releaseStub()) rs.env.Releases.Create(releaseStub())
rs.env.KubeClient = newHookFailingKubeClient() rs.env.KubeClient = newHookFailingKubeClient()
req := &services.InstallReleaseRequest{ req := installRequest()
Chart: chartStub(),
}
res, err := rs.InstallRelease(c, req) res, err := rs.InstallRelease(c, req)
if err == nil { if err == nil {
t.Error("Expected failed install") t.Error("Expected failed install")
...@@ -429,11 +359,10 @@ func TestInstallRelease_ReuseName(t *testing.T) { ...@@ -429,11 +359,10 @@ func TestInstallRelease_ReuseName(t *testing.T) {
rel.Info.Status.Code = release.Status_DELETED rel.Info.Status.Code = release.Status_DELETED
rs.env.Releases.Create(rel) rs.env.Releases.Create(rel)
req := &services.InstallReleaseRequest{ req := installRequest(
Chart: chartStub(), withReuseName(),
ReuseName: true, withName(rel.Name),
Name: rel.Name, )
}
res, err := rs.InstallRelease(c, req) res, err := rs.InstallRelease(c, req)
if err != nil { if err != nil {
t.Fatalf("Failed install: %s", err) t.Fatalf("Failed install: %s", err)
...@@ -457,16 +386,9 @@ func TestInstallRelease_KubeVersion(t *testing.T) { ...@@ -457,16 +386,9 @@ func TestInstallRelease_KubeVersion(t *testing.T) {
c := helm.NewContext() c := helm.NewContext()
rs := rsFixture() rs := rsFixture()
// TODO: Refactor this into a mock. req := installRequest(
req := &services.InstallReleaseRequest{ withChart(withKube(">=0.0.0")),
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)},
},
},
}
_, err := rs.InstallRelease(c, req) _, err := rs.InstallRelease(c, req)
if err != nil { if err != nil {
t.Fatalf("Expected valid range. Got %q", err) t.Fatalf("Expected valid range. Got %q", err)
...@@ -477,16 +399,10 @@ func TestInstallRelease_WrongKubeVersion(t *testing.T) { ...@@ -477,16 +399,10 @@ func TestInstallRelease_WrongKubeVersion(t *testing.T) {
c := helm.NewContext() c := helm.NewContext()
rs := rsFixture() rs := rsFixture()
// TODO: Refactor this into a mock. req := installRequest(
req := &services.InstallReleaseRequest{ withChart(withKube(">=5.0.0")),
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)},
},
},
}
_, err := rs.InstallRelease(c, req) _, err := rs.InstallRelease(c, req)
if err == nil { if err == nil {
t.Fatalf("Expected to fail because of wrong version") t.Fatalf("Expected to fail because of wrong version")
......
...@@ -105,23 +105,128 @@ func rsFixture() *ReleaseServer { ...@@ -105,23 +105,128 @@ func rsFixture() *ReleaseServer {
} }
} }
// chartStub creates a fully stubbed out chart. type chartOptions struct {
func chartStub() *chart.Chart { *chart.Chart
return &chart.Chart{ }
// TODO: This should be more complete.
Metadata: &chart.Metadata{ type chartOption func(*chartOptions)
Name: "hello",
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/goodbye", Data: []byte("goodbye: world")},
{Name: "templates/empty", Data: []byte("")}, {Name: "templates/empty", Data: []byte("")},
{Name: "templates/with-partials", Data: []byte(`hello: {{ template "_planet" . }}`)}, {Name: "templates/with-partials", Data: []byte(`hello: {{ template "_planet" . }}`)},
{Name: "templates/partials/_planet", Data: []byte(`{{define "_planet"}}Earth{{end}}`)}, {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. // releaseStub creates a release stub, complete with the chartStub as its chart.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment