github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/spacequota/update_space_quota_test.go (about) 1 package spacequota_test 2 3 import ( 4 "encoding/json" 5 6 "code.cloudfoundry.org/cli/cf" 7 "code.cloudfoundry.org/cli/cf/api/spacequotas/spacequotasfakes" 8 "code.cloudfoundry.org/cli/cf/commandregistry" 9 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 10 "code.cloudfoundry.org/cli/cf/errors" 11 "code.cloudfoundry.org/cli/cf/models" 12 "code.cloudfoundry.org/cli/cf/requirements" 13 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 14 testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands" 15 testconfig "code.cloudfoundry.org/cli/util/testhelpers/configuration" 16 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 17 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/ginkgo/extensions/table" 20 . "github.com/onsi/gomega" 21 ) 22 23 var _ = Describe("update-space-quota command", func() { 24 var ( 25 ui *testterm.FakeUI 26 quotaRepo *spacequotasfakes.FakeSpaceQuotaRepository 27 requirementsFactory *requirementsfakes.FakeFactory 28 29 quota models.SpaceQuota 30 quotaPaidService models.SpaceQuota 31 configRepo coreconfig.Repository 32 deps commandregistry.Dependency 33 ) 34 35 updateCommandDependency := func(pluginCall bool) { 36 deps.UI = ui 37 deps.Config = configRepo 38 deps.RepoLocator = deps.RepoLocator.SetSpaceQuotaRepository(quotaRepo) 39 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("update-space-quota").SetDependency(deps, pluginCall)) 40 } 41 42 runCommand := func(args ...string) bool { 43 return testcmd.RunCLICommand("update-space-quota", args, requirementsFactory, updateCommandDependency, false, ui) 44 } 45 46 BeforeEach(func() { 47 ui = &testterm.FakeUI{} 48 configRepo = testconfig.NewRepositoryWithDefaults() 49 quotaRepo = new(spacequotasfakes.FakeSpaceQuotaRepository) 50 requirementsFactory = new(requirementsfakes.FakeFactory) 51 }) 52 53 Describe("requirements", func() { 54 It("fails when the user is not logged in", func() { 55 requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"}) 56 Expect(runCommand("my-quota", "-m", "50G")).NotTo(HavePassedRequirements()) 57 }) 58 59 It("fails when the user does not have an org targeted", func() { 60 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 61 orgReq := new(requirementsfakes.FakeTargetedOrgRequirement) 62 orgReq.ExecuteReturns(errors.New("not targeting org")) 63 requirementsFactory.NewTargetedOrgRequirementReturns(orgReq) 64 Expect(runCommand()).NotTo(HavePassedRequirements()) 65 Expect(runCommand("my-quota", "-m", "50G")).NotTo(HavePassedRequirements()) 66 }) 67 68 It("fails with usage if space quota name is not provided", func() { 69 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 70 requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement)) 71 runCommand() 72 73 Expect(ui.Outputs()).To(ContainSubstrings( 74 []string{"Incorrect Usage", "Requires an argument"}, 75 )) 76 }) 77 78 Context("the minimum API version requirement", func() { 79 BeforeEach(func() { 80 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 81 requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement)) 82 requirementsFactory.NewMinAPIVersionRequirementReturns(requirements.Failing{Message: "not min api"}) 83 }) 84 85 It("fails when the -a option is provided", func() { 86 Expect(runCommand("my-quota", "-a", "10")).To(BeFalse()) 87 Expect(requirementsFactory.NewMinAPIVersionRequirementCallCount()).To(Equal(1)) 88 option, version := requirementsFactory.NewMinAPIVersionRequirementArgsForCall(0) 89 Expect(option).To(Equal("Option '-a'")) 90 Expect(version).To(Equal(cf.SpaceAppInstanceLimitMinimumAPIVersion)) 91 }) 92 93 It("does not fail when the -a option is not provided", func() { 94 Expect(runCommand("my-quota", "-m", "10G")).To(BeTrue()) 95 }) 96 }) 97 }) 98 99 Context("when the user is logged in", func() { 100 BeforeEach(func() { 101 quota = models.SpaceQuota{ 102 GUID: "my-quota-guid", 103 Name: "my-quota", 104 MemoryLimit: 1024, 105 InstanceMemoryLimit: 512, 106 RoutesLimit: 111, 107 ServicesLimit: 222, 108 AppInstanceLimit: 333, 109 NonBasicServicesAllowed: false, 110 OrgGUID: "my-org-guid", 111 } 112 113 quotaPaidService = models.SpaceQuota{NonBasicServicesAllowed: true} 114 115 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 116 requirementsFactory.NewTargetedOrgRequirementReturns(new(requirementsfakes.FakeTargetedOrgRequirement)) 117 requirementsFactory.NewMinAPIVersionRequirementReturns(requirements.Passing{}) 118 }) 119 120 JustBeforeEach(func() { 121 quotaRepo.FindByNameReturns(quota, nil) 122 }) 123 124 Context("when the -m flag is provided", func() { 125 It("updates the memory limit", func() { 126 runCommand("-m", "15G", "my-quota") 127 Expect(quotaRepo.UpdateArgsForCall(0).Name).To(Equal("my-quota")) 128 Expect(quotaRepo.UpdateArgsForCall(0).MemoryLimit).To(Equal(int64(15360))) 129 }) 130 131 It("alerts the user when parsing the memory limit fails", func() { 132 runCommand("-m", "whoops", "wit mah hussle", "my-org") 133 134 Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"})) 135 }) 136 }) 137 138 Context("when the -i flag is provided", func() { 139 It("sets the memory limit", func() { 140 runCommand("-i", "50G", "my-quota") 141 Expect(quotaRepo.UpdateCallCount()).To(Equal(1)) 142 Expect(quotaRepo.UpdateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(51200))) 143 }) 144 145 It("sets the memory limit to -1", func() { 146 runCommand("-i", "-1", "my-quota") 147 Expect(quotaRepo.UpdateCallCount()).To(Equal(1)) 148 Expect(quotaRepo.UpdateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(-1))) 149 }) 150 151 It("alerts the user when parsing the memory limit fails", func() { 152 runCommand("-i", "whoops", "my-quota") 153 Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"})) 154 }) 155 }) 156 157 Context("when the -a flag is provided", func() { 158 It("sets the instance limit", func() { 159 runCommand("-a", "50", "my-quota") 160 Expect(quotaRepo.UpdateCallCount()).To(Equal(1)) 161 Expect(quotaRepo.UpdateArgsForCall(0).AppInstanceLimit).To(Equal(50)) 162 }) 163 164 It("does not override the value if it's not provided", func() { 165 runCommand("-s", "5", "my-quota") 166 Expect(quotaRepo.UpdateCallCount()).To(Equal(1)) 167 Expect(quotaRepo.UpdateArgsForCall(0).AppInstanceLimit).To(Equal(333)) 168 }) 169 }) 170 171 Context("when the -r flag is provided", func() { 172 It("sets the route limit", func() { 173 runCommand("-r", "12", "ecstatic") 174 Expect(quotaRepo.UpdateCallCount()).To(Equal(1)) 175 Expect(quotaRepo.UpdateArgsForCall(0).RoutesLimit).To(Equal(12)) 176 }) 177 }) 178 179 Context("when the -s flag is provided", func() { 180 It("sets the service instance limit", func() { 181 runCommand("-s", "42", "my-quota") 182 Expect(quotaRepo.UpdateCallCount()).To(Equal(1)) 183 Expect(quotaRepo.UpdateArgsForCall(0).ServicesLimit).To(Equal(42)) 184 }) 185 }) 186 187 Context("when the -n flag is provided", func() { 188 It("sets the service instance name", func() { 189 runCommand("-n", "foo", "my-quota") 190 Expect(quotaRepo.UpdateCallCount()).To(Equal(1)) 191 Expect(quotaRepo.UpdateArgsForCall(0).Name).To(Equal("foo")) 192 }) 193 }) 194 195 Context("when --allow-non-basic-services is provided", func() { 196 It("updates the quota to allow paid service plans", func() { 197 runCommand("--allow-paid-service-plans", "my-for-profit-quota") 198 Expect(quotaRepo.UpdateCallCount()).To(Equal(1)) 199 Expect(quotaRepo.UpdateArgsForCall(0).NonBasicServicesAllowed).To(BeTrue()) 200 }) 201 }) 202 203 Context("when --disallow-non-basic-services is provided", func() { 204 It("updates the quota to disallow paid service plans", func() { 205 quotaRepo.FindByNameReturns(quotaPaidService, nil) 206 207 runCommand("--disallow-paid-service-plans", "my-for-profit-quota") 208 Expect(quotaRepo.UpdateCallCount()).To(Equal(1)) 209 Expect(quotaRepo.UpdateArgsForCall(0).NonBasicServicesAllowed).To(BeFalse()) 210 }) 211 }) 212 213 Context("when --reserved-route-ports is provided", func() { 214 DescribeTable("updates the quota to the given number of reserved route ports", 215 func(numberOfReservedRoutes string) { 216 quotaRepo.FindByNameReturns(quotaPaidService, nil) 217 218 runCommand("--reserved-route-ports", numberOfReservedRoutes, "my-for-profit-quota") 219 Expect(quotaRepo.UpdateCallCount()).To(Equal(1)) 220 Expect(quotaRepo.UpdateArgsForCall(0).ReservedRoutePortsLimit).To(Equal(json.Number(numberOfReservedRoutes))) 221 }, 222 Entry("for positive values", "42"), 223 Entry("for 0", "0"), 224 Entry("for -1", "-1"), 225 ) 226 }) 227 228 Context("when updating a quota returns an error", func() { 229 It("alerts the user when creating the quota fails", func() { 230 quotaRepo.UpdateReturns(errors.New("WHOOP THERE IT IS")) 231 runCommand("my-quota") 232 233 Expect(ui.Outputs()).To(ContainSubstrings( 234 []string{"Updating space quota", "my-quota", "my-user"}, 235 []string{"FAILED"}, 236 )) 237 }) 238 239 It("fails if the allow and disallow flag are both passed", func() { 240 runCommand("--disallow-paid-service-plans", "--allow-paid-service-plans", "my-for-profit-quota") 241 Expect(ui.Outputs()).To(ContainSubstrings( 242 []string{"FAILED"}, 243 )) 244 }) 245 }) 246 }) 247 })