github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+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("GetOrganizationSpaces", func() {
   152  		When("there are spaces in the org", func() {
   153  			BeforeEach(func() {
   154  				fakeCloudControllerClient.GetSpacesReturns(
   155  					[]ccv3.Space{
   156  						{
   157  							GUID: "space-1-guid",
   158  							Name: "space-1",
   159  						},
   160  						{
   161  							GUID: "space-2-guid",
   162  							Name: "space-2",
   163  						},
   164  					},
   165  					ccv3.Warnings{"warning-1", "warning-2"},
   166  					nil)
   167  			})
   168  
   169  			It("returns all spaces and all warnings", func() {
   170  				spaces, warnings, err := actor.GetOrganizationSpaces("some-org-guid")
   171  
   172  				Expect(err).ToNot(HaveOccurred())
   173  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   174  				Expect(spaces).To(Equal(
   175  					[]Space{
   176  						{
   177  							GUID: "space-1-guid",
   178  							Name: "space-1",
   179  						},
   180  						{
   181  							GUID: "space-2-guid",
   182  							Name: "space-2",
   183  						},
   184  					}))
   185  
   186  				Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   187  				Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(Equal(
   188  					[]ccv3.Query{
   189  						{
   190  							Key:    ccv3.OrganizationGUIDFilter,
   191  							Values: []string{"some-org-guid"},
   192  						},
   193  					}))
   194  			})
   195  		})
   196  
   197  		When("an error is encountered", func() {
   198  			var returnedErr error
   199  
   200  			BeforeEach(func() {
   201  				returnedErr = errors.New("cc-get-spaces-error")
   202  				fakeCloudControllerClient.GetSpacesReturns(
   203  					[]ccv3.Space{},
   204  					ccv3.Warnings{"warning-1", "warning-2"},
   205  					returnedErr,
   206  				)
   207  			})
   208  
   209  			It("returns the error and all warnings", func() {
   210  				_, warnings, err := actor.GetOrganizationSpaces("some-org-guid")
   211  
   212  				Expect(err).To(MatchError(returnedErr))
   213  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   214  			})
   215  		})
   216  	})
   217  
   218  	Describe("GetSpaceByNameAndOrganization", func() {
   219  		var (
   220  			spaceName string
   221  			orgGUID   string
   222  
   223  			space      Space
   224  			warnings   Warnings
   225  			executeErr error
   226  		)
   227  
   228  		BeforeEach(func() {
   229  			spaceName = "some-space"
   230  			orgGUID = "some-org-guid"
   231  		})
   232  
   233  		JustBeforeEach(func() {
   234  			space, warnings, executeErr = actor.GetSpaceByNameAndOrganization(spaceName, orgGUID)
   235  		})
   236  
   237  		When("the GetSpace call is successful", func() {
   238  			When("the cloud controller returns back one space", func() {
   239  				BeforeEach(func() {
   240  					fakeCloudControllerClient.GetSpacesReturns(
   241  						[]ccv3.Space{{GUID: "some-space-guid", Name: spaceName}},
   242  						ccv3.Warnings{"some-space-warning"}, nil)
   243  				})
   244  
   245  				It("returns back the first space and warnings", func() {
   246  					Expect(executeErr).ToNot(HaveOccurred())
   247  
   248  					Expect(space).To(Equal(Space{
   249  						GUID: "some-space-guid",
   250  						Name: spaceName,
   251  					}))
   252  					Expect(warnings).To(ConsistOf("some-space-warning"))
   253  
   254  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   255  					Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(ConsistOf(
   256  						ccv3.Query{Key: ccv3.NameFilter, Values: []string{spaceName}},
   257  						ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}},
   258  					))
   259  				})
   260  			})
   261  
   262  			When("the cloud controller returns back no spaces", func() {
   263  				BeforeEach(func() {
   264  					fakeCloudControllerClient.GetSpacesReturns(
   265  						nil, ccv3.Warnings{"some-space-warning"}, nil)
   266  				})
   267  
   268  				It("returns a SpaceNotFoundError and warnings", func() {
   269  					Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: spaceName}))
   270  
   271  					Expect(warnings).To(ConsistOf("some-space-warning"))
   272  				})
   273  			})
   274  		})
   275  
   276  		When("the GetSpace call is unsuccessful", func() {
   277  			BeforeEach(func() {
   278  				fakeCloudControllerClient.GetSpacesReturns(
   279  					nil,
   280  					ccv3.Warnings{"some-space-warning"},
   281  					errors.New("cannot get space"))
   282  			})
   283  
   284  			It("returns an error and warnings", func() {
   285  				Expect(executeErr).To(MatchError("cannot get space"))
   286  				Expect(warnings).To(ConsistOf("some-space-warning"))
   287  			})
   288  		})
   289  
   290  	})
   291  
   292  	Describe("GetSpacesByGUIDs", func() {
   293  		Context("when the api returns a bogus semver", func() {
   294  			BeforeEach(func() {
   295  				fakeCloudControllerClient.CloudControllerAPIVersionReturns("bogus")
   296  			})
   297  
   298  			It("defaults to client side filtering", func() {
   299  				_, _, executeErr := actor.GetSpacesByGUIDs("space-guid-1", "space-guid-2")
   300  				Expect(executeErr).ToNot(HaveOccurred())
   301  
   302  				Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(BeEmpty())
   303  			})
   304  		})
   305  
   306  		Context("when CC API supports the 'guids' query param", func() {
   307  			BeforeEach(func() {
   308  				fakeCloudControllerClient.CloudControllerAPIVersionReturns("3.56.0")
   309  			})
   310  			When("the GetSpace call is successful", func() {
   311  				When("the cloud controller returns back multiple spaces", func() {
   312  					BeforeEach(func() {
   313  						fakeCloudControllerClient.GetSpacesReturns(
   314  							[]ccv3.Space{
   315  								{
   316  									GUID: "space-guid-1",
   317  									Name: "space-1",
   318  									Relationships: ccv3.Relationships{
   319  										constant.RelationshipTypeOrganization: ccv3.Relationship{GUID: "org-guid-1"},
   320  									},
   321  								},
   322  								{
   323  									GUID: "space-guid-2",
   324  									Name: "space-2",
   325  									Relationships: ccv3.Relationships{
   326  										constant.RelationshipTypeOrganization: ccv3.Relationship{GUID: "org-guid-2"},
   327  									},
   328  								},
   329  							},
   330  							ccv3.Warnings{"space-warning-1", "space-warning-2"}, nil)
   331  					})
   332  
   333  					It("returns back the first space and warnings", func() {
   334  						spaces, warnings, executeErr := actor.GetSpacesByGUIDs("space-guid-1", "space-guid-2")
   335  						Expect(executeErr).ToNot(HaveOccurred())
   336  
   337  						Expect(spaces).To(ConsistOf(
   338  							Space{
   339  								GUID:             "space-guid-1",
   340  								Name:             "space-1",
   341  								OrganizationGUID: "org-guid-1",
   342  							},
   343  							Space{
   344  								GUID:             "space-guid-2",
   345  								Name:             "space-2",
   346  								OrganizationGUID: "org-guid-2",
   347  							},
   348  						))
   349  						Expect(warnings).To(ConsistOf("space-warning-1", "space-warning-2"))
   350  
   351  						Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   352  						Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(ConsistOf(
   353  							ccv3.Query{Key: ccv3.GUIDFilter, Values: []string{"space-guid-1", "space-guid-2"}},
   354  						))
   355  					})
   356  				})
   357  			})
   358  
   359  			When("the GetSpace call is unsuccessful", func() {
   360  				BeforeEach(func() {
   361  					fakeCloudControllerClient.GetSpacesReturns(
   362  						nil,
   363  						ccv3.Warnings{"space-warning-1", "space-warning-2"},
   364  						errors.New("cannot get space"))
   365  				})
   366  
   367  				It("returns an error and warnings", func() {
   368  					_, warnings, executeErr := actor.GetSpacesByGUIDs("space-guid-1", "space-guid-2")
   369  					Expect(executeErr).To(MatchError("cannot get space"))
   370  					Expect(warnings).To(ConsistOf("space-warning-1", "space-warning-2"))
   371  				})
   372  			})
   373  		})
   374  
   375  		Context("when CC API does not support the 'guids' query param", func() {
   376  			BeforeEach(func() {
   377  				fakeCloudControllerClient.CloudControllerAPIVersionReturns("3.55.0")
   378  			})
   379  
   380  			When("the GetSpace call is successful", func() {
   381  				When("the cloud controller returns back multiple spaces", func() {
   382  					BeforeEach(func() {
   383  						fakeCloudControllerClient.GetSpacesReturns(
   384  							[]ccv3.Space{
   385  								{
   386  									GUID: "space-guid-1",
   387  									Name: "space-1",
   388  									Relationships: ccv3.Relationships{
   389  										constant.RelationshipTypeOrganization: ccv3.Relationship{GUID: "org-guid-1"},
   390  									},
   391  								},
   392  								{
   393  									GUID: "space-guid-2",
   394  									Name: "space-2",
   395  									Relationships: ccv3.Relationships{
   396  										constant.RelationshipTypeOrganization: ccv3.Relationship{GUID: "org-guid-2"},
   397  									},
   398  								},
   399  								{
   400  									GUID: "space-guid-3",
   401  									Name: "space-3",
   402  									Relationships: ccv3.Relationships{
   403  										constant.RelationshipTypeOrganization: ccv3.Relationship{GUID: "org-guid-3"},
   404  									},
   405  								},
   406  								{
   407  									GUID: "space-guid-4",
   408  									Name: "space-4",
   409  									Relationships: ccv3.Relationships{
   410  										constant.RelationshipTypeOrganization: ccv3.Relationship{GUID: "org-guid-4"},
   411  									},
   412  								},
   413  							},
   414  							ccv3.Warnings{"space-warning-1", "space-warning-2"}, nil)
   415  					})
   416  
   417  					It("returns back the first space and warnings", func() {
   418  						spaces, warnings, executeErr := actor.GetSpacesByGUIDs("space-guid-1", "space-guid-2")
   419  						Expect(executeErr).ToNot(HaveOccurred())
   420  
   421  						Expect(spaces).To(ConsistOf(
   422  							Space{
   423  								GUID:             "space-guid-1",
   424  								Name:             "space-1",
   425  								OrganizationGUID: "org-guid-1",
   426  							},
   427  							Space{
   428  								GUID:             "space-guid-2",
   429  								Name:             "space-2",
   430  								OrganizationGUID: "org-guid-2",
   431  							},
   432  						))
   433  						Expect(warnings).To(ConsistOf("space-warning-1", "space-warning-2"))
   434  
   435  						Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   436  						Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(BeEmpty())
   437  					})
   438  				})
   439  			})
   440  		})
   441  	})
   442  })