mirror of https://github.com/helm/helm
Merge pull request #921 from adamreese/ref/command-tests
ref(cmd): refactor cmd for unit testingpull/924/head
commit
d5aa4852a7
@ -0,0 +1,73 @@
|
||||
/*
|
||||
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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/helm/pkg/helm"
|
||||
)
|
||||
|
||||
var getManifestHelp = `
|
||||
This command fetches the generated manifest for a given release.
|
||||
|
||||
A manifest is a YAML-encoded representation of the Kubernetes resources that
|
||||
were generated from this release's chart(s). If a chart is dependent on other
|
||||
charts, those resources will also be included in the manifest.
|
||||
`
|
||||
|
||||
type getManifestCmd struct {
|
||||
release string
|
||||
out io.Writer
|
||||
client helm.Interface
|
||||
}
|
||||
|
||||
func newGetManifestCmd(client helm.Interface, out io.Writer) *cobra.Command {
|
||||
get := &getManifestCmd{
|
||||
out: out,
|
||||
client: client,
|
||||
}
|
||||
cmd := &cobra.Command{
|
||||
Use: "manifest [flags] RELEASE_NAME",
|
||||
Short: "download the manifest for a named release",
|
||||
Long: getManifestHelp,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return errReleaseRequired
|
||||
}
|
||||
get.release = args[0]
|
||||
if get.client == nil {
|
||||
get.client = helm.NewClient(helm.HelmHost(helm.Config.ServAddr))
|
||||
}
|
||||
return get.run()
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
|
||||
// getManifest implements 'helm get manifest'
|
||||
func (g *getManifestCmd) run() error {
|
||||
res, err := g.client.ReleaseContent(g.release)
|
||||
if err != nil {
|
||||
return prettyError(err)
|
||||
}
|
||||
fmt.Fprintln(g.out, res.Release.Manifest)
|
||||
return nil
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
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 main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"k8s.io/helm/pkg/proto/hapi/release"
|
||||
)
|
||||
|
||||
func TestGetCmd(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
resp *release.Release
|
||||
expected string
|
||||
err bool
|
||||
}{
|
||||
{
|
||||
name: "with a release",
|
||||
resp: releaseMock("thomas-guide"),
|
||||
args: []string{"thomas-guide"},
|
||||
expected: "CHART: foo-0.1.0-beta.1\nRELEASED: (.*)\nUSER-SUPPLIED VALUES:\nname: \"value\"\nCOMPUTED VALUES:\nname: value\n\nMANIFEST:",
|
||||
},
|
||||
{
|
||||
name: "requires release name arg",
|
||||
err: true,
|
||||
},
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
for _, tt := range tests {
|
||||
c := &fakeReleaseClient{
|
||||
rels: []*release.Release{tt.resp},
|
||||
}
|
||||
cmd := newGetCmd(c, &buf)
|
||||
err := cmd.RunE(cmd, tt.args)
|
||||
if (err != nil) != tt.err {
|
||||
t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err)
|
||||
}
|
||||
re := regexp.MustCompile(tt.expected)
|
||||
if !re.Match(buf.Bytes()) {
|
||||
t.Errorf("%q. expected %q, got %q", tt.name, tt.expected, buf.String())
|
||||
}
|
||||
buf.Reset()
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
/*
|
||||
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 main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/helm/pkg/chartutil"
|
||||
"k8s.io/helm/pkg/helm"
|
||||
)
|
||||
|
||||
var getValuesHelp = `
|
||||
This command downloads a values file for a given release.
|
||||
`
|
||||
|
||||
type getValuesCmd struct {
|
||||
release string
|
||||
allValues bool
|
||||
out io.Writer
|
||||
client helm.Interface
|
||||
}
|
||||
|
||||
func newGetValuesCmd(client helm.Interface, out io.Writer) *cobra.Command {
|
||||
get := &getValuesCmd{
|
||||
out: out,
|
||||
client: client,
|
||||
}
|
||||
cmd := &cobra.Command{
|
||||
Use: "values [flags] RELEASE_NAME",
|
||||
Short: "download the values file for a named release",
|
||||
Long: getValuesHelp,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return errReleaseRequired
|
||||
}
|
||||
get.release = args[0]
|
||||
if get.client == nil {
|
||||
get.client = helm.NewClient(helm.HelmHost(helm.Config.ServAddr))
|
||||
}
|
||||
return get.run()
|
||||
},
|
||||
}
|
||||
cmd.Flags().BoolVarP(&get.allValues, "all", "a", false, "dump all (computed) values")
|
||||
return cmd
|
||||
}
|
||||
|
||||
// getValues implements 'helm get values'
|
||||
func (g *getValuesCmd) run() error {
|
||||
res, err := g.client.ReleaseContent(g.release)
|
||||
if err != nil {
|
||||
return prettyError(err)
|
||||
}
|
||||
|
||||
// If the user wants all values, compute the values and return.
|
||||
if g.allValues {
|
||||
cfg, err := chartutil.CoalesceValues(res.Release.Chart, res.Release.Config, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfgStr, err := cfg.YAML()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintln(g.out, cfgStr)
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Fprintln(g.out, res.Release.Config.Raw)
|
||||
return nil
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
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 main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"k8s.io/helm/pkg/proto/hapi/release"
|
||||
)
|
||||
|
||||
func TestGetValuesCmd(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
resp *release.Release
|
||||
expected string
|
||||
err bool
|
||||
}{
|
||||
{
|
||||
name: "with a release",
|
||||
resp: releaseMock("thomas-guide"),
|
||||
args: []string{"thomas-guide"},
|
||||
expected: "name: \"value\"",
|
||||
},
|
||||
{
|
||||
name: "requires release name arg",
|
||||
err: true,
|
||||
},
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
for _, tt := range tests {
|
||||
c := &fakeReleaseClient{
|
||||
rels: []*release.Release{tt.resp},
|
||||
}
|
||||
cmd := newGetValuesCmd(c, &buf)
|
||||
err := cmd.RunE(cmd, tt.args)
|
||||
if (err != nil) != tt.err {
|
||||
t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err)
|
||||
}
|
||||
actual := string(bytes.TrimSpace(buf.Bytes()))
|
||||
if actual != tt.expected {
|
||||
t.Errorf("%q. expected %q, got %q", tt.name, tt.expected, actual)
|
||||
}
|
||||
buf.Reset()
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
/*
|
||||
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 main
|
||||
|
||||
import (
|
||||
"github.com/golang/protobuf/ptypes/timestamp"
|
||||
|
||||
"k8s.io/helm/pkg/helm"
|
||||
"k8s.io/helm/pkg/proto/hapi/chart"
|
||||
"k8s.io/helm/pkg/proto/hapi/release"
|
||||
rls "k8s.io/helm/pkg/proto/hapi/services"
|
||||
)
|
||||
|
||||
func releaseMock(name string) *release.Release {
|
||||
date := timestamp.Timestamp{Seconds: 242085845, Nanos: 0}
|
||||
return &release.Release{
|
||||
Name: name,
|
||||
Info: &release.Info{
|
||||
FirstDeployed: &date,
|
||||
LastDeployed: &date,
|
||||
Status: &release.Status{Code: release.Status_DEPLOYED},
|
||||
},
|
||||
Chart: &chart.Chart{
|
||||
Metadata: &chart.Metadata{
|
||||
Name: "foo",
|
||||
Version: "0.1.0-beta.1",
|
||||
},
|
||||
Templates: []*chart.Template{
|
||||
{Name: "foo.tpl", Data: []byte("Hello")},
|
||||
},
|
||||
},
|
||||
Config: &chart.Config{Raw: `name: "value"`},
|
||||
}
|
||||
}
|
||||
|
||||
type fakeReleaseClient struct {
|
||||
rels []*release.Release
|
||||
err error
|
||||
}
|
||||
|
||||
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 string, opts ...helm.InstallOption) (*rls.InstallReleaseResponse, error) {
|
||||
return nil, 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) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *fakeReleaseClient) UpdateRelease(rlsName string, opts ...helm.UpdateOption) (*rls.UpdateReleaseResponse, 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
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
/*
|
||||
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 main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"k8s.io/helm/pkg/proto/hapi/release"
|
||||
)
|
||||
|
||||
func TestListRun(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
listCmd *listCmd
|
||||
expected string
|
||||
err bool
|
||||
}{
|
||||
{
|
||||
name: "with a release",
|
||||
listCmd: &listCmd{
|
||||
client: &fakeReleaseClient{
|
||||
rels: []*release.Release{
|
||||
releaseMock("thomas-guide"),
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: "thomas-guide",
|
||||
},
|
||||
{
|
||||
name: "list --long",
|
||||
listCmd: &listCmd{
|
||||
client: &fakeReleaseClient{
|
||||
rels: []*release.Release{
|
||||
releaseMock("atlas"),
|
||||
},
|
||||
},
|
||||
long: true,
|
||||
},
|
||||
expected: "NAME \tUPDATED \tSTATUS \tCHART \natlas\t(.*)\tDEPLOYED\tfoo-0.1.0-beta.1",
|
||||
},
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
for _, tt := range tests {
|
||||
tt.listCmd.out = &buf
|
||||
err := tt.listCmd.run()
|
||||
if (err != nil) != tt.err {
|
||||
t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err)
|
||||
}
|
||||
re := regexp.MustCompile(tt.expected)
|
||||
if !re.Match(buf.Bytes()) {
|
||||
t.Errorf("%q. expected %q, got %q", tt.name, tt.expected, buf.String())
|
||||
}
|
||||
buf.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
func TestListCmd(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
flags map[string]string
|
||||
resp []*release.Release
|
||||
expected string
|
||||
err bool
|
||||
}{
|
||||
{
|
||||
name: "with a release",
|
||||
resp: []*release.Release{
|
||||
releaseMock("thomas-guide"),
|
||||
},
|
||||
expected: "thomas-guide",
|
||||
},
|
||||
{
|
||||
name: "list --long",
|
||||
flags: map[string]string{"long": "1"},
|
||||
resp: []*release.Release{
|
||||
releaseMock("atlas"),
|
||||
},
|
||||
expected: "NAME \tUPDATED \tSTATUS \tCHART \natlas\t(.*)\tDEPLOYED\tfoo-0.1.0-beta.1",
|
||||
},
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
for _, tt := range tests {
|
||||
c := &fakeReleaseClient{
|
||||
rels: tt.resp,
|
||||
}
|
||||
cmd := newListCmd(c, &buf)
|
||||
for flag, value := range tt.flags {
|
||||
cmd.Flags().Set(flag, value)
|
||||
}
|
||||
err := cmd.RunE(cmd, tt.args)
|
||||
if (err != nil) != tt.err {
|
||||
t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err)
|
||||
}
|
||||
re := regexp.MustCompile(tt.expected)
|
||||
if !re.Match(buf.Bytes()) {
|
||||
t.Errorf("%q. expected %q, got %q", tt.name, tt.expected, buf.String())
|
||||
}
|
||||
buf.Reset()
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
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 (
|
||||
rls "k8s.io/helm/pkg/proto/hapi/services"
|
||||
)
|
||||
|
||||
// Interface for helm client for mocking in tests
|
||||
type Interface interface {
|
||||
ListReleases(opts ...ReleaseListOption) (*rls.ListReleasesResponse, error)
|
||||
InstallRelease(chStr string, opts ...InstallOption) (*rls.InstallReleaseResponse, error)
|
||||
DeleteRelease(rlsName string, opts ...DeleteOption) (*rls.UninstallReleaseResponse, error)
|
||||
ReleaseStatus(rlsName string, opts ...StatusOption) (*rls.GetReleaseStatusResponse, error)
|
||||
UpdateRelease(rlsName string, opts ...UpdateOption) (*rls.UpdateReleaseResponse, error)
|
||||
ReleaseContent(rlsName string, opts ...ContentOption) (*rls.GetReleaseContentResponse, error)
|
||||
}
|
Loading…
Reference in new issue