github.com/orange-cloudfoundry/cli@v7.1.0+incompatible/actor/v7action/space_quota_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/resources"
    11  	"code.cloudfoundry.org/cli/types"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Space Quota Actions", func() {
    17  	var (
    18  		actor                     *Actor
    19  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    20  		trueValue                 = true
    21  		falseValue                = true
    22  	)
    23  
    24  	BeforeEach(func() {
    25  		actor, fakeCloudControllerClient, _, _, _, _, _ = NewTestActor()
    26  	})
    27  
    28  	Describe("ApplySpaceQuotaByName", func() {
    29  		var (
    30  			warnings   Warnings
    31  			executeErr error
    32  			quotaName  = "space-quota-name"
    33  			quotaGUID  = "space-quota-guid"
    34  			spaceGUID  = "space-guid"
    35  			orgGUID    = "org-guid"
    36  		)
    37  
    38  		JustBeforeEach(func() {
    39  			warnings, executeErr = actor.ApplySpaceQuotaByName(quotaName, spaceGUID, orgGUID)
    40  		})
    41  
    42  		When("the space quota could not be found", func() {
    43  			BeforeEach(func() {
    44  				fakeCloudControllerClient.GetSpaceQuotasReturns(
    45  					[]resources.SpaceQuota{},
    46  					ccv3.Warnings{"some-quota-warning"},
    47  					nil,
    48  				)
    49  			})
    50  
    51  			It("returns the error and prints warnings", func() {
    52  				Expect(fakeCloudControllerClient.GetSpaceQuotasCallCount()).To(Equal(1))
    53  				Expect(fakeCloudControllerClient.ApplySpaceQuotaCallCount()).To(Equal(0))
    54  
    55  				Expect(warnings).To(ConsistOf("some-quota-warning"))
    56  				Expect(executeErr).To(MatchError(actionerror.SpaceQuotaNotFoundForNameError{Name: quotaName}))
    57  			})
    58  		})
    59  
    60  		When("applying the quota returns an error", func() {
    61  			BeforeEach(func() {
    62  				fakeCloudControllerClient.GetSpaceQuotasReturns(
    63  					[]resources.SpaceQuota{
    64  						{Quota: resources.Quota{GUID: "some-quota-guid"}},
    65  					},
    66  					ccv3.Warnings{"some-quota-warning"},
    67  					nil,
    68  				)
    69  				fakeCloudControllerClient.ApplySpaceQuotaReturns(
    70  					resources.RelationshipList{},
    71  					ccv3.Warnings{"apply-quota-warning"},
    72  					errors.New("apply-quota-error"),
    73  				)
    74  			})
    75  
    76  			It("returns the error and prints warnings", func() {
    77  				Expect(fakeCloudControllerClient.GetSpaceQuotasCallCount()).To(Equal(1))
    78  				Expect(fakeCloudControllerClient.ApplySpaceQuotaCallCount()).To(Equal(1))
    79  
    80  				Expect(warnings).To(ConsistOf("some-quota-warning", "apply-quota-warning"))
    81  				Expect(executeErr).To(MatchError("apply-quota-error"))
    82  			})
    83  		})
    84  
    85  		When("the quota is successfully applied to the space", func() {
    86  			BeforeEach(func() {
    87  				fakeCloudControllerClient.GetSpaceQuotasReturns(
    88  					[]resources.SpaceQuota{
    89  						{Quota: resources.Quota{GUID: quotaGUID}},
    90  					},
    91  					ccv3.Warnings{"some-quota-warning"},
    92  					nil,
    93  				)
    94  				fakeCloudControllerClient.ApplySpaceQuotaReturns(
    95  					resources.RelationshipList{
    96  						GUIDs: []string{orgGUID},
    97  					},
    98  					ccv3.Warnings{"apply-quota-warning"},
    99  					nil,
   100  				)
   101  			})
   102  
   103  			It("returns the error and prints warnings", func() {
   104  				Expect(fakeCloudControllerClient.GetSpaceQuotasCallCount()).To(Equal(1))
   105  				passedQuotaQuery := fakeCloudControllerClient.GetSpaceQuotasArgsForCall(0)
   106  				Expect(passedQuotaQuery).To(Equal(
   107  					[]ccv3.Query{
   108  						{
   109  							Key:    "organization_guids",
   110  							Values: []string{orgGUID},
   111  						},
   112  						{
   113  							Key:    "names",
   114  							Values: []string{quotaName},
   115  						},
   116  					},
   117  				))
   118  
   119  				Expect(fakeCloudControllerClient.ApplySpaceQuotaCallCount()).To(Equal(1))
   120  				passedQuotaGUID, passedSpaceGUID := fakeCloudControllerClient.ApplySpaceQuotaArgsForCall(0)
   121  				Expect(passedQuotaGUID).To(Equal(quotaGUID))
   122  				Expect(passedSpaceGUID).To(Equal(spaceGUID))
   123  
   124  				Expect(warnings).To(ConsistOf("some-quota-warning", "apply-quota-warning"))
   125  				Expect(executeErr).To(BeNil())
   126  			})
   127  		})
   128  	})
   129  
   130  	Describe("CreateSpaceQuota", func() {
   131  		var (
   132  			spaceQuotaName   string
   133  			organizationGuid string
   134  			warnings         Warnings
   135  			executeErr       error
   136  			limits           QuotaLimits
   137  		)
   138  
   139  		JustBeforeEach(func() {
   140  			warnings, executeErr = actor.CreateSpaceQuota(spaceQuotaName, organizationGuid, limits)
   141  		})
   142  
   143  		When("creating a space quota with all values set", func() {
   144  			BeforeEach(func() {
   145  				fakeCloudControllerClient.CreateSpaceQuotaReturns(resources.SpaceQuota{}, ccv3.Warnings{"some-quota-warning"}, nil)
   146  				limits = QuotaLimits{
   147  					TotalMemoryInMB:       &types.NullInt{IsSet: true, Value: 2},
   148  					PerProcessMemoryInMB:  &types.NullInt{IsSet: true, Value: 3},
   149  					TotalInstances:        &types.NullInt{IsSet: true, Value: 4},
   150  					PaidServicesAllowed:   &trueValue,
   151  					TotalServiceInstances: &types.NullInt{IsSet: true, Value: 6},
   152  					TotalRoutes:           &types.NullInt{IsSet: true, Value: 8},
   153  					TotalReservedPorts:    &types.NullInt{IsSet: true, Value: 9},
   154  				}
   155  			})
   156  
   157  			It("makes the space quota", func() {
   158  				Expect(executeErr).ToNot(HaveOccurred())
   159  
   160  				Expect(fakeCloudControllerClient.CreateSpaceQuotaCallCount()).To(Equal(1))
   161  				givenSpaceQuota := fakeCloudControllerClient.CreateSpaceQuotaArgsForCall(0)
   162  
   163  				Expect(givenSpaceQuota).To(Equal(resources.SpaceQuota{
   164  					Quota: resources.Quota{
   165  						Name: spaceQuotaName,
   166  						Apps: resources.AppLimit{
   167  							TotalMemory:       &types.NullInt{IsSet: true, Value: 2},
   168  							InstanceMemory:    &types.NullInt{IsSet: true, Value: 3},
   169  							TotalAppInstances: &types.NullInt{IsSet: true, Value: 4},
   170  						},
   171  						Services: resources.ServiceLimit{
   172  							TotalServiceInstances: &types.NullInt{IsSet: true, Value: 6},
   173  							PaidServicePlans:      &trueValue,
   174  						},
   175  						Routes: resources.RouteLimit{
   176  							TotalRoutes:        &types.NullInt{IsSet: true, Value: 8},
   177  							TotalReservedPorts: &types.NullInt{IsSet: true, Value: 9},
   178  						},
   179  					},
   180  					OrgGUID:    organizationGuid,
   181  					SpaceGUIDs: nil,
   182  				}))
   183  
   184  				Expect(warnings).To(ConsistOf("some-quota-warning"))
   185  			})
   186  		})
   187  
   188  		When("creating a space quota with empty limits", func() {
   189  			var (
   190  				ccv3Quota resources.SpaceQuota
   191  			)
   192  
   193  			BeforeEach(func() {
   194  				spaceQuotaName = "quota-name"
   195  				limits = QuotaLimits{}
   196  
   197  				ccv3Quota = resources.SpaceQuota{
   198  					Quota: resources.Quota{
   199  						Name: spaceQuotaName,
   200  						Apps: resources.AppLimit{
   201  							TotalMemory:       &types.NullInt{Value: 0, IsSet: true},
   202  							InstanceMemory:    nil,
   203  							TotalAppInstances: nil,
   204  						},
   205  						Services: resources.ServiceLimit{
   206  							TotalServiceInstances: &types.NullInt{Value: 0, IsSet: true},
   207  							PaidServicePlans:      nil,
   208  						},
   209  						Routes: resources.RouteLimit{
   210  							TotalRoutes:        &types.NullInt{Value: 0, IsSet: true},
   211  							TotalReservedPorts: &types.NullInt{Value: 0, IsSet: true},
   212  						},
   213  					},
   214  				}
   215  				fakeCloudControllerClient.CreateSpaceQuotaReturns(
   216  					ccv3Quota,
   217  					ccv3.Warnings{"some-quota-warning"},
   218  					nil,
   219  				)
   220  			})
   221  
   222  			It("call the create endpoint with the respective values and returns warnings", func() {
   223  				Expect(fakeCloudControllerClient.CreateSpaceQuotaCallCount()).To(Equal(1))
   224  
   225  				Expect(warnings).To(ConsistOf("some-quota-warning"))
   226  
   227  				passedQuota := fakeCloudControllerClient.CreateSpaceQuotaArgsForCall(0)
   228  				Expect(passedQuota).To(Equal(ccv3Quota))
   229  			})
   230  		})
   231  
   232  		When("creating a quota with all values set to unlimited", func() {
   233  			var (
   234  				ccv3Quota resources.SpaceQuota
   235  			)
   236  
   237  			BeforeEach(func() {
   238  				spaceQuotaName = "quota-name"
   239  				limits = QuotaLimits{
   240  					TotalMemoryInMB:       &types.NullInt{Value: -1, IsSet: true},
   241  					PerProcessMemoryInMB:  &types.NullInt{Value: -1, IsSet: true},
   242  					TotalInstances:        &types.NullInt{Value: -1, IsSet: true},
   243  					PaidServicesAllowed:   &trueValue,
   244  					TotalServiceInstances: &types.NullInt{Value: -1, IsSet: true},
   245  					TotalRoutes:           &types.NullInt{Value: -1, IsSet: true},
   246  					TotalReservedPorts:    &types.NullInt{Value: -1, IsSet: true},
   247  				}
   248  				ccv3Quota = resources.SpaceQuota{
   249  					Quota: resources.Quota{
   250  						Name: spaceQuotaName,
   251  						Apps: resources.AppLimit{
   252  							TotalMemory:       &types.NullInt{Value: 0, IsSet: false},
   253  							InstanceMemory:    &types.NullInt{Value: 0, IsSet: false},
   254  							TotalAppInstances: &types.NullInt{Value: 0, IsSet: false},
   255  						},
   256  						Services: resources.ServiceLimit{
   257  							TotalServiceInstances: &types.NullInt{Value: 0, IsSet: false},
   258  							PaidServicePlans:      &trueValue,
   259  						},
   260  						Routes: resources.RouteLimit{
   261  							TotalRoutes:        &types.NullInt{Value: 0, IsSet: false},
   262  							TotalReservedPorts: &types.NullInt{Value: 0, IsSet: false},
   263  						},
   264  					},
   265  				}
   266  				fakeCloudControllerClient.CreateSpaceQuotaReturns(
   267  					ccv3Quota,
   268  					ccv3.Warnings{"some-quota-warning"},
   269  					nil,
   270  				)
   271  			})
   272  
   273  			It("call the create endpoint with the respective values and returns warnings", func() {
   274  				Expect(fakeCloudControllerClient.CreateSpaceQuotaCallCount()).To(Equal(1))
   275  
   276  				Expect(warnings).To(ConsistOf("some-quota-warning"))
   277  
   278  				passedQuota := fakeCloudControllerClient.CreateSpaceQuotaArgsForCall(0)
   279  				Expect(passedQuota).To(Equal(ccv3Quota))
   280  			})
   281  		})
   282  
   283  		When("creating a quota returns an error", func() {
   284  			BeforeEach(func() {
   285  				fakeCloudControllerClient.CreateSpaceQuotaReturns(
   286  					resources.SpaceQuota{},
   287  					ccv3.Warnings{"some-quota-warning"},
   288  					errors.New("create-error"),
   289  				)
   290  			})
   291  
   292  			It("returns the error and warnings", func() {
   293  				Expect(fakeCloudControllerClient.CreateSpaceQuotaCallCount()).To(Equal(1))
   294  
   295  				Expect(warnings).To(ConsistOf("some-quota-warning"))
   296  				Expect(executeErr).To(MatchError("create-error"))
   297  			})
   298  		})
   299  	})
   300  
   301  	Describe("DeleteSpaceQuotaByName", func() {
   302  		var (
   303  			quotaName  string
   304  			orgGUID    string
   305  			warnings   Warnings
   306  			executeErr error
   307  		)
   308  
   309  		BeforeEach(func() {
   310  			quotaName = "quota-name"
   311  			orgGUID = "some-org-guid"
   312  
   313  			fakeCloudControllerClient.GetSpaceQuotasReturns(
   314  				[]resources.SpaceQuota{{Quota: resources.Quota{GUID: "some-quota-guid"}}},
   315  				ccv3.Warnings{"get-quota-warning"},
   316  				nil,
   317  			)
   318  
   319  			fakeCloudControllerClient.DeleteSpaceQuotaReturns(
   320  				ccv3.JobURL("some-job-url"),
   321  				ccv3.Warnings{"delete-quota-warning"},
   322  				nil,
   323  			)
   324  
   325  			fakeCloudControllerClient.PollJobReturns(
   326  				ccv3.Warnings{"poll-job-warning"},
   327  				nil,
   328  			)
   329  		})
   330  
   331  		JustBeforeEach(func() {
   332  			warnings, executeErr = actor.DeleteSpaceQuotaByName(quotaName, orgGUID)
   333  		})
   334  
   335  		When("no errors occur", func() {
   336  			It("retrieves the space quota by name, makes the API call, and polls the deletion job until completion", func() {
   337  				Expect(executeErr).NotTo(HaveOccurred())
   338  				Expect(warnings).To(ConsistOf("get-quota-warning", "delete-quota-warning", "poll-job-warning"))
   339  
   340  				Expect(fakeCloudControllerClient.GetSpaceQuotasCallCount()).To(Equal(1))
   341  				query := fakeCloudControllerClient.GetSpaceQuotasArgsForCall(0)
   342  				Expect(query).To(ConsistOf(
   343  					ccv3.Query{
   344  						Key:    ccv3.OrganizationGUIDFilter,
   345  						Values: []string{orgGUID},
   346  					},
   347  					ccv3.Query{
   348  						Key:    ccv3.NameFilter,
   349  						Values: []string{quotaName},
   350  					},
   351  				))
   352  
   353  				Expect(fakeCloudControllerClient.DeleteSpaceQuotaCallCount()).To(Equal(1))
   354  				quotaGUID := fakeCloudControllerClient.DeleteSpaceQuotaArgsForCall(0)
   355  				Expect(quotaGUID).To(Equal("some-quota-guid"))
   356  
   357  				Expect(fakeCloudControllerClient.PollJobCallCount()).To(Equal(1))
   358  				inputJobURL := fakeCloudControllerClient.PollJobArgsForCall(0)
   359  				Expect(inputJobURL).To(Equal(ccv3.JobURL("some-job-url")))
   360  			})
   361  		})
   362  
   363  		When("there is an error getting the space quota", func() {
   364  			BeforeEach(func() {
   365  				fakeCloudControllerClient.GetSpaceQuotasReturns(
   366  					[]resources.SpaceQuota{},
   367  					ccv3.Warnings{"get-quota-warning"},
   368  					errors.New("get-quota-error"),
   369  				)
   370  			})
   371  
   372  			It("returns warnings and error", func() {
   373  				Expect(executeErr).To(MatchError("get-quota-error"))
   374  				Expect(warnings).To(ConsistOf("get-quota-warning"))
   375  			})
   376  		})
   377  
   378  		When("there is an error deleting the space quota", func() {
   379  			BeforeEach(func() {
   380  				fakeCloudControllerClient.DeleteSpaceQuotaReturns(
   381  					"",
   382  					ccv3.Warnings{"delete-quota-warning"},
   383  					errors.New("delete-quota-error"),
   384  				)
   385  			})
   386  
   387  			It("returns warnings and error", func() {
   388  				Expect(executeErr).To(MatchError("delete-quota-error"))
   389  				Expect(warnings).To(ConsistOf("get-quota-warning", "delete-quota-warning"))
   390  			})
   391  		})
   392  
   393  		When("there is an error polling the job", func() {
   394  			BeforeEach(func() {
   395  				fakeCloudControllerClient.PollJobReturns(
   396  					ccv3.Warnings{"poll-job-warning"},
   397  					errors.New("poll-job-error"),
   398  				)
   399  			})
   400  
   401  			It("returns warnings and error", func() {
   402  				Expect(executeErr).To(MatchError("poll-job-error"))
   403  				Expect(warnings).To(ConsistOf("get-quota-warning", "delete-quota-warning", "poll-job-warning"))
   404  			})
   405  		})
   406  	})
   407  
   408  	Describe("GetSpaceQuotaByName", func() {
   409  		var (
   410  			quotaName  string
   411  			orgGUID    string
   412  			quota      resources.SpaceQuota
   413  			warnings   Warnings
   414  			executeErr error
   415  		)
   416  
   417  		BeforeEach(func() {
   418  			quotaName = "quota-name"
   419  			orgGUID = "some-org-guid"
   420  		})
   421  
   422  		JustBeforeEach(func() {
   423  			quota, warnings, executeErr = actor.GetSpaceQuotaByName(quotaName, orgGUID)
   424  		})
   425  
   426  		When("when the API layer call returns an error", func() {
   427  			BeforeEach(func() {
   428  				fakeCloudControllerClient.GetSpaceQuotasReturns(
   429  					[]resources.SpaceQuota{},
   430  					ccv3.Warnings{"some-quota-warning"},
   431  					errors.New("list-error"),
   432  				)
   433  			})
   434  
   435  			It("returns the error and prints warnings", func() {
   436  				Expect(fakeCloudControllerClient.GetSpaceQuotasCallCount()).To(Equal(1))
   437  
   438  				Expect(warnings).To(ConsistOf("some-quota-warning"))
   439  				Expect(executeErr).To(MatchError("list-error"))
   440  				Expect(quota).To(Equal(resources.SpaceQuota{}))
   441  			})
   442  		})
   443  
   444  		When("when the space quota could not be found", func() {
   445  			BeforeEach(func() {
   446  				fakeCloudControllerClient.GetSpaceQuotasReturns(
   447  					[]resources.SpaceQuota{},
   448  					ccv3.Warnings{"some-quota-warning"},
   449  					nil,
   450  				)
   451  			})
   452  
   453  			It("returns the error and prints warnings", func() {
   454  				Expect(fakeCloudControllerClient.GetSpaceQuotasCallCount()).To(Equal(1))
   455  
   456  				Expect(warnings).To(ConsistOf("some-quota-warning"))
   457  				Expect(executeErr).To(MatchError(actionerror.SpaceQuotaNotFoundForNameError{Name: quotaName}))
   458  				Expect(quota).To(Equal(resources.SpaceQuota{}))
   459  			})
   460  		})
   461  
   462  		When("getting a single quota by name", func() {
   463  			BeforeEach(func() {
   464  				fakeCloudControllerClient.GetSpaceQuotasReturns(
   465  					[]resources.SpaceQuota{
   466  						{
   467  							Quota: resources.Quota{
   468  								GUID: "quota-guid",
   469  								Name: quotaName,
   470  							},
   471  						},
   472  					},
   473  					ccv3.Warnings{"some-quota-warning"},
   474  					nil,
   475  				)
   476  			})
   477  
   478  			It("queries the API and returns the matching space quota", func() {
   479  				Expect(executeErr).ToNot(HaveOccurred())
   480  
   481  				Expect(fakeCloudControllerClient.GetSpaceQuotasCallCount()).To(Equal(1))
   482  				query := fakeCloudControllerClient.GetSpaceQuotasArgsForCall(0)
   483  				Expect(query).To(ConsistOf(
   484  					ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}},
   485  					ccv3.Query{Key: ccv3.NameFilter, Values: []string{quotaName}},
   486  				))
   487  
   488  				Expect(warnings).To(ConsistOf("some-quota-warning"))
   489  				Expect(quota).To(Equal(resources.SpaceQuota{
   490  					Quota: resources.Quota{
   491  						GUID: "quota-guid",
   492  						Name: quotaName,
   493  					},
   494  				}))
   495  			})
   496  		})
   497  	})
   498  
   499  	Describe("GetSpaceQuotasByOrgGUID", func() {
   500  		var (
   501  			orgGUID    string
   502  			quotas     []resources.SpaceQuota
   503  			warnings   Warnings
   504  			executeErr error
   505  		)
   506  
   507  		BeforeEach(func() {
   508  			orgGUID = "org-guid"
   509  		})
   510  
   511  		JustBeforeEach(func() {
   512  			quotas, warnings, executeErr = actor.GetSpaceQuotasByOrgGUID(orgGUID)
   513  		})
   514  
   515  		When("when the API layer call returns an error", func() {
   516  			BeforeEach(func() {
   517  				fakeCloudControllerClient.GetSpaceQuotasReturns(
   518  					[]resources.SpaceQuota{},
   519  					ccv3.Warnings{"some-quota-warning"},
   520  					errors.New("list-error"),
   521  				)
   522  			})
   523  
   524  			It("returns the error and prints warnings", func() {
   525  				Expect(fakeCloudControllerClient.GetSpaceQuotasCallCount()).To(Equal(1))
   526  
   527  				Expect(warnings).To(ConsistOf("some-quota-warning"))
   528  				Expect(executeErr).To(MatchError("list-error"))
   529  				Expect(quotas).To(Equal([]resources.SpaceQuota{}))
   530  			})
   531  		})
   532  
   533  		When("getting all space quotas associated with the same organization", func() {
   534  			BeforeEach(func() {
   535  				fakeCloudControllerClient.GetSpaceQuotasReturns(
   536  					[]resources.SpaceQuota{
   537  						{
   538  							Quota: resources.Quota{
   539  								GUID: "quota-guid",
   540  								Name: "quota-beluga",
   541  							},
   542  							OrgGUID: orgGUID,
   543  						},
   544  						{
   545  							Quota: resources.Quota{
   546  								GUID: "quota-2-guid",
   547  								Name: "quota-manatee",
   548  							},
   549  							OrgGUID: orgGUID,
   550  						},
   551  					},
   552  					ccv3.Warnings{"some-quota-warning"},
   553  					nil,
   554  				)
   555  			})
   556  
   557  			It("queries the API and returns the matching space quotas", func() {
   558  				Expect(executeErr).ToNot(HaveOccurred())
   559  
   560  				Expect(fakeCloudControllerClient.GetSpaceQuotasCallCount()).To(Equal(1))
   561  				query := fakeCloudControllerClient.GetSpaceQuotasArgsForCall(0)
   562  				Expect(query).To(ConsistOf(
   563  					ccv3.Query{Key: ccv3.OrganizationGUIDFilter, Values: []string{orgGUID}},
   564  				))
   565  
   566  				Expect(warnings).To(ConsistOf("some-quota-warning"))
   567  				Expect(quotas).To(ConsistOf(
   568  					resources.SpaceQuota{
   569  						Quota: resources.Quota{
   570  							GUID: "quota-guid",
   571  							Name: "quota-beluga",
   572  						},
   573  						OrgGUID: orgGUID,
   574  					},
   575  					resources.SpaceQuota{
   576  						Quota: resources.Quota{
   577  							GUID: "quota-2-guid",
   578  							Name: "quota-manatee",
   579  						},
   580  						OrgGUID: orgGUID,
   581  					},
   582  				))
   583  			})
   584  		})
   585  	})
   586  
   587  	Describe("UpdateSpaceQuota", func() {
   588  		var (
   589  			oldQuotaName string
   590  			orgGUID      string
   591  			newQuotaName string
   592  			quotaLimits  QuotaLimits
   593  			warnings     Warnings
   594  			executeErr   error
   595  		)
   596  
   597  		BeforeEach(func() {
   598  			oldQuotaName = "old-quota-name"
   599  			orgGUID = "some-org-guid"
   600  			newQuotaName = "new-quota-name"
   601  
   602  			quotaLimits = QuotaLimits{
   603  				TotalMemoryInMB:       &types.NullInt{Value: 2048, IsSet: true},
   604  				PerProcessMemoryInMB:  &types.NullInt{Value: 1024, IsSet: true},
   605  				TotalInstances:        &types.NullInt{Value: 0, IsSet: false},
   606  				TotalServiceInstances: &types.NullInt{Value: 0, IsSet: true},
   607  				PaidServicesAllowed:   &trueValue,
   608  				TotalRoutes:           &types.NullInt{Value: 6, IsSet: true},
   609  				TotalReservedPorts:    &types.NullInt{Value: 5, IsSet: true},
   610  			}
   611  
   612  			fakeCloudControllerClient.GetSpaceQuotasReturns(
   613  				[]resources.SpaceQuota{{Quota: resources.Quota{Name: oldQuotaName}}},
   614  				ccv3.Warnings{"get-quotas-warning"},
   615  				nil,
   616  			)
   617  		})
   618  
   619  		JustBeforeEach(func() {
   620  			warnings, executeErr = actor.UpdateSpaceQuota(oldQuotaName, orgGUID, newQuotaName, quotaLimits)
   621  		})
   622  
   623  		When("the update-quota endpoint returns an error", func() {
   624  			BeforeEach(func() {
   625  				fakeCloudControllerClient.UpdateSpaceQuotaReturns(
   626  					resources.SpaceQuota{},
   627  					ccv3.Warnings{"update-quota-warning"},
   628  					errors.New("update-error"),
   629  				)
   630  			})
   631  
   632  			It("returns the error and warnings", func() {
   633  				Expect(fakeCloudControllerClient.UpdateSpaceQuotaCallCount()).To(Equal(1))
   634  
   635  				Expect(warnings).To(ConsistOf("get-quotas-warning", "update-quota-warning"))
   636  				Expect(executeErr).To(MatchError("update-error"))
   637  			})
   638  		})
   639  
   640  		When("no quota limits are being updated", func() {
   641  			var (
   642  				ccv3Quota resources.SpaceQuota
   643  			)
   644  
   645  			BeforeEach(func() {
   646  				quotaLimits = QuotaLimits{}
   647  
   648  				ccv3Quota = resources.SpaceQuota{
   649  					Quota: resources.Quota{
   650  						Name: oldQuotaName,
   651  						Apps: resources.AppLimit{
   652  							TotalMemory:       nil,
   653  							InstanceMemory:    nil,
   654  							TotalAppInstances: nil,
   655  						},
   656  						Services: resources.ServiceLimit{
   657  							TotalServiceInstances: nil,
   658  							PaidServicePlans:      nil,
   659  						},
   660  						Routes: resources.RouteLimit{
   661  							TotalRoutes:        nil,
   662  							TotalReservedPorts: nil,
   663  						},
   664  					},
   665  				}
   666  
   667  				fakeCloudControllerClient.UpdateSpaceQuotaReturns(
   668  					ccv3Quota,
   669  					ccv3.Warnings{"update-quota-warning"},
   670  					nil,
   671  				)
   672  			})
   673  
   674  			It("calls the update endpoint with the respective values and returns warnings", func() {
   675  				Expect(fakeCloudControllerClient.UpdateSpaceQuotaCallCount()).To(Equal(1))
   676  
   677  				Expect(warnings).To(ConsistOf("get-quotas-warning", "update-quota-warning"))
   678  
   679  				passedQuota := fakeCloudControllerClient.UpdateSpaceQuotaArgsForCall(0)
   680  
   681  				updatedQuota := ccv3Quota
   682  				updatedQuota.Name = newQuotaName
   683  
   684  				Expect(passedQuota).To(Equal(updatedQuota))
   685  			})
   686  		})
   687  
   688  		When("the update space quota has all values set to unlimited", func() {
   689  			var (
   690  				ccv3Quota resources.SpaceQuota
   691  			)
   692  
   693  			BeforeEach(func() {
   694  				quotaLimits = QuotaLimits{
   695  					TotalMemoryInMB:       &types.NullInt{Value: -1, IsSet: true},
   696  					PerProcessMemoryInMB:  &types.NullInt{Value: -1, IsSet: true},
   697  					TotalInstances:        &types.NullInt{Value: -1, IsSet: true},
   698  					PaidServicesAllowed:   &falseValue,
   699  					TotalServiceInstances: &types.NullInt{Value: -1, IsSet: true},
   700  					TotalRoutes:           &types.NullInt{Value: -1, IsSet: true},
   701  					TotalReservedPorts:    &types.NullInt{Value: -1, IsSet: true},
   702  				}
   703  
   704  				ccv3Quota = resources.SpaceQuota{
   705  					Quota: resources.Quota{
   706  						Name: oldQuotaName,
   707  						Apps: resources.AppLimit{
   708  							TotalMemory:       &types.NullInt{Value: 0, IsSet: false},
   709  							InstanceMemory:    &types.NullInt{Value: 0, IsSet: false},
   710  							TotalAppInstances: &types.NullInt{Value: 0, IsSet: false},
   711  						},
   712  						Services: resources.ServiceLimit{
   713  							TotalServiceInstances: &types.NullInt{Value: 0, IsSet: false},
   714  							PaidServicePlans:      &falseValue,
   715  						},
   716  						Routes: resources.RouteLimit{
   717  							TotalRoutes:        &types.NullInt{Value: 0, IsSet: false},
   718  							TotalReservedPorts: &types.NullInt{Value: 0, IsSet: false},
   719  						},
   720  					},
   721  				}
   722  
   723  				fakeCloudControllerClient.UpdateSpaceQuotaReturns(
   724  					ccv3Quota,
   725  					ccv3.Warnings{"update-quota-warning"},
   726  					nil,
   727  				)
   728  			})
   729  
   730  			It("calls the update endpoint with the respective values and returns warnings", func() {
   731  				Expect(fakeCloudControllerClient.UpdateSpaceQuotaCallCount()).To(Equal(1))
   732  
   733  				Expect(warnings).To(ConsistOf("get-quotas-warning", "update-quota-warning"))
   734  
   735  				passedQuota := fakeCloudControllerClient.UpdateSpaceQuotaArgsForCall(0)
   736  
   737  				updatedQuota := ccv3Quota
   738  				updatedQuota.Name = newQuotaName
   739  
   740  				Expect(passedQuota).To(Equal(updatedQuota))
   741  			})
   742  		})
   743  
   744  		When("The update space quota endpoint succeeds", func() {
   745  			var (
   746  				ccv3Quota resources.SpaceQuota
   747  			)
   748  
   749  			BeforeEach(func() {
   750  				ccv3Quota = resources.SpaceQuota{
   751  					Quota: resources.Quota{
   752  						Name: oldQuotaName,
   753  						Apps: resources.AppLimit{
   754  							TotalMemory:       &types.NullInt{Value: 2048, IsSet: true},
   755  							InstanceMemory:    &types.NullInt{Value: 1024, IsSet: true},
   756  							TotalAppInstances: &types.NullInt{Value: 0, IsSet: false},
   757  						},
   758  						Services: resources.ServiceLimit{
   759  							TotalServiceInstances: &types.NullInt{Value: 0, IsSet: true},
   760  							PaidServicePlans:      &trueValue,
   761  						},
   762  						Routes: resources.RouteLimit{
   763  							TotalRoutes:        &types.NullInt{Value: 6, IsSet: true},
   764  							TotalReservedPorts: &types.NullInt{Value: 5, IsSet: true},
   765  						},
   766  					},
   767  				}
   768  
   769  				fakeCloudControllerClient.UpdateSpaceQuotaReturns(
   770  					ccv3Quota,
   771  					ccv3.Warnings{"update-quota-warning"},
   772  					nil,
   773  				)
   774  			})
   775  
   776  			It("calls the update endpoint with the respective values and returns warnings", func() {
   777  				Expect(fakeCloudControllerClient.UpdateSpaceQuotaCallCount()).To(Equal(1))
   778  
   779  				Expect(warnings).To(ConsistOf("get-quotas-warning", "update-quota-warning"))
   780  
   781  				passedQuota := fakeCloudControllerClient.UpdateSpaceQuotaArgsForCall(0)
   782  
   783  				updatedQuota := ccv3Quota
   784  				updatedQuota.Name = newQuotaName
   785  
   786  				Expect(passedQuota).To(Equal(updatedQuota))
   787  			})
   788  		})
   789  
   790  		When("the space quota name is not being updated", func() {
   791  			var (
   792  				ccv3Quota resources.SpaceQuota
   793  			)
   794  
   795  			BeforeEach(func() {
   796  				newQuotaName = ""
   797  
   798  				ccv3Quota = resources.SpaceQuota{
   799  					Quota: resources.Quota{
   800  						Name: oldQuotaName,
   801  						Apps: resources.AppLimit{
   802  							TotalMemory:       &types.NullInt{Value: 2048, IsSet: true},
   803  							InstanceMemory:    &types.NullInt{Value: 1024, IsSet: true},
   804  							TotalAppInstances: &types.NullInt{Value: 0, IsSet: false},
   805  						},
   806  						Services: resources.ServiceLimit{
   807  							TotalServiceInstances: &types.NullInt{Value: 0, IsSet: true},
   808  							PaidServicePlans:      &trueValue,
   809  						},
   810  						Routes: resources.RouteLimit{
   811  							TotalRoutes:        &types.NullInt{Value: 6, IsSet: true},
   812  							TotalReservedPorts: &types.NullInt{Value: 5, IsSet: true},
   813  						},
   814  					},
   815  				}
   816  
   817  				fakeCloudControllerClient.UpdateSpaceQuotaReturns(
   818  					ccv3Quota,
   819  					ccv3.Warnings{"update-quota-warning"},
   820  					nil,
   821  				)
   822  			})
   823  			It("uses the current space quota name in the API request", func() {
   824  				Expect(executeErr).NotTo(HaveOccurred())
   825  				inputQuota := fakeCloudControllerClient.UpdateSpaceQuotaArgsForCall(0)
   826  				Expect(inputQuota.Name).To(Equal("old-quota-name"))
   827  			})
   828  		})
   829  	})
   830  
   831  	Describe("UnsetSpaceQuota", func() {
   832  		var (
   833  			spaceQuotaName string
   834  			orgGUID        string
   835  			spaceName      string
   836  			warnings       Warnings
   837  			executeErr     error
   838  		)
   839  
   840  		BeforeEach(func() {
   841  			spaceQuotaName = "some-quota-name"
   842  			orgGUID = "some-org-guid"
   843  			spaceName = "some-space-name"
   844  
   845  			fakeCloudControllerClient.GetSpaceQuotasReturns(
   846  				[]resources.SpaceQuota{{Quota: resources.Quota{Name: spaceQuotaName}}},
   847  				ccv3.Warnings{"get-quotas-warning"},
   848  				nil,
   849  			)
   850  
   851  			fakeCloudControllerClient.GetSpacesReturns(
   852  				[]resources.Space{{Name: spaceName}},
   853  				ccv3.IncludedResources{},
   854  				ccv3.Warnings{"get-spaces-warning"},
   855  				nil,
   856  			)
   857  		})
   858  
   859  		JustBeforeEach(func() {
   860  			warnings, executeErr = actor.UnsetSpaceQuota(spaceQuotaName, spaceName, orgGUID)
   861  		})
   862  
   863  		When("getting the space fails", func() {
   864  			When("no space with that name exists", func() {
   865  				BeforeEach(func() {
   866  					fakeCloudControllerClient.GetSpacesReturns(
   867  						[]resources.Space{},
   868  						ccv3.IncludedResources{},
   869  						ccv3.Warnings{"get-spaces-warning"},
   870  						nil,
   871  					)
   872  				})
   873  
   874  				It("returns the error and prints warnings", func() {
   875  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   876  
   877  					Expect(warnings).To(ContainElement("get-spaces-warning"))
   878  					Expect(executeErr).To(MatchError(actionerror.SpaceNotFoundError{Name: spaceName}))
   879  				})
   880  			})
   881  
   882  			When("getting the space returns an error", func() {
   883  				BeforeEach(func() {
   884  					fakeCloudControllerClient.GetSpacesReturns(
   885  						[]resources.Space{},
   886  						ccv3.IncludedResources{},
   887  						ccv3.Warnings{"get-spaces-warning"},
   888  						errors.New("some-get-spaces-error"),
   889  					)
   890  				})
   891  
   892  				It("returns the error and prints warnings", func() {
   893  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   894  
   895  					Expect(warnings).To(ContainElement("get-spaces-warning"))
   896  					Expect(executeErr).To(MatchError("some-get-spaces-error"))
   897  				})
   898  			})
   899  		})
   900  
   901  		When("getting the space quota fails", func() {
   902  			When("no space with that name exists", func() {
   903  
   904  				BeforeEach(func() {
   905  					fakeCloudControllerClient.GetSpaceQuotasReturns(
   906  						[]resources.SpaceQuota{},
   907  						ccv3.Warnings{"get-quota-warning"},
   908  						nil,
   909  					)
   910  				})
   911  
   912  				It("returns the error and prints warnings", func() {
   913  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   914  
   915  					Expect(warnings).To(ContainElement("get-quota-warning"))
   916  					Expect(executeErr).To(MatchError(actionerror.SpaceQuotaNotFoundForNameError{Name: spaceQuotaName}))
   917  				})
   918  			})
   919  
   920  			When("getting the space returns an error", func() {
   921  
   922  				BeforeEach(func() {
   923  					fakeCloudControllerClient.GetSpaceQuotasReturns(
   924  						[]resources.SpaceQuota{},
   925  						ccv3.Warnings{"get-quota-warning"},
   926  						errors.New("some-get-quotas-error"),
   927  					)
   928  				})
   929  
   930  				It("returns the error and prints warnings", func() {
   931  					Expect(fakeCloudControllerClient.GetSpacesCallCount()).To(Equal(1))
   932  
   933  					Expect(warnings).To(ContainElement("get-quota-warning"))
   934  					Expect(executeErr).To(MatchError("some-get-quotas-error"))
   935  				})
   936  			})
   937  		})
   938  
   939  		When("unsetting the space quota fails", func() {
   940  			BeforeEach(func() {
   941  				fakeCloudControllerClient.UnsetSpaceQuotaReturns(
   942  					ccv3.Warnings{"unset-quota-warning"},
   943  					errors.New("some-unset-error"),
   944  				)
   945  			})
   946  
   947  			It("returns the error and prints warnings", func() {
   948  				Expect(warnings).To(ConsistOf("unset-quota-warning", "get-spaces-warning", "get-quotas-warning"))
   949  				Expect(executeErr).To(MatchError("some-unset-error"))
   950  			})
   951  		})
   952  
   953  	})
   954  
   955  })