github.com/arunkumar7540/cli@v6.45.0+incompatible/actor/v3action/space_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  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Space", func() {
    16  	var (
    17  		actor                     *Actor
    18  		fakeCloudControllerClient *v3actionfakes.FakeCloudControllerClient
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeCloudControllerClient = new(v3actionfakes.FakeCloudControllerClient)
    23  		fakeConfig := new(v3actionfakes.FakeConfig)
    24  		actor = NewActor(fakeCloudControllerClient, fakeConfig, nil, nil)
    25  	})
    26  
    27  	Describe("ResetSpaceIsolationSegment", func() {
    28  		When("the organization does not have a default isolation segment", func() {
    29  			BeforeEach(func() {
    30  				fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipReturns(
    31  					ccv3.Relationship{GUID: ""},
    32  					ccv3.Warnings{"warning-1", "warning-2"}, nil)
    33  			})
    34  
    35  			It("returns an empty isolation segment GUID", func() {
    36  				newIsolationSegmentName, warnings, err := actor.ResetSpaceIsolationSegment("some-org-guid", "some-space-guid")
    37  
    38  				Expect(err).ToNot(HaveOccurred())
    39  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    40  				Expect(newIsolationSegmentName).To(BeEmpty())
    41  
    42  				Expect(fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipCallCount()).To(Equal(1))
    43  				spaceGUID, isolationSegmentGUID := fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipArgsForCall(0)
    44  				Expect(spaceGUID).To(Equal("some-space-guid"))
    45  				Expect(isolationSegmentGUID).To(BeEmpty())
    46  
    47  				Expect(fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentCallCount()).To(Equal(1))
    48  				orgGUID := fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentArgsForCall(0)
    49  				Expect(orgGUID).To(Equal("some-org-guid"))
    50  
    51  				Expect(fakeCloudControllerClient.GetIsolationSegmentCallCount()).To(Equal(0))
    52  			})
    53  		})
    54  
    55  		When("the organization has a default isolation segment", func() {
    56  			BeforeEach(func() {
    57  				fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipReturns(
    58  					ccv3.Relationship{GUID: ""},
    59  					ccv3.Warnings{"warning-1", "warning-2"}, nil)
    60  				fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentReturns(
    61  					ccv3.Relationship{GUID: "some-iso-guid"},
    62  					ccv3.Warnings{"warning-3", "warning-4"}, nil)
    63  				fakeCloudControllerClient.GetIsolationSegmentReturns(
    64  					ccv3.IsolationSegment{Name: "some-iso-name"},
    65  					ccv3.Warnings{"warning-5", "warning-6"}, nil)
    66  			})
    67  
    68  			It("returns the org's isolation segment GUID", func() {
    69  				newIsolationSegmentName, warnings, err := actor.ResetSpaceIsolationSegment("some-org-guid", "some-space-guid")
    70  
    71  				Expect(fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipCallCount()).To(Equal(1))
    72  				spaceGUID, isolationSegmentGUID := fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipArgsForCall(0)
    73  				Expect(spaceGUID).To(Equal("some-space-guid"))
    74  				Expect(isolationSegmentGUID).To(BeEmpty())
    75  
    76  				Expect(fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentCallCount()).To(Equal(1))
    77  				orgGUID := fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentArgsForCall(0)
    78  				Expect(orgGUID).To(Equal("some-org-guid"))
    79  
    80  				Expect(fakeCloudControllerClient.GetIsolationSegmentCallCount()).To(Equal(1))
    81  				isoSegGUID := fakeCloudControllerClient.GetIsolationSegmentArgsForCall(0)
    82  				Expect(isoSegGUID).To(Equal("some-iso-guid"))
    83  
    84  				Expect(err).ToNot(HaveOccurred())
    85  				Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4", "warning-5", "warning-6"))
    86  				Expect(newIsolationSegmentName).To(Equal("some-iso-name"))
    87  			})
    88  		})
    89  
    90  		When("assigning the space returns an error", func() {
    91  			var expectedErr error
    92  
    93  			BeforeEach(func() {
    94  				expectedErr = errors.New("some error")
    95  				fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipReturns(
    96  					ccv3.Relationship{GUID: ""},
    97  					ccv3.Warnings{"warning-1", "warning-2"}, expectedErr)
    98  			})
    99  
   100  			It("returns warnings and the error", func() {
   101  				_, warnings, err := actor.ResetSpaceIsolationSegment("some-org-guid", "some-space-guid")
   102  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   103  				Expect(err).To(MatchError(expectedErr))
   104  			})
   105  		})
   106  
   107  		When("getting the org's default isolation segments returns an error", func() {
   108  			var expectedErr error
   109  
   110  			BeforeEach(func() {
   111  				expectedErr = errors.New("some error")
   112  				fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipReturns(
   113  					ccv3.Relationship{GUID: ""},
   114  					ccv3.Warnings{"warning-1", "warning-2"}, nil)
   115  				fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentReturns(
   116  					ccv3.Relationship{GUID: "some-iso-guid"},
   117  					ccv3.Warnings{"warning-3", "warning-4"}, expectedErr)
   118  			})
   119  
   120  			It("returns the warnings and an error", func() {
   121  				_, warnings, err := actor.ResetSpaceIsolationSegment("some-org-guid", "some-space-guid")
   122  				Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4"))
   123  				Expect(err).To(MatchError(expectedErr))
   124  			})
   125  		})
   126  
   127  		When("getting the isolation segment returns an error", func() {
   128  			var expectedErr error
   129  
   130  			BeforeEach(func() {
   131  				expectedErr = errors.New("some error")
   132  				fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipReturns(
   133  					ccv3.Relationship{GUID: ""},
   134  					ccv3.Warnings{"warning-1", "warning-2"}, nil)
   135  				fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentReturns(
   136  					ccv3.Relationship{GUID: "some-iso-guid"},
   137  					ccv3.Warnings{"warning-3", "warning-4"}, nil)
   138  				fakeCloudControllerClient.GetIsolationSegmentReturns(
   139  					ccv3.IsolationSegment{Name: "some-iso-name"},
   140  					ccv3.Warnings{"warning-5", "warning-6"}, expectedErr)
   141  			})
   142  
   143  			It("returns the warnings and an error", func() {
   144  				_, warnings, err := actor.ResetSpaceIsolationSegment("some-org-guid", "some-space-guid")
   145  				Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4", "warning-5", "warning-6"))
   146  				Expect(err).To(MatchError(expectedErr))
   147  			})
   148  		})
   149  	})
   150  
   151  	Describe("GetSpaceByNameAndOrganization", func() {
   152  		var (
   153  			spaceName string
   154  			orgGUID   string
   155  
   156  			space      Space
   157  			warnings   Warnings
   158  			executeErr error
   159  		)
   160  
   161  		BeforeEach(func() {
   162  			spaceName = "some-space"
   163  			orgGUID = "some-org-guid"
   164  		})
   165  
   166  		JustBeforeEach(func() {
   167  			space, warnings, executeErr = actor.GetSpaceByNameAndOrganization(spaceName, orgGUID)
   168  		})
   169  
   170  		When("the GetSpace call is successful", func() {
   171  			When("the cloud controller returns back one space", func() {
   172  				BeforeEach(func() {
   173  					fakeCloudControllerClient.GetSpacesReturns(
   174  						[]ccv3.Space{{GUID: "some-space-guid", Name: spaceName}},
   175  						ccv3.Warnings{"some-space-warning"}, nil)
   176  				})
   177  
   178  				It("returns back the first space and warnings", func() {
   179  					Expect(executeErr).ToNot(HaveOccurred())
   180  
   181  					Expect(space).To(Equal(Space{
   182  						GUID: "some-space-guid",
   183  						Name: spaceName,
   184  					}))
   185  					Expect(warnings).To(ConsistOf("some-space-warning"))
   186  
   187  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   188  					Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(ConsistOf(
   189  						ccv3.Query{Key: ccv3.NameFilter, Values: []string{spaceName}},
   190  						ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}},
   191  					))
   192  				})
   193  			})
   194  
   195  			When("the cloud controller returns back no spaces", func() {
   196  				BeforeEach(func() {
   197  					fakeCloudControllerClient.GetSpacesReturns(
   198  						nil, ccv3.Warnings{"some-space-warning"}, nil)
   199  				})
   200  
   201  				It("returns a SpaceNotFoundError and warnings", func() {
   202  					Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: spaceName}))
   203  
   204  					Expect(warnings).To(ConsistOf("some-space-warning"))
   205  				})
   206  			})
   207  		})
   208  
   209  		When("the GetSpace call is unsuccessful", func() {
   210  			BeforeEach(func() {
   211  				fakeCloudControllerClient.GetSpacesReturns(
   212  					nil,
   213  					ccv3.Warnings{"some-space-warning"},
   214  					errors.New("cannot get space"))
   215  			})
   216  
   217  			It("returns an error and warnings", func() {
   218  				Expect(executeErr).To(MatchError("cannot get space"))
   219  				Expect(warnings).To(ConsistOf("some-space-warning"))
   220  			})
   221  		})
   222  
   223  	})
   224  
   225  	Describe("GetSpacesByGUIDs", func() {
   226  		Context("when the api returns a bogus semver", func() {
   227  			BeforeEach(func() {
   228  				fakeCloudControllerClient.CloudControllerAPIVersionReturns("bogus")
   229  			})
   230  
   231  			It("defaults to client side filtering", func() {
   232  				_, _, executeErr := actor.GetSpacesByGUIDs("space-guid-1", "space-guid-2")
   233  				Expect(executeErr).ToNot(HaveOccurred())
   234  
   235  				Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(BeEmpty())
   236  			})
   237  		})
   238  
   239  		Context("when CC API supports the 'guids' query param", func() {
   240  			BeforeEach(func() {
   241  				fakeCloudControllerClient.CloudControllerAPIVersionReturns("3.56.0")
   242  			})
   243  			When("the GetSpace call is successful", func() {
   244  				When("the cloud controller returns back multiple spaces", func() {
   245  					BeforeEach(func() {
   246  						fakeCloudControllerClient.GetSpacesReturns(
   247  							[]ccv3.Space{
   248  								{
   249  									GUID: "space-guid-1",
   250  									Name: "space-1",
   251  									Relationships: ccv3.Relationships{
   252  										constant.RelationshipTypeOrganization: ccv3.Relationship{GUID: "org-guid-1"},
   253  									},
   254  								},
   255  								{
   256  									GUID: "space-guid-2",
   257  									Name: "space-2",
   258  									Relationships: ccv3.Relationships{
   259  										constant.RelationshipTypeOrganization: ccv3.Relationship{GUID: "org-guid-2"},
   260  									},
   261  								},
   262  							},
   263  							ccv3.Warnings{"space-warning-1", "space-warning-2"}, nil)
   264  					})
   265  
   266  					It("returns back the first space and warnings", func() {
   267  						spaces, warnings, executeErr := actor.GetSpacesByGUIDs("space-guid-1", "space-guid-2")
   268  						Expect(executeErr).ToNot(HaveOccurred())
   269  
   270  						Expect(spaces).To(ConsistOf(
   271  							Space{
   272  								GUID:             "space-guid-1",
   273  								Name:             "space-1",
   274  								OrganizationGUID: "org-guid-1",
   275  							},
   276  							Space{
   277  								GUID:             "space-guid-2",
   278  								Name:             "space-2",
   279  								OrganizationGUID: "org-guid-2",
   280  							},
   281  						))
   282  						Expect(warnings).To(ConsistOf("space-warning-1", "space-warning-2"))
   283  
   284  						Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   285  						Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(ConsistOf(
   286  							ccv3.Query{Key: ccv3.GUIDFilter, Values: []string{"space-guid-1", "space-guid-2"}},
   287  						))
   288  					})
   289  				})
   290  			})
   291  
   292  			When("the GetSpace call is unsuccessful", func() {
   293  				BeforeEach(func() {
   294  					fakeCloudControllerClient.GetSpacesReturns(
   295  						nil,
   296  						ccv3.Warnings{"space-warning-1", "space-warning-2"},
   297  						errors.New("cannot get space"))
   298  				})
   299  
   300  				It("returns an error and warnings", func() {
   301  					_, warnings, executeErr := actor.GetSpacesByGUIDs("space-guid-1", "space-guid-2")
   302  					Expect(executeErr).To(MatchError("cannot get space"))
   303  					Expect(warnings).To(ConsistOf("space-warning-1", "space-warning-2"))
   304  				})
   305  			})
   306  		})
   307  
   308  		Context("when CC API does not support the 'guids' query param", func() {
   309  			BeforeEach(func() {
   310  				fakeCloudControllerClient.CloudControllerAPIVersionReturns("3.55.0")
   311  			})
   312  
   313  			When("the GetSpace call is successful", func() {
   314  				When("the cloud controller returns back multiple spaces", func() {
   315  					BeforeEach(func() {
   316  						fakeCloudControllerClient.GetSpacesReturns(
   317  							[]ccv3.Space{
   318  								{
   319  									GUID: "space-guid-1",
   320  									Name: "space-1",
   321  									Relationships: ccv3.Relationships{
   322  										constant.RelationshipTypeOrganization: ccv3.Relationship{GUID: "org-guid-1"},
   323  									},
   324  								},
   325  								{
   326  									GUID: "space-guid-2",
   327  									Name: "space-2",
   328  									Relationships: ccv3.Relationships{
   329  										constant.RelationshipTypeOrganization: ccv3.Relationship{GUID: "org-guid-2"},
   330  									},
   331  								},
   332  								{
   333  									GUID: "space-guid-3",
   334  									Name: "space-3",
   335  									Relationships: ccv3.Relationships{
   336  										constant.RelationshipTypeOrganization: ccv3.Relationship{GUID: "org-guid-3"},
   337  									},
   338  								},
   339  								{
   340  									GUID: "space-guid-4",
   341  									Name: "space-4",
   342  									Relationships: ccv3.Relationships{
   343  										constant.RelationshipTypeOrganization: ccv3.Relationship{GUID: "org-guid-4"},
   344  									},
   345  								},
   346  							},
   347  							ccv3.Warnings{"space-warning-1", "space-warning-2"}, nil)
   348  					})
   349  
   350  					It("returns back the first space and warnings", func() {
   351  						spaces, warnings, executeErr := actor.GetSpacesByGUIDs("space-guid-1", "space-guid-2")
   352  						Expect(executeErr).ToNot(HaveOccurred())
   353  
   354  						Expect(spaces).To(ConsistOf(
   355  							Space{
   356  								GUID:             "space-guid-1",
   357  								Name:             "space-1",
   358  								OrganizationGUID: "org-guid-1",
   359  							},
   360  							Space{
   361  								GUID:             "space-guid-2",
   362  								Name:             "space-2",
   363  								OrganizationGUID: "org-guid-2",
   364  							},
   365  						))
   366  						Expect(warnings).To(ConsistOf("space-warning-1", "space-warning-2"))
   367  
   368  						Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   369  						Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(BeEmpty())
   370  					})
   371  				})
   372  			})
   373  		})
   374  	})
   375  })