[fakeclient-releasestatus] Move ReleaseMock and associates into helm package and update tests to use them

pull/3025/head
Brad Bowman 7 years ago
parent 8d5cf13f69
commit e974ab31d8

@ -19,10 +19,10 @@ package helm // import "k8s.io/helm/pkg/helm"
import (
"errors"
"fmt"
"strings"
"math/rand"
"sync"
"github.com/technosophos/moniker"
"github.com/golang/protobuf/ptypes/timestamp"
"k8s.io/helm/pkg/proto/hapi/chart"
"k8s.io/helm/pkg/proto/hapi/release"
rls "k8s.io/helm/pkg/proto/hapi/services"
@ -56,32 +56,19 @@ func (c *FakeClient) ListReleases(opts ...ReleaseListOption) (*rls.ListReleasesR
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) {
chart := &chart.Chart{}
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) {
for _, opt := range opts {
opt(&c.Opts)
}
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.
rel, err := c.ReleaseStatus(releaseName, nil)
@ -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")
}
release := &release.Release{
Name: releaseName,
Namespace: ns,
}
release := ReleaseMock(&MockReleaseOptions{Name: releaseName, Namespace: ns})
c.Rels = append(c.Rels, release)
return &rls.InstallReleaseResponse{
@ -101,7 +84,7 @@ func (c *FakeClient) InstallReleaseFromChart(chart *chart.Chart, ns string, opts
}, nil
}
// DeleteRelease returns nil, nil
// DeleteRelease deletes a release from the FakeClient
func (c *FakeClient) DeleteRelease(rlsName string, opts ...DeleteOption) (*rls.UninstallReleaseResponse, error) {
for i, rel := range c.Rels {
if rel.Name == rlsName {
@ -124,12 +107,12 @@ func (c *FakeClient) GetVersion(opts ...VersionOption) (*rls.GetVersionResponse,
}, 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) {
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) {
// Check to see if the release already exists.
rel, err := c.ReleaseContent(rlsName, nil)
@ -145,7 +128,7 @@ func (c *FakeClient) RollbackRelease(rlsName string, opts ...RollbackOption) (*r
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) {
for _, rel := range c.Rels {
if rel.Name == rlsName {
@ -200,3 +183,90 @@ func (c *FakeClient) RunReleaseTest(rlsName string, opts ...ReleaseTestOption) (
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 (
)
func TestFakeClient_ReleaseStatus(t *testing.T) {
releasePresent := &release.Release{Name: "release-present", Namespace: "default"}
releaseNotPresent := &release.Release{Name: "release-not-present", Namespace: "default"}
releasePresent := ReleaseMock(&MockReleaseOptions{Name: "release-present"})
releaseNotPresent := ReleaseMock(&MockReleaseOptions{Name: "release-not-present"})
type fields struct {
Rels []*release.Release
@ -80,8 +80,8 @@ func TestFakeClient_ReleaseStatus(t *testing.T) {
name: "Get a single release that exists from list",
fields: fields{
Rels: []*release.Release{
{Name: "angry-dolphin", Namespace: "default"},
{Name: "trepid-tapir", Namespace: "default"},
ReleaseMock(&MockReleaseOptions{Name: "angry-dolphin", Namespace: "default"}),
ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir", Namespace: "default"}),
releasePresent,
},
},
@ -143,13 +143,10 @@ func TestFakeClient_InstallReleaseFromChart(t *testing.T) {
opts: []InstallOption{ReleaseName("new-release")},
},
want: &rls.InstallReleaseResponse{
Release: &release.Release{
Name: "new-release",
Namespace: "default",
},
Release: ReleaseMock(&MockReleaseOptions{Name: "new-release"}),
},
relsAfter: []*release.Release{
{Name: "new-release", Namespace: "default"},
ReleaseMock(&MockReleaseOptions{Name: "new-release"}),
},
wantErr: false,
},
@ -157,10 +154,7 @@ func TestFakeClient_InstallReleaseFromChart(t *testing.T) {
name: "Try to add a release where the name already exists.",
fields: fields{
Rels: []*release.Release{
&release.Release{
Name: "new-release",
Namespace: "default",
},
ReleaseMock(&MockReleaseOptions{Name: "new-release"}),
},
},
args: args{
@ -168,10 +162,7 @@ func TestFakeClient_InstallReleaseFromChart(t *testing.T) {
opts: []InstallOption{ReleaseName("new-release")},
},
relsAfter: []*release.Release{
{
Name: "new-release",
Namespace: "default",
},
ReleaseMock(&MockReleaseOptions{Name: "new-release"}),
},
want: nil,
wantErr: true,
@ -217,12 +208,8 @@ func TestFakeClient_DeleteRelease(t *testing.T) {
name: "Delete a release that exists.",
fields: fields{
Rels: []*release.Release{
{
Name: "angry-dolphin",
},
{
Name: "trepid-tapir",
},
ReleaseMock(&MockReleaseOptions{Name: "angry-dolphin"}),
ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir"}),
},
},
args: args{
@ -230,12 +217,10 @@ func TestFakeClient_DeleteRelease(t *testing.T) {
opts: []DeleteOption{},
},
relsAfter: []*release.Release{
{
Name: "angry-dolphin",
},
ReleaseMock(&MockReleaseOptions{Name: "angry-dolphin"}),
},
want: &rls.UninstallReleaseResponse{
Release: &release.Release{Name: "trepid-tapir"},
Release: ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir"}),
},
wantErr: false,
},
@ -243,12 +228,8 @@ func TestFakeClient_DeleteRelease(t *testing.T) {
name: "Delete a release that does not exist.",
fields: fields{
Rels: []*release.Release{
{
Name: "angry-dolphin",
},
{
Name: "trepid-tapir",
},
ReleaseMock(&MockReleaseOptions{Name: "angry-dolphin"}),
ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir"}),
},
},
args: args{
@ -256,12 +237,8 @@ func TestFakeClient_DeleteRelease(t *testing.T) {
opts: []DeleteOption{},
},
relsAfter: []*release.Release{
{
Name: "angry-dolphin",
},
{
Name: "trepid-tapir",
},
ReleaseMock(&MockReleaseOptions{Name: "angry-dolphin"}),
ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir"}),
},
want: nil,
wantErr: true,
@ -270,9 +247,7 @@ func TestFakeClient_DeleteRelease(t *testing.T) {
name: "Delete when only 1 item exists.",
fields: fields{
Rels: []*release.Release{
{
Name: "trepid-tapir",
},
ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir"}),
},
},
args: args{
@ -281,7 +256,7 @@ func TestFakeClient_DeleteRelease(t *testing.T) {
},
relsAfter: []*release.Release{},
want: &rls.UninstallReleaseResponse{
Release: &release.Release{Name: "trepid-tapir"},
Release: ReleaseMock(&MockReleaseOptions{Name: "trepid-tapir"}),
},
wantErr: false,
},

Loading…
Cancel
Save