github.com/sleungcy-sap/cli@v7.1.0+incompatible/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 fakeConfig.CurrentUserReturns(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.MemoryWithUnlimited{IsSet: true, Value: 9} 98 cmd.TotalMemory = flag.MemoryWithUnlimited{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 fakeActor.UpdateSpaceQuotaReturns( 103 v7action.Warnings{"warning"}, 104 nil) 105 }) 106 107 It("updates the org quota to the new values", func() { 108 Expect(executeErr).NotTo(HaveOccurred()) 109 Expect(fakeActor.UpdateSpaceQuotaCallCount()).To(Equal(1)) 110 111 oldQuotaName, orgGUID, newQuotaName, quotaLimits := fakeActor.UpdateSpaceQuotaArgsForCall(0) 112 113 Expect(oldQuotaName).To(Equal("old-space-quota-name")) 114 Expect(orgGUID).To(Equal("targeted-org-guid")) 115 Expect(newQuotaName).To(Equal("new-space-quota-name")) 116 117 Expect(quotaLimits.TotalInstances.IsSet).To(Equal(true)) 118 Expect(quotaLimits.TotalInstances.Value).To(Equal(10)) 119 120 Expect(quotaLimits.PerProcessMemoryInMB.IsSet).To(Equal(true)) 121 Expect(quotaLimits.PerProcessMemoryInMB.Value).To(Equal(9)) 122 123 Expect(*quotaLimits.PaidServicesAllowed).To(Equal(true)) 124 125 Expect(quotaLimits.TotalMemoryInMB.IsSet).To(Equal(true)) 126 Expect(quotaLimits.TotalMemoryInMB.Value).To(Equal(2048)) 127 128 Expect(quotaLimits.TotalRoutes.IsSet).To(Equal(true)) 129 Expect(quotaLimits.TotalRoutes.Value).To(Equal(7)) 130 131 Expect(quotaLimits.TotalReservedPorts.IsSet).To(Equal(true)) 132 Expect(quotaLimits.TotalReservedPorts.Value).To(Equal(1)) 133 134 Expect(quotaLimits.TotalServiceInstances.IsSet).To(Equal(true)) 135 Expect(quotaLimits.TotalServiceInstances.Value).To(Equal(2)) 136 137 Expect(testUI.Out).To(Say("Updating space quota %s for org %s as bob...", spaceQuotaName, orgName)) 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.MemoryWithUnlimited{IsSet: true, Value: 2048} 145 cmd.TotalServiceInstances = flag.IntegerLimit{IsSet: true, Value: 2} 146 fakeActor.UpdateSpaceQuotaReturns( 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.UpdateSpaceQuotaCallCount()).To(Equal(1)) 154 155 oldQuotaName, orgGUID, newQuotaName, quotaLimits := fakeActor.UpdateSpaceQuotaArgsForCall(0) 156 157 Expect(oldQuotaName).To(Equal("old-space-quota-name")) 158 Expect(orgGUID).To(Equal("targeted-org-guid")) 159 Expect(newQuotaName).To(Equal("")) 160 161 Expect(quotaLimits.TotalServiceInstances.IsSet).To(Equal(true)) 162 Expect(quotaLimits.TotalServiceInstances.Value).To(Equal(2)) 163 164 Expect(quotaLimits.TotalMemoryInMB.IsSet).To(Equal(true)) 165 Expect(quotaLimits.TotalMemoryInMB.Value).To(Equal(2048)) 166 167 Expect(quotaLimits.TotalInstances).To(BeNil()) 168 169 Expect(quotaLimits.PerProcessMemoryInMB).To(BeNil()) 170 171 Expect(quotaLimits.PaidServicesAllowed).To(BeNil()) 172 173 Expect(quotaLimits.TotalRoutes).To(BeNil()) 174 175 Expect(quotaLimits.TotalReservedPorts).To(BeNil()) 176 177 Expect(testUI.Out).To(Say("Updating space quota %s for org %s as bob...", spaceQuotaName, orgName)) 178 Expect(testUI.Out).To(Say("OK")) 179 }) 180 }) 181 }) 182 183 When("conflicting flags are given", func() { 184 BeforeEach(func() { 185 cmd.PaidServicePlans = true 186 cmd.NoPaidServicePlans = true 187 }) 188 189 It("returns with a helpful error", func() { 190 Expect(executeErr).To(MatchError(translatableerror.ArgumentCombinationError{ 191 Args: []string{"--allow-paid-service-plans", "--disallow-paid-service-plans"}, 192 })) 193 }) 194 }) 195 })