github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/actor/v3action/service_instance_test.go (about)

     1  package v3action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v3action"
     8  	"code.cloudfoundry.org/cli/actor/v3action/v3actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Service Instance Actions", func() {
    16  	var (
    17  		actor                     *Actor
    18  		fakeCloudControllerClient *v3actionfakes.FakeCloudControllerClient
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeCloudControllerClient = new(v3actionfakes.FakeCloudControllerClient)
    23  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil)
    24  	})
    25  
    26  	Describe("GetServiceInstanceByNameAndSpace", func() {
    27  		var (
    28  			serviceInstanceName string
    29  			sourceSpaceGUID     string
    30  
    31  			serviceInstance ServiceInstance
    32  			warnings        Warnings
    33  			executionError  error
    34  		)
    35  
    36  		BeforeEach(func() {
    37  			serviceInstanceName = "some-service-instance"
    38  			sourceSpaceGUID = "some-source-space-guid"
    39  		})
    40  
    41  		JustBeforeEach(func() {
    42  			serviceInstance, warnings, executionError = actor.GetServiceInstanceByNameAndSpace(serviceInstanceName, sourceSpaceGUID)
    43  		})
    44  
    45  		Context("when the cloud controller request is successful", func() {
    46  			Context("when the cloud controller returns one service instance", func() {
    47  				BeforeEach(func() {
    48  					fakeCloudControllerClient.GetServiceInstancesReturns([]ccv3.ServiceInstance{
    49  						{
    50  							Name: "some-service-instance",
    51  							GUID: "some-service-instance-guid",
    52  						},
    53  					}, ccv3.Warnings{"some-service-instance-warning"}, nil)
    54  				})
    55  
    56  				It("returns a service instance and warnings", func() {
    57  					Expect(executionError).NotTo(HaveOccurred())
    58  
    59  					Expect(serviceInstance).To(Equal(ServiceInstance{Name: "some-service-instance", GUID: "some-service-instance-guid"}))
    60  					Expect(warnings).To(ConsistOf("some-service-instance-warning"))
    61  					Expect(fakeCloudControllerClient.GetServiceInstancesCallCount()).To(Equal(1))
    62  					Expect(fakeCloudControllerClient.GetServiceInstancesArgsForCall(0)).To(ConsistOf(
    63  						ccv3.Query{Key: ccv3.NameFilter, Values: []string{serviceInstanceName}},
    64  						ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{sourceSpaceGUID}},
    65  					))
    66  				})
    67  			})
    68  
    69  			Context("when the cloud controller returns no service instances", func() {
    70  				BeforeEach(func() {
    71  					fakeCloudControllerClient.GetServiceInstancesReturns(
    72  						nil,
    73  						ccv3.Warnings{"some-service-instance-warning"},
    74  						nil)
    75  				})
    76  
    77  				It("returns an error and warnings", func() {
    78  					Expect(executionError).To(MatchError(actionerror.ServiceInstanceNotFoundError{Name: serviceInstanceName}))
    79  
    80  					Expect(warnings).To(ConsistOf("some-service-instance-warning"))
    81  				})
    82  			})
    83  		})
    84  
    85  		Context("when the cloud controller returns an error", func() {
    86  			BeforeEach(func() {
    87  				fakeCloudControllerClient.GetServiceInstancesReturns(
    88  					nil,
    89  					ccv3.Warnings{"some-service-instance-warning"},
    90  					errors.New("no service instance"))
    91  			})
    92  
    93  			It("returns an error and warnings", func() {
    94  				Expect(executionError).To(MatchError("no service instance"))
    95  				Expect(warnings).To(ConsistOf("some-service-instance-warning"))
    96  			})
    97  		})
    98  	})
    99  
   100  	Describe("UnshareServiceInstanceByServiceInstanceAndSpace", func() {
   101  		var (
   102  			serviceInstanceGUID string
   103  			sharedToSpaceGUID   string
   104  
   105  			warnings   Warnings
   106  			executeErr error
   107  		)
   108  
   109  		BeforeEach(func() {
   110  			serviceInstanceGUID = "some-service-instance-guid"
   111  			sharedToSpaceGUID = "some-other-space-guid"
   112  		})
   113  
   114  		JustBeforeEach(func() {
   115  			warnings, executeErr = actor.UnshareServiceInstanceByServiceInstanceAndSpace(serviceInstanceGUID, sharedToSpaceGUID)
   116  		})
   117  
   118  		Context("when no errors occur deleting the service instance share relationship", func() {
   119  			BeforeEach(func() {
   120  				fakeCloudControllerClient.DeleteServiceInstanceRelationshipsSharedSpaceReturns(
   121  					ccv3.Warnings{"delete-share-relationship-warning"},
   122  					nil)
   123  			})
   124  
   125  			It("returns no errors and all warnings", func() {
   126  				Expect(executeErr).ToNot(HaveOccurred())
   127  				Expect(warnings).To(ConsistOf("delete-share-relationship-warning"))
   128  
   129  				Expect(fakeCloudControllerClient.DeleteServiceInstanceRelationshipsSharedSpaceCallCount()).To(Equal(1))
   130  				serviceInstanceGUIDArg, sharedToSpaceGUIDArg := fakeCloudControllerClient.DeleteServiceInstanceRelationshipsSharedSpaceArgsForCall(0)
   131  				Expect(serviceInstanceGUIDArg).To(Equal(serviceInstanceGUID))
   132  				Expect(sharedToSpaceGUIDArg).To(Equal(sharedToSpaceGUID))
   133  			})
   134  		})
   135  
   136  		Context("when an error occurs deleting the service instance share relationship", func() {
   137  			var expectedErr error
   138  
   139  			BeforeEach(func() {
   140  				expectedErr = errors.New("delete share relationship error")
   141  				fakeCloudControllerClient.DeleteServiceInstanceRelationshipsSharedSpaceReturns(
   142  					ccv3.Warnings{"delete-share-relationship-warning"},
   143  					expectedErr)
   144  			})
   145  
   146  			It("returns the error and all warnings", func() {
   147  				Expect(executeErr).To(MatchError(expectedErr))
   148  				Expect(warnings).To(ConsistOf("delete-share-relationship-warning"))
   149  			})
   150  		})
   151  	})
   152  })