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