github.com/pensu/helm@v2.6.1+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/helm" 25 "k8s.io/helm/pkg/proto/hapi/release" 26 ) 27 28 func TestListCmd(t *testing.T) { 29 tests := []struct { 30 name string 31 args []string 32 resp []*release.Release 33 expected string 34 err bool 35 }{ 36 { 37 name: "with a release", 38 resp: []*release.Release{ 39 releaseMock(&releaseOptions{name: "thomas-guide"}), 40 }, 41 expected: "thomas-guide", 42 }, 43 { 44 name: "list", 45 args: []string{}, 46 resp: []*release.Release{ 47 releaseMock(&releaseOptions{name: "atlas"}), 48 }, 49 expected: "NAME \tREVISION\tUPDATED \tSTATUS \tCHART \tNAMESPACE\natlas\t1 \t(.*)\tDEPLOYED\tfoo-0.1.0-beta.1\tdefault \n", 50 }, 51 { 52 name: "list, one deployed, one failed", 53 args: []string{"-q"}, 54 resp: []*release.Release{ 55 releaseMock(&releaseOptions{name: "thomas-guide", statusCode: release.Status_FAILED}), 56 releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}), 57 }, 58 expected: "thomas-guide\natlas-guide", 59 }, 60 { 61 name: "with a release, multiple flags", 62 args: []string{"--deleted", "--deployed", "--failed", "-q"}, 63 resp: []*release.Release{ 64 releaseMock(&releaseOptions{name: "thomas-guide", statusCode: release.Status_DELETED}), 65 releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}), 66 }, 67 // Note: We're really only testing that the flags parsed correctly. Which results are returned 68 // depends on the backend. And until pkg/helm is done, we can't mock this. 69 expected: "thomas-guide\natlas-guide", 70 }, 71 { 72 name: "with a release, multiple flags", 73 args: []string{"--all", "-q"}, 74 resp: []*release.Release{ 75 releaseMock(&releaseOptions{name: "thomas-guide", statusCode: release.Status_DELETED}), 76 releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}), 77 }, 78 // See note on previous test. 79 expected: "thomas-guide\natlas-guide", 80 }, 81 { 82 name: "with a release, multiple flags, deleting", 83 args: []string{"--all", "-q"}, 84 resp: []*release.Release{ 85 releaseMock(&releaseOptions{name: "thomas-guide", statusCode: release.Status_DELETING}), 86 releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}), 87 }, 88 // See note on previous test. 89 expected: "thomas-guide\natlas-guide", 90 }, 91 { 92 name: "namespace defined, multiple flags", 93 args: []string{"--all", "-q", "--namespace test123"}, 94 resp: []*release.Release{ 95 releaseMock(&releaseOptions{name: "thomas-guide", namespace: "test123"}), 96 releaseMock(&releaseOptions{name: "atlas-guide", namespace: "test321"}), 97 }, 98 // See note on previous test. 99 expected: "thomas-guide", 100 }, 101 { 102 name: "with a pending release, multiple flags", 103 args: []string{"--all", "-q"}, 104 resp: []*release.Release{ 105 releaseMock(&releaseOptions{name: "thomas-guide", statusCode: release.Status_PENDING_INSTALL}), 106 releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}), 107 }, 108 expected: "thomas-guide\natlas-guide", 109 }, 110 { 111 name: "with a pending release, pending flag", 112 args: []string{"--pending", "-q"}, 113 resp: []*release.Release{ 114 releaseMock(&releaseOptions{name: "thomas-guide", statusCode: release.Status_PENDING_INSTALL}), 115 releaseMock(&releaseOptions{name: "wild-idea", statusCode: release.Status_PENDING_UPGRADE}), 116 releaseMock(&releaseOptions{name: "crazy-maps", statusCode: release.Status_PENDING_ROLLBACK}), 117 releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}), 118 }, 119 expected: "thomas-guide\nwild-idea\ncrazy-maps", 120 }, 121 } 122 123 var buf bytes.Buffer 124 for _, tt := range tests { 125 c := &helm.FakeClient{ 126 Rels: tt.resp, 127 } 128 cmd := newListCmd(c, &buf) 129 cmd.ParseFlags(tt.args) 130 err := cmd.RunE(cmd, tt.args) 131 if (err != nil) != tt.err { 132 t.Errorf("%q. expected error: %v, got %v", tt.name, tt.err, err) 133 } 134 re := regexp.MustCompile(tt.expected) 135 if !re.Match(buf.Bytes()) { 136 t.Errorf("%q. expected\n%q\ngot\n%q", tt.name, tt.expected, buf.String()) 137 } 138 buf.Reset() 139 } 140 }