github.com/sri09kanth/helm@v3.0.0-beta.3+incompatible/pkg/action/list_test.go (about) 1 /* 2 Copyright The Helm Authors. 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 action 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 24 "helm.sh/helm/pkg/release" 25 "helm.sh/helm/pkg/storage" 26 ) 27 28 func TestListStates(t *testing.T) { 29 for input, expect := range map[string]ListStates{ 30 "deployed": ListDeployed, 31 "uninstalled": ListUninstalled, 32 "uninstalling": ListUninstalling, 33 "superseded": ListSuperseded, 34 "failed": ListFailed, 35 "pending-install": ListPendingInstall, 36 "pending-rollback": ListPendingRollback, 37 "pending-upgrade": ListPendingUpgrade, 38 "unknown": ListUnknown, 39 "totally made up key": ListUnknown, 40 } { 41 if expect != expect.FromName(input) { 42 t.Errorf("Expected %d for %s", expect, input) 43 } 44 // This is a cheap way to verify that ListAll actually allows everything but Unknown 45 if got := expect.FromName(input); got != ListUnknown && got&ListAll == 0 { 46 t.Errorf("Expected %s to match the ListAll filter", input) 47 } 48 } 49 50 filter := ListDeployed | ListPendingRollback 51 if status := filter.FromName("deployed"); filter&status == 0 { 52 t.Errorf("Expected %d to match mask %d", status, filter) 53 } 54 if status := filter.FromName("failed"); filter&status != 0 { 55 t.Errorf("Expected %d to fail to match mask %d", status, filter) 56 } 57 } 58 59 func TestList_Empty(t *testing.T) { 60 lister := NewList(actionConfigFixture(t)) 61 list, err := lister.Run() 62 assert.NoError(t, err) 63 assert.Len(t, list, 0) 64 } 65 66 func newListFixture(t *testing.T) *List { 67 return NewList(actionConfigFixture(t)) 68 } 69 70 func TestList_OneNamespace(t *testing.T) { 71 is := assert.New(t) 72 lister := newListFixture(t) 73 makeMeSomeReleases(lister.cfg.Releases, t) 74 list, err := lister.Run() 75 is.NoError(err) 76 is.Len(list, 3) 77 } 78 79 func TestList_AllNamespaces(t *testing.T) { 80 is := assert.New(t) 81 lister := newListFixture(t) 82 makeMeSomeReleases(lister.cfg.Releases, t) 83 lister.AllNamespaces = true 84 lister.SetStateMask() 85 list, err := lister.Run() 86 is.NoError(err) 87 is.Len(list, 3) 88 } 89 90 func TestList_Sort(t *testing.T) { 91 is := assert.New(t) 92 lister := newListFixture(t) 93 lister.Sort = ByNameDesc // Other sorts are tested elsewhere 94 makeMeSomeReleases(lister.cfg.Releases, t) 95 list, err := lister.Run() 96 is.NoError(err) 97 is.Len(list, 3) 98 is.Equal("two", list[0].Name) 99 is.Equal("three", list[1].Name) 100 is.Equal("one", list[2].Name) 101 } 102 103 func TestList_Limit(t *testing.T) { 104 is := assert.New(t) 105 lister := newListFixture(t) 106 lister.Limit = 2 107 makeMeSomeReleases(lister.cfg.Releases, t) 108 list, err := lister.Run() 109 is.NoError(err) 110 is.Len(list, 2) 111 // Lex order means one, three, two 112 is.Equal("one", list[0].Name) 113 is.Equal("three", list[1].Name) 114 } 115 116 func TestList_BigLimit(t *testing.T) { 117 is := assert.New(t) 118 lister := newListFixture(t) 119 lister.Limit = 20 120 makeMeSomeReleases(lister.cfg.Releases, t) 121 list, err := lister.Run() 122 is.NoError(err) 123 is.Len(list, 3) 124 125 // Lex order means one, three, two 126 is.Equal("one", list[0].Name) 127 is.Equal("three", list[1].Name) 128 is.Equal("two", list[2].Name) 129 } 130 131 func TestList_LimitOffset(t *testing.T) { 132 is := assert.New(t) 133 lister := newListFixture(t) 134 lister.Limit = 2 135 lister.Offset = 1 136 makeMeSomeReleases(lister.cfg.Releases, t) 137 list, err := lister.Run() 138 is.NoError(err) 139 is.Len(list, 2) 140 141 // Lex order means one, three, two 142 is.Equal("three", list[0].Name) 143 is.Equal("two", list[1].Name) 144 } 145 146 func TestList_LimitOffsetOutOfBounds(t *testing.T) { 147 is := assert.New(t) 148 lister := newListFixture(t) 149 lister.Limit = 2 150 lister.Offset = 3 // Last item is index 2 151 makeMeSomeReleases(lister.cfg.Releases, t) 152 list, err := lister.Run() 153 is.NoError(err) 154 is.Len(list, 0) 155 156 lister.Limit = 10 157 lister.Offset = 1 158 list, err = lister.Run() 159 is.NoError(err) 160 is.Len(list, 2) 161 } 162 func TestList_StateMask(t *testing.T) { 163 is := assert.New(t) 164 lister := newListFixture(t) 165 makeMeSomeReleases(lister.cfg.Releases, t) 166 one, err := lister.cfg.Releases.Get("one", 1) 167 is.NoError(err) 168 one.SetStatus(release.StatusUninstalled, "uninstalled") 169 lister.cfg.Releases.Update(one) 170 171 res, err := lister.Run() 172 is.NoError(err) 173 is.Len(res, 2) 174 is.Equal("three", res[0].Name) 175 is.Equal("two", res[1].Name) 176 177 lister.StateMask = ListUninstalled 178 res, err = lister.Run() 179 is.NoError(err) 180 is.Len(res, 1) 181 is.Equal("one", res[0].Name) 182 183 lister.StateMask |= ListDeployed 184 res, err = lister.Run() 185 is.NoError(err) 186 is.Len(res, 3) 187 } 188 189 func TestList_Filter(t *testing.T) { 190 is := assert.New(t) 191 lister := newListFixture(t) 192 lister.Filter = "th." 193 makeMeSomeReleases(lister.cfg.Releases, t) 194 195 res, err := lister.Run() 196 is.NoError(err) 197 is.Len(res, 1) 198 is.Equal("three", res[0].Name) 199 } 200 201 func TestList_FilterFailsCompile(t *testing.T) { 202 is := assert.New(t) 203 lister := newListFixture(t) 204 lister.Filter = "t[h.{{{" 205 makeMeSomeReleases(lister.cfg.Releases, t) 206 207 _, err := lister.Run() 208 is.Error(err) 209 } 210 211 func makeMeSomeReleases(store *storage.Storage, t *testing.T) { 212 t.Helper() 213 one := releaseStub() 214 one.Name = "one" 215 one.Namespace = "default" 216 one.Version = 1 217 two := releaseStub() 218 two.Name = "two" 219 two.Namespace = "default" 220 two.Version = 2 221 three := releaseStub() 222 three.Name = "three" 223 three.Namespace = "default" 224 three.Version = 3 225 four := releaseStub() 226 four.Name = "four" 227 four.Namespace = "default" 228 four.Version = 4 229 four.Info.Status = release.StatusSuperseded 230 231 for _, rel := range []*release.Release{one, two, three} { 232 if err := store.Create(rel); err != nil { 233 t.Fatal(err) 234 } 235 } 236 237 all, err := store.ListReleases() 238 assert.NoError(t, err) 239 assert.Len(t, all, 3, "sanity test: three items added") 240 }