Unverified Commit 28d87e17 authored by Matthew Fisher's avatar Matthew Fisher Committed by GitHub
Browse files

Merge pull request #3860 from adshmh/refactor-tiller-release-install-unit-tests-using-stubs

refactor tiller release install unit tests using chart and install request stubs
parents 64fe6d61 c22492ff
main Release add-codeql dependabot/go_modules/github.com/docker/distribution-2.8.2incompatible dependabot/go_modules/github.com/lib/pq-1.10.9 dependabot/go_modules/github.com/rubenv/sql-migrate-1.4.0 dependabot/go_modules/golang.org/x/crypto-0.9.0 dependabot/go_modules/golang.org/x/term-0.8.0 dependabot/go_modules/k8s.io/klog/v2-2.100.1 dev-v2 feat-v3/event-emitter-lua release-2.10 release-2.11 release-2.12 release-2.13 release-2.14 release-2.15 release-2.16 release-2.17 release-3.0 release-3.1 release-3.10 release-3.11 release-3.12 release-3.2 release-3.3 release-3.4 release-3.5 release-3.6 release-3.6.1 release-3.6.2 release-3.7 release-3.8 release-3.9 release-v3.0.0-beta.4 v3.12.0 v3.12.0-rc.1 v3.12.0-dev.1 v3.11.3 v3.11.2 v3.11.1 v3.11.0 v3.11.0-rc.2 v3.11.0-rc.1 v3.10.3 v3.10.2 v3.10.1 v3.10.0 v3.10.0-rc.1 v3.9.4 v3.9.3 v3.9.2 v3.9.1 v3.9.0 v3.9.0-rc.1 v3.8.2 v3.8.1 v3.8.0 v3.8.0-rc.2 v3.8.0-rc.1 v3.7.2 v3.7.1 v3.7.0 v3.7.0-rc.3 v3.7.0-rc.2 v3.7.0-rc.1 v3.6.3 v3.6.2 v3.6.1 v3.6.0 v3.6.0-rc.1 v3.5.4 v3.5.3 v3.5.2 v3.5.1 v3.5.0 v3.5.0-rc.2 v3.5.0-rc.1 v3.4.2 v3.4.1 v3.4.0 v3.4.0-rc.1 v3.3.4 v3.3.3 v3.3.2 v3.3.1 v3.3.0 v3.3.0-rc.2 v3.3.0-rc.1 v3.2.4 v3.2.3 v3.2.2 v3.2.1 v3.2.0 v3.2.0-rc.1 v3.1.3 v3.1.2 v3.1.1 v3.1.0 v3.1.0-rc.3 v3.1.0-rc.2 v3.1.0-rc.1 v3.0.3 v3.0.2 v3.0.1 v3.0.0 v3.0.0-rc.4 v3.0.0-rc.3 v3.0.0-rc.2 v3.0.0-rc.1 v3.0.0-beta.5 v3.0.0-beta.4 v3.0.0-beta.3 v3.0.0-beta.2 v3.0.0-beta.1 v3.0.0-alpha.2 v3.0.0-alpha.1 v2.17.0 v2.17.0-rc.1 v2.16.12 v2.16.11 v2.16.10 v2.16.9 v2.16.8 v2.16.7 v2.16.6 v2.16.5 v2.16.4 v2.16.3 v2.16.2 v2.16.1 v2.16.0 v2.16.0-rc.2 v2.16.0-rc.1 v2.15.2 v2.15.1 v2.15.0 v2.15.0-rc.2 v2.15.0-rc.1 v2.14.3 v2.14.2 v2.14.1 v2.14.0 v2.14.0-rc.2 v2.14.0-rc.1 v2.13.1 v2.13.1-rc.1 v2.13.0 v2.13.0-rc.2 v2.13.0-rc.1 v2.12.3 v2.12.2 v2.12.1 v2.12.0 v2.12.0-rc.2 v2.12.0-rc.1 v2.11.0 v2.11.0-rc.4 v2.11.0-rc.3 v2.11.0-rc.2 v2.11.0-rc.1 v2.10.0 v2.10.0-rc.3 v2.10.0-rc.2 v2.10.0-rc.1
No related merge requests found
Showing with 148 additions and 127 deletions
+148 -127
...@@ -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.
......
Supports Markdown
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