github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+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  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  )
    13  
    14  var _ = Describe("Space", func() {
    15  	var (
    16  		actor                     *Actor
    17  		fakeCloudControllerClient *v3actionfakes.FakeCloudControllerClient
    18  	)
    19  
    20  	BeforeEach(func() {
    21  		fakeCloudControllerClient = new(v3actionfakes.FakeCloudControllerClient)
    22  		fakeConfig := new(v3actionfakes.FakeConfig)
    23  		actor = NewActor(fakeCloudControllerClient, fakeConfig, nil, nil)
    24  	})
    25  
    26  	Describe("ResetSpaceIsolationSegment", func() {
    27  		Context("when the organization does not have a default isolation segment", func() {
    28  			BeforeEach(func() {
    29  				fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipReturns(
    30  					ccv3.Relationship{GUID: ""},
    31  					ccv3.Warnings{"warning-1", "warning-2"}, nil)
    32  			})
    33  
    34  			It("returns an empty isolation segment GUID", func() {
    35  				newIsolationSegmentName, warnings, err := actor.ResetSpaceIsolationSegment("some-org-guid", "some-space-guid")
    36  
    37  				Expect(err).ToNot(HaveOccurred())
    38  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    39  				Expect(newIsolationSegmentName).To(BeEmpty())
    40  
    41  				Expect(fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipCallCount()).To(Equal(1))
    42  				spaceGUID, isolationSegmentGUID := fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipArgsForCall(0)
    43  				Expect(spaceGUID).To(Equal("some-space-guid"))
    44  				Expect(isolationSegmentGUID).To(BeEmpty())
    45  
    46  				Expect(fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentCallCount()).To(Equal(1))
    47  				orgGUID := fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentArgsForCall(0)
    48  				Expect(orgGUID).To(Equal("some-org-guid"))
    49  
    50  				Expect(fakeCloudControllerClient.GetIsolationSegmentCallCount()).To(Equal(0))
    51  			})
    52  		})
    53  
    54  		Context("when the organization has a default isolation segment", func() {
    55  			BeforeEach(func() {
    56  				fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipReturns(
    57  					ccv3.Relationship{GUID: ""},
    58  					ccv3.Warnings{"warning-1", "warning-2"}, nil)
    59  				fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentReturns(
    60  					ccv3.Relationship{GUID: "some-iso-guid"},
    61  					ccv3.Warnings{"warning-3", "warning-4"}, nil)
    62  				fakeCloudControllerClient.GetIsolationSegmentReturns(
    63  					ccv3.IsolationSegment{Name: "some-iso-name"},
    64  					ccv3.Warnings{"warning-5", "warning-6"}, nil)
    65  			})
    66  
    67  			It("returns the org's isolation segment GUID", func() {
    68  				newIsolationSegmentName, warnings, err := actor.ResetSpaceIsolationSegment("some-org-guid", "some-space-guid")
    69  
    70  				Expect(fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipCallCount()).To(Equal(1))
    71  				spaceGUID, isolationSegmentGUID := fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipArgsForCall(0)
    72  				Expect(spaceGUID).To(Equal("some-space-guid"))
    73  				Expect(isolationSegmentGUID).To(BeEmpty())
    74  
    75  				Expect(fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentCallCount()).To(Equal(1))
    76  				orgGUID := fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentArgsForCall(0)
    77  				Expect(orgGUID).To(Equal("some-org-guid"))
    78  
    79  				Expect(fakeCloudControllerClient.GetIsolationSegmentCallCount()).To(Equal(1))
    80  				isoSegGUID := fakeCloudControllerClient.GetIsolationSegmentArgsForCall(0)
    81  				Expect(isoSegGUID).To(Equal("some-iso-guid"))
    82  
    83  				Expect(err).ToNot(HaveOccurred())
    84  				Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4", "warning-5", "warning-6"))
    85  				Expect(newIsolationSegmentName).To(Equal("some-iso-name"))
    86  			})
    87  		})
    88  
    89  		Context("when assigning the space returns an error", func() {
    90  			var expectedErr error
    91  
    92  			BeforeEach(func() {
    93  				expectedErr = errors.New("some error")
    94  				fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipReturns(
    95  					ccv3.Relationship{GUID: ""},
    96  					ccv3.Warnings{"warning-1", "warning-2"}, expectedErr)
    97  			})
    98  
    99  			It("returns warnings and the error", func() {
   100  				_, warnings, err := actor.ResetSpaceIsolationSegment("some-org-guid", "some-space-guid")
   101  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   102  				Expect(err).To(MatchError(expectedErr))
   103  			})
   104  		})
   105  
   106  		Context("when getting the org's default isolation segments returns an error", func() {
   107  			var expectedErr error
   108  
   109  			BeforeEach(func() {
   110  				expectedErr = errors.New("some error")
   111  				fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipReturns(
   112  					ccv3.Relationship{GUID: ""},
   113  					ccv3.Warnings{"warning-1", "warning-2"}, nil)
   114  				fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentReturns(
   115  					ccv3.Relationship{GUID: "some-iso-guid"},
   116  					ccv3.Warnings{"warning-3", "warning-4"}, expectedErr)
   117  			})
   118  
   119  			It("returns the warnings and an error", func() {
   120  				_, warnings, err := actor.ResetSpaceIsolationSegment("some-org-guid", "some-space-guid")
   121  				Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4"))
   122  				Expect(err).To(MatchError(expectedErr))
   123  			})
   124  		})
   125  
   126  		Context("when getting the isolation segment returns an error", func() {
   127  			var expectedErr error
   128  
   129  			BeforeEach(func() {
   130  				expectedErr = errors.New("some error")
   131  				fakeCloudControllerClient.UpdateSpaceIsolationSegmentRelationshipReturns(
   132  					ccv3.Relationship{GUID: ""},
   133  					ccv3.Warnings{"warning-1", "warning-2"}, nil)
   134  				fakeCloudControllerClient.GetOrganizationDefaultIsolationSegmentReturns(
   135  					ccv3.Relationship{GUID: "some-iso-guid"},
   136  					ccv3.Warnings{"warning-3", "warning-4"}, nil)
   137  				fakeCloudControllerClient.GetIsolationSegmentReturns(
   138  					ccv3.IsolationSegment{Name: "some-iso-name"},
   139  					ccv3.Warnings{"warning-5", "warning-6"}, expectedErr)
   140  			})
   141  
   142  			It("returns the warnings and an error", func() {
   143  				_, warnings, err := actor.ResetSpaceIsolationSegment("some-org-guid", "some-space-guid")
   144  				Expect(warnings).To(ConsistOf("warning-1", "warning-2", "warning-3", "warning-4", "warning-5", "warning-6"))
   145  				Expect(err).To(MatchError(expectedErr))
   146  			})
   147  		})
   148  	})
   149  
   150  	Describe("GetSpaceByNameAndOrganization", func() {
   151  		var (
   152  			spaceName string
   153  			orgGUID   string
   154  
   155  			space      Space
   156  			warnings   Warnings
   157  			executeErr error
   158  		)
   159  
   160  		BeforeEach(func() {
   161  			spaceName = "some-space"
   162  			orgGUID = "some-org-guid"
   163  		})
   164  
   165  		JustBeforeEach(func() {
   166  			space, warnings, executeErr = actor.GetSpaceByNameAndOrganization(spaceName, orgGUID)
   167  		})
   168  
   169  		Context("when the GetSpace call is successful", func() {
   170  			Context("when the cloud controller returns back one space", func() {
   171  				BeforeEach(func() {
   172  					fakeCloudControllerClient.GetSpacesReturns(
   173  						[]ccv3.Space{{GUID: "some-space-guid", Name: spaceName}},
   174  						ccv3.Warnings{"some-space-warning"}, nil)
   175  				})
   176  
   177  				It("returns back the first space and warnings", func() {
   178  					Expect(executeErr).ToNot(HaveOccurred())
   179  
   180  					Expect(space).To(Equal(Space{
   181  						GUID: "some-space-guid",
   182  						Name: spaceName,
   183  					}))
   184  					Expect(warnings).To(ConsistOf("some-space-warning"))
   185  
   186  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   187  					Expect(fakeCloudControllerClient.GetSpacesArgsForCall(0)).To(ConsistOf(
   188  						ccv3.Query{Key: ccv3.NameFilter, Values: []string{spaceName}},
   189  						ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}},
   190  					))
   191  				})
   192  			})
   193  
   194  			Context("when the cloud controller returns back no spaces", func() {
   195  				BeforeEach(func() {
   196  					fakeCloudControllerClient.GetSpacesReturns(
   197  						nil, ccv3.Warnings{"some-space-warning"}, nil)
   198  				})
   199  
   200  				It("returns a SpaceNotFoundError and warnings", func() {
   201  					Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: spaceName}))
   202  
   203  					Expect(warnings).To(ConsistOf("some-space-warning"))
   204  				})
   205  			})
   206  		})
   207  
   208  		Context("when the GetSpace call is unsuccessful", func() {
   209  			BeforeEach(func() {
   210  				fakeCloudControllerClient.GetSpacesReturns(
   211  					nil,
   212  					ccv3.Warnings{"some-space-warning"},
   213  					errors.New("cannot get space"))
   214  			})
   215  
   216  			It("returns an error and warnings", func() {
   217  				Expect(executeErr).To(MatchError("cannot get space"))
   218  				Expect(warnings).To(ConsistOf("some-space-warning"))
   219  			})
   220  		})
   221  
   222  	})
   223  })