github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/command/v6/bind_service_command_test.go (about) 1 package v6_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/ccv2" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 12 "code.cloudfoundry.org/cli/command/commandfakes" 13 "code.cloudfoundry.org/cli/command/translatableerror" 14 . "code.cloudfoundry.org/cli/command/v6" 15 "code.cloudfoundry.org/cli/command/v6/v6fakes" 16 "code.cloudfoundry.org/cli/util/configv3" 17 "code.cloudfoundry.org/cli/util/ui" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 . "github.com/onsi/gomega/gbytes" 21 ) 22 23 var _ = Describe("bind-service Command", func() { 24 var ( 25 cmd BindServiceCommand 26 testUI *ui.UI 27 fakeConfig *commandfakes.FakeConfig 28 fakeSharedActor *commandfakes.FakeSharedActor 29 fakeActor *v6fakes.FakeBindServiceActor 30 binaryName string 31 executeErr error 32 ) 33 34 BeforeEach(func() { 35 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 36 fakeConfig = new(commandfakes.FakeConfig) 37 fakeSharedActor = new(commandfakes.FakeSharedActor) 38 fakeActor = new(v6fakes.FakeBindServiceActor) 39 40 cmd = BindServiceCommand{ 41 UI: testUI, 42 Config: fakeConfig, 43 SharedActor: fakeSharedActor, 44 Actor: fakeActor, 45 } 46 47 cmd.RequiredArgs.AppName = "some-app" 48 cmd.RequiredArgs.ServiceInstanceName = "some-service" 49 cmd.ParametersAsJSON = map[string]interface{}{ 50 "some-parameter": "some-value", 51 } 52 53 binaryName = "faceman" 54 fakeConfig.BinaryNameReturns("faceman") 55 }) 56 57 JustBeforeEach(func() { 58 executeErr = cmd.Execute(nil) 59 }) 60 61 When("a cloud controller API endpoint is set", func() { 62 BeforeEach(func() { 63 fakeConfig.TargetReturns("some-url") 64 }) 65 66 When("checking target fails", func() { 67 BeforeEach(func() { 68 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 69 }) 70 71 It("returns an error", func() { 72 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 73 74 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 75 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 76 Expect(checkTargetedOrg).To(BeTrue()) 77 Expect(checkTargetedSpace).To(BeTrue()) 78 }) 79 }) 80 81 When("the user is logged in, and an org and space are targeted", func() { 82 BeforeEach(func() { 83 fakeConfig.CurrentUserReturns( 84 configv3.User{Name: "some-user"}, 85 nil) 86 fakeConfig.HasTargetedOrganizationReturns(true) 87 fakeConfig.HasTargetedSpaceReturns(true) 88 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 89 GUID: "some-org-guid", 90 Name: "some-org", 91 }) 92 fakeConfig.TargetedSpaceReturns(configv3.Space{ 93 GUID: "some-space-guid", 94 Name: "some-space", 95 }) 96 }) 97 98 When("a binding name is not passed", func() { 99 It("displays flavor text", func() { 100 Expect(testUI.Out).To(Say("Binding service some-service to app some-app in org some-org / space some-space as some-user...")) 101 102 Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1)) 103 }) 104 105 It("displays OK and the TIP", func() { 106 Expect(executeErr).ToNot(HaveOccurred()) 107 108 Expect(fakeActor.BindServiceBySpaceCallCount()).To(Equal(1)) 109 appName, serviceInstanceName, spaceGUID, bindingName, parameters := fakeActor.BindServiceBySpaceArgsForCall(0) 110 Expect(appName).To(Equal("some-app")) 111 Expect(serviceInstanceName).To(Equal("some-service")) 112 Expect(spaceGUID).To(Equal("some-space-guid")) 113 Expect(bindingName).To(BeEmpty()) 114 Expect(parameters).To(Equal(map[string]interface{}{"some-parameter": "some-value"})) 115 }) 116 }) 117 118 When("passed a binding name", func() { 119 BeforeEach(func() { 120 cmd.BindingName.Value = "some-binding-name" 121 }) 122 123 When("the version check fails", func() { 124 BeforeEach(func() { 125 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinV2ClientVersion) 126 }) 127 128 It("returns a MinimumAPIVersionNotMetError", func() { 129 Expect(executeErr).To(MatchError(translatableerror.MinimumCFAPIVersionNotMetError{ 130 Command: "Option '--name'", 131 CurrentVersion: ccversion.MinV2ClientVersion, 132 MinimumVersion: ccversion.MinVersionProvideNameForServiceBindingV2, 133 })) 134 Expect(fakeActor.CloudControllerAPIVersionCallCount()).To(Equal(1)) 135 }) 136 }) 137 138 When("the version check succeeds", func() { 139 BeforeEach(func() { 140 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionProvideNameForServiceBindingV2) 141 }) 142 143 When("getting the current user returns an error", func() { 144 var expectedErr error 145 146 BeforeEach(func() { 147 expectedErr = errors.New("got bananapants??") 148 fakeConfig.CurrentUserReturns( 149 configv3.User{}, 150 expectedErr) 151 }) 152 153 It("returns the error", func() { 154 Expect(executeErr).To(MatchError(expectedErr)) 155 }) 156 }) 157 158 When("getting the current user does not return an error", func() { 159 It("displays flavor text", func() { 160 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...")) 161 162 Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1)) 163 }) 164 165 When("the service was already bound", func() { 166 BeforeEach(func() { 167 fakeActor.BindServiceBySpaceReturns( 168 v2action.ServiceBinding{}, 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 When("binding the service instance results in an error other than ServiceBindingTakenError", func() { 184 BeforeEach(func() { 185 fakeActor.BindServiceBySpaceReturns( 186 v2action.ServiceBinding{}, 187 nil, 188 actionerror.ApplicationNotFoundError{Name: "some-app"}) 189 }) 190 191 It("should return the error", func() { 192 Expect(executeErr).To(MatchError(actionerror.ApplicationNotFoundError{ 193 Name: "some-app", 194 })) 195 }) 196 }) 197 198 When("the service binding is successful", func() { 199 BeforeEach(func() { 200 fakeActor.BindServiceBySpaceReturns( 201 v2action.ServiceBinding{}, 202 v2action.Warnings{"some-warning", "another-warning"}, 203 nil, 204 ) 205 }) 206 207 It("displays OK and the TIP", func() { 208 Expect(executeErr).ToNot(HaveOccurred()) 209 210 Expect(testUI.Out).To(Say("OK")) 211 Expect(testUI.Out).To(Say("TIP: Use 'faceman restage some-app' to ensure your env variable changes take effect")) 212 Expect(testUI.Err).To(Say("some-warning")) 213 Expect(testUI.Err).To(Say("another-warning")) 214 215 Expect(fakeActor.BindServiceBySpaceCallCount()).To(Equal(1)) 216 appName, serviceInstanceName, spaceGUID, bindingName, parameters := fakeActor.BindServiceBySpaceArgsForCall(0) 217 Expect(appName).To(Equal("some-app")) 218 Expect(serviceInstanceName).To(Equal("some-service")) 219 Expect(spaceGUID).To(Equal("some-space-guid")) 220 Expect(bindingName).To(Equal("some-binding-name")) 221 Expect(parameters).To(Equal(map[string]interface{}{"some-parameter": "some-value"})) 222 }) 223 }) 224 }) 225 }) 226 }) 227 228 When("the binding is not created asynchroncously", func() { 229 It("does not display binding in progress", func() { 230 Expect(executeErr).ToNot(HaveOccurred()) 231 232 Expect(testUI.Out).ToNot(Say("Binding in progress...")) 233 }) 234 }) 235 236 When("the binding is created asynchroncously", func() { 237 BeforeEach(func() { 238 fakeActor.BindServiceBySpaceReturns( 239 v2action.ServiceBinding{LastOperation: ccv2.LastOperation{State: constant.LastOperationInProgress}}, 240 v2action.Warnings{"some-warning", "another-warning"}, 241 nil, 242 ) 243 244 }) 245 246 It("displays Binding in Progress", func() { 247 Expect(executeErr).ToNot(HaveOccurred()) 248 249 Expect(testUI.Out).To(Say("OK")) 250 Expect(testUI.Out).To(Say("Binding in progress. Use 'faceman service %s' to check operation status.", cmd.RequiredArgs.ServiceInstanceName)) 251 252 Expect(testUI.Err).To(Say("some-warning")) 253 Expect(testUI.Err).To(Say("another-warning")) 254 255 Expect(testUI.Out).To(Say("TIP: Once this operation succeeds, use 'faceman restage %s' to ensure your env variable changes take effect.", cmd.RequiredArgs.AppName)) 256 }) 257 }) 258 }) 259 }) 260 })