github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/command/v7/update_space_quota_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/command/translatableerror"
     7  
     8  	"code.cloudfoundry.org/cli/actor/actionerror"
     9  	"code.cloudfoundry.org/cli/actor/v7action"
    10  	"code.cloudfoundry.org/cli/command/commandfakes"
    11  	"code.cloudfoundry.org/cli/command/flag"
    12  	v7 "code.cloudfoundry.org/cli/command/v7"
    13  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    14  	"code.cloudfoundry.org/cli/util/configv3"
    15  	"code.cloudfoundry.org/cli/util/ui"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	. "github.com/onsi/gomega/gbytes"
    19  )
    20  
    21  var _ = Describe("UpdateSpaceQuotaCommand", func() {
    22  	var (
    23  		cmd             v7.UpdateSpaceQuotaCommand
    24  		testUI          *ui.UI
    25  		fakeConfig      *commandfakes.FakeConfig
    26  		fakeSharedActor *commandfakes.FakeSharedActor
    27  		fakeActor       *v7fakes.FakeActor
    28  		spaceQuotaName  string
    29  		executeErr      error
    30  		orgName         = "some-org"
    31  
    32  		currentUserName string
    33  	)
    34  
    35  	BeforeEach(func() {
    36  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    37  		fakeConfig = new(commandfakes.FakeConfig)
    38  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    39  		fakeActor = new(v7fakes.FakeActor)
    40  		spaceQuotaName = "old-space-quota-name"
    41  
    42  		cmd = v7.UpdateSpaceQuotaCommand{
    43  			BaseCommand: v7.BaseCommand{
    44  				UI:          testUI,
    45  				Config:      fakeConfig,
    46  				SharedActor: fakeSharedActor,
    47  				Actor:       fakeActor,
    48  			},
    49  			RequiredArgs: flag.SpaceQuota{SpaceQuota: spaceQuotaName},
    50  		}
    51  
    52  		currentUserName = "bob"
    53  		fakeActor.GetCurrentUserReturns(configv3.User{Name: currentUserName}, nil)
    54  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{GUID: "targeted-org-guid"})
    55  		fakeConfig.TargetedOrganizationNameReturns(orgName)
    56  	})
    57  
    58  	JustBeforeEach(func() {
    59  		executeErr = cmd.Execute(nil)
    60  	})
    61  
    62  	When("the environment is not set up correctly", func() {
    63  		BeforeEach(func() {
    64  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{})
    65  		})
    66  
    67  		It("returns an error", func() {
    68  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{}))
    69  
    70  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    71  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    72  			Expect(checkTargetedOrg).To(BeTrue())
    73  			Expect(checkTargetedSpace).To(BeFalse())
    74  		})
    75  	})
    76  
    77  	When("updating the space quota fails", func() {
    78  		BeforeEach(func() {
    79  			fakeActor.UpdateSpaceQuotaReturns(v7action.Warnings{"warn-456", "warn-789"}, errors.New("update-org-quota-err"))
    80  		})
    81  
    82  		It("returns an error", func() {
    83  			Expect(executeErr).To(MatchError("update-org-quota-err"))
    84  
    85  			Expect(fakeActor.UpdateSpaceQuotaCallCount()).To(Equal(1))
    86  			Expect(testUI.Err).To(Say("warn-456"))
    87  			Expect(testUI.Err).To(Say("warn-789"))
    88  		})
    89  	})
    90  
    91  	When("the org quota is updated successfully", func() {
    92  		When("the org quota is provided a new value for each flag", func() {
    93  			BeforeEach(func() {
    94  				cmd.NewName = "new-space-quota-name"
    95  				cmd.PaidServicePlans = true
    96  				cmd.NumAppInstances = flag.IntegerLimit{IsSet: true, Value: 10}
    97  				cmd.PerProcessMemory = flag.MegabytesWithUnlimited{IsSet: true, Value: 9}
    98  				cmd.TotalMemory = flag.MegabytesWithUnlimited{IsSet: true, Value: 2048}
    99  				cmd.TotalRoutes = flag.IntegerLimit{IsSet: true, Value: 7}
   100  				cmd.TotalReservedPorts = flag.IntegerLimit{IsSet: true, Value: 1}
   101  				cmd.TotalServiceInstances = flag.IntegerLimit{IsSet: true, Value: 2}
   102  				cmd.TotalLogVolume = flag.BytesWithUnlimited{IsSet: true, Value: 512}
   103  				fakeActor.UpdateSpaceQuotaReturns(
   104  					v7action.Warnings{"warning"},
   105  					nil)
   106  			})
   107  
   108  			It("updates the org quota to the new values", func() {
   109  				Expect(executeErr).NotTo(HaveOccurred())
   110  				Expect(fakeActor.UpdateSpaceQuotaCallCount()).To(Equal(1))
   111  
   112  				oldQuotaName, orgGUID, newQuotaName, quotaLimits := fakeActor.UpdateSpaceQuotaArgsForCall(0)
   113  
   114  				Expect(oldQuotaName).To(Equal("old-space-quota-name"))
   115  				Expect(orgGUID).To(Equal("targeted-org-guid"))
   116  				Expect(newQuotaName).To(Equal("new-space-quota-name"))
   117  
   118  				Expect(quotaLimits.TotalInstances.IsSet).To(Equal(true))
   119  				Expect(quotaLimits.TotalInstances.Value).To(Equal(10))
   120  
   121  				Expect(quotaLimits.PerProcessMemoryInMB.IsSet).To(Equal(true))
   122  				Expect(quotaLimits.PerProcessMemoryInMB.Value).To(Equal(9))
   123  
   124  				Expect(*quotaLimits.PaidServicesAllowed).To(Equal(true))
   125  
   126  				Expect(quotaLimits.TotalMemoryInMB.IsSet).To(Equal(true))
   127  				Expect(quotaLimits.TotalMemoryInMB.Value).To(Equal(2048))
   128  
   129  				Expect(quotaLimits.TotalRoutes.IsSet).To(Equal(true))
   130  				Expect(quotaLimits.TotalRoutes.Value).To(Equal(7))
   131  
   132  				Expect(quotaLimits.TotalReservedPorts.IsSet).To(Equal(true))
   133  				Expect(quotaLimits.TotalReservedPorts.Value).To(Equal(1))
   134  
   135  				Expect(quotaLimits.TotalServiceInstances.IsSet).To(Equal(true))
   136  				Expect(quotaLimits.TotalServiceInstances.Value).To(Equal(2))
   137  
   138  				Expect(quotaLimits.TotalLogVolume.IsSet).To(Equal(true))
   139  				Expect(quotaLimits.TotalLogVolume.Value).To(Equal(512))
   140  
   141  				Expect(testUI.Out).To(Say("Updating space quota %s for org %s as bob...", spaceQuotaName, orgName))
   142  				Expect(testUI.Out).To(Say("OK"))
   143  			})
   144  		})
   145  
   146  		When("only some org quota limits are updated", func() {
   147  			BeforeEach(func() {
   148  				cmd.TotalMemory = flag.MegabytesWithUnlimited{IsSet: true, Value: 2048}
   149  				cmd.TotalServiceInstances = flag.IntegerLimit{IsSet: true, Value: 2}
   150  				fakeActor.UpdateSpaceQuotaReturns(
   151  					v7action.Warnings{"warning"},
   152  					nil)
   153  			})
   154  
   155  			It("updates the org quota to the new values", func() {
   156  				Expect(executeErr).NotTo(HaveOccurred())
   157  				Expect(fakeActor.UpdateSpaceQuotaCallCount()).To(Equal(1))
   158  
   159  				oldQuotaName, orgGUID, newQuotaName, quotaLimits := fakeActor.UpdateSpaceQuotaArgsForCall(0)
   160  
   161  				Expect(oldQuotaName).To(Equal("old-space-quota-name"))
   162  				Expect(orgGUID).To(Equal("targeted-org-guid"))
   163  				Expect(newQuotaName).To(Equal(""))
   164  
   165  				Expect(quotaLimits.TotalServiceInstances.IsSet).To(Equal(true))
   166  				Expect(quotaLimits.TotalServiceInstances.Value).To(Equal(2))
   167  
   168  				Expect(quotaLimits.TotalMemoryInMB.IsSet).To(Equal(true))
   169  				Expect(quotaLimits.TotalMemoryInMB.Value).To(Equal(2048))
   170  
   171  				Expect(quotaLimits.TotalInstances).To(BeNil())
   172  
   173  				Expect(quotaLimits.PerProcessMemoryInMB).To(BeNil())
   174  
   175  				Expect(quotaLimits.PaidServicesAllowed).To(BeNil())
   176  
   177  				Expect(quotaLimits.TotalRoutes).To(BeNil())
   178  
   179  				Expect(quotaLimits.TotalReservedPorts).To(BeNil())
   180  
   181  				Expect(quotaLimits.TotalLogVolume).To(BeNil())
   182  
   183  				Expect(testUI.Out).To(Say("Updating space quota %s for org %s as bob...", spaceQuotaName, orgName))
   184  				Expect(testUI.Out).To(Say("OK"))
   185  			})
   186  		})
   187  	})
   188  
   189  	When("conflicting flags are given", func() {
   190  		BeforeEach(func() {
   191  			cmd.PaidServicePlans = true
   192  			cmd.NoPaidServicePlans = true
   193  		})
   194  
   195  		It("returns with a helpful error", func() {
   196  			Expect(executeErr).To(MatchError(translatableerror.ArgumentCombinationError{
   197  				Args: []string{"--allow-paid-service-plans", "--disallow-paid-service-plans"},
   198  			}))
   199  		})
   200  	})
   201  })