github.com/lrills/helm@v2.8.1+incompatible/pkg/helm/helm_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 helm // import "k8s.io/helm/pkg/helm" 18 19 import ( 20 "errors" 21 "path/filepath" 22 "reflect" 23 "testing" 24 25 "github.com/golang/protobuf/proto" 26 "golang.org/x/net/context" 27 28 "k8s.io/helm/pkg/chartutil" 29 cpb "k8s.io/helm/pkg/proto/hapi/chart" 30 rls "k8s.io/helm/pkg/proto/hapi/release" 31 tpb "k8s.io/helm/pkg/proto/hapi/services" 32 ) 33 34 // Path to example charts relative to pkg/helm. 35 const chartsDir = "../../docs/examples/" 36 37 // Sentinel error to indicate to the Helm client to not send the request to Tiller. 38 var errSkip = errors.New("test: skip") 39 40 // Verify each ReleaseListOption is applied to a ListReleasesRequest correctly. 41 func TestListReleases_VerifyOptions(t *testing.T) { 42 // Options testdata 43 var limit = 2 44 var offset = "offset" 45 var filter = "filter" 46 var sortBy = int32(2) 47 var sortOrd = int32(1) 48 var codes = []rls.Status_Code{ 49 rls.Status_FAILED, 50 rls.Status_DELETED, 51 rls.Status_DEPLOYED, 52 rls.Status_SUPERSEDED, 53 } 54 var namespace = "namespace" 55 56 // Expected ListReleasesRequest message 57 exp := &tpb.ListReleasesRequest{ 58 Limit: int64(limit), 59 Offset: offset, 60 Filter: filter, 61 SortBy: tpb.ListSort_SortBy(sortBy), 62 SortOrder: tpb.ListSort_SortOrder(sortOrd), 63 StatusCodes: codes, 64 Namespace: namespace, 65 } 66 67 // Options used in ListReleases 68 ops := []ReleaseListOption{ 69 ReleaseListSort(sortBy), 70 ReleaseListOrder(sortOrd), 71 ReleaseListLimit(limit), 72 ReleaseListOffset(offset), 73 ReleaseListFilter(filter), 74 ReleaseListStatuses(codes), 75 ReleaseListNamespace(namespace), 76 } 77 78 // BeforeCall option to intercept Helm client ListReleasesRequest 79 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 80 switch act := msg.(type) { 81 case *tpb.ListReleasesRequest: 82 t.Logf("ListReleasesRequest: %#+v\n", act) 83 assert(t, exp, act) 84 default: 85 t.Fatalf("expected message of type ListReleasesRequest, got %T\n", act) 86 } 87 return errSkip 88 }) 89 90 client := NewClient(b4c) 91 92 if _, err := client.ListReleases(ops...); err != errSkip { 93 t.Fatalf("did not expect error but got (%v)\n``", err) 94 } 95 96 // ensure options for call are not saved to client 97 assert(t, "", client.opts.listReq.Filter) 98 } 99 100 // Verify each InstallOption is applied to an InstallReleaseRequest correctly. 101 func TestInstallRelease_VerifyOptions(t *testing.T) { 102 // Options testdata 103 var disableHooks = true 104 var releaseName = "test" 105 var namespace = "default" 106 var reuseName = true 107 var dryRun = true 108 var chartName = "alpine" 109 var chartPath = filepath.Join(chartsDir, chartName) 110 var overrides = []byte("key1=value1,key2=value2") 111 112 // Expected InstallReleaseRequest message 113 exp := &tpb.InstallReleaseRequest{ 114 Chart: loadChart(t, chartName), 115 Values: &cpb.Config{Raw: string(overrides)}, 116 DryRun: dryRun, 117 Name: releaseName, 118 DisableHooks: disableHooks, 119 Namespace: namespace, 120 ReuseName: reuseName, 121 } 122 123 // Options used in InstallRelease 124 ops := []InstallOption{ 125 ValueOverrides(overrides), 126 InstallDryRun(dryRun), 127 ReleaseName(releaseName), 128 InstallReuseName(reuseName), 129 InstallDisableHooks(disableHooks), 130 } 131 132 // BeforeCall option to intercept Helm client InstallReleaseRequest 133 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 134 switch act := msg.(type) { 135 case *tpb.InstallReleaseRequest: 136 t.Logf("InstallReleaseRequest: %#+v\n", act) 137 assert(t, exp, act) 138 default: 139 t.Fatalf("expected message of type InstallReleaseRequest, got %T\n", act) 140 } 141 return errSkip 142 }) 143 144 client := NewClient(b4c) 145 if _, err := client.InstallRelease(chartPath, namespace, ops...); err != errSkip { 146 t.Fatalf("did not expect error but got (%v)\n``", err) 147 } 148 149 // ensure options for call are not saved to client 150 assert(t, "", client.opts.instReq.Name) 151 } 152 153 // Verify each DeleteOptions is applied to an UninstallReleaseRequest correctly. 154 func TestDeleteRelease_VerifyOptions(t *testing.T) { 155 // Options testdata 156 var releaseName = "test" 157 var disableHooks = true 158 var purgeFlag = true 159 160 // Expected DeleteReleaseRequest message 161 exp := &tpb.UninstallReleaseRequest{ 162 Name: releaseName, 163 Purge: purgeFlag, 164 DisableHooks: disableHooks, 165 } 166 167 // Options used in DeleteRelease 168 ops := []DeleteOption{ 169 DeletePurge(purgeFlag), 170 DeleteDisableHooks(disableHooks), 171 } 172 173 // BeforeCall option to intercept Helm client DeleteReleaseRequest 174 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 175 switch act := msg.(type) { 176 case *tpb.UninstallReleaseRequest: 177 t.Logf("UninstallReleaseRequest: %#+v\n", act) 178 assert(t, exp, act) 179 default: 180 t.Fatalf("expected message of type UninstallReleaseRequest, got %T\n", act) 181 } 182 return errSkip 183 }) 184 185 client := NewClient(b4c) 186 if _, err := client.DeleteRelease(releaseName, ops...); err != errSkip { 187 t.Fatalf("did not expect error but got (%v)\n``", err) 188 } 189 190 // ensure options for call are not saved to client 191 assert(t, "", client.opts.uninstallReq.Name) 192 } 193 194 // Verify each UpdateOption is applied to an UpdateReleaseRequest correctly. 195 func TestUpdateRelease_VerifyOptions(t *testing.T) { 196 // Options testdata 197 var chartName = "alpine" 198 var chartPath = filepath.Join(chartsDir, chartName) 199 var releaseName = "test" 200 var disableHooks = true 201 var overrides = []byte("key1=value1,key2=value2") 202 var dryRun = false 203 204 // Expected UpdateReleaseRequest message 205 exp := &tpb.UpdateReleaseRequest{ 206 Name: releaseName, 207 Chart: loadChart(t, chartName), 208 Values: &cpb.Config{Raw: string(overrides)}, 209 DryRun: dryRun, 210 DisableHooks: disableHooks, 211 } 212 213 // Options used in UpdateRelease 214 ops := []UpdateOption{ 215 UpgradeDryRun(dryRun), 216 UpdateValueOverrides(overrides), 217 UpgradeDisableHooks(disableHooks), 218 } 219 220 // BeforeCall option to intercept Helm client UpdateReleaseRequest 221 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 222 switch act := msg.(type) { 223 case *tpb.UpdateReleaseRequest: 224 t.Logf("UpdateReleaseRequest: %#+v\n", act) 225 assert(t, exp, act) 226 default: 227 t.Fatalf("expected message of type UpdateReleaseRequest, got %T\n", act) 228 } 229 return errSkip 230 }) 231 232 client := NewClient(b4c) 233 if _, err := client.UpdateRelease(releaseName, chartPath, ops...); err != errSkip { 234 t.Fatalf("did not expect error but got (%v)\n``", err) 235 } 236 237 // ensure options for call are not saved to client 238 assert(t, "", client.opts.updateReq.Name) 239 } 240 241 // Verify each RollbackOption is applied to a RollbackReleaseRequest correctly. 242 func TestRollbackRelease_VerifyOptions(t *testing.T) { 243 // Options testdata 244 var disableHooks = true 245 var releaseName = "test" 246 var revision = int32(2) 247 var dryRun = true 248 249 // Expected RollbackReleaseRequest message 250 exp := &tpb.RollbackReleaseRequest{ 251 Name: releaseName, 252 DryRun: dryRun, 253 Version: revision, 254 DisableHooks: disableHooks, 255 } 256 257 // Options used in RollbackRelease 258 ops := []RollbackOption{ 259 RollbackDryRun(dryRun), 260 RollbackVersion(revision), 261 RollbackDisableHooks(disableHooks), 262 } 263 264 // BeforeCall option to intercept Helm client RollbackReleaseRequest 265 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 266 switch act := msg.(type) { 267 case *tpb.RollbackReleaseRequest: 268 t.Logf("RollbackReleaseRequest: %#+v\n", act) 269 assert(t, exp, act) 270 default: 271 t.Fatalf("expected message of type RollbackReleaseRequest, got %T\n", act) 272 } 273 return errSkip 274 }) 275 276 client := NewClient(b4c) 277 if _, err := client.RollbackRelease(releaseName, ops...); err != errSkip { 278 t.Fatalf("did not expect error but got (%v)\n``", err) 279 } 280 281 // ensure options for call are not saved to client 282 assert(t, "", client.opts.rollbackReq.Name) 283 } 284 285 // Verify each StatusOption is applied to a GetReleaseStatusRequest correctly. 286 func TestReleaseStatus_VerifyOptions(t *testing.T) { 287 // Options testdata 288 var releaseName = "test" 289 var revision = int32(2) 290 291 // Expected GetReleaseStatusRequest message 292 exp := &tpb.GetReleaseStatusRequest{ 293 Name: releaseName, 294 Version: revision, 295 } 296 297 // BeforeCall option to intercept Helm client GetReleaseStatusRequest 298 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 299 switch act := msg.(type) { 300 case *tpb.GetReleaseStatusRequest: 301 t.Logf("GetReleaseStatusRequest: %#+v\n", act) 302 assert(t, exp, act) 303 default: 304 t.Fatalf("expected message of type GetReleaseStatusRequest, got %T\n", act) 305 } 306 return errSkip 307 }) 308 309 client := NewClient(b4c) 310 if _, err := client.ReleaseStatus(releaseName, StatusReleaseVersion(revision)); err != errSkip { 311 t.Fatalf("did not expect error but got (%v)\n``", err) 312 } 313 314 // ensure options for call are not saved to client 315 assert(t, "", client.opts.statusReq.Name) 316 } 317 318 // Verify each ContentOption is applied to a GetReleaseContentRequest correctly. 319 func TestReleaseContent_VerifyOptions(t *testing.T) { 320 // Options testdata 321 var releaseName = "test" 322 var revision = int32(2) 323 324 // Expected GetReleaseContentRequest message 325 exp := &tpb.GetReleaseContentRequest{ 326 Name: releaseName, 327 Version: revision, 328 } 329 330 // BeforeCall option to intercept Helm client GetReleaseContentRequest 331 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 332 switch act := msg.(type) { 333 case *tpb.GetReleaseContentRequest: 334 t.Logf("GetReleaseContentRequest: %#+v\n", act) 335 assert(t, exp, act) 336 default: 337 t.Fatalf("expected message of type GetReleaseContentRequest, got %T\n", act) 338 } 339 return errSkip 340 }) 341 342 client := NewClient(b4c) 343 if _, err := client.ReleaseContent(releaseName, ContentReleaseVersion(revision)); err != errSkip { 344 t.Fatalf("did not expect error but got (%v)\n``", err) 345 } 346 347 // ensure options for call are not saved to client 348 assert(t, "", client.opts.contentReq.Name) 349 } 350 351 func assert(t *testing.T, expect, actual interface{}) { 352 if !reflect.DeepEqual(expect, actual) { 353 t.Fatalf("expected %#+v, actual %#+v\n", expect, actual) 354 } 355 } 356 357 func loadChart(t *testing.T, name string) *cpb.Chart { 358 c, err := chartutil.Load(filepath.Join(chartsDir, name)) 359 if err != nil { 360 t.Fatalf("failed to load test chart (%q): %s\n", name, err) 361 } 362 return c 363 }