github.com/zoumo/helm@v2.5.0+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 ReleaseListOption's are 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 if _, err := NewClient(b4c).ListReleases(ops...); err != errSkip { 91 t.Fatalf("did not expect error but got (%v)\n``", err) 92 } 93 } 94 95 // Verify InstallOption's are applied to an InstallReleaseRequest correctly. 96 func TestInstallRelease_VerifyOptions(t *testing.T) { 97 // Options testdata 98 var disableHooks = true 99 var releaseName = "test" 100 var namespace = "default" 101 var reuseName = true 102 var dryRun = true 103 var chartName = "alpine" 104 var chartPath = filepath.Join(chartsDir, chartName) 105 var overrides = []byte("key1=value1,key2=value2") 106 107 // Expected InstallReleaseRequest message 108 exp := &tpb.InstallReleaseRequest{ 109 Chart: loadChart(t, chartName), 110 Values: &cpb.Config{Raw: string(overrides)}, 111 DryRun: dryRun, 112 Name: releaseName, 113 DisableHooks: disableHooks, 114 Namespace: namespace, 115 ReuseName: reuseName, 116 } 117 118 // Options used in InstallRelease 119 ops := []InstallOption{ 120 ValueOverrides(overrides), 121 InstallDryRun(dryRun), 122 ReleaseName(releaseName), 123 InstallReuseName(reuseName), 124 InstallDisableHooks(disableHooks), 125 } 126 127 // BeforeCall option to intercept helm client InstallReleaseRequest 128 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 129 switch act := msg.(type) { 130 case *tpb.InstallReleaseRequest: 131 t.Logf("InstallReleaseRequest: %#+v\n", act) 132 assert(t, exp, act) 133 default: 134 t.Fatalf("expected message of type InstallReleaseRequest, got %T\n", act) 135 } 136 return errSkip 137 }) 138 139 if _, err := NewClient(b4c).InstallRelease(chartPath, namespace, ops...); err != errSkip { 140 t.Fatalf("did not expect error but got (%v)\n``", err) 141 } 142 } 143 144 // Verify DeleteOptions's are applied to an UninstallReleaseRequest correctly. 145 func TestDeleteRelease_VerifyOptions(t *testing.T) { 146 // Options testdata 147 var releaseName = "test" 148 var disableHooks = true 149 var purgeFlag = true 150 151 // Expected DeleteReleaseRequest message 152 exp := &tpb.UninstallReleaseRequest{ 153 Name: releaseName, 154 Purge: purgeFlag, 155 DisableHooks: disableHooks, 156 } 157 158 // Options used in DeleteRelease 159 ops := []DeleteOption{ 160 DeletePurge(purgeFlag), 161 DeleteDisableHooks(disableHooks), 162 } 163 164 // BeforeCall option to intercept helm client DeleteReleaseRequest 165 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 166 switch act := msg.(type) { 167 case *tpb.UninstallReleaseRequest: 168 t.Logf("UninstallReleaseRequest: %#+v\n", act) 169 assert(t, exp, act) 170 default: 171 t.Fatalf("expected message of type UninstallReleaseRequest, got %T\n", act) 172 } 173 return errSkip 174 }) 175 176 if _, err := NewClient(b4c).DeleteRelease(releaseName, ops...); err != errSkip { 177 t.Fatalf("did not expect error but got (%v)\n``", err) 178 } 179 } 180 181 // Verify UpdateOption's are applied to an UpdateReleaseRequest correctly. 182 func TestUpdateRelease_VerifyOptions(t *testing.T) { 183 // Options testdata 184 var chartName = "alpine" 185 var chartPath = filepath.Join(chartsDir, chartName) 186 var releaseName = "test" 187 var disableHooks = true 188 var overrides = []byte("key1=value1,key2=value2") 189 var dryRun = false 190 191 // Expected UpdateReleaseRequest message 192 exp := &tpb.UpdateReleaseRequest{ 193 Name: releaseName, 194 Chart: loadChart(t, chartName), 195 Values: &cpb.Config{Raw: string(overrides)}, 196 DryRun: dryRun, 197 DisableHooks: disableHooks, 198 } 199 200 // Options used in UpdateRelease 201 ops := []UpdateOption{ 202 UpgradeDryRun(dryRun), 203 UpdateValueOverrides(overrides), 204 UpgradeDisableHooks(disableHooks), 205 } 206 207 // BeforeCall option to intercept helm client UpdateReleaseRequest 208 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 209 switch act := msg.(type) { 210 case *tpb.UpdateReleaseRequest: 211 t.Logf("UpdateReleaseRequest: %#+v\n", act) 212 assert(t, exp, act) 213 default: 214 t.Fatalf("expected message of type UpdateReleaseRequest, got %T\n", act) 215 } 216 return errSkip 217 }) 218 219 if _, err := NewClient(b4c).UpdateRelease(releaseName, chartPath, ops...); err != errSkip { 220 t.Fatalf("did not expect error but got (%v)\n``", err) 221 } 222 } 223 224 // Verify RollbackOption's are applied to a RollbackReleaseRequest correctly. 225 func TestRollbackRelease_VerifyOptions(t *testing.T) { 226 // Options testdata 227 var disableHooks = true 228 var releaseName = "test" 229 var revision = int32(2) 230 var dryRun = true 231 232 // Expected RollbackReleaseRequest message 233 exp := &tpb.RollbackReleaseRequest{ 234 Name: releaseName, 235 DryRun: dryRun, 236 Version: revision, 237 DisableHooks: disableHooks, 238 } 239 240 // Options used in RollbackRelease 241 ops := []RollbackOption{ 242 RollbackDryRun(dryRun), 243 RollbackVersion(revision), 244 RollbackDisableHooks(disableHooks), 245 } 246 247 // BeforeCall option to intercept helm client RollbackReleaseRequest 248 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 249 switch act := msg.(type) { 250 case *tpb.RollbackReleaseRequest: 251 t.Logf("RollbackReleaseRequest: %#+v\n", act) 252 assert(t, exp, act) 253 default: 254 t.Fatalf("expected message of type RollbackReleaseRequest, got %T\n", act) 255 } 256 return errSkip 257 }) 258 259 if _, err := NewClient(b4c).RollbackRelease(releaseName, ops...); err != errSkip { 260 t.Fatalf("did not expect error but got (%v)\n``", err) 261 } 262 } 263 264 // Verify StatusOption's are applied to a GetReleaseStatusRequest correctly. 265 func TestReleaseStatus_VerifyOptions(t *testing.T) { 266 // Options testdata 267 var releaseName = "test" 268 var revision = int32(2) 269 270 // Expected GetReleaseStatusRequest message 271 exp := &tpb.GetReleaseStatusRequest{ 272 Name: releaseName, 273 Version: revision, 274 } 275 276 // BeforeCall option to intercept helm client GetReleaseStatusRequest 277 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 278 switch act := msg.(type) { 279 case *tpb.GetReleaseStatusRequest: 280 t.Logf("GetReleaseStatusRequest: %#+v\n", act) 281 assert(t, exp, act) 282 default: 283 t.Fatalf("expected message of type GetReleaseStatusRequest, got %T\n", act) 284 } 285 return errSkip 286 }) 287 288 if _, err := NewClient(b4c).ReleaseStatus(releaseName, StatusReleaseVersion(revision)); err != errSkip { 289 t.Fatalf("did not expect error but got (%v)\n``", err) 290 } 291 } 292 293 // Verify ContentOption's are applied to a GetReleaseContentRequest correctly. 294 func TestReleaseContent_VerifyOptions(t *testing.T) { 295 // Options testdata 296 var releaseName = "test" 297 var revision = int32(2) 298 299 // Expected GetReleaseContentRequest message 300 exp := &tpb.GetReleaseContentRequest{ 301 Name: releaseName, 302 Version: revision, 303 } 304 305 // BeforeCall option to intercept helm client GetReleaseContentRequest 306 b4c := BeforeCall(func(_ context.Context, msg proto.Message) error { 307 switch act := msg.(type) { 308 case *tpb.GetReleaseContentRequest: 309 t.Logf("GetReleaseContentRequest: %#+v\n", act) 310 assert(t, exp, act) 311 default: 312 t.Fatalf("expected message of type GetReleaseContentRequest, got %T\n", act) 313 } 314 return errSkip 315 }) 316 317 if _, err := NewClient(b4c).ReleaseContent(releaseName, ContentReleaseVersion(revision)); err != errSkip { 318 t.Fatalf("did not expect error but got (%v)\n``", err) 319 } 320 } 321 322 func assert(t *testing.T, expect, actual interface{}) { 323 if !reflect.DeepEqual(expect, actual) { 324 t.Fatalf("expected %#+v, actual %#+v\n", expect, actual) 325 } 326 } 327 328 func loadChart(t *testing.T, name string) *cpb.Chart { 329 c, err := chartutil.Load(filepath.Join(chartsDir, name)) 330 if err != nil { 331 t.Fatalf("failed to load test chart (%q): %s\n", name, err) 332 } 333 return c 334 }