github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/command/v3/v3_set_env_command_test.go (about) 1 package v3_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/sharedaction" 7 "code.cloudfoundry.org/cli/actor/v3action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 "code.cloudfoundry.org/cli/command/translatableerror" 11 "code.cloudfoundry.org/cli/command/v3" 12 "code.cloudfoundry.org/cli/command/v3/v3fakes" 13 "code.cloudfoundry.org/cli/util/configv3" 14 "code.cloudfoundry.org/cli/util/ui" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 . "github.com/onsi/gomega/gbytes" 18 ) 19 20 var _ = Describe("v3-set-env Command", func() { 21 var ( 22 cmd v3.V3SetEnvCommand 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v3fakes.FakeV3SetEnvActor 27 binaryName string 28 executeErr error 29 appName string 30 ) 31 32 BeforeEach(func() { 33 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 34 fakeConfig = new(commandfakes.FakeConfig) 35 fakeSharedActor = new(commandfakes.FakeSharedActor) 36 fakeActor = new(v3fakes.FakeV3SetEnvActor) 37 38 cmd = v3.V3SetEnvCommand{ 39 UI: testUI, 40 Config: fakeConfig, 41 SharedActor: fakeSharedActor, 42 Actor: fakeActor, 43 } 44 45 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionV3) 46 binaryName = "faceman" 47 fakeConfig.BinaryNameReturns(binaryName) 48 appName = "some-app" 49 50 cmd.RequiredArgs.AppName = appName 51 cmd.RequiredArgs.EnvironmentVariableName = "some-key" 52 cmd.RequiredArgs.EnvironmentVariableValue = "some-value" 53 }) 54 55 JustBeforeEach(func() { 56 executeErr = cmd.Execute(nil) 57 }) 58 59 Context("when the API version is below the minimum", func() { 60 BeforeEach(func() { 61 fakeActor.CloudControllerAPIVersionReturns("0.0.0") 62 }) 63 64 It("returns a MinimumAPIVersionNotMetError", func() { 65 Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{ 66 CurrentVersion: "0.0.0", 67 MinimumVersion: ccversion.MinVersionV3, 68 })) 69 }) 70 }) 71 72 Context("when checking target fails", func() { 73 BeforeEach(func() { 74 fakeSharedActor.CheckTargetReturns(sharedaction.NotLoggedInError{BinaryName: binaryName}) 75 }) 76 77 It("returns an error", func() { 78 Expect(executeErr).To(MatchError(translatableerror.NotLoggedInError{BinaryName: binaryName})) 79 80 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 81 _, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 82 Expect(checkTargetedOrg).To(BeTrue()) 83 Expect(checkTargetedSpace).To(BeTrue()) 84 }) 85 }) 86 87 Context("when the user is logged in, an org is targeted and a space is targeted", func() { 88 BeforeEach(func() { 89 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"}) 90 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 91 }) 92 93 Context("when getting the current user returns an error", func() { 94 BeforeEach(func() { 95 fakeConfig.CurrentUserReturns(configv3.User{}, errors.New("some-error")) 96 }) 97 98 It("returns the error", func() { 99 Expect(executeErr).To(MatchError("some-error")) 100 }) 101 }) 102 103 Context("when getting the current user succeeds", func() { 104 BeforeEach(func() { 105 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 106 }) 107 108 Context("when setting the environment succeeds", func() { 109 BeforeEach(func() { 110 fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceReturns(v3action.Warnings{"set-warning-1", "set-warning-2"}, nil) 111 }) 112 113 It("sets the environment variable and value pair", func() { 114 Expect(executeErr).ToNot(HaveOccurred()) 115 116 Expect(testUI.Out).To(Say("Setting env variable some-key for app some-app in org some-org / space some-space as banana\\.\\.\\.")) 117 118 Expect(testUI.Err).To(Say("set-warning-1")) 119 Expect(testUI.Err).To(Say("set-warning-2")) 120 Expect(testUI.Out).To(Say("OK")) 121 Expect(testUI.Out).To(Say("TIP: Use 'cf v3-stage some-app' to ensure your env variable changes take effect\\.")) 122 123 Expect(fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceCallCount()).To(Equal(1)) 124 appName, spaceGUID, envVariablePair := fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceArgsForCall(0) 125 Expect(appName).To(Equal("some-app")) 126 Expect(spaceGUID).To(Equal("some-space-guid")) 127 Expect(envVariablePair.Key).To(Equal("some-key")) 128 Expect(envVariablePair.Value).To(Equal("some-value")) 129 }) 130 }) 131 132 Context("when the set environment variable returns an unknown error", func() { 133 var expectedErr error 134 BeforeEach(func() { 135 expectedErr = errors.New("some-error") 136 fakeActor.SetEnvironmentVariableByApplicationNameAndSpaceReturns(v3action.Warnings{"get-warning-1", "get-warning-2"}, expectedErr) 137 }) 138 139 It("returns the error", func() { 140 Expect(executeErr).To(Equal(expectedErr)) 141 Expect(testUI.Out).To(Say("Setting env variable some-key for app some-app in org some-org / space some-space as banana\\.\\.\\.")) 142 143 Expect(testUI.Err).To(Say("get-warning-1")) 144 Expect(testUI.Err).To(Say("get-warning-2")) 145 Expect(testUI.Out).ToNot(Say("OK")) 146 }) 147 }) 148 }) 149 }) 150 })