github.com/sleungcy/cli@v7.1.0+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/command/commandfakes" 12 . "code.cloudfoundry.org/cli/command/v6" 13 "code.cloudfoundry.org/cli/command/v6/v6fakes" 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 *v6fakes.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(v6fakes.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 When("a cloud controller API endpoint is set", func() { 60 BeforeEach(func() { 61 fakeConfig.TargetReturns("some-url") 62 }) 63 64 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 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 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 When("passed a binding name", func() { 117 BeforeEach(func() { 118 cmd.BindingName.Value = "some-binding-name" 119 }) 120 121 When("getting the current user returns an error", func() { 122 var expectedErr error 123 124 BeforeEach(func() { 125 expectedErr = errors.New("got bananapants??") 126 fakeConfig.CurrentUserReturns( 127 configv3.User{}, 128 expectedErr) 129 }) 130 131 It("returns the error", func() { 132 Expect(executeErr).To(MatchError(expectedErr)) 133 }) 134 }) 135 136 When("getting the current user does not return an error", func() { 137 It("displays flavor text", func() { 138 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...")) 139 140 Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1)) 141 }) 142 143 When("the service was already bound", func() { 144 BeforeEach(func() { 145 fakeActor.BindServiceBySpaceReturns( 146 v2action.ServiceBinding{}, 147 []string{"foo", "bar"}, 148 ccerror.ServiceBindingTakenError{}) 149 }) 150 151 It("displays warnings and 'OK'", func() { 152 Expect(executeErr).NotTo(HaveOccurred()) 153 154 Expect(testUI.Err).To(Say("foo")) 155 Expect(testUI.Err).To(Say("bar")) 156 Expect(testUI.Out).To(Say("App some-app is already bound to some-service.")) 157 Expect(testUI.Out).To(Say("OK")) 158 }) 159 }) 160 161 When("binding the service instance results in an error other than ServiceBindingTakenError", func() { 162 BeforeEach(func() { 163 fakeActor.BindServiceBySpaceReturns( 164 v2action.ServiceBinding{}, 165 nil, 166 actionerror.ApplicationNotFoundError{Name: "some-app"}) 167 }) 168 169 It("should return the error", func() { 170 Expect(executeErr).To(MatchError(actionerror.ApplicationNotFoundError{ 171 Name: "some-app", 172 })) 173 }) 174 }) 175 176 When("the service binding is successful", func() { 177 BeforeEach(func() { 178 fakeActor.BindServiceBySpaceReturns( 179 v2action.ServiceBinding{}, 180 v2action.Warnings{"some-warning", "another-warning"}, 181 nil, 182 ) 183 }) 184 185 It("displays OK and the TIP", func() { 186 Expect(executeErr).ToNot(HaveOccurred()) 187 188 Expect(testUI.Out).To(Say("OK")) 189 Expect(testUI.Out).To(Say("TIP: Use 'faceman restage some-app' to ensure your env variable changes take effect")) 190 Expect(testUI.Err).To(Say("some-warning")) 191 Expect(testUI.Err).To(Say("another-warning")) 192 193 Expect(fakeActor.BindServiceBySpaceCallCount()).To(Equal(1)) 194 appName, serviceInstanceName, spaceGUID, bindingName, parameters := fakeActor.BindServiceBySpaceArgsForCall(0) 195 Expect(appName).To(Equal("some-app")) 196 Expect(serviceInstanceName).To(Equal("some-service")) 197 Expect(spaceGUID).To(Equal("some-space-guid")) 198 Expect(bindingName).To(Equal("some-binding-name")) 199 Expect(parameters).To(Equal(map[string]interface{}{"some-parameter": "some-value"})) 200 }) 201 }) 202 }) 203 }) 204 205 When("the binding is not created asynchroncously", func() { 206 It("does not display binding in progress", func() { 207 Expect(executeErr).ToNot(HaveOccurred()) 208 209 Expect(testUI.Out).ToNot(Say("Binding in progress...")) 210 }) 211 }) 212 213 When("the binding is created asynchroncously", func() { 214 BeforeEach(func() { 215 fakeActor.BindServiceBySpaceReturns( 216 v2action.ServiceBinding{LastOperation: ccv2.LastOperation{State: constant.LastOperationInProgress}}, 217 v2action.Warnings{"some-warning", "another-warning"}, 218 nil, 219 ) 220 221 }) 222 223 It("displays Binding in Progress", func() { 224 Expect(executeErr).ToNot(HaveOccurred()) 225 226 Expect(testUI.Out).To(Say("OK")) 227 Expect(testUI.Out).To(Say("Binding in progress. Use 'faceman service %s' to check operation status.", cmd.RequiredArgs.ServiceInstanceName)) 228 229 Expect(testUI.Err).To(Say("some-warning")) 230 Expect(testUI.Err).To(Say("another-warning")) 231 232 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)) 233 }) 234 }) 235 }) 236 }) 237 })