github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/actor/pushaction/service_binding_test.go (about)

     1  package pushaction_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "code.cloudfoundry.org/cli/actor/pushaction"
     7  	"code.cloudfoundry.org/cli/actor/pushaction/pushactionfakes"
     8  	"code.cloudfoundry.org/cli/actor/v2action"
     9  
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Binding Services", func() {
    15  	var (
    16  		actor       *Actor
    17  		fakeV2Actor *pushactionfakes.FakeV2Actor
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		fakeV2Actor = new(pushactionfakes.FakeV2Actor)
    22  		actor = NewActor(fakeV2Actor, nil, nil)
    23  	})
    24  
    25  	Describe("BindServices", func() {
    26  		var (
    27  			config ApplicationConfig
    28  
    29  			returnedConfig ApplicationConfig
    30  			boundServices  bool
    31  			warnings       Warnings
    32  			executeErr     error
    33  		)
    34  
    35  		BeforeEach(func() {
    36  			config = ApplicationConfig{}
    37  			config.DesiredApplication.GUID = "some-app-guid"
    38  			config.CurrentServices = map[string]v2action.ServiceInstance{"service_instance_1": {GUID: "instance_1_guid"}}
    39  			config.DesiredServices = map[string]v2action.ServiceInstance{
    40  				"service_instance_1": {GUID: "instance_1_guid"},
    41  				"service_instance_2": {GUID: "instance_2_guid"},
    42  				"service_instance_3": {GUID: "instance_3_guid"},
    43  			}
    44  		})
    45  
    46  		JustBeforeEach(func() {
    47  			returnedConfig, boundServices, warnings, executeErr = actor.BindServices(config)
    48  		})
    49  
    50  		Context("when binding services is successful", func() {
    51  			BeforeEach(func() {
    52  				fakeV2Actor.BindServiceByApplicationAndServiceInstanceReturnsOnCall(0, v2action.Warnings{"service-instance-warning-1"}, nil)
    53  				fakeV2Actor.BindServiceByApplicationAndServiceInstanceReturnsOnCall(1, v2action.Warnings{"service-instance-warning-2"}, nil)
    54  			})
    55  
    56  			It("it updates CurrentServices to match DesiredServices", func() {
    57  				Expect(executeErr).ToNot(HaveOccurred())
    58  				Expect(warnings).To(ConsistOf("service-instance-warning-1", "service-instance-warning-2"))
    59  				Expect(boundServices).To(BeTrue())
    60  				Expect(returnedConfig.CurrentServices).To(Equal(map[string]v2action.ServiceInstance{
    61  					"service_instance_1": {GUID: "instance_1_guid"},
    62  					"service_instance_2": {GUID: "instance_2_guid"},
    63  					"service_instance_3": {GUID: "instance_3_guid"},
    64  				}))
    65  
    66  				var serviceInstanceGUIDs []string
    67  				Expect(fakeV2Actor.BindServiceByApplicationAndServiceInstanceCallCount()).To(Equal(2))
    68  				appGUID, serviceInstanceGUID := fakeV2Actor.BindServiceByApplicationAndServiceInstanceArgsForCall(0)
    69  				Expect(appGUID).To(Equal("some-app-guid"))
    70  				serviceInstanceGUIDs = append(serviceInstanceGUIDs, serviceInstanceGUID)
    71  
    72  				appGUID, serviceInstanceGUID = fakeV2Actor.BindServiceByApplicationAndServiceInstanceArgsForCall(1)
    73  				Expect(appGUID).To(Equal("some-app-guid"))
    74  				serviceInstanceGUIDs = append(serviceInstanceGUIDs, serviceInstanceGUID)
    75  
    76  				Expect(serviceInstanceGUIDs).To(ConsistOf("instance_2_guid", "instance_3_guid"))
    77  			})
    78  		})
    79  
    80  		Context("when binding services fails", func() {
    81  			BeforeEach(func() {
    82  				fakeV2Actor.BindServiceByApplicationAndServiceInstanceReturns(v2action.Warnings{"service-instance-warning-1"}, errors.New("some-error"))
    83  			})
    84  
    85  			It("it returns the error", func() {
    86  				Expect(executeErr).To(MatchError("some-error"))
    87  				Expect(warnings).To(ConsistOf("service-instance-warning-1"))
    88  				Expect(boundServices).To(BeFalse())
    89  			})
    90  		})
    91  	})
    92  })