github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7action/service_instance_sharing_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  	"code.cloudfoundry.org/cli/resources"
    12  	"code.cloudfoundry.org/cli/types"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("Service Instance Sharing", func() {
    18  	var (
    19  		actor                        *Actor
    20  		fakeCloudControllerClient    *v7actionfakes.FakeCloudControllerClient
    21  		serviceInstanceSharingParams ServiceInstanceSharingParams
    22  		warnings                     Warnings
    23  		executionError               error
    24  	)
    25  
    26  	const (
    27  		serviceInstanceName = "some-service-instance"
    28  		targetedSpaceGUID   = "some-source-space-guid"
    29  		targetedOrgGUID     = "some-org-guid"
    30  
    31  		shareToSpaceName = "share-to-space-name"
    32  		shareToOrgName   = "share-to-org-name"
    33  	)
    34  
    35  	itValidatesParameters := func() {
    36  		When("the service instance cannot be found", func() {
    37  			BeforeEach(func() {
    38  				fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceReturns(
    39  					resources.ServiceInstance{},
    40  					ccv3.IncludedResources{},
    41  					ccv3.Warnings{"some-service-instance-warning"},
    42  					ccerror.ServiceInstanceNotFoundError{Name: serviceInstanceName, SpaceGUID: targetedSpaceGUID},
    43  				)
    44  			})
    45  
    46  			It("returns an actor error and warnings", func() {
    47  				Expect(fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceCallCount()).To(Equal(1))
    48  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(0))
    49  				Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(0))
    50  
    51  				Expect(executionError).To(MatchError(actionerror.ServiceInstanceNotFoundError{Name: serviceInstanceName}))
    52  				Expect(warnings).To(ConsistOf("some-service-instance-warning"))
    53  			})
    54  		})
    55  
    56  		When("the specified org cannot be found", func() {
    57  			BeforeEach(func() {
    58  				serviceInstanceSharingParams.OrgName = types.NewOptionalString(shareToOrgName)
    59  
    60  				fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceReturns(
    61  					resources.ServiceInstance{Name: serviceInstanceName},
    62  					ccv3.IncludedResources{},
    63  					ccv3.Warnings{"some-service-instance-warning-1"},
    64  					nil,
    65  				)
    66  
    67  				fakeCloudControllerClient.GetOrganizationsReturns(
    68  					[]resources.Organization{},
    69  					ccv3.Warnings{"some-org-warning"},
    70  					nil,
    71  				)
    72  			})
    73  
    74  			It("searches for the specified org", func() {
    75  				Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
    76  				Expect(fakeCloudControllerClient.GetOrganizationsArgsForCall(0)).To(Equal(
    77  					[]ccv3.Query{
    78  						{Key: ccv3.NameFilter, Values: []string{shareToOrgName}},
    79  						{Key: ccv3.PerPage, Values: []string{"1"}},
    80  						{Key: ccv3.Page, Values: []string{"1"}},
    81  					}))
    82  				Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(0))
    83  			})
    84  
    85  			It("returns an actor error and warnings", func() {
    86  				Expect(executionError).To(MatchError(actionerror.OrganizationNotFoundError{Name: shareToOrgName}))
    87  				Expect(warnings).To(ConsistOf("some-service-instance-warning-1", "some-org-warning"))
    88  			})
    89  		})
    90  
    91  		When("the space cannot be found", func() {
    92  			BeforeEach(func() {
    93  				fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceReturns(
    94  					resources.ServiceInstance{Name: serviceInstanceName},
    95  					ccv3.IncludedResources{},
    96  					ccv3.Warnings{"some-service-instance-warning-1"},
    97  					nil,
    98  				)
    99  
   100  				fakeCloudControllerClient.GetSpacesReturns(
   101  					[]resources.Space{},
   102  					ccv3.IncludedResources{},
   103  					ccv3.Warnings{"some-space-warnings"},
   104  					nil,
   105  				)
   106  			})
   107  
   108  			When("the org to share to is the targeted org", func() {
   109  				It("searches for the space in the specified org", func() {
   110  					Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(0))
   111  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   112  					Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(Equal(
   113  						[]ccv3.Query{
   114  							{Key: ccv3.NameFilter, Values: []string{shareToSpaceName}},
   115  							{Key: ccv3.OrganizationGUIDFilter, Values: []string{targetedOrgGUID}},
   116  							{Key: ccv3.PerPage, Values: []string{"1"}},
   117  							{Key: ccv3.Page, Values: []string{"1"}},
   118  						}))
   119  				})
   120  
   121  				It("returns an actor error and warnings", func() {
   122  					Expect(executionError).To(MatchError(actionerror.SpaceNotFoundError{Name: shareToSpaceName}))
   123  					Expect(warnings).To(ConsistOf("some-service-instance-warning-1", "some-space-warnings"))
   124  				})
   125  			})
   126  
   127  			When("the org to share to is specified by user", func() {
   128  				const shareToOrgGUID = "share-to-org-guid"
   129  
   130  				BeforeEach(func() {
   131  					serviceInstanceSharingParams.OrgName = types.NewOptionalString(shareToOrgName)
   132  
   133  					fakeCloudControllerClient.GetOrganizationsReturns(
   134  						[]resources.Organization{{GUID: shareToOrgGUID}},
   135  						ccv3.Warnings{"some-org-warning"},
   136  						nil,
   137  					)
   138  				})
   139  
   140  				It("searches for the space in the specified org", func() {
   141  					Expect(fakeCloudControllerClient.GetOrganizationsCallCount()).To(Equal(1))
   142  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   143  					Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(Equal(
   144  						[]ccv3.Query{
   145  							{Key: ccv3.NameFilter, Values: []string{shareToSpaceName}},
   146  							{Key: ccv3.OrganizationGUIDFilter, Values: []string{shareToOrgGUID}},
   147  							{Key: ccv3.PerPage, Values: []string{"1"}},
   148  							{Key: ccv3.Page, Values: []string{"1"}},
   149  						}))
   150  				})
   151  
   152  				It("returns an actor error and warnings", func() {
   153  					Expect(executionError).To(MatchError(actionerror.SpaceNotFoundError{Name: shareToSpaceName}))
   154  					Expect(warnings).To(ConsistOf("some-service-instance-warning-1", "some-org-warning", "some-space-warnings"))
   155  				})
   156  			})
   157  		})
   158  	}
   159  
   160  	BeforeEach(func() {
   161  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
   162  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil)
   163  	})
   164  
   165  	Describe("ShareServiceInstanceToSpaceAndOrg", func() {
   166  		BeforeEach(func() {
   167  			serviceInstanceSharingParams = ServiceInstanceSharingParams{
   168  				SpaceName: shareToSpaceName,
   169  				OrgName:   types.OptionalString{},
   170  			}
   171  		})
   172  
   173  		JustBeforeEach(func() {
   174  			warnings, executionError = actor.ShareServiceInstanceToSpaceAndOrg(
   175  				serviceInstanceName,
   176  				targetedSpaceGUID,
   177  				targetedOrgGUID,
   178  				serviceInstanceSharingParams,
   179  			)
   180  		})
   181  
   182  		itValidatesParameters()
   183  
   184  		When("a successful share request is made", func() {
   185  			spaceGUID := "fake-space-guid"
   186  			serviceInstanceGUID := "fake-service-instance-guid"
   187  
   188  			BeforeEach(func() {
   189  				fakeCloudControllerClient.GetSpacesReturns(
   190  					[]resources.Space{{GUID: spaceGUID}},
   191  					ccv3.IncludedResources{},
   192  					ccv3.Warnings{"some-space-warning"},
   193  					nil,
   194  				)
   195  
   196  				fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceReturns(
   197  					resources.ServiceInstance{GUID: serviceInstanceGUID},
   198  					ccv3.IncludedResources{},
   199  					ccv3.Warnings{"some-service-instance-warning"},
   200  					nil,
   201  				)
   202  			})
   203  
   204  			It("makes a request to the cloud controller", func() {
   205  				Expect(fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceCallCount()).To(Equal(1))
   206  				Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   207  				Expect(fakeCloudControllerClient.ShareServiceInstanceToSpacesCallCount()).To(Equal(1))
   208  
   209  				actualServiceInstanceGUID, actualSpaces := fakeCloudControllerClient.ShareServiceInstanceToSpacesArgsForCall(0)
   210  				Expect(actualServiceInstanceGUID).To(Equal(serviceInstanceGUID))
   211  				Expect(actualSpaces[0]).To(Equal(spaceGUID))
   212  
   213  				Expect(executionError).NotTo(HaveOccurred())
   214  				Expect(warnings).To(ConsistOf("some-space-warning", "some-service-instance-warning"))
   215  			})
   216  		})
   217  
   218  		When("sharing request returns an error", func() {
   219  			spaceGUID := "fake-space-guid"
   220  			serviceInstanceGUID := "fake-service-instance-guid"
   221  
   222  			BeforeEach(func() {
   223  				fakeCloudControllerClient.GetSpacesReturns(
   224  					[]resources.Space{{GUID: spaceGUID}},
   225  					ccv3.IncludedResources{},
   226  					ccv3.Warnings{"some-space-warning"},
   227  					nil,
   228  				)
   229  				fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceReturns(
   230  					resources.ServiceInstance{GUID: serviceInstanceGUID},
   231  					ccv3.IncludedResources{},
   232  					ccv3.Warnings{"some-service-instance-warning"},
   233  					nil,
   234  				)
   235  				fakeCloudControllerClient.ShareServiceInstanceToSpacesReturns(
   236  					resources.RelationshipList{},
   237  					ccv3.Warnings{"some-share-warning"},
   238  					errors.New("cannot share the instance"),
   239  				)
   240  			})
   241  
   242  			It("returns an error and warnings", func() {
   243  				Expect(executionError).To(MatchError("cannot share the instance"))
   244  				Expect(warnings).To(ConsistOf("some-space-warning", "some-service-instance-warning", "some-share-warning"))
   245  			})
   246  		})
   247  	})
   248  
   249  	Describe("UnshareServiceInstanceFromSpaceAndOrg", func() {
   250  		BeforeEach(func() {
   251  			serviceInstanceSharingParams = ServiceInstanceSharingParams{
   252  				SpaceName: shareToSpaceName,
   253  				OrgName:   types.OptionalString{},
   254  			}
   255  		})
   256  
   257  		JustBeforeEach(func() {
   258  			warnings, executionError = actor.UnshareServiceInstanceFromSpaceAndOrg(
   259  				serviceInstanceName,
   260  				targetedSpaceGUID,
   261  				targetedOrgGUID,
   262  				serviceInstanceSharingParams,
   263  			)
   264  		})
   265  
   266  		itValidatesParameters()
   267  
   268  		When("a unshare request is made", func() {
   269  			expectedSpaceGUID := "fake-space-guid"
   270  			expectedServiceInstanceGUID := "fake-service-instance-guid"
   271  
   272  			BeforeEach(func() {
   273  				fakeCloudControllerClient.GetSpacesReturns(
   274  					[]resources.Space{{GUID: expectedSpaceGUID}},
   275  					ccv3.IncludedResources{},
   276  					ccv3.Warnings{"some-space-warning"},
   277  					nil,
   278  				)
   279  
   280  				fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceReturns(
   281  					resources.ServiceInstance{GUID: expectedServiceInstanceGUID},
   282  					ccv3.IncludedResources{},
   283  					ccv3.Warnings{"some-service-instance-warning"},
   284  					nil,
   285  				)
   286  			})
   287  
   288  			It("makes the right request", func() {
   289  				Expect(fakeCloudControllerClient.GetServiceInstanceByNameAndSpaceCallCount()).To(Equal(1))
   290  				Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   291  				Expect(fakeCloudControllerClient.UnshareServiceInstanceFromSpaceCallCount()).To(Equal(1))
   292  
   293  				actualServiceInstanceGUID, actualSpace := fakeCloudControllerClient.UnshareServiceInstanceFromSpaceArgsForCall(0)
   294  				Expect(actualServiceInstanceGUID).To(Equal(expectedServiceInstanceGUID))
   295  				Expect(actualSpace).To(Equal(expectedSpaceGUID))
   296  			})
   297  
   298  			When("the request is successful", func() {
   299  				BeforeEach(func() {
   300  					fakeCloudControllerClient.UnshareServiceInstanceFromSpaceReturns(
   301  						ccv3.Warnings{"some-unshare-warning"},
   302  						nil,
   303  					)
   304  				})
   305  
   306  				It("returns warnings and no error", func() {
   307  					Expect(executionError).NotTo(HaveOccurred())
   308  					Expect(warnings).To(ConsistOf("some-space-warning", "some-service-instance-warning", "some-unshare-warning"))
   309  				})
   310  			})
   311  
   312  			When("the request fails", func() {
   313  				BeforeEach(func() {
   314  					fakeCloudControllerClient.UnshareServiceInstanceFromSpaceReturns(
   315  						ccv3.Warnings{"some-unshare-warning"},
   316  						errors.New("cannot unshare the instance"),
   317  					)
   318  				})
   319  
   320  				It("returns warnings and an error", func() {
   321  					Expect(executionError).To(MatchError("cannot unshare the instance"))
   322  					Expect(warnings).To(ConsistOf("some-space-warning", "some-service-instance-warning", "some-unshare-warning"))
   323  
   324  				})
   325  			})
   326  		})
   327  	})
   328  })