Add support for all CRUD operations + test coverage for delete and install

pull/3025/head
Brad Bowman 7 years ago
parent 61f2b24dca
commit cab3057ee3

@ -17,9 +17,12 @@ limitations under the License.
package helm // import "k8s.io/helm/pkg/helm" package helm // import "k8s.io/helm/pkg/helm"
import ( import (
"errors"
"fmt" "fmt"
"strings"
"sync" "sync"
"github.com/technosophos/moniker"
"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"
@ -30,7 +33,15 @@ import (
type FakeClient struct { type FakeClient struct {
Rels []*release.Release Rels []*release.Release
Responses map[string]release.TestRun_Status Responses map[string]release.TestRun_Status
Err error Opts options
}
// Option returns the fake release client
func (c *FakeClient) Option(opts ...Option) Interface {
for _, opt := range opts {
opt(&c.Opts)
}
return c
} }
var _ Interface = &FakeClient{} var _ Interface = &FakeClient{}
@ -42,31 +53,66 @@ func (c *FakeClient) ListReleases(opts ...ReleaseListOption) (*rls.ListReleasesR
Count: int64(len(c.Rels)), Count: int64(len(c.Rels)),
Releases: c.Rels, Releases: c.Rels,
} }
return resp, c.Err return resp, nil
} }
// InstallRelease returns a response with the first Release on the fake release client // InstallRelease returns a response with the first Release on the fake release client
func (c *FakeClient) InstallRelease(chStr, ns string, opts ...InstallOption) (*rls.InstallReleaseResponse, error) { func (c *FakeClient) InstallRelease(chStr, ns string, opts ...InstallOption) (*rls.InstallReleaseResponse, error) {
return &rls.InstallReleaseResponse{ chart := &chart.Chart{}
Release: c.Rels[0], return c.InstallReleaseFromChart(chart, ns, opts...)
}, nil
} }
// InstallReleaseFromChart returns a response with the first Release on the fake release client // InstallReleaseFromChart returns a response with the first Release on the fake release client
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 {
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)
if err == nil && rel != nil {
return nil, errors.New("cannot re-use a name that is still in use")
}
release := &release.Release{
Name: releaseName,
Namespace: ns,
}
c.Rels = append(c.Rels, release)
return &rls.InstallReleaseResponse{ return &rls.InstallReleaseResponse{
Release: c.Rels[0], Release: release,
}, nil }, nil
} }
// DeleteRelease returns nil, nil // DeleteRelease returns nil, nil
func (c *FakeClient) DeleteRelease(rlsName string, opts ...DeleteOption) (*rls.UninstallReleaseResponse, error) { func (c *FakeClient) DeleteRelease(rlsName string, opts ...DeleteOption) (*rls.UninstallReleaseResponse, error) {
return nil, nil for i, rel := range c.Rels {
if rel.Name == rlsName {
c.Rels = append(c.Rels[:i], c.Rels[i+1:]...)
return &rls.UninstallReleaseResponse{
Release: rel,
}, nil
}
} }
// UpdateRelease returns nil, nil return nil, fmt.Errorf("No such release: %s", rlsName)
func (c *FakeClient) UpdateRelease(rlsName string, chStr string, opts ...UpdateOption) (*rls.UpdateReleaseResponse, error) {
return nil, nil
} }
// GetVersion returns a fake version // GetVersion returns a fake version
@ -78,9 +124,20 @@ func (c *FakeClient) GetVersion(opts ...VersionOption) (*rls.GetVersionResponse,
}, nil }, nil
} }
// UpdateRelease returns nil, nil
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 nil, nil
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) {
return nil, nil // Check to see if the release already exists.
rel, err := c.ReleaseContent(rlsName, nil)
if err != nil {
return nil, err
}
return &rls.UpdateReleaseResponse{Release: rel.Release}, nil
} }
// RollbackRelease returns nil, nil // RollbackRelease returns nil, nil
@ -90,37 +147,33 @@ func (c *FakeClient) RollbackRelease(rlsName string, opts ...RollbackOption) (*r
// 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 in the fake release client.
func (c *FakeClient) ReleaseStatus(rlsName string, opts ...StatusOption) (*rls.GetReleaseStatusResponse, error) { func (c *FakeClient) ReleaseStatus(rlsName string, opts ...StatusOption) (*rls.GetReleaseStatusResponse, error) {
if len(c.Rels) > 0 { for _, rel := range c.Rels {
for _, release := range c.Rels { if rel.Name == rlsName {
if release.Name == rlsName {
return &rls.GetReleaseStatusResponse{ return &rls.GetReleaseStatusResponse{
Name: release.Name, Name: rel.Name,
Info: release.Info, Info: rel.Info,
Namespace: release.Namespace, Namespace: rel.Namespace,
}, nil }, nil
} }
} }
}
return nil, fmt.Errorf("No such release: %s", rlsName) return nil, fmt.Errorf("No such release: %s", rlsName)
} }
// ReleaseContent returns the configuration for the matching release name in the fake release client. // ReleaseContent returns the configuration for the matching release name in the fake release client.
func (c *FakeClient) ReleaseContent(rlsName string, opts ...ContentOption) (resp *rls.GetReleaseContentResponse, err error) { func (c *FakeClient) ReleaseContent(rlsName string, opts ...ContentOption) (resp *rls.GetReleaseContentResponse, err error) {
if len(c.Rels) > 0 { for _, rel := range c.Rels {
for _, release := range c.Rels { if rel.Name == rlsName {
if release.Name == rlsName { return &rls.GetReleaseContentResponse{
resp = &rls.GetReleaseContentResponse{ Release: rel,
Release: release, }, nil
}
}
} }
} }
return resp, c.Err return resp, fmt.Errorf("No such release: %s", rlsName)
} }
// ReleaseHistory returns a release's revision history. // ReleaseHistory returns a release's revision history.
func (c *FakeClient) ReleaseHistory(rlsName string, opts ...HistoryOption) (*rls.GetHistoryResponse, error) { func (c *FakeClient) ReleaseHistory(rlsName string, opts ...HistoryOption) (*rls.GetHistoryResponse, error) {
return &rls.GetHistoryResponse{Releases: c.Rels}, c.Err return &rls.GetHistoryResponse{Releases: c.Rels}, nil
} }
// RunReleaseTest executes a pre-defined tests on a release // RunReleaseTest executes a pre-defined tests on a release
@ -147,8 +200,3 @@ func (c *FakeClient) RunReleaseTest(rlsName string, opts ...ReleaseTestOption) (
return results, errc return results, errc
} }
// Option returns the fake release client
func (c *FakeClient) Option(opt ...Option) Interface {
return c
}

