github.com/Thanhphan1147/cloudfoundry-cli@v7.1.0+incompatible/actor/v7action/environment_variable_test.go (about) 1 package v7action_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 . "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/actor/v7action/v7actionfakes" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 11 "code.cloudfoundry.org/cli/resources" 12 "code.cloudfoundry.org/cli/types" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 ) 16 17 var _ = Describe("Environment Variable Actions", func() { 18 Describe("GetEnvironmentVariableGroup", func() { 19 var ( 20 actor *Actor 21 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 22 fetchedEnvVariables EnvironmentVariableGroup 23 executeErr error 24 warnings Warnings 25 ) 26 27 BeforeEach(func() { 28 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 29 actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil) 30 }) 31 32 JustBeforeEach(func() { 33 fetchedEnvVariables, warnings, executeErr = actor.GetEnvironmentVariableGroup(constant.StagingEnvironmentVariableGroup) 34 }) 35 36 When("getting the environment variable group fails", func() { 37 BeforeEach(func() { 38 fakeCloudControllerClient.GetEnvironmentVariableGroupReturns( 39 nil, 40 ccv3.Warnings{"get-env-var-group-warning"}, 41 errors.New("get-env-var-group-error"), 42 ) 43 }) 44 45 It("fetches the environment variable group from CC", func() { 46 Expect(fakeCloudControllerClient.GetEnvironmentVariableGroupCallCount()).To(Equal(1)) 47 Expect(fakeCloudControllerClient.GetEnvironmentVariableGroupArgsForCall(0)).To(Equal(constant.StagingEnvironmentVariableGroup)) 48 }) 49 50 It("returns warnings and error", func() { 51 Expect(executeErr).To(MatchError("get-env-var-group-error")) 52 Expect(warnings).To(ConsistOf("get-env-var-group-warning")) 53 }) 54 }) 55 56 When("getting the environment variable group succeeds", func() { 57 var envVars ccv3.EnvironmentVariables 58 59 BeforeEach(func() { 60 envVars = map[string]types.FilteredString{ 61 "var_one": {IsSet: true, Value: "val_one"}, 62 } 63 64 fakeCloudControllerClient.GetEnvironmentVariableGroupReturns( 65 envVars, 66 ccv3.Warnings{"get-env-var-group-warning"}, 67 nil, 68 ) 69 }) 70 71 It("fetches the environment variable group from CC", func() { 72 Expect(fakeCloudControllerClient.GetEnvironmentVariableGroupCallCount()).To(Equal(1)) 73 Expect(fakeCloudControllerClient.GetEnvironmentVariableGroupArgsForCall(0)).To(Equal(constant.StagingEnvironmentVariableGroup)) 74 }) 75 76 It("returns result and warnings", func() { 77 Expect(executeErr).NotTo(HaveOccurred()) 78 Expect(fetchedEnvVariables).To(Equal(EnvironmentVariableGroup(envVars))) 79 Expect(warnings).To(ConsistOf("get-env-var-group-warning")) 80 }) 81 }) 82 }) 83 84 Describe("SetEnvironmentVariableGroup", func() { 85 var ( 86 actor *Actor 87 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 88 executeErr error 89 warnings Warnings 90 envVars ccv3.EnvironmentVariables 91 ) 92 93 BeforeEach(func() { 94 envVars = ccv3.EnvironmentVariables{} 95 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 96 actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil) 97 }) 98 99 JustBeforeEach(func() { 100 warnings, executeErr = actor.SetEnvironmentVariableGroup(constant.RunningEnvironmentVariableGroup, envVars) 101 }) 102 103 When("Setting the environment variable group fails", func() { 104 When("user passes some env var group", func() { 105 BeforeEach(func() { 106 envVars = ccv3.EnvironmentVariables{ 107 "key1": {Value: "val1", IsSet: true}, 108 "key2": {Value: "val2", IsSet: true}, 109 } 110 111 fakeCloudControllerClient.UpdateEnvironmentVariableGroupReturns( 112 nil, 113 ccv3.Warnings{"update-env-var-group-warning"}, 114 errors.New("update-env-var-group-error"), 115 ) 116 }) 117 118 It("sets the environment variable group via CC", func() { 119 Expect(fakeCloudControllerClient.UpdateEnvironmentVariableGroupCallCount()).To(Equal(1)) 120 actualGroup, actualEnvPair := fakeCloudControllerClient.UpdateEnvironmentVariableGroupArgsForCall(0) 121 Expect(actualGroup).To(Equal(constant.RunningEnvironmentVariableGroup)) 122 Expect(actualEnvPair).To(Equal(envVars)) 123 }) 124 125 It("returns warnings and error", func() { 126 Expect(executeErr).To(MatchError("update-env-var-group-error")) 127 Expect(warnings).To(ConsistOf("update-env-var-group-warning")) 128 }) 129 130 }) 131 132 When("user passes in '{}'", func() { 133 BeforeEach(func() { 134 fakeCloudControllerClient.GetEnvironmentVariableGroupReturns( 135 ccv3.EnvironmentVariables{}, 136 ccv3.Warnings{}, 137 errors.New("I love my corgi, Pancho!"), 138 ) 139 }) 140 141 It("propagates the error", func() { 142 Expect(executeErr).To(HaveOccurred()) 143 Expect(executeErr).To(MatchError(errors.New("I love my corgi, Pancho!"))) 144 }) 145 }) 146 }) 147 148 When("Setting the environment variable group succeeds", func() { 149 When("user passes some env var group", func() { 150 BeforeEach(func() { 151 fakeCloudControllerClient.UpdateEnvironmentVariableGroupReturns( 152 ccv3.EnvironmentVariables{ 153 "key1": {Value: "val1", IsSet: true}, 154 "key2": {Value: "val2", IsSet: true}, 155 }, 156 ccv3.Warnings{"update-env-var-group-warning"}, 157 nil, 158 ) 159 }) 160 161 It("makes the API call to update the environment variable group and returns all warnings", func() { 162 Expect(fakeCloudControllerClient.UpdateEnvironmentVariableGroupCallCount()).To(Equal(1)) 163 actualGroup, actualEnvPair := fakeCloudControllerClient.UpdateEnvironmentVariableGroupArgsForCall(0) 164 Expect(actualGroup).To(Equal(constant.RunningEnvironmentVariableGroup)) 165 Expect(actualEnvPair).To(Equal(envVars)) 166 Expect(executeErr).ToNot(HaveOccurred()) 167 Expect(warnings).To(ConsistOf("update-env-var-group-warning")) 168 }) 169 }) 170 171 When("user passes in '{}'", func() { 172 BeforeEach(func() { 173 fakeCloudControllerClient.GetEnvironmentVariableGroupReturns( 174 ccv3.EnvironmentVariables{ 175 "delete-me1": {Value: "val1", IsSet: true}, 176 "delete-me2": {Value: "val2", IsSet: true}, 177 }, 178 ccv3.Warnings{"get-env-var-group-warning"}, 179 nil, 180 ) 181 fakeCloudControllerClient.UpdateEnvironmentVariableGroupReturns( 182 ccv3.EnvironmentVariables{}, 183 ccv3.Warnings{"update-env-var-group-warning"}, 184 nil, 185 ) 186 }) 187 188 It("nils the values of existing vars", func() { 189 Expect(fakeCloudControllerClient.GetEnvironmentVariableGroupCallCount()).To(Equal(1)) 190 actualGroup := fakeCloudControllerClient.GetEnvironmentVariableGroupArgsForCall(0) 191 Expect(actualGroup).To(Equal(constant.RunningEnvironmentVariableGroup)) 192 193 actualGroup, actualEnvPair := fakeCloudControllerClient.UpdateEnvironmentVariableGroupArgsForCall(0) 194 Expect(actualGroup).To(Equal(constant.RunningEnvironmentVariableGroup)) 195 196 Expect(actualEnvPair).To(Equal(ccv3.EnvironmentVariables{ 197 "delete-me1": {Value: "", IsSet: false}, 198 "delete-me2": {Value: "", IsSet: false}, 199 })) 200 201 Expect(executeErr).ToNot(HaveOccurred()) 202 Expect(warnings).To(ConsistOf("get-env-var-group-warning", "update-env-var-group-warning")) 203 }) 204 }) 205 206 When("user excludes some existing env vars", func() { 207 BeforeEach(func() { 208 envVars = ccv3.EnvironmentVariables{ 209 "keep-me": {Value: "val1", IsSet: true}, 210 } 211 fakeCloudControllerClient.GetEnvironmentVariableGroupReturns( 212 ccv3.EnvironmentVariables{ 213 "keep-me": {Value: "val1", IsSet: true}, 214 "delete-me": {Value: "val2", IsSet: true}, 215 }, 216 ccv3.Warnings{"get-env-var-group-warning"}, 217 nil, 218 ) 219 fakeCloudControllerClient.UpdateEnvironmentVariableGroupReturns( 220 envVars, 221 ccv3.Warnings{"update-env-var-group-warning"}, 222 nil, 223 ) 224 }) 225 226 It("nils the values of excluded existing vars", func() { 227 Expect(fakeCloudControllerClient.GetEnvironmentVariableGroupCallCount()).To(Equal(1)) 228 actualGroup := fakeCloudControllerClient.GetEnvironmentVariableGroupArgsForCall(0) 229 Expect(actualGroup).To(Equal(constant.RunningEnvironmentVariableGroup)) 230 231 actualGroup, actualEnvPair := fakeCloudControllerClient.UpdateEnvironmentVariableGroupArgsForCall(0) 232 Expect(actualGroup).To(Equal(constant.RunningEnvironmentVariableGroup)) 233 Expect(actualEnvPair).To(Equal(ccv3.EnvironmentVariables{ 234 "keep-me": {Value: "val1", IsSet: true}, 235 "delete-me": {Value: "", IsSet: false}, 236 })) 237 238 Expect(executeErr).ToNot(HaveOccurred()) 239 Expect(warnings).To(ConsistOf("get-env-var-group-warning", "update-env-var-group-warning")) 240 }) 241 }) 242 }) 243 }) 244 245 Describe("GetEnvironmentVariablesByApplicationNameAndSpace", func() { 246 var ( 247 actor *Actor 248 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 249 appName string 250 spaceGUID string 251 fetchedEnvVariables EnvironmentVariableGroups 252 executeErr error 253 warnings Warnings 254 ) 255 256 BeforeEach(func() { 257 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 258 actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil) 259 appName = "some-app" 260 spaceGUID = "space-guid" 261 }) 262 263 JustBeforeEach(func() { 264 fetchedEnvVariables, warnings, executeErr = actor.GetEnvironmentVariablesByApplicationNameAndSpace(appName, spaceGUID) 265 }) 266 267 When("finding the app fails", func() { 268 BeforeEach(func() { 269 fakeCloudControllerClient.GetApplicationsReturns(nil, ccv3.Warnings{"get-application-warning"}, errors.New("get-application-error")) 270 }) 271 272 It("returns an error", func() { 273 Expect(executeErr).To(MatchError("get-application-error")) 274 Expect(warnings).To(ConsistOf("get-application-warning")) 275 }) 276 }) 277 278 When("finding the app succeeds", func() { 279 BeforeEach(func() { 280 fakeCloudControllerClient.GetApplicationsReturns([]resources.Application{{Name: "some-app", GUID: "some-app-guid"}}, ccv3.Warnings{"get-application-warning"}, nil) 281 }) 282 283 When("getting the app environment variables fails", func() { 284 BeforeEach(func() { 285 fakeCloudControllerClient.GetApplicationEnvironmentReturns(ccv3.Environment{}, ccv3.Warnings{"some-env-var-warnings"}, errors.New("some-env-var-error")) 286 }) 287 It("returns an error", func() { 288 Expect(executeErr).To(MatchError("some-env-var-error")) 289 Expect(warnings).To(ConsistOf("get-application-warning", "some-env-var-warnings")) 290 }) 291 }) 292 293 When("getting the app environment variables succeeds", func() { 294 BeforeEach(func() { 295 fakeCloudControllerClient.GetApplicationEnvironmentReturns( 296 ccv3.Environment{ 297 System: map[string]interface{}{"system-var": "system-val"}, 298 Application: map[string]interface{}{"app-var": "app-val"}, 299 EnvironmentVariables: map[string]interface{}{"user-var": "user-val"}, 300 Running: map[string]interface{}{"running-var": "running-val"}, 301 Staging: map[string]interface{}{"staging-var": "staging-val"}, 302 }, 303 ccv3.Warnings{"some-env-var-warnings"}, 304 nil, 305 ) 306 }) 307 308 It("makes the API call to get the app environment variables and returns all warnings", func() { 309 Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1)) 310 Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(ConsistOf( 311 ccv3.Query{Key: ccv3.NameFilter, Values: []string{appName}}, 312 ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{spaceGUID}}, 313 )) 314 315 Expect(fakeCloudControllerClient.GetApplicationEnvironmentCallCount()).To(Equal(1)) 316 Expect(fakeCloudControllerClient.GetApplicationEnvironmentArgsForCall(0)).To(Equal("some-app-guid")) 317 Expect(fetchedEnvVariables).To(Equal(EnvironmentVariableGroups{ 318 System: map[string]interface{}{"system-var": "system-val"}, 319 Application: map[string]interface{}{"app-var": "app-val"}, 320 EnvironmentVariables: map[string]interface{}{"user-var": "user-val"}, 321 Running: map[string]interface{}{"running-var": "running-val"}, 322 Staging: map[string]interface{}{"staging-var": "staging-val"}, 323 })) 324 Expect(executeErr).ToNot(HaveOccurred()) 325 Expect(warnings).To(ConsistOf("get-application-warning", "some-env-var-warnings")) 326 }) 327 }) 328 }) 329 }) 330 331 Describe("SetEnvironmentVariableByApplicationNameAndSpace", func() { 332 var ( 333 actor *Actor 334 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 335 appName string 336 spaceGUID string 337 envPair EnvironmentVariablePair 338 executeErr error 339 warnings Warnings 340 ) 341 342 BeforeEach(func() { 343 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 344 actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil) 345 appName = "some-app" 346 spaceGUID = "space-guid" 347 envPair = EnvironmentVariablePair{Key: "my-var", Value: "my-val"} 348 }) 349 350 JustBeforeEach(func() { 351 warnings, executeErr = actor.SetEnvironmentVariableByApplicationNameAndSpace(appName, spaceGUID, envPair) 352 }) 353 354 When("finding the app fails", func() { 355 BeforeEach(func() { 356 fakeCloudControllerClient.GetApplicationsReturns(nil, ccv3.Warnings{"get-application-warning"}, errors.New("get-application-error")) 357 }) 358 359 It("returns an error", func() { 360 Expect(executeErr).To(MatchError("get-application-error")) 361 Expect(warnings).To(ConsistOf("get-application-warning")) 362 }) 363 }) 364 365 When("finding the app succeeds", func() { 366 BeforeEach(func() { 367 fakeCloudControllerClient.GetApplicationsReturns([]resources.Application{{Name: "some-app", GUID: "some-app-guid"}}, ccv3.Warnings{"get-application-warning"}, nil) 368 }) 369 370 When("updating the app environment variables fails", func() { 371 BeforeEach(func() { 372 fakeCloudControllerClient.UpdateApplicationEnvironmentVariablesReturns(ccv3.EnvironmentVariables{}, ccv3.Warnings{"some-env-var-warnings"}, errors.New("some-env-var-error")) 373 }) 374 It("returns an error", func() { 375 Expect(executeErr).To(MatchError("some-env-var-error")) 376 Expect(warnings).To(ConsistOf("get-application-warning", "some-env-var-warnings")) 377 }) 378 }) 379 380 When("updating the app environment variables succeeds", func() { 381 BeforeEach(func() { 382 fakeCloudControllerClient.UpdateApplicationEnvironmentVariablesReturns( 383 ccv3.EnvironmentVariables{ 384 "my-var": {Value: "my-val", IsSet: true}, 385 }, 386 ccv3.Warnings{"some-env-var-warnings"}, 387 nil, 388 ) 389 }) 390 391 It("makes the API call to update the app environment variables and returns all warnings", func() { 392 Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1)) 393 Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(ConsistOf( 394 ccv3.Query{Key: ccv3.NameFilter, Values: []string{appName}}, 395 ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{spaceGUID}}, 396 )) 397 398 Expect(fakeCloudControllerClient.UpdateApplicationEnvironmentVariablesCallCount()).To(Equal(1)) 399 appGUIDArg, envVarsArg := fakeCloudControllerClient.UpdateApplicationEnvironmentVariablesArgsForCall(0) 400 Expect(appGUIDArg).To(Equal("some-app-guid")) 401 Expect(envVarsArg).To(Equal(ccv3.EnvironmentVariables{ 402 "my-var": {Value: "my-val", IsSet: true}, 403 })) 404 Expect(executeErr).ToNot(HaveOccurred()) 405 Expect(warnings).To(ConsistOf("get-application-warning", "some-env-var-warnings")) 406 }) 407 }) 408 }) 409 }) 410 411 Describe("UnsetEnvironmentVariableByApplicationNameAndSpace", func() { 412 var ( 413 actor *Actor 414 fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient 415 appName string 416 spaceGUID string 417 envVariableName string 418 executeErr error 419 warnings Warnings 420 ) 421 422 BeforeEach(func() { 423 fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient) 424 actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil) 425 appName = "some-app" 426 spaceGUID = "space-guid" 427 envVariableName = "my-var" 428 }) 429 430 JustBeforeEach(func() { 431 warnings, executeErr = actor.UnsetEnvironmentVariableByApplicationNameAndSpace(appName, spaceGUID, envVariableName) 432 }) 433 434 When("finding the app fails", func() { 435 BeforeEach(func() { 436 fakeCloudControllerClient.GetApplicationsReturns(nil, ccv3.Warnings{"get-application-warning"}, errors.New("get-application-error")) 437 }) 438 439 It("returns an error", func() { 440 Expect(executeErr).To(MatchError("get-application-error")) 441 Expect(warnings).To(ConsistOf("get-application-warning")) 442 }) 443 }) 444 445 When("finding the app succeeds", func() { 446 BeforeEach(func() { 447 fakeCloudControllerClient.GetApplicationsReturns([]resources.Application{{Name: "some-app", GUID: "some-app-guid"}}, ccv3.Warnings{"get-application-warning"}, nil) 448 }) 449 450 When("getting the app environment variables fails", func() { 451 BeforeEach(func() { 452 fakeCloudControllerClient.GetApplicationEnvironmentReturns(ccv3.Environment{}, ccv3.Warnings{"some-get-env-var-warnings"}, errors.New("some-env-var-error")) 453 }) 454 It("returns an error", func() { 455 Expect(executeErr).To(MatchError("some-env-var-error")) 456 Expect(warnings).To(ConsistOf("get-application-warning", "some-get-env-var-warnings")) 457 }) 458 }) 459 460 When("the variable doesn't exists", func() { 461 BeforeEach(func() { 462 fakeCloudControllerClient.GetApplicationEnvironmentReturns( 463 ccv3.Environment{}, 464 ccv3.Warnings{"some-get-env-var-warnings"}, 465 nil, 466 ) 467 }) 468 It("returns an EnvironmentVariableNotSetError", func() { 469 Expect(executeErr).To(MatchError(actionerror.EnvironmentVariableNotSetError{EnvironmentVariableName: "my-var"})) 470 Expect(warnings).To(ConsistOf("get-application-warning", "some-get-env-var-warnings")) 471 }) 472 }) 473 474 When("the variable exists", func() { 475 BeforeEach(func() { 476 fakeCloudControllerClient.GetApplicationEnvironmentReturns( 477 ccv3.Environment{ 478 EnvironmentVariables: map[string]interface{}{"my-var": "my-val"}, 479 }, 480 ccv3.Warnings{"some-get-env-var-warnings"}, 481 nil, 482 ) 483 }) 484 When("updating the app environment variables fails", func() { 485 BeforeEach(func() { 486 fakeCloudControllerClient.UpdateApplicationEnvironmentVariablesReturns(ccv3.EnvironmentVariables{}, ccv3.Warnings{"some-patch-env-var-warnings"}, errors.New("some-env-var-error")) 487 }) 488 It("returns an error", func() { 489 Expect(executeErr).To(MatchError("some-env-var-error")) 490 Expect(warnings).To(ConsistOf("get-application-warning", "some-get-env-var-warnings", "some-patch-env-var-warnings")) 491 }) 492 }) 493 494 When("updating the app environment variables succeeds", func() { 495 BeforeEach(func() { 496 fakeCloudControllerClient.UpdateApplicationEnvironmentVariablesReturns( 497 ccv3.EnvironmentVariables{ 498 "my-var": {Value: "my-val", IsSet: true}, 499 }, 500 ccv3.Warnings{"some-patch-env-var-warnings"}, 501 nil, 502 ) 503 }) 504 505 It("makes the API call to update the app environment variables and returns all warnings", func() { 506 Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1)) 507 Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(ConsistOf( 508 ccv3.Query{Key: ccv3.NameFilter, Values: []string{appName}}, 509 ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{spaceGUID}}, 510 )) 511 512 Expect(fakeCloudControllerClient.UpdateApplicationEnvironmentVariablesCallCount()).To(Equal(1)) 513 appGUIDArg, envVarsArg := fakeCloudControllerClient.UpdateApplicationEnvironmentVariablesArgsForCall(0) 514 Expect(appGUIDArg).To(Equal("some-app-guid")) 515 Expect(envVarsArg).To(Equal(ccv3.EnvironmentVariables{ 516 "my-var": {Value: "", IsSet: false}, 517 })) 518 519 Expect(executeErr).ToNot(HaveOccurred()) 520 Expect(warnings).To(ConsistOf("get-application-warning", "some-get-env-var-warnings", "some-patch-env-var-warnings")) 521 }) 522 }) 523 }) 524 }) 525 }) 526 })