github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/actor/v7action/space_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/resources"
     5  	"errors"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	. "code.cloudfoundry.org/cli/actor/v7action"
     9  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Space", func() {
    16  	var (
    17  		actor                     *Actor
    18  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    23  		fakeConfig := new(v7actionfakes.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  					resources.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  					resources.Relationship{GUID: ""},
    59  					ccv3.Warnings{"warning-1", "warning-2"}, nil)
    60  				fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentReturns(
    61  					resources.Relationship{GUID: "some-iso-guid"},
    62  					ccv3.Warnings{"warning-3", "warning-4"}, nil)
    63  				fakeCloudControllerClient.GetIsolationSegmentReturns(
    64  					resources.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  					resources.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  					resources.Relationship{GUID: ""},
   114  					ccv3.Warnings{"warning-1", "warning-2"}, nil)
   115  				fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentReturns(
   116  					resources.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  					resources.Relationship{GUID: ""},
   134  					ccv3.Warnings{"warning-1", "warning-2"}, nil)
   135  				fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentReturns(
   136  					resources.Relationship{GUID: "some-iso-guid"},
   137  					ccv3.Warnings{"warning-3", "warning-4"}, nil)
   138  				fakeCloudControllerClient.GetIsolationSegmentReturns(
   139  					resources.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  						[]resources.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("GetOrganizationSpaces", func() {
   226  		When("there are spaces in the org", func() {
   227  			BeforeEach(func() {
   228  				fakeCloudControllerClient.GetSpacesReturns(
   229  					[]resources.Space{
   230  						{
   231  							GUID: "space-1-guid",
   232  							Name: "space-1",
   233  						},
   234  						{
   235  							GUID: "space-2-guid",
   236  							Name: "space-2",
   237  						},
   238  					},
   239  					ccv3.Warnings{"warning-1", "warning-2"},
   240  					nil)
   241  			})
   242  
   243  			It("returns all spaces and all warnings", func() {
   244  				spaces, warnings, err := actor.GetOrganizationSpaces("some-org-guid")
   245  
   246  				Expect(err).ToNot(HaveOccurred())
   247  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   248  				Expect(spaces).To(Equal(
   249  					[]Space{
   250  						{
   251  							GUID: "space-1-guid",
   252  							Name: "space-1",
   253  						},
   254  						{
   255  							GUID: "space-2-guid",
   256  							Name: "space-2",
   257  						},
   258  					}))
   259  
   260  				Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   261  				Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(Equal(
   262  					[]ccv3.Query{
   263  						{
   264  							Key:    ccv3.OrganizationGUIDFilter,
   265  							Values: []string{"some-org-guid"},
   266  						},
   267  					}))
   268  			})
   269  		})
   270  
   271  		When("an error is encountered", func() {
   272  			var returnedErr error
   273  
   274  			BeforeEach(func() {
   275  				returnedErr = errors.New("cc-get-spaces-error")
   276  				fakeCloudControllerClient.GetSpacesReturns(
   277  					[]resources.Space{},
   278  					ccv3.Warnings{"warning-1", "warning-2"},
   279  					returnedErr,
   280  				)
   281  			})
   282  
   283  			It("returns the error and all warnings", func() {
   284  				_, warnings, err := actor.GetOrganizationSpaces("some-org-guid")
   285  
   286  				Expect(err).To(MatchError(returnedErr))
   287  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   288  			})
   289  		})
   290  	})
   291  })