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