@ -20,6 +20,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"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"
) )
@ -114,3 +115,175 @@ func TestFakeClient_ReleaseStatus(t *testing.T) {
}) })
} }
} }
func TestFakeClient_InstallReleaseFromChart(t *testing.T) {
installChart := &chart.Chart{}
type fields struct {
Rels []*release.Release
}
type args struct {
ns string
opts []InstallOption
}
tests := []struct {
name string
fields fields
args args
want *rls.InstallReleaseResponse
relsAfter []*release.Release
wantErr bool
}{
{
name: "Add release to an empty list.",
fields: fields{
Rels: []*release.Release{},
},
args: args{
ns: "default",
opts: []InstallOption{ReleaseName("new-release")},
},
want: &rls.InstallReleaseResponse{
Release: &release.Release{
Name: "new-release",
Namespace: "default",
},
},
relsAfter: []*release.Release{
{Name: "new-release", Namespace: "default"},
},
wantErr: false,
},
{
name: "Try to add a release where the name already exists.",
fields: fields{
Rels: []*release.Release{
&release.Release{
Name: "new-release",
Namespace: "default",
},
},
},
args: args{
ns: "default",
opts: []InstallOption{ReleaseName("new-release")},
},
relsAfter: []*release.Release{
{
Name: "new-release",
Namespace: "default",
},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &FakeClient{
Rels: tt.fields.Rels,
}
got, err := c.InstallReleaseFromChart(installChart, tt.args.ns, tt.args.opts...)
if (err != nil) != tt.wantErr {
t.Errorf("FakeClient.InstallReleaseFromChart() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FakeClient.InstallReleaseFromChart() = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(c.Rels, tt.relsAfter) {
t.Errorf("FakeClient.InstallReleaseFromChart() rels = %v, expected %v", got, tt.relsAfter)
}
})
}
}
func TestFakeClient_DeleteRelease(t *testing.T) {
type fields struct {
Rels []*release.Release
}
type args struct {
rlsName string
opts []DeleteOption
}
tests := []struct {
name string
fields fields
args args
want *rls.UninstallReleaseResponse
relsAfter []*release.Release
wantErr bool
}{
{
name: "Delete a release that exists.",
fields: fields{
Rels: []*release.Release{
{
Name: "angry-dolphin",
},
{
Name: "trepid-tapir",
},
},
},
args: args{
rlsName: "trepid-tapir",
opts: []DeleteOption{},
},
relsAfter: []*release.Release{
{
Name: "angry-dolphin",
},
},
want: &rls.UninstallReleaseResponse{
Release: &release.Release{Name: "trepid-tapir"},
},
wantErr: false,
},
{
name: "Delete a release that does not exist.",
fields: fields{
Rels: []*release.Release{
{
Name: "angry-dolphin",
},
{
Name: "trepid-tapir",
},
},
},
args: args{
rlsName: "release-that-does-not-exists",
opts: []DeleteOption{},
},
relsAfter: []*release.Release{
{
Name: "angry-dolphin",
},
{
Name: "trepid-tapir",
},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &FakeClient{
Rels: tt.fields.Rels,
}
got, err := c.DeleteRelease(tt.args.rlsName, tt.args.opts...)
if (err != nil) != tt.wantErr {
t.Errorf("FakeClient.DeleteRelease() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("FakeClient.DeleteRelease() = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(c.Rels, tt.relsAfter) {
t.Errorf("FakeClient.InstallReleaseFromChart() rels = %v, expected %v", got, tt.relsAfter)
}
})
}
}

Loading…
Cancel
Save