From 70520efac401acf3f8b6e22d007d94a172742eac Mon Sep 17 00:00:00 2001 From: Michelle Noorali Date: Wed, 7 Jun 2017 01:37:55 -0400 Subject: [PATCH] ref(helm): make FakeReleaseClient public so it can be used by other projects that use the helm client --- cmd/helm/delete_test.go | 4 +- cmd/helm/get_hooks_test.go | 4 +- cmd/helm/get_manifest_test.go | 4 +- cmd/helm/get_test.go | 4 +- cmd/helm/get_values_test.go | 4 +- cmd/helm/helm_test.go | 116 +----------------------- cmd/helm/history_test.go | 3 +- cmd/helm/install_test.go | 4 +- cmd/helm/list_test.go | 5 +- cmd/helm/release_testing_test.go | 3 +- cmd/helm/reset_test.go | 13 +-- cmd/helm/rollback_test.go | 4 +- cmd/helm/status_test.go | 7 +- cmd/helm/upgrade_test.go | 3 +- cmd/helm/version_test.go | 3 +- pkg/helm/fake.go | 147 +++++++++++++++++++++++++++++++ 16 files changed, 194 insertions(+), 134 deletions(-) create mode 100644 pkg/helm/fake.go diff --git a/cmd/helm/delete_test.go b/cmd/helm/delete_test.go index 9d7c184d0..79b68f022 100644 --- a/cmd/helm/delete_test.go +++ b/cmd/helm/delete_test.go @@ -21,6 +21,8 @@ import ( "testing" "github.com/spf13/cobra" + + "k8s.io/helm/pkg/helm" ) func TestDelete(t *testing.T) { @@ -60,7 +62,7 @@ func TestDelete(t *testing.T) { err: true, }, } - runReleaseCases(t, tests, func(c *fakeReleaseClient, out io.Writer) *cobra.Command { + runReleaseCases(t, tests, func(c *helm.FakeReleaseClient, out io.Writer) *cobra.Command { return newDeleteCmd(c, out) }) } diff --git a/cmd/helm/get_hooks_test.go b/cmd/helm/get_hooks_test.go index 8d9eda2d5..2b3ce0ad0 100644 --- a/cmd/helm/get_hooks_test.go +++ b/cmd/helm/get_hooks_test.go @@ -21,6 +21,8 @@ import ( "testing" "github.com/spf13/cobra" + + "k8s.io/helm/pkg/helm" ) func TestGetHooks(t *testing.T) { @@ -37,7 +39,7 @@ func TestGetHooks(t *testing.T) { err: true, }, } - runReleaseCases(t, tests, func(c *fakeReleaseClient, out io.Writer) *cobra.Command { + runReleaseCases(t, tests, func(c *helm.FakeReleaseClient, out io.Writer) *cobra.Command { return newGetHooksCmd(c, out) }) } diff --git a/cmd/helm/get_manifest_test.go b/cmd/helm/get_manifest_test.go index 47d6d9053..e5c86c98e 100644 --- a/cmd/helm/get_manifest_test.go +++ b/cmd/helm/get_manifest_test.go @@ -21,6 +21,8 @@ import ( "testing" "github.com/spf13/cobra" + + "k8s.io/helm/pkg/helm" ) func TestGetManifest(t *testing.T) { @@ -37,7 +39,7 @@ func TestGetManifest(t *testing.T) { err: true, }, } - runReleaseCases(t, tests, func(c *fakeReleaseClient, out io.Writer) *cobra.Command { + runReleaseCases(t, tests, func(c *helm.FakeReleaseClient, out io.Writer) *cobra.Command { return newGetManifestCmd(c, out) }) } diff --git a/cmd/helm/get_test.go b/cmd/helm/get_test.go index 77d8d4d19..74266f5c8 100644 --- a/cmd/helm/get_test.go +++ b/cmd/helm/get_test.go @@ -21,6 +21,8 @@ import ( "testing" "github.com/spf13/cobra" + + "k8s.io/helm/pkg/helm" ) func TestGetCmd(t *testing.T) { @@ -37,7 +39,7 @@ func TestGetCmd(t *testing.T) { }, } - cmd := func(c *fakeReleaseClient, out io.Writer) *cobra.Command { + cmd := func(c *helm.FakeReleaseClient, out io.Writer) *cobra.Command { return newGetCmd(c, out) } runReleaseCases(t, tests, cmd) diff --git a/cmd/helm/get_values_test.go b/cmd/helm/get_values_test.go index 635161366..d1019e7f3 100644 --- a/cmd/helm/get_values_test.go +++ b/cmd/helm/get_values_test.go @@ -21,6 +21,8 @@ import ( "testing" "github.com/spf13/cobra" + + "k8s.io/helm/pkg/helm" ) func TestGetValuesCmd(t *testing.T) { @@ -36,7 +38,7 @@ func TestGetValuesCmd(t *testing.T) { err: true, }, } - cmd := func(c *fakeReleaseClient, out io.Writer) *cobra.Command { + cmd := func(c *helm.FakeReleaseClient, out io.Writer) *cobra.Command { return newGetValuesCmd(c, out) } runReleaseCases(t, tests, cmd) diff --git a/cmd/helm/helm_test.go b/cmd/helm/helm_test.go index 910daa22a..21a2f31da 100644 --- a/cmd/helm/helm_test.go +++ b/cmd/helm/helm_test.go @@ -25,7 +25,6 @@ import ( "os" "path/filepath" "regexp" - "sync" "testing" "github.com/golang/protobuf/ptypes/timestamp" @@ -35,8 +34,6 @@ import ( "k8s.io/helm/pkg/helm/helmpath" "k8s.io/helm/pkg/proto/hapi/chart" "k8s.io/helm/pkg/proto/hapi/release" - rls "k8s.io/helm/pkg/proto/hapi/services" - "k8s.io/helm/pkg/proto/hapi/version" "k8s.io/helm/pkg/repo" ) @@ -123,120 +120,15 @@ func releaseMock(opts *releaseOptions) *release.Release { } } -type fakeReleaseClient struct { - rels []*release.Release - responses map[string]release.TestRun_Status - err error -} - -var _ helm.Interface = &fakeReleaseClient{} -var _ helm.Interface = &helm.Client{} - -func (c *fakeReleaseClient) ListReleases(opts ...helm.ReleaseListOption) (*rls.ListReleasesResponse, error) { - resp := &rls.ListReleasesResponse{ - Count: int64(len(c.rels)), - Releases: c.rels, - } - return resp, c.err -} - -func (c *fakeReleaseClient) InstallRelease(chStr, ns string, opts ...helm.InstallOption) (*rls.InstallReleaseResponse, error) { - return &rls.InstallReleaseResponse{ - Release: c.rels[0], - }, nil -} - -func (c *fakeReleaseClient) InstallReleaseFromChart(chart *chart.Chart, ns string, opts ...helm.InstallOption) (*rls.InstallReleaseResponse, error) { - return &rls.InstallReleaseResponse{ - Release: c.rels[0], - }, nil -} - -func (c *fakeReleaseClient) DeleteRelease(rlsName string, opts ...helm.DeleteOption) (*rls.UninstallReleaseResponse, error) { - return nil, nil -} - -func (c *fakeReleaseClient) ReleaseStatus(rlsName string, opts ...helm.StatusOption) (*rls.GetReleaseStatusResponse, error) { - if c.rels[0] != nil { - return &rls.GetReleaseStatusResponse{ - Name: c.rels[0].Name, - Info: c.rels[0].Info, - Namespace: c.rels[0].Namespace, - }, nil - } - return nil, fmt.Errorf("No such release: %s", rlsName) -} - -func (c *fakeReleaseClient) GetVersion(opts ...helm.VersionOption) (*rls.GetVersionResponse, error) { - return &rls.GetVersionResponse{ - Version: &version.Version{ - SemVer: "1.2.3-fakeclient+testonly", - }, - }, nil -} - -func (c *fakeReleaseClient) UpdateRelease(rlsName string, chStr string, opts ...helm.UpdateOption) (*rls.UpdateReleaseResponse, error) { - return nil, nil -} - -func (c *fakeReleaseClient) UpdateReleaseFromChart(rlsName string, chart *chart.Chart, opts ...helm.UpdateOption) (*rls.UpdateReleaseResponse, error) { - return nil, nil -} - -func (c *fakeReleaseClient) RollbackRelease(rlsName string, opts ...helm.RollbackOption) (*rls.RollbackReleaseResponse, error) { - return nil, nil -} - -func (c *fakeReleaseClient) ReleaseContent(rlsName string, opts ...helm.ContentOption) (resp *rls.GetReleaseContentResponse, err error) { - if len(c.rels) > 0 { - resp = &rls.GetReleaseContentResponse{ - Release: c.rels[0], - } - } - return resp, c.err -} - -func (c *fakeReleaseClient) ReleaseHistory(rlsName string, opts ...helm.HistoryOption) (*rls.GetHistoryResponse, error) { - return &rls.GetHistoryResponse{Releases: c.rels}, c.err -} - -func (c *fakeReleaseClient) RunReleaseTest(rlsName string, opts ...helm.ReleaseTestOption) (<-chan *rls.TestReleaseResponse, <-chan error) { - - results := make(chan *rls.TestReleaseResponse) - errc := make(chan error, 1) - - go func() { - var wg sync.WaitGroup - for m, s := range c.responses { - wg.Add(1) - - go func(msg string, status release.TestRun_Status) { - defer wg.Done() - results <- &rls.TestReleaseResponse{Msg: msg, Status: status} - }(m, s) - } - - wg.Wait() - close(results) - close(errc) - }() - - return results, errc -} - -func (c *fakeReleaseClient) Option(opt ...helm.Option) helm.Interface { - return c -} - -// releaseCmd is a command that works with a fakeReleaseClient -type releaseCmd func(c *fakeReleaseClient, out io.Writer) *cobra.Command +// releaseCmd is a command that works with a FakeReleaseClient +type releaseCmd func(c *helm.FakeReleaseClient, out io.Writer) *cobra.Command // runReleaseCases runs a set of release cases through the given releaseCmd. func runReleaseCases(t *testing.T, tests []releaseCase, rcmd releaseCmd) { var buf bytes.Buffer for _, tt := range tests { - c := &fakeReleaseClient{ - rels: []*release.Release{tt.resp}, + c := &helm.FakeReleaseClient{ + Rels: []*release.Release{tt.resp}, } cmd := rcmd(c, &buf) cmd.ParseFlags(tt.flags) diff --git a/cmd/helm/history_test.go b/cmd/helm/history_test.go index 5f57e1748..954a79867 100644 --- a/cmd/helm/history_test.go +++ b/cmd/helm/history_test.go @@ -21,6 +21,7 @@ import ( "regexp" "testing" + "k8s.io/helm/pkg/helm" rpb "k8s.io/helm/pkg/proto/hapi/release" ) @@ -66,7 +67,7 @@ func TestHistoryCmd(t *testing.T) { var buf bytes.Buffer for _, tt := range tests { - frc := &fakeReleaseClient{rels: tt.resp} + frc := &helm.FakeReleaseClient{Rels: tt.resp} cmd := newHistoryCmd(frc, &buf) cmd.ParseFlags(tt.args) diff --git a/cmd/helm/install_test.go b/cmd/helm/install_test.go index b2e47a1dd..7f0fe27f4 100644 --- a/cmd/helm/install_test.go +++ b/cmd/helm/install_test.go @@ -24,6 +24,8 @@ import ( "testing" "github.com/spf13/cobra" + + "k8s.io/helm/pkg/helm" ) func TestInstall(t *testing.T) { @@ -146,7 +148,7 @@ func TestInstall(t *testing.T) { }, } - runReleaseCases(t, tests, func(c *fakeReleaseClient, out io.Writer) *cobra.Command { + runReleaseCases(t, tests, func(c *helm.FakeReleaseClient, out io.Writer) *cobra.Command { return newInstallCmd(c, out) }) } diff --git a/cmd/helm/list_test.go b/cmd/helm/list_test.go index 611f47973..4c1775b1b 100644 --- a/cmd/helm/list_test.go +++ b/cmd/helm/list_test.go @@ -21,6 +21,7 @@ import ( "regexp" "testing" + "k8s.io/helm/pkg/helm" "k8s.io/helm/pkg/proto/hapi/release" ) @@ -101,8 +102,8 @@ func TestListCmd(t *testing.T) { var buf bytes.Buffer for _, tt := range tests { - c := &fakeReleaseClient{ - rels: tt.resp, + c := &helm.FakeReleaseClient{ + Rels: tt.resp, } cmd := newListCmd(c, &buf) cmd.ParseFlags(tt.args) diff --git a/cmd/helm/release_testing_test.go b/cmd/helm/release_testing_test.go index 2c7a5867a..aa179ca8b 100644 --- a/cmd/helm/release_testing_test.go +++ b/cmd/helm/release_testing_test.go @@ -20,6 +20,7 @@ import ( "bytes" "testing" + "k8s.io/helm/pkg/helm" "k8s.io/helm/pkg/proto/hapi/release" ) @@ -82,7 +83,7 @@ func TestReleaseTesting(t *testing.T) { } for _, tt := range tests { - c := &fakeReleaseClient{responses: tt.responses} + c := &helm.FakeReleaseClient{Responses: tt.responses} buf := bytes.NewBuffer(nil) cmd := newReleaseTestCmd(c, buf) diff --git a/cmd/helm/reset_test.go b/cmd/helm/reset_test.go index 5df8f2234..fe2477ce1 100644 --- a/cmd/helm/reset_test.go +++ b/cmd/helm/reset_test.go @@ -26,6 +26,7 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake" + "k8s.io/helm/pkg/helm" "k8s.io/helm/pkg/helm/helmpath" "k8s.io/helm/pkg/proto/hapi/release" ) @@ -38,7 +39,7 @@ func TestResetCmd(t *testing.T) { defer os.Remove(home) var buf bytes.Buffer - c := &fakeReleaseClient{} + c := &helm.FakeReleaseClient{} fc := fake.NewSimpleClientset() cmd := &resetCmd{ out: &buf, @@ -71,7 +72,7 @@ func TestResetCmd_removeHelmHome(t *testing.T) { defer os.Remove(home) var buf bytes.Buffer - c := &fakeReleaseClient{} + c := &helm.FakeReleaseClient{} fc := fake.NewSimpleClientset() cmd := &resetCmd{ removeHelmHome: true, @@ -108,8 +109,8 @@ func TestReset_deployedReleases(t *testing.T) { resp := []*release.Release{ releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}), } - c := &fakeReleaseClient{ - rels: resp, + c := &helm.FakeReleaseClient{ + Rels: resp, } fc := fake.NewSimpleClientset() cmd := &resetCmd{ @@ -140,8 +141,8 @@ func TestReset_forceFlag(t *testing.T) { resp := []*release.Release{ releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}), } - c := &fakeReleaseClient{ - rels: resp, + c := &helm.FakeReleaseClient{ + Rels: resp, } fc := fake.NewSimpleClientset() cmd := &resetCmd{ diff --git a/cmd/helm/rollback_test.go b/cmd/helm/rollback_test.go index f02068247..c71b894f5 100644 --- a/cmd/helm/rollback_test.go +++ b/cmd/helm/rollback_test.go @@ -21,6 +21,8 @@ import ( "testing" "github.com/spf13/cobra" + + "k8s.io/helm/pkg/helm" ) func TestRollbackCmd(t *testing.T) { @@ -50,7 +52,7 @@ func TestRollbackCmd(t *testing.T) { }, } - cmd := func(c *fakeReleaseClient, out io.Writer) *cobra.Command { + cmd := func(c *helm.FakeReleaseClient, out io.Writer) *cobra.Command { return newRollbackCmd(c, out) } diff --git a/cmd/helm/status_test.go b/cmd/helm/status_test.go index 0ab0db08f..7ab6542ac 100644 --- a/cmd/helm/status_test.go +++ b/cmd/helm/status_test.go @@ -26,6 +26,7 @@ import ( "github.com/golang/protobuf/ptypes/timestamp" "github.com/spf13/cobra" + "k8s.io/helm/pkg/helm" "k8s.io/helm/pkg/proto/hapi/release" "k8s.io/helm/pkg/timeconv" ) @@ -106,14 +107,14 @@ func TestStatusCmd(t *testing.T) { }, } - scmd := func(c *fakeReleaseClient, out io.Writer) *cobra.Command { + scmd := func(c *helm.FakeReleaseClient, out io.Writer) *cobra.Command { return newStatusCmd(c, out) } var buf bytes.Buffer for _, tt := range tests { - c := &fakeReleaseClient{ - rels: []*release.Release{tt.rel}, + c := &helm.FakeReleaseClient{ + Rels: []*release.Release{tt.rel}, } cmd := scmd(c, &buf) cmd.ParseFlags(tt.flags) diff --git a/cmd/helm/upgrade_test.go b/cmd/helm/upgrade_test.go index d9bff45bb..66abe7107 100644 --- a/cmd/helm/upgrade_test.go +++ b/cmd/helm/upgrade_test.go @@ -26,6 +26,7 @@ import ( "github.com/spf13/cobra" "k8s.io/helm/pkg/chartutil" + "k8s.io/helm/pkg/helm" "k8s.io/helm/pkg/proto/hapi/chart" ) @@ -152,7 +153,7 @@ func TestUpgradeCmd(t *testing.T) { }, } - cmd := func(c *fakeReleaseClient, out io.Writer) *cobra.Command { + cmd := func(c *helm.FakeReleaseClient, out io.Writer) *cobra.Command { return newUpgradeCmd(c, out) } diff --git a/cmd/helm/version_test.go b/cmd/helm/version_test.go index d22373b68..1438c8be7 100644 --- a/cmd/helm/version_test.go +++ b/cmd/helm/version_test.go @@ -20,6 +20,7 @@ import ( "strings" "testing" + "k8s.io/helm/pkg/helm" "k8s.io/helm/pkg/version" ) @@ -42,7 +43,7 @@ func TestVersion(t *testing.T) { settings.TillerHost = "fake-localhost" for _, tt := range tests { b := new(bytes.Buffer) - c := &fakeReleaseClient{} + c := &helm.FakeReleaseClient{} cmd := newVersionCmd(c, b) cmd.ParseFlags(tt.args) diff --git a/pkg/helm/fake.go b/pkg/helm/fake.go new file mode 100644 index 000000000..c6f8e9027 --- /dev/null +++ b/pkg/helm/fake.go @@ -0,0 +1,147 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package helm // import "k8s.io/helm/pkg/helm" + +import ( + "fmt" + "sync" + + "k8s.io/helm/pkg/proto/hapi/chart" + "k8s.io/helm/pkg/proto/hapi/release" + rls "k8s.io/helm/pkg/proto/hapi/services" + "k8s.io/helm/pkg/proto/hapi/version" +) + +// FakeReleaseClient implements Interface +type FakeReleaseClient struct { + Rels []*release.Release + Responses map[string]release.TestRun_Status + Err error +} + +var _ Interface = &FakeReleaseClient{} +var _ Interface = (*FakeReleaseClient)(nil) + +// Listreleases lists the current releases +func (c *FakeReleaseClient) ListReleases(opts ...ReleaseListOption) (*rls.ListReleasesResponse, error) { + resp := &rls.ListReleasesResponse{ + Count: int64(len(c.Rels)), + Releases: c.Rels, + } + return resp, c.Err +} + +// InstallRelease returns a response with the first Release on the fake release client +func (c *FakeReleaseClient) InstallRelease(chStr, ns string, opts ...InstallOption) (*rls.InstallReleaseResponse, error) { + return &rls.InstallReleaseResponse{ + Release: c.Rels[0], + }, nil +} + +// InstallReleaseFromChart returns a response with the first Release on the fake release client +func (c *FakeReleaseClient) InstallReleaseFromChart(chart *chart.Chart, ns string, opts ...InstallOption) (*rls.InstallReleaseResponse, error) { + return &rls.InstallReleaseResponse{ + Release: c.Rels[0], + }, nil +} + +// DeleteRelease returns nil, nil +func (c *FakeReleaseClient) DeleteRelease(rlsName string, opts ...DeleteOption) (*rls.UninstallReleaseResponse, error) { + return nil, nil +} + +// UpdateRelease returns nil, nil +func (c *FakeReleaseClient) UpdateRelease(rlsName string, chStr string, opts ...UpdateOption) (*rls.UpdateReleaseResponse, error) { + return nil, nil +} + +// GetVersion returns a fake version +func (c *FakeReleaseClient) GetVersion(opts ...VersionOption) (*rls.GetVersionResponse, error) { + return &rls.GetVersionResponse{ + Version: &version.Version{ + SemVer: "1.2.3-fakeclient+testonly", + }, + }, nil +} + +// UpdateReleaseFromChart returns nil, nil +func (c *FakeReleaseClient) UpdateReleaseFromChart(rlsName string, chart *chart.Chart, opts ...UpdateOption) (*rls.UpdateReleaseResponse, error) { + return nil, nil +} + +// RollbackRelease returns nil, nil +func (c *FakeReleaseClient) RollbackRelease(rlsName string, opts ...RollbackOption) (*rls.RollbackReleaseResponse, error) { + return nil, nil +} + +// ReleaseStatus returns a release status response with info from the first release in the fake +// release client +func (c *FakeReleaseClient) ReleaseStatus(rlsName string, opts ...StatusOption) (*rls.GetReleaseStatusResponse, error) { + if c.Rels[0] != nil { + return &rls.GetReleaseStatusResponse{ + Name: c.Rels[0].Name, + Info: c.Rels[0].Info, + Namespace: c.Rels[0].Namespace, + }, nil + } + return nil, fmt.Errorf("No such release: %s", rlsName) +} + +// ReleaseContent returns the configuration for the first release in the fake release client +func (c *FakeReleaseClient) ReleaseContent(rlsName string, opts ...ContentOption) (resp *rls.GetReleaseContentResponse, err error) { + if len(c.Rels) > 0 { + resp = &rls.GetReleaseContentResponse{ + Release: c.Rels[0], + } + } + return resp, c.Err +} + +// ReleaseHistory returns a release's revision history. +func (c *FakeReleaseClient) ReleaseHistory(rlsName string, opts ...HistoryOption) (*rls.GetHistoryResponse, error) { + return &rls.GetHistoryResponse{Releases: c.Rels}, c.Err +} + +// RunReleaseTest executes a pre-defined tests on a release +func (c *FakeReleaseClient) RunReleaseTest(rlsName string, opts ...ReleaseTestOption) (<-chan *rls.TestReleaseResponse, <-chan error) { + + results := make(chan *rls.TestReleaseResponse) + errc := make(chan error, 1) + + go func() { + var wg sync.WaitGroup + for m, s := range c.Responses { + wg.Add(1) + + go func(msg string, status release.TestRun_Status) { + defer wg.Done() + results <- &rls.TestReleaseResponse{Msg: msg, Status: status} + }(m, s) + } + + wg.Wait() + close(results) + close(errc) + }() + + return results, errc +} + +// Option returns the fake release client +func (c *FakeReleaseClient) Option(opt ...Option) Interface { + return c +}