github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+incompatible/cmd/helm/list_test.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "bytes" 21 "regexp" 22 "testing" 23 24 "k8s.io/helm/pkg/proto/hapi/release" 25 ) 26 27 func TestListCmd(t *testing.T) { 28 tests := []struct { 29 name string 30 args []string 31 resp []*release.Release 32 expected string 33 err bool 34 }{ 35 { 36 name: "with a release", 37 resp: []*release.Release{ 38 releaseMock(&releaseOptions{name: "thomas-guide"}), 39 }, 40 expected: "thomas-guide", 41 }, 42 { 43 name: "list", 44 args: []string{}, 45 resp: []*release.Release{ 46 releaseMock(&releaseOptions{name: "atlas"}), 47 }, 48 expected: "NAME \tREVISION\tUPDATED \tSTATUS \tCHART \tNAMESPACE\natlas\t1 \t(.*)\tDEPLOYED\tfoo-0.1.0-beta.1\tdefault \n", 49 }, 50 { 51 name: "list, one deployed, one failed", 52 args: []string{"-q"}, 53 resp: []*release.Release{ 54 releaseMock(&releaseOptions{name: "thomas-guide", statusCode: release.Status_FAILED}), 55 releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}), 56 }, 57 expected: "thomas-guide\natlas-guide", 58 }, 59 { 60 name: "with a release, multiple flags", 61 args: []string{"--deleted", "--deployed", "--failed", "-q"}, 62 resp: []*release.Release{ 63 releaseMock(&releaseOptions{name: "thomas-guide", statusCode: release.Status_DELETED}), 64 releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}), 65 }, 66 // Note: We're really only testing that the flags parsed correctly. Which results are returned 67 // depends on the backend. And until pkg/helm is done, we can't mock this. 68 expected: "thomas-guide\natlas-guide", 69 }, 70 { 71 name: "with a release, multiple flags", 72 args: []string{"--all", "-q"}, 73 resp: []*release.Release{ 74 releaseMock(&releaseOptions{name: "thomas-guide", statusCode: release.Status_DELETED}), 75 releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}), 76 }, 77 // See note on previous test. 78 expected: "thomas-guide\natlas-guide", 79 }, 80 { 81 name: "with a release, multiple flags, deleting", 82 args: []string{"--all", "-q"}, 83 resp: []*release.Release{ 84 releaseMock(&releaseOptions{name: "thomas-guide", statusCode: release.Status_DELETING}), 85 releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}), 86 }, 87 // See note on previous test. 88 expected: "thomas-guide\natlas-guide", 89 }, 90 { 91 name: "namespace defined, multiple flags", 92 args: []string{"--all", "-q", "--namespace test123"}, 93 resp: []*release.Release{ 94 releaseMock(&releaseOptions{name: "thomas-guide", namespace: "test123"}), 95 releaseMock(&releaseOptions{name: "atlas-guide", namespace: "test321"}), 96 }, 97 // See note on previous test. 98 expected: "thomas-guide", 99 }, 100 } 101 102 var buf bytes.Buffer 103 for _, tt := range tests { 104 c := &fakeReleaseClient{ 105 rels: tt.resp, 106 } 107 cmd := newListCmd(c, &buf) 108 cmd.ParseFlags(tt.args) 109 err := cmd.RunE(cmd, tt.args) 110 if (err != nil) != tt.err { 111 t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err) 112 } 113 re := regexp.MustCompile(tt.expected) 114 if !re.Match(buf.Bytes()) { 115 t.Errorf("%q. expected\n%q\ngot\n%q", tt.name, tt.expected, buf.String()) 116 } 117 buf.Reset() 118 } 119 }