Commit e974ab31 authored by Brad Bowman's avatar Brad Bowman

[fakeclient-releasestatus] Move ReleaseMock and associates into helm package and…

[fakeclient-releasestatus] Move ReleaseMock and associates into helm package and update tests to use them
parent 8d5cf13f
...@@ -19,10 +19,10 @@ package helm // import "k8s.io/helm/pkg/helm" ...@@ -19,10 +19,10 @@ package helm // import "k8s.io/helm/pkg/helm"
import ( import (
"errors" "errors"
"fmt" "fmt"
"strings" "math/rand"
"sync" "sync"
"github.com/technosophos/moniker" "github.com/golang/protobuf/ptypes/timestamp"
"k8s.io/helm/pkg/proto/hapi/chart" "k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/proto/hapi/release" "k8s.io/helm/pkg/proto/hapi/release"
rls "k8s.io/helm/pkg/proto/hapi/services" rls "k8s.io/helm/pkg/proto/hapi/services"
...@@ -56,32 +56,19 @@ func (c *FakeClient) ListReleases(opts ...ReleaseListOption) (*rls.ListReleasesR ...@@ -56,32 +56,19 @@ func (c *FakeClient) ListReleases(opts ...ReleaseListOption) (*rls.ListReleasesR
return resp, nil return resp, nil
} }
// InstallRelease returns a response with the first Release on the fake release client // InstallRelease creates a new release and returns a InstallReleaseResponse containing that release
func (c *FakeClient) InstallRelease(chStr, ns string, opts ...InstallOption) (*rls.InstallReleaseResponse, error) { func (c *FakeClient) InstallRelease(chStr, ns string, opts ...InstallOption) (*rls.InstallReleaseResponse, error) {
chart := &chart.Chart{} chart := &chart.Chart{}
return c.InstallReleaseFromChart(chart, ns, opts...) return c.InstallReleaseFromChart(chart, ns, opts...)
} }
// InstallReleaseFromChart returns a response with the first Release on the fake release client // InstallReleaseFromChart adds a new MockRelease to the fake client and returns a InstallReleaseResponse containing that release
func (c *FakeClient) InstallReleaseFromChart(chart *chart.Chart, ns string, opts ...InstallOption) (*rls.InstallReleaseResponse, error) { func (c *FakeClient) InstallReleaseFromChart(chart *chart.Chart, ns string, opts ...InstallOption) (*rls.InstallReleaseResponse, error) {
for _, opt := range opts { for _, opt := range opts {
opt(&c.Opts) opt(&c.Opts)
} }
releaseName := c.Opts.instReq.Name releaseName := c.Opts.instReq.Name
if releaseName == "" {
for i := 0; i < 5; i++ {
namer := moniker.New()
name := namer.NameSep("-")
if len(name) > 53 {
name = name[:53]
}
if _, err := c.ReleaseStatus(name, nil); strings.Contains(err.Error(), "No such release") {
releaseName = name
break
}
}
}
// Check to see if the release already exists. // Check to see if the release already exists.
rel, err := c.ReleaseStatus(releaseName, nil) rel, err := c.ReleaseStatus(releaseName, nil)
...@@ -89,11 +76,7 @@ func (c *FakeClient) InstallReleaseFromChart(chart *chart.Chart, ns string, opts ...@@ -89,11 +76,7 @@ func (c *FakeClient) InstallReleaseFromChart(chart *chart.Chart, ns string, opts
return nil, errors.New("cannot re-use a name that is still in use") return nil, errors.New("cannot re-use a name that is still in use")
} }
release := &release.Release{ release := ReleaseMock(&MockReleaseOptions{Name: releaseName, Namespace: ns})
Name: releaseName,
Namespace: ns,
}
c.Rels = append(c.Rels, release) c.Rels = append(c.Rels, release)
return &rls.InstallReleaseResponse{ return &rls.InstallReleaseResponse{
...@@ -101,7 +84,7 @@ func (c *FakeClient) InstallReleaseFromChart(chart *chart.Chart, ns string, opts ...@@ -101,7 +84,7 @@ func (c *FakeClient) InstallReleaseFromChart(chart *chart.Chart, ns string, opts
}, nil }, nil
} }
// DeleteRelease returns nil, nil // DeleteRelease deletes a release from the FakeClient
func (c *FakeClient) DeleteRelease(rlsName string, opts ...DeleteOption) (*rls.UninstallReleaseResponse, error) { func (c *FakeClient) DeleteRelease(rlsName string, opts ...DeleteOption) (*rls.UninstallReleaseResponse, error) {
for i, rel := range c.Rels { for i, rel := range c.Rels {
if rel.Name == rlsName { if rel.Name == rlsName {
...@@ -124,12 +107,12 @@ func (c *FakeClient) GetVersion(opts ...VersionOption) (*rls.GetVersionResponse, ...@@ -124,12 +107,12 @@ func (c *FakeClient) GetVersion(opts ...VersionOption) (*rls.GetVersionResponse,
}, nil }, nil
} }
// UpdateRelease returns nil, nil // UpdateRelease returns an UpdateReleaseResponse containing the updated release, if it exists
func (c *FakeClient) UpdateRelease(rlsName string, chStr string, opts ...UpdateOption) (*rls.UpdateReleaseResponse, error) { func (c *FakeClient) UpdateRelease(rlsName string, chStr string, opts ...UpdateOption) (*rls.UpdateReleaseResponse, error) {
return c.UpdateReleaseFromChart(rlsName, &chart.Chart{}, opts...) return c.UpdateReleaseFromChart(rlsName, &chart.Chart{}, opts...)
} }
// UpdateReleaseFromChart returns nil, nil // UpdateReleaseFromChart returns an UpdateReleaseResponse containing the updated release, if it exists
func (c *FakeClient) UpdateReleaseFromChart(rlsName string, chart *chart.Chart, opts ...UpdateOption) (*rls.UpdateReleaseResponse, error) { func (c *FakeClient) UpdateReleaseFromChart(rlsName string, chart *chart.Chart, opts ...UpdateOption) (*rls.UpdateReleaseResponse, error) {
// Check to see if the release already exists. // Check to see if the release already exists.
rel, err := c.ReleaseContent(rlsName, nil) rel, err := c.ReleaseContent(rlsName, nil)
...@@ -145,7 +128,7 @@ func (c *FakeClient) RollbackRelease(rlsName string, opts ...RollbackOption) (*r ...@@ -145,7 +128,7 @@ func (c *FakeClient) RollbackRelease(rlsName string, opts ...RollbackOption) (*r
return nil, nil return nil, nil
} }
// ReleaseStatus returns a release status response with info from the matching release name in the fake release client. // ReleaseStatus returns a release status response with info from the matching release name.
func (c *FakeClient) ReleaseStatus(rlsName string, opts ...StatusOption) (*rls.GetReleaseStatusResponse, error) { func (c *FakeClient) ReleaseStatus(rlsName string, opts ...StatusOption) (*rls.GetReleaseStatusResponse, error) {
for _, rel := range c.Rels { for _, rel := range c.Rels {
if rel.Name == rlsName { if rel.Name == rlsName {
...@@ -200,3 +183,90 @@ func (c *FakeClient) RunReleaseTest(rlsName string, opts ...ReleaseTestOption) ( ...@@ -200,3 +183,90 @@ func (c *FakeClient) RunReleaseTest(rlsName string, opts ...ReleaseTestOption) (
return results, errc return results, errc
} }
// MockHookTemplate is the hook template used for all mock release objects.
var MockHookTemplate = `apiVersion: v1
kind: Job
metadata:
annotations:
"helm.sh/hooks": pre-install
`
// MockManifest is the manifest used for all mock release objects.
var MockManifest = `apiVersion: v1
kind: Secret
metadata:
name: fixture
`
// MockReleaseOptions allows for user-configurable options on mock release objects.
type MockReleaseOptions struct {
Name string
Version int32
Chart *chart.Chart
StatusCode release.Status_Code
Namespace string
}
// ReleaseMock creates a mock release object based on options set by MockReleaseOptions. This function should typically not be used outside of testing.
func ReleaseMock(opts *MockReleaseOptions) *release.Release {
date := timestamp.Timestamp{Seconds: 242085845, Nanos: 0}
name := opts.Name
if name == "" {
name = "testrelease-" + string(rand.Intn(100))
}
var version int32 = 1
if opts.Version != 0 {
version = opts.Version
}
namespace := opts.Namespace
if namespace == "" {
namespace = "default"
}
ch := opts.Chart
if opts.Chart == nil {
ch = &chart.Chart{
Metadata: &chart.Metadata{
Name: "foo",
Version: "0.1.0-beta.1",
},
Templates: []*chart.Template{
{Name: "templates/foo.tpl", Data: []byte(MockManifest)},
},
}
}
scode := release.Status_DEPLOYED
if opts.StatusCode > 0 {
scode = opts.StatusCode
}
return &release.Release{
Name: name,
Info: &release.Info{
FirstDeployed: &date,
LastDeployed: &date,
Status: &release.Status{Code: scode},
Description: "Release mock",
},
Chart: ch,
Config: &chart.Config{Raw: `name: "value"`},
Version: version,
Namespace: namespace,
Hooks: []*release.Hook{
{
Name: "pre-install-hook",
Kind: "Job",
Path: "pre-install-hook.yaml",
Manifest: MockHookTemplate,
LastRun: &date,
Events: []release.Hook_Event{release.Hook_PRE_INSTALL},
},
},
Manifest: MockManifest,
}
}
...@@ -26,8 +26,8 @@ import ( ...@@ -26,8 +26,8 @@ import (
) )
func TestFakeClient_ReleaseStatus(t *testing.T) { func TestFakeClient_ReleaseStatus(t *testing.T) {
releasePresent := &release.Release{Name: "release-present", Namespace: "default"} releasePresent := ReleaseMock(&MockReleaseOptions{Name: "release-present"})
releaseNotPresent := &release.Release{Name: "release-not-present", Namespace: "default"} releaseNotPresent := ReleaseMock(&MockReleaseOptions{Name: "release-not-present"})
type fields struct { type fields struct {
Rels []*release.Release Rels []*release.Release
...@@ -80,8 +80,8 @@ func TestFakeClient_ReleaseStatus(t *testing.T) { ...@@ -80,8 +80,8 @@ func TestFakeClient_ReleaseStatus(t *testing.T) {
name: "Get a single release that exists from list", name: "Get a single release that exists from list",
fields: fields{ fields: fields{
Rels: []*release.Release{ Rels: []*release.Release{
{Name: "angry-dolphin", Namespace: "default"}, ReleaseMock(&MockReleaseOptions{Name: "angry-dolphin", Namespace: "default"}),
{Name: "trepid-tapir", Namespace: "default"}, ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir", Namespace: "default"}),
releasePresent, releasePresent,
}, },
}, },
...@@ -143,13 +143,10 @@ func TestFakeClient_InstallReleaseFromChart(t *testing.T) { ...@@ -143,13 +143,10 @@ func TestFakeClient_InstallReleaseFromChart(t *testing.T) {
opts: []InstallOption{ReleaseName("new-release")}, opts: []InstallOption{ReleaseName("new-release")},
}, },
want: &rls.InstallReleaseResponse{ want: &rls.InstallReleaseResponse{
Release: &release.Release{ Release: ReleaseMock(&MockReleaseOptions{Name: "new-release"}),
Name: "new-release",
Namespace: "default",
},
}, },
relsAfter: []*release.Release{ relsAfter: []*release.Release{
{Name: "new-release", Namespace: "default"}, ReleaseMock(&MockReleaseOptions{Name: "new-release"}),
}, },
wantErr: false, wantErr: false,
}, },
...@@ -157,10 +154,7 @@ func TestFakeClient_InstallReleaseFromChart(t *testing.T) { ...@@ -157,10 +154,7 @@ func TestFakeClient_InstallReleaseFromChart(t *testing.T) {
name: "Try to add a release where the name already exists.", name: "Try to add a release where the name already exists.",
fields: fields{ fields: fields{
Rels: []*release.Release{ Rels: []*release.Release{
&release.Release{ ReleaseMock(&MockReleaseOptions{Name: "new-release"}),
Name: "new-release",
Namespace: "default",
},
}, },
}, },
args: args{ args: args{
...@@ -168,10 +162,7 @@ func TestFakeClient_InstallReleaseFromChart(t *testing.T) { ...@@ -168,10 +162,7 @@ func TestFakeClient_InstallReleaseFromChart(t *testing.T) {
opts: []InstallOption{ReleaseName("new-release")}, opts: []InstallOption{ReleaseName("new-release")},
}, },
relsAfter: []*release.Release{ relsAfter: []*release.Release{
{ ReleaseMock(&MockReleaseOptions{Name: "new-release"}),
Name: "new-release",
Namespace: "default",
},
}, },
want: nil, want: nil,
wantErr: true, wantErr: true,
...@@ -217,12 +208,8 @@ func TestFakeClient_DeleteRelease(t *testing.T) { ...@@ -217,12 +208,8 @@ func TestFakeClient_DeleteRelease(t *testing.T) {
name: "Delete a release that exists.", name: "Delete a release that exists.",
fields: fields{ fields: fields{
Rels: []*release.Release{ Rels: []*release.Release{
{ ReleaseMock(&MockReleaseOptions{Name: "angry-dolphin"}),
Name: "angry-dolphin", ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir"}),
},
{
Name: "trepid-tapir",
},
}, },
}, },
args: args{ args: args{
...@@ -230,12 +217,10 @@ func TestFakeClient_DeleteRelease(t *testing.T) { ...@@ -230,12 +217,10 @@ func TestFakeClient_DeleteRelease(t *testing.T) {
opts: []DeleteOption{}, opts: []DeleteOption{},
}, },
relsAfter: []*release.Release{ relsAfter: []*release.Release{
{ ReleaseMock(&MockReleaseOptions{Name: "angry-dolphin"}),
Name: "angry-dolphin",
},
}, },
want: &rls.UninstallReleaseResponse{ want: &rls.UninstallReleaseResponse{
Release: &release.Release{Name: "trepid-tapir"}, Release: ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir"}),
}, },
wantErr: false, wantErr: false,
}, },
...@@ -243,12 +228,8 @@ func TestFakeClient_DeleteRelease(t *testing.T) { ...@@ -243,12 +228,8 @@ func TestFakeClient_DeleteRelease(t *testing.T) {
name: "Delete a release that does not exist.", name: "Delete a release that does not exist.",
fields: fields{ fields: fields{
Rels: []*release.Release{ Rels: []*release.Release{
{ ReleaseMock(&MockReleaseOptions{Name: "angry-dolphin"}),
Name: "angry-dolphin", ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir"}),
},
{
Name: "trepid-tapir",
},
}, },
}, },
args: args{ args: args{
...@@ -256,12 +237,8 @@ func TestFakeClient_DeleteRelease(t *testing.T) { ...@@ -256,12 +237,8 @@ func TestFakeClient_DeleteRelease(t *testing.T) {
opts: []DeleteOption{}, opts: []DeleteOption{},
}, },
relsAfter: []*release.Release{ relsAfter: []*release.Release{
{ ReleaseMock(&MockReleaseOptions{Name: "angry-dolphin"}),
Name: "angry-dolphin", ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir"}),
},
{
Name: "trepid-tapir",
},
}, },
want: nil, want: nil,
wantErr: true, wantErr: true,
...@@ -270,9 +247,7 @@ func TestFakeClient_DeleteRelease(t *testing.T) { ...@@ -270,9 +247,7 @@ func TestFakeClient_DeleteRelease(t *testing.T) {
name: "Delete when only 1 item exists.", name: "Delete when only 1 item exists.",
fields: fields{ fields: fields{
Rels: []*release.Release{ Rels: []*release.Release{
{ ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir"}),
Name: "trepid-tapir",
},
}, },
}, },
args: args{ args: args{
...@@ -281,7 +256,7 @@ func TestFakeClient_DeleteRelease(t *testing.T) { ...@@ -281,7 +256,7 @@ func TestFakeClient_DeleteRelease(t *testing.T) {
}, },
relsAfter: []*release.Release{}, relsAfter: []*release.Release{},
want: &rls.UninstallReleaseResponse{ want: &rls.UninstallReleaseResponse{
Release: &release.Release{Name: "trepid-tapir"}, Release: ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir"}),
}, },
wantErr: false, wantErr: false,
}, },
......
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