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