github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/actor/v2action/service_instance_test.go (about) 1 package v2action_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/actor/v2action/v2actionfakes" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 10 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 12 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/ginkgo/extensions/table" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("Service Instance Actions", func() { 19 var ( 20 actor *Actor 21 fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient 22 ) 23 24 BeforeEach(func() { 25 fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient) 26 actor = NewActor(fakeCloudControllerClient, nil, nil) 27 }) 28 29 Describe("ServiceInstance", func() { 30 DescribeTable("IsManaged", 31 func(iType ccv2.ServiceInstanceType, expected bool) { 32 Expect(ServiceInstance{Type: iType}.IsManaged()).To(Equal(expected)) 33 }, 34 35 Entry("return true for managed service", ccv2.ManagedService, true), 36 Entry("return false for any other type of service", ccv2.UserProvidedService, false), 37 ) 38 39 DescribeTable("IsUserProvided", 40 func(iType ccv2.ServiceInstanceType, expected bool) { 41 Expect(ServiceInstance{Type: iType}.IsUserProvided()).To(Equal(expected)) 42 }, 43 44 Entry("return true for UserProvidedService service", ccv2.UserProvidedService, true), 45 Entry("return false for any other type of service", ccv2.ManagedService, false), 46 ) 47 }) 48 49 Describe("GetServiceInstance", func() { 50 var ( 51 serviceInstanceGUID string 52 53 serviceInstance ServiceInstance 54 warnings Warnings 55 executeErr error 56 ) 57 58 BeforeEach(func() { 59 serviceInstanceGUID = "service-instance-guid" 60 }) 61 62 JustBeforeEach(func() { 63 serviceInstance, warnings, executeErr = actor.GetServiceInstance(serviceInstanceGUID) 64 }) 65 66 Context("when the service instance exists", func() { 67 BeforeEach(func() { 68 fakeCloudControllerClient.GetServiceInstanceReturns(ccv2.ServiceInstance{Name: "some-service-instance", GUID: "service-instance-guid"}, ccv2.Warnings{"service-instance-warnings"}, nil) 69 }) 70 71 It("returns the service instance and warnings", func() { 72 Expect(executeErr).ToNot(HaveOccurred()) 73 Expect(serviceInstance).To(Equal(ServiceInstance{ 74 GUID: "service-instance-guid", 75 Name: "some-service-instance", 76 })) 77 Expect(warnings).To(Equal(Warnings{"service-instance-warnings"})) 78 79 Expect(fakeCloudControllerClient.GetServiceInstanceCallCount()).To(Equal(1)) 80 Expect(fakeCloudControllerClient.GetServiceInstanceArgsForCall(0)).To(Equal(serviceInstanceGUID)) 81 }) 82 }) 83 84 Context("when the service instance does not exist", func() { 85 BeforeEach(func() { 86 fakeCloudControllerClient.GetServiceInstanceReturns(ccv2.ServiceInstance{}, ccv2.Warnings{"service-instance-warnings-1"}, ccerror.ResourceNotFoundError{}) 87 }) 88 89 It("returns errors and warnings", func() { 90 Expect(executeErr).To(MatchError(actionerror.ServiceInstanceNotFoundError{GUID: serviceInstanceGUID})) 91 Expect(warnings).To(ConsistOf("service-instance-warnings-1")) 92 }) 93 }) 94 95 Context("when retrieving the application's bound services returns an error", func() { 96 var expectedErr error 97 98 BeforeEach(func() { 99 expectedErr = errors.New("this is indeed an error, kudos!") 100 fakeCloudControllerClient.GetServiceInstanceReturns(ccv2.ServiceInstance{}, ccv2.Warnings{"service-instance-warnings-1"}, expectedErr) 101 }) 102 103 It("returns errors and warnings", func() { 104 Expect(executeErr).To(MatchError(expectedErr)) 105 Expect(warnings).To(ConsistOf("service-instance-warnings-1")) 106 }) 107 }) 108 }) 109 110 Describe("GetServiceInstanceByNameAndSpace", func() { 111 Context("when the service instance exists", func() { 112 BeforeEach(func() { 113 fakeCloudControllerClient.GetSpaceServiceInstancesReturns( 114 []ccv2.ServiceInstance{ 115 { 116 GUID: "some-service-instance-guid", 117 Name: "some-service-instance", 118 }, 119 }, 120 ccv2.Warnings{"foo"}, 121 nil, 122 ) 123 }) 124 125 It("returns the service instance and warnings", func() { 126 serviceInstance, warnings, err := actor.GetServiceInstanceByNameAndSpace("some-service-instance", "some-space-guid") 127 Expect(err).ToNot(HaveOccurred()) 128 Expect(serviceInstance).To(Equal(ServiceInstance{ 129 GUID: "some-service-instance-guid", 130 Name: "some-service-instance", 131 })) 132 Expect(warnings).To(Equal(Warnings{"foo"})) 133 134 Expect(fakeCloudControllerClient.GetSpaceServiceInstancesCallCount()).To(Equal(1)) 135 136 spaceGUID, includeUserProvidedServices, queries := fakeCloudControllerClient.GetSpaceServiceInstancesArgsForCall(0) 137 Expect(spaceGUID).To(Equal("some-space-guid")) 138 Expect(includeUserProvidedServices).To(BeTrue()) 139 Expect(queries).To(ConsistOf([]ccv2.Filter{ 140 ccv2.Filter{ 141 Type: constant.NameFilter, 142 Operator: constant.EqualOperator, 143 Values: []string{"some-service-instance"}, 144 }, 145 })) 146 }) 147 }) 148 149 Context("when the service instance does not exists", func() { 150 BeforeEach(func() { 151 fakeCloudControllerClient.GetSpaceServiceInstancesReturns([]ccv2.ServiceInstance{}, nil, nil) 152 }) 153 154 It("returns a ServiceInstanceNotFoundError", func() { 155 _, _, err := actor.GetServiceInstanceByNameAndSpace("some-service-instance", "some-space-guid") 156 Expect(err).To(MatchError(actionerror.ServiceInstanceNotFoundError{Name: "some-service-instance"})) 157 }) 158 }) 159 160 Context("when the cloud controller client returns an error", func() { 161 var expectedError error 162 163 BeforeEach(func() { 164 expectedError = errors.New("I am a CloudControllerClient Error") 165 fakeCloudControllerClient.GetSpaceServiceInstancesReturns([]ccv2.ServiceInstance{}, nil, expectedError) 166 }) 167 168 It("returns the error", func() { 169 _, _, err := actor.GetServiceInstanceByNameAndSpace("some-service-instance", "some-space-guid") 170 Expect(err).To(MatchError(expectedError)) 171 }) 172 }) 173 }) 174 175 Describe("GetServiceInstancesByApplication", func() { 176 var ( 177 appGUID string 178 179 serviceInstances []ServiceInstance 180 warnings Warnings 181 executeErr error 182 ) 183 184 BeforeEach(func() { 185 appGUID = "some-app-guid" 186 }) 187 188 JustBeforeEach(func() { 189 serviceInstances, warnings, executeErr = actor.GetServiceInstancesByApplication(appGUID) 190 }) 191 192 Context("when the application has services bound", func() { 193 var serviceBindings []ccv2.ServiceBinding 194 195 BeforeEach(func() { 196 serviceBindings = []ccv2.ServiceBinding{ 197 {ServiceInstanceGUID: "service-instance-guid-1"}, 198 {ServiceInstanceGUID: "service-instance-guid-2"}, 199 {ServiceInstanceGUID: "service-instance-guid-3"}, 200 } 201 202 fakeCloudControllerClient.GetServiceBindingsReturns(serviceBindings, ccv2.Warnings{"service-bindings-warnings-1", "service-bindings-warnings-2"}, nil) 203 }) 204 205 Context("when retrieving the service instances is successful", func() { 206 BeforeEach(func() { 207 fakeCloudControllerClient.GetServiceInstanceReturnsOnCall(0, ccv2.ServiceInstance{Name: "some-service-instance-1"}, ccv2.Warnings{"service-instance-warnings-1"}, nil) 208 fakeCloudControllerClient.GetServiceInstanceReturnsOnCall(1, ccv2.ServiceInstance{Name: "some-service-instance-2"}, ccv2.Warnings{"service-instance-warnings-2"}, nil) 209 fakeCloudControllerClient.GetServiceInstanceReturnsOnCall(2, ccv2.ServiceInstance{Name: "some-service-instance-3"}, ccv2.Warnings{"service-instance-warnings-3"}, nil) 210 }) 211 212 It("returns the service instances and warnings", func() { 213 Expect(executeErr).ToNot(HaveOccurred()) 214 Expect(warnings).To(ConsistOf("service-bindings-warnings-1", "service-bindings-warnings-2", "service-instance-warnings-1", "service-instance-warnings-2", "service-instance-warnings-3")) 215 Expect(serviceInstances).To(ConsistOf( 216 ServiceInstance{Name: "some-service-instance-1"}, 217 ServiceInstance{Name: "some-service-instance-2"}, 218 ServiceInstance{Name: "some-service-instance-3"}, 219 )) 220 221 Expect(fakeCloudControllerClient.GetServiceInstanceCallCount()).To(Equal(3)) 222 Expect(fakeCloudControllerClient.GetServiceInstanceArgsForCall(0)).To(Equal("service-instance-guid-1")) 223 Expect(fakeCloudControllerClient.GetServiceInstanceArgsForCall(1)).To(Equal("service-instance-guid-2")) 224 Expect(fakeCloudControllerClient.GetServiceInstanceArgsForCall(2)).To(Equal("service-instance-guid-3")) 225 }) 226 }) 227 228 Context("when retrieving the service instances returns an error", func() { 229 var expectedErr error 230 231 BeforeEach(func() { 232 expectedErr = errors.New("this is indeed an error, kudos!") 233 fakeCloudControllerClient.GetServiceInstanceReturns(ccv2.ServiceInstance{}, ccv2.Warnings{"service-instance-warnings-1", "service-instance-warnings-2"}, expectedErr) 234 }) 235 236 It("returns errors and warnings", func() { 237 Expect(executeErr).To(MatchError(expectedErr)) 238 Expect(warnings).To(ConsistOf("service-bindings-warnings-1", "service-bindings-warnings-2", "service-instance-warnings-1", "service-instance-warnings-2")) 239 }) 240 }) 241 }) 242 243 Context("when the application has no services bound", func() { 244 BeforeEach(func() { 245 fakeCloudControllerClient.GetServiceBindingsReturns(nil, ccv2.Warnings{"service-bindings-warnings-1", "service-bindings-warnings-2"}, nil) 246 }) 247 248 It("returns an empty list and warnings", func() { 249 Expect(executeErr).ToNot(HaveOccurred()) 250 Expect(warnings).To(ConsistOf("service-bindings-warnings-1", "service-bindings-warnings-2")) 251 Expect(serviceInstances).To(BeEmpty()) 252 }) 253 }) 254 255 Context("when retrieving the application's bound services returns an error", func() { 256 var expectedErr error 257 258 BeforeEach(func() { 259 expectedErr = errors.New("this is indeed an error, kudos!") 260 fakeCloudControllerClient.GetServiceBindingsReturns(nil, ccv2.Warnings{"service-bindings-warnings-1", "service-bindings-warnings-2"}, expectedErr) 261 }) 262 263 It("returns errors and warnings", func() { 264 Expect(executeErr).To(MatchError(expectedErr)) 265 Expect(warnings).To(ConsistOf("service-bindings-warnings-1", "service-bindings-warnings-2")) 266 }) 267 }) 268 }) 269 270 Describe("GetServiceInstancesBySpace", func() { 271 Context("when there are service instances", func() { 272 BeforeEach(func() { 273 fakeCloudControllerClient.GetSpaceServiceInstancesReturns( 274 []ccv2.ServiceInstance{ 275 { 276 GUID: "some-service-instance-guid-1", 277 Name: "some-service-instance-1", 278 }, 279 { 280 GUID: "some-service-instance-guid-2", 281 Name: "some-service-instance-2", 282 }, 283 }, 284 ccv2.Warnings{"warning-1", "warning-2"}, 285 nil, 286 ) 287 }) 288 289 It("returns the service instances and warnings", func() { 290 serviceInstances, warnings, err := actor.GetServiceInstancesBySpace("some-space-guid") 291 Expect(err).ToNot(HaveOccurred()) 292 Expect(serviceInstances).To(ConsistOf( 293 ServiceInstance{ 294 GUID: "some-service-instance-guid-1", 295 Name: "some-service-instance-1", 296 }, 297 ServiceInstance{ 298 GUID: "some-service-instance-guid-2", 299 Name: "some-service-instance-2", 300 }, 301 )) 302 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 303 304 Expect(fakeCloudControllerClient.GetSpaceServiceInstancesCallCount()).To(Equal(1)) 305 306 spaceGUID, includeUserProvidedServices, queries := fakeCloudControllerClient.GetSpaceServiceInstancesArgsForCall(0) 307 Expect(spaceGUID).To(Equal("some-space-guid")) 308 Expect(includeUserProvidedServices).To(BeTrue()) 309 Expect(queries).To(BeNil()) 310 }) 311 }) 312 313 Context("when the cloud controller client returns an error", func() { 314 var expectedError error 315 316 BeforeEach(func() { 317 expectedError = errors.New("I am a CloudControllerClient Error") 318 fakeCloudControllerClient.GetSpaceServiceInstancesReturns( 319 []ccv2.ServiceInstance{}, 320 ccv2.Warnings{"warning-1", "warning-2"}, 321 expectedError) 322 }) 323 324 It("returns the error and warnings", func() { 325 _, warnings, err := actor.GetServiceInstancesBySpace("some-space-guid") 326 Expect(err).To(MatchError(expectedError)) 327 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 328 }) 329 }) 330 }) 331 })