github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/bind_service_command_test.go (about) 1 package v2_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v2action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 10 "code.cloudfoundry.org/cli/command/commandfakes" 11 "code.cloudfoundry.org/cli/command/translatableerror" 12 . "code.cloudfoundry.org/cli/command/v2" 13 "code.cloudfoundry.org/cli/command/v2/v2fakes" 14 "code.cloudfoundry.org/cli/util/configv3" 15 "code.cloudfoundry.org/cli/util/ui" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 . "github.com/onsi/gomega/gbytes" 19 ) 20 21 var _ = Describe("bind-service Command", func() { 22 var ( 23 cmd BindServiceCommand 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 fakeSharedActor *commandfakes.FakeSharedActor 27 fakeActor *v2fakes.FakeBindServiceActor 28 binaryName string 29 executeErr error 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(v2fakes.FakeBindServiceActor) 37 38 cmd = BindServiceCommand{ 39 UI: testUI, 40 Config: fakeConfig, 41 SharedActor: fakeSharedActor, 42 Actor: fakeActor, 43 } 44 45 cmd.RequiredArgs.AppName = "some-app" 46 cmd.RequiredArgs.ServiceInstanceName = "some-service" 47 cmd.ParametersAsJSON = map[string]interface{}{ 48 "some-parameter": "some-value", 49 } 50 51 binaryName = "faceman" 52 fakeConfig.BinaryNameReturns("faceman") 53 }) 54 55 JustBeforeEach(func() { 56 executeErr = cmd.Execute(nil) 57 }) 58 59 Context("when a cloud controller API endpoint is set", func() { 60 BeforeEach(func() { 61 fakeConfig.TargetReturns("some-url") 62 }) 63 64 Context("when checking target fails", func() { 65 BeforeEach(func() { 66 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 67 }) 68 69 It("returns an error", func() { 70 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 71 72 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 73 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 74 Expect(checkTargetedOrg).To(BeTrue()) 75 Expect(checkTargetedSpace).To(BeTrue()) 76 }) 77 }) 78 79 Context("when the user is logged in, and an org and space are targeted", func() { 80 BeforeEach(func() { 81 fakeConfig.CurrentUserReturns( 82 configv3.User{Name: "some-user"}, 83 nil) 84 fakeConfig.HasTargetedOrganizationReturns(true) 85 fakeConfig.HasTargetedSpaceReturns(true) 86 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 87 GUID: "some-org-guid", 88 Name: "some-org", 89 }) 90 fakeConfig.TargetedSpaceReturns(configv3.Space{ 91 GUID: "some-space-guid", 92 Name: "some-space", 93 }) 94 }) 95 96 Context("when a binding name is not passed", func() { 97 It("displays flavor text", func() { 98 Expect(testUI.Out).To(Say("Binding service some-service to app some-app in org some-org / space some-space as some-user...")) 99 100 Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1)) 101 }) 102 103 It("displays OK and the TIP", func() { 104 Expect(executeErr).ToNot(HaveOccurred()) 105 106 Expect(fakeActor.BindServiceBySpaceCallCount()).To(Equal(1)) 107 appName, serviceInstanceName, spaceGUID, bindingName, parameters := fakeActor.BindServiceBySpaceArgsForCall(0) 108 Expect(appName).To(Equal("some-app")) 109 Expect(serviceInstanceName).To(Equal("some-service")) 110 Expect(spaceGUID).To(Equal("some-space-guid")) 111 Expect(bindingName).To(BeEmpty()) 112 Expect(parameters).To(Equal(map[string]interface{}{"some-parameter": "some-value"})) 113 }) 114 }) 115 116 Context("when passed a binding name", func() { 117 BeforeEach(func() { 118 cmd.BindingName.Value = "some-binding-name" 119 }) 120 121 Context("when the version check fails", func() { 122 BeforeEach(func() { 123 fakeActor.CloudControllerAPIVersionReturns("2.34.0") 124 }) 125 126 It("returns a MinimumAPIVersionNotMetError", func() { 127 Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{ 128 Command: "Option '--name'", 129 CurrentVersion: "2.34.0", 130 MinimumVersion: ccversion.MinVersionProvideNameForServiceBinding, 131 })) 132 Expect(fakeActor.CloudControllerAPIVersionCallCount()).To(Equal(1)) 133 }) 134 }) 135 136 Context("when the version check succeeds", func() { 137 BeforeEach(func() { 138 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionProvideNameForServiceBinding) 139 }) 140 141 Context("when getting the current user returns an error", func() { 142 var expectedErr error 143 144 BeforeEach(func() { 145 expectedErr = errors.New("got bananapants??") 146 fakeConfig.CurrentUserReturns( 147 configv3.User{}, 148 expectedErr) 149 }) 150 151 It("returns the error", func() { 152 Expect(executeErr).To(MatchError(expectedErr)) 153 }) 154 }) 155 156 Context("when getting the current user does not return an error", func() { 157 BeforeEach(func() { 158 }) 159 160 It("displays flavor text", func() { 161 Expect(testUI.Out).To(Say("Binding service some-service to app some-app with binding name some-binding-name in org some-org / space some-space as some-user...")) 162 163 Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1)) 164 }) 165 166 Context("when the service was already bound", func() { 167 BeforeEach(func() { 168 fakeActor.BindServiceBySpaceReturns( 169 []string{"foo", "bar"}, 170 ccerror.ServiceBindingTakenError{}) 171 }) 172 173 It("displays warnings and 'OK'", func() { 174 Expect(executeErr).NotTo(HaveOccurred()) 175 176 Expect(testUI.Err).To(Say("foo")) 177 Expect(testUI.Err).To(Say("bar")) 178 Expect(testUI.Out).To(Say("App some-app is already bound to some-service.")) 179 Expect(testUI.Out).To(Say("OK")) 180 }) 181 }) 182 183 Context("when binding the service instance results in an error other than ServiceBindingTakenError", func() { 184 BeforeEach(func() { 185 fakeActor.BindServiceBySpaceReturns( 186 nil, 187 actionerror.ApplicationNotFoundError{Name: "some-app"}) 188 }) 189 190 It("should return the error", func() { 191 Expect(executeErr).To(MatchError(actionerror.ApplicationNotFoundError{ 192 Name: "some-app", 193 })) 194 }) 195 }) 196 197 Context("when the service binding is successful", func() { 198 BeforeEach(func() { 199 fakeActor.BindServiceBySpaceReturns( 200 v2action.Warnings{"some-warning", "another-warning"}, 201 nil, 202 ) 203 }) 204 205 It("displays OK and the TIP", func() { 206 Expect(executeErr).ToNot(HaveOccurred()) 207 208 Expect(testUI.Out).To(Say("OK")) 209 Expect(testUI.Out).To(Say("TIP: Use 'faceman restage some-app' to ensure your env variable changes take effect")) 210 Expect(testUI.Err).To(Say("some-warning")) 211 Expect(testUI.Err).To(Say("another-warning")) 212 213 Expect(fakeActor.BindServiceBySpaceCallCount()).To(Equal(1)) 214 appName, serviceInstanceName, spaceGUID, bindingName, parameters := fakeActor.BindServiceBySpaceArgsForCall(0) 215 Expect(appName).To(Equal("some-app")) 216 Expect(serviceInstanceName).To(Equal("some-service")) 217 Expect(spaceGUID).To(Equal("some-space-guid")) 218 Expect(bindingName).To(Equal("some-binding-name")) 219 Expect(parameters).To(Equal(map[string]interface{}{"some-parameter": "some-value"})) 220 }) 221 }) 222 }) 223 }) 224 }) 225 }) 226 }) 227 })