github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/actor/v2action/service_binding_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/ccv2"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/ginkgo/extensions/table"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Service Binding Actions", func() {
    18  	var (
    19  		actor                     *Actor
    20  		fakeCloudControllerClient *v2actionfakes.FakeCloudControllerClient
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeCloudControllerClient = new(v2actionfakes.FakeCloudControllerClient)
    25  		actor = NewActor(fakeCloudControllerClient, nil, nil)
    26  	})
    27  
    28  	Describe("ServiceBinding", func() {
    29  		DescribeTable("IsInProgress",
    30  			func(state constant.LastOperationState, expected bool) {
    31  				serviceBinding := ServiceBinding{
    32  					LastOperation: ccv2.LastOperation{
    33  						State: state,
    34  					},
    35  				}
    36  				Expect(serviceBinding.IsInProgress()).To(Equal(expected))
    37  			},
    38  
    39  			Entry("returns true for 'in progress'", constant.LastOperationInProgress, true),
    40  			Entry("returns false for 'succeeded'", constant.LastOperationSucceeded, false),
    41  			Entry("returns false for 'failed'", constant.LastOperationFailed, false),
    42  			Entry("returns false for empty", constant.LastOperationState(""), false),
    43  		)
    44  	})
    45  
    46  	Describe("BindServiceByApplicationAndServiceInstance", func() {
    47  		var (
    48  			applicationGUID     string
    49  			serviceInstanceGUID string
    50  
    51  			executeErr error
    52  			warnings   Warnings
    53  		)
    54  
    55  		BeforeEach(func() {
    56  			applicationGUID = "some-app-guid"
    57  			serviceInstanceGUID = "some-service-instance-guid"
    58  		})
    59  
    60  		JustBeforeEach(func() {
    61  			warnings, executeErr = actor.BindServiceByApplicationAndServiceInstance(applicationGUID, serviceInstanceGUID)
    62  		})
    63  
    64  		When("the binding is successful", func() {
    65  			BeforeEach(func() {
    66  				fakeCloudControllerClient.CreateServiceBindingReturns(ccv2.ServiceBinding{}, ccv2.Warnings{"some-warnings"}, nil)
    67  			})
    68  
    69  			It("returns errors and warnings", func() {
    70  				Expect(executeErr).ToNot(HaveOccurred())
    71  				Expect(warnings).To(ConsistOf("some-warnings"))
    72  
    73  				Expect(fakeCloudControllerClient.CreateServiceBindingCallCount()).To(Equal(1))
    74  				inputAppGUID, inputServiceInstanceGUID, inputBindingName, inputAcceptsIncomplete, inputParameters := fakeCloudControllerClient.CreateServiceBindingArgsForCall(0)
    75  				Expect(inputAppGUID).To(Equal(applicationGUID))
    76  				Expect(inputServiceInstanceGUID).To(Equal(serviceInstanceGUID))
    77  				Expect(inputBindingName).To(Equal(""))
    78  				Expect(inputAcceptsIncomplete).To(BeFalse())
    79  				Expect(inputParameters).To(BeNil())
    80  			})
    81  		})
    82  
    83  		When("the binding fails", func() {
    84  			BeforeEach(func() {
    85  				fakeCloudControllerClient.CreateServiceBindingReturns(ccv2.ServiceBinding{}, ccv2.Warnings{"some-warnings"}, errors.New("some-error"))
    86  			})
    87  
    88  			It("returns errors and warnings", func() {
    89  				Expect(executeErr).To(MatchError("some-error"))
    90  				Expect(warnings).To(ConsistOf("some-warnings"))
    91  			})
    92  		})
    93  	})
    94  
    95  	Describe("BindServiceBySpace", func() {
    96  		var (
    97  			executeErr     error
    98  			warnings       Warnings
    99  			serviceBinding ServiceBinding
   100  		)
   101  
   102  		JustBeforeEach(func() {
   103  			serviceBinding, warnings, executeErr = actor.BindServiceBySpace("some-app-name", "some-service-instance-name", "some-space-guid", "some-binding-name", map[string]interface{}{"some-parameter": "some-value"})
   104  		})
   105  
   106  		When("getting the application errors", func() {
   107  			BeforeEach(func() {
   108  				fakeCloudControllerClient.GetApplicationsReturns(
   109  					nil,
   110  					ccv2.Warnings{"foo-1"},
   111  					errors.New("some-error"),
   112  				)
   113  			})
   114  
   115  			It("returns the error", func() {
   116  				Expect(executeErr).To(MatchError(errors.New("some-error")))
   117  				Expect(warnings).To(ConsistOf("foo-1"))
   118  			})
   119  		})
   120  
   121  		When("getting the application succeeds", func() {
   122  			BeforeEach(func() {
   123  				fakeCloudControllerClient.GetApplicationsReturns(
   124  					[]ccv2.Application{{GUID: "some-app-guid"}},
   125  					ccv2.Warnings{"foo-1"},
   126  					nil,
   127  				)
   128  			})
   129  
   130  			When("getting the service instance errors", func() {
   131  				BeforeEach(func() {
   132  					fakeCloudControllerClient.GetSpaceServiceInstancesReturns(
   133  						[]ccv2.ServiceInstance{},
   134  						ccv2.Warnings{"foo-2"},
   135  						errors.New("some-error"),
   136  					)
   137  				})
   138  
   139  				It("returns the error", func() {
   140  					Expect(executeErr).To(MatchError(errors.New("some-error")))
   141  					Expect(warnings).To(ConsistOf("foo-1", "foo-2"))
   142  				})
   143  			})
   144  
   145  			When("getting the service instance succeeds", func() {
   146  				BeforeEach(func() {
   147  					fakeCloudControllerClient.GetSpaceServiceInstancesReturns(
   148  						[]ccv2.ServiceInstance{{GUID: "some-service-instance-guid"}},
   149  						ccv2.Warnings{"foo-2"},
   150  						nil,
   151  					)
   152  				})
   153  
   154  				When("binding the service instance to the application errors", func() {
   155  					BeforeEach(func() {
   156  						fakeCloudControllerClient.CreateServiceBindingReturns(
   157  							ccv2.ServiceBinding{},
   158  							ccv2.Warnings{"foo-3"},
   159  							errors.New("some-error"),
   160  						)
   161  					})
   162  
   163  					It("returns the error", func() {
   164  						Expect(executeErr).To(MatchError(errors.New("some-error")))
   165  						Expect(warnings).To(ConsistOf("foo-1", "foo-2", "foo-3"))
   166  					})
   167  				})
   168  
   169  				When("binding the service instance to the application succeeds", func() {
   170  					BeforeEach(func() {
   171  						fakeCloudControllerClient.CreateServiceBindingReturns(
   172  							ccv2.ServiceBinding{GUID: "some-service-binding-guid"},
   173  							ccv2.Warnings{"foo-3"},
   174  							nil,
   175  						)
   176  					})
   177  
   178  					It("returns all warnings", func() {
   179  						Expect(executeErr).ToNot(HaveOccurred())
   180  						Expect(warnings).To(ConsistOf("foo-1", "foo-2", "foo-3"))
   181  						Expect(serviceBinding).To(Equal(ServiceBinding{GUID: "some-service-binding-guid"}))
   182  
   183  						Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1))
   184  
   185  						Expect(fakeCloudControllerClient.GetSpaceServiceInstancesCallCount()).To(Equal(1))
   186  
   187  						Expect(fakeCloudControllerClient.CreateServiceBindingCallCount()).To(Equal(1))
   188  						appGUID, serviceInstanceGUID, bindingName, acceptsIncomplete, parameters := fakeCloudControllerClient.CreateServiceBindingArgsForCall(0)
   189  						Expect(appGUID).To(Equal("some-app-guid"))
   190  						Expect(serviceInstanceGUID).To(Equal("some-service-instance-guid"))
   191  						Expect(bindingName).To(Equal("some-binding-name"))
   192  						Expect(acceptsIncomplete).To(BeTrue())
   193  						Expect(parameters).To(Equal(map[string]interface{}{"some-parameter": "some-value"}))
   194  					})
   195  				})
   196  			})
   197  		})
   198  	})
   199  
   200  	Describe("GetServiceBindingByApplicationAndServiceInstance", func() {
   201  		When("the service binding exists", func() {
   202  			BeforeEach(func() {
   203  				fakeCloudControllerClient.GetServiceBindingsReturns(
   204  					[]ccv2.ServiceBinding{
   205  						{
   206  							GUID: "some-service-binding-guid",
   207  						},
   208  					},
   209  					ccv2.Warnings{"foo"},
   210  					nil,
   211  				)
   212  			})
   213  
   214  			It("returns the service binding and warnings", func() {
   215  				serviceBinding, warnings, err := actor.GetServiceBindingByApplicationAndServiceInstance("some-app-guid", "some-service-instance-guid")
   216  				Expect(err).ToNot(HaveOccurred())
   217  				Expect(serviceBinding).To(Equal(ServiceBinding{
   218  					GUID: "some-service-binding-guid",
   219  				}))
   220  				Expect(warnings).To(ConsistOf("foo"))
   221  
   222  				Expect(fakeCloudControllerClient.GetServiceBindingsCallCount()).To(Equal(1))
   223  				Expect(fakeCloudControllerClient.GetServiceBindingsArgsForCall(0)).To(ConsistOf([]ccv2.Filter{
   224  					ccv2.Filter{
   225  						Type:     constant.AppGUIDFilter,
   226  						Operator: constant.EqualOperator,
   227  						Values:   []string{"some-app-guid"},
   228  					},
   229  					ccv2.Filter{
   230  						Type:     constant.ServiceInstanceGUIDFilter,
   231  						Operator: constant.EqualOperator,
   232  						Values:   []string{"some-service-instance-guid"},
   233  					},
   234  				}))
   235  			})
   236  		})
   237  
   238  		When("the service binding does not exists", func() {
   239  			BeforeEach(func() {
   240  				fakeCloudControllerClient.GetServiceBindingsReturns([]ccv2.ServiceBinding{}, nil, nil)
   241  			})
   242  
   243  			It("returns a ServiceBindingNotFoundError", func() {
   244  				_, _, err := actor.GetServiceBindingByApplicationAndServiceInstance("some-app-guid", "some-service-instance-guid")
   245  				Expect(err).To(MatchError(actionerror.ServiceBindingNotFoundError{
   246  					AppGUID:             "some-app-guid",
   247  					ServiceInstanceGUID: "some-service-instance-guid",
   248  				}))
   249  			})
   250  		})
   251  
   252  		When("the cloud controller client returns an error", func() {
   253  			var expectedError error
   254  
   255  			BeforeEach(func() {
   256  				expectedError = errors.New("I am a CloudControllerClient Error")
   257  				fakeCloudControllerClient.GetServiceBindingsReturns([]ccv2.ServiceBinding{}, nil, expectedError)
   258  			})
   259  
   260  			It("returns the error", func() {
   261  				_, _, err := actor.GetServiceBindingByApplicationAndServiceInstance("some-app-guid", "some-service-instance-guid")
   262  				Expect(err).To(MatchError(expectedError))
   263  			})
   264  		})
   265  	})
   266  
   267  	Describe("UnbindServiceBySpace", func() {
   268  		var (
   269  			executeErr     error
   270  			warnings       Warnings
   271  			serviceBinding ServiceBinding
   272  		)
   273  
   274  		JustBeforeEach(func() {
   275  			serviceBinding, warnings, executeErr = actor.UnbindServiceBySpace("some-app", "some-service-instance", "some-space-guid")
   276  		})
   277  
   278  		When("the service binding exists", func() {
   279  			BeforeEach(func() {
   280  				fakeCloudControllerClient.GetApplicationsReturns(
   281  					[]ccv2.Application{
   282  						{
   283  							GUID: "some-app-guid",
   284  							Name: "some-app",
   285  						},
   286  					},
   287  					ccv2.Warnings{"foo-1"},
   288  					nil,
   289  				)
   290  				fakeCloudControllerClient.GetSpaceServiceInstancesReturns(
   291  					[]ccv2.ServiceInstance{
   292  						{
   293  							GUID: "some-service-instance-guid",
   294  							Name: "some-service-instance",
   295  						},
   296  					},
   297  					ccv2.Warnings{"foo-2"},
   298  					nil,
   299  				)
   300  				fakeCloudControllerClient.GetServiceBindingsReturns(
   301  					[]ccv2.ServiceBinding{
   302  						{
   303  							GUID: "some-service-binding-guid",
   304  						},
   305  					},
   306  					ccv2.Warnings{"foo-3"},
   307  					nil,
   308  				)
   309  
   310  				fakeCloudControllerClient.DeleteServiceBindingReturns(
   311  					ccv2.ServiceBinding{GUID: "deleted-service-binding-guid"},
   312  					ccv2.Warnings{"foo-4", "foo-5"},
   313  					nil,
   314  				)
   315  			})
   316  
   317  			It("deletes the service binding", func() {
   318  				Expect(executeErr).NotTo(HaveOccurred())
   319  				Expect(warnings).To(ConsistOf(Warnings{"foo-1", "foo-2", "foo-3", "foo-4", "foo-5"}))
   320  				Expect(serviceBinding).To(Equal(ServiceBinding{GUID: "deleted-service-binding-guid"}))
   321  
   322  				Expect(fakeCloudControllerClient.DeleteServiceBindingCallCount()).To(Equal(1))
   323  				passedGUID, acceptsIncomplete := fakeCloudControllerClient.DeleteServiceBindingArgsForCall(0)
   324  				Expect(passedGUID).To(Equal("some-service-binding-guid"))
   325  				Expect(acceptsIncomplete).To(BeTrue())
   326  			})
   327  
   328  			When("the cloud controller API returns warnings and an error", func() {
   329  				var expectedError error
   330  
   331  				BeforeEach(func() {
   332  					expectedError = errors.New("I am a CC error")
   333  					fakeCloudControllerClient.DeleteServiceBindingReturns(ccv2.ServiceBinding{}, ccv2.Warnings{"foo-4", "foo-5"}, expectedError)
   334  				})
   335  
   336  				It("returns the warnings and the error", func() {
   337  					Expect(executeErr).To(MatchError(expectedError))
   338  					Expect(warnings).To(ConsistOf(Warnings{"foo-1", "foo-2", "foo-3", "foo-4", "foo-5"}))
   339  				})
   340  			})
   341  		})
   342  	})
   343  
   344  	Describe("GetServiceBindingsByServiceInstance", func() {
   345  		var (
   346  			serviceBindings         []ServiceBinding
   347  			serviceBindingsWarnings Warnings
   348  			serviceBindingsErr      error
   349  		)
   350  
   351  		JustBeforeEach(func() {
   352  			serviceBindings, serviceBindingsWarnings, serviceBindingsErr = actor.GetServiceBindingsByServiceInstance("some-service-instance-guid")
   353  		})
   354  
   355  		When("no errors are encountered", func() {
   356  			BeforeEach(func() {
   357  				fakeCloudControllerClient.GetServiceInstanceServiceBindingsReturns(
   358  					[]ccv2.ServiceBinding{
   359  						{GUID: "some-service-binding-1-guid"},
   360  						{GUID: "some-service-binding-2-guid"},
   361  					},
   362  					ccv2.Warnings{"get-service-bindings-warning"},
   363  					nil,
   364  				)
   365  			})
   366  
   367  			It("returns service bindings and all warnings", func() {
   368  				Expect(serviceBindingsErr).ToNot(HaveOccurred())
   369  				Expect(serviceBindings).To(ConsistOf(
   370  					ServiceBinding{GUID: "some-service-binding-1-guid"},
   371  					ServiceBinding{GUID: "some-service-binding-2-guid"},
   372  				))
   373  				Expect(serviceBindingsWarnings).To(ConsistOf("get-service-bindings-warning"))
   374  
   375  				Expect(fakeCloudControllerClient.GetServiceInstanceServiceBindingsCallCount()).To(Equal(1))
   376  				Expect(fakeCloudControllerClient.GetServiceInstanceServiceBindingsArgsForCall(0)).To(Equal("some-service-instance-guid"))
   377  			})
   378  		})
   379  
   380  		When("an error is encountered", func() {
   381  			var expectedErr error
   382  
   383  			BeforeEach(func() {
   384  				expectedErr = errors.New("get service bindings error")
   385  				fakeCloudControllerClient.GetServiceInstanceServiceBindingsReturns(
   386  					[]ccv2.ServiceBinding{},
   387  					ccv2.Warnings{"get-service-bindings-warning"},
   388  					expectedErr,
   389  				)
   390  			})
   391  
   392  			It("returns the error and all warnings", func() {
   393  				Expect(serviceBindingsErr).To(MatchError(expectedErr))
   394  				Expect(serviceBindingsWarnings).To(ConsistOf("get-service-bindings-warning"))
   395  
   396  				Expect(fakeCloudControllerClient.GetServiceInstanceServiceBindingsCallCount()).To(Equal(1))
   397  				Expect(fakeCloudControllerClient.GetServiceInstanceServiceBindingsArgsForCall(0)).To(Equal("some-service-instance-guid"))
   398  			})
   399  		})
   400  	})
   401  
   402  	Describe("GetServiceBindingsByUserProvidedServiceInstance", func() {
   403  		var (
   404  			serviceBindings         []ServiceBinding
   405  			serviceBindingsWarnings Warnings
   406  			serviceBindingsErr      error
   407  		)
   408  
   409  		JustBeforeEach(func() {
   410  			serviceBindings, serviceBindingsWarnings, serviceBindingsErr = actor.GetServiceBindingsByUserProvidedServiceInstance("some-user-provided-service-instance-guid")
   411  		})
   412  
   413  		When("no errors are encountered", func() {
   414  			BeforeEach(func() {
   415  				fakeCloudControllerClient.GetUserProvidedServiceInstanceServiceBindingsReturns(
   416  					[]ccv2.ServiceBinding{
   417  						{GUID: "some-service-binding-1-guid"},
   418  						{GUID: "some-service-binding-2-guid"},
   419  					},
   420  					ccv2.Warnings{"get-service-bindings-warning"},
   421  					nil,
   422  				)
   423  			})
   424  
   425  			It("returns service bindings and all warnings", func() {
   426  				Expect(serviceBindingsErr).ToNot(HaveOccurred())
   427  				Expect(serviceBindings).To(ConsistOf(
   428  					ServiceBinding{GUID: "some-service-binding-1-guid"},
   429  					ServiceBinding{GUID: "some-service-binding-2-guid"},
   430  				))
   431  				Expect(serviceBindingsWarnings).To(ConsistOf("get-service-bindings-warning"))
   432  
   433  				Expect(fakeCloudControllerClient.GetUserProvidedServiceInstanceServiceBindingsCallCount()).To(Equal(1))
   434  				Expect(fakeCloudControllerClient.GetUserProvidedServiceInstanceServiceBindingsArgsForCall(0)).To(Equal("some-user-provided-service-instance-guid"))
   435  			})
   436  		})
   437  
   438  		When("an error is encountered", func() {
   439  			var expectedErr error
   440  
   441  			BeforeEach(func() {
   442  				expectedErr = errors.New("get service bindings error")
   443  				fakeCloudControllerClient.GetUserProvidedServiceInstanceServiceBindingsReturns(
   444  					[]ccv2.ServiceBinding{},
   445  					ccv2.Warnings{"get-service-bindings-warning"},
   446  					expectedErr,
   447  				)
   448  			})
   449  
   450  			It("returns the error and all warnings", func() {
   451  				Expect(serviceBindingsErr).To(MatchError(expectedErr))
   452  				Expect(serviceBindingsWarnings).To(ConsistOf("get-service-bindings-warning"))
   453  
   454  				Expect(fakeCloudControllerClient.GetUserProvidedServiceInstanceServiceBindingsCallCount()).To(Equal(1))
   455  				Expect(fakeCloudControllerClient.GetUserProvidedServiceInstanceServiceBindingsArgsForCall(0)).To(Equal("some-user-provided-service-instance-guid"))
   456  			})
   457  		})
   458  	})
   459  })