github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/spacequota/create_space_quota_test.go (about) 1 package spacequota_test 2 3 import ( 4 "encoding/json" 5 6 "code.cloudfoundry.org/cli/cf/commandregistry" 7 "code.cloudfoundry.org/cli/cf/errors" 8 "code.cloudfoundry.org/cli/cf/models" 9 "code.cloudfoundry.org/cli/cf/requirements" 10 11 "code.cloudfoundry.org/cli/cf/api/resources" 12 "code.cloudfoundry.org/cli/cf/api/spacequotas/spacequotasfakes" 13 "code.cloudfoundry.org/cli/cf/configuration/coreconfig/coreconfigfakes" 14 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 15 testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal" 16 17 "code.cloudfoundry.org/cli/cf/api" 18 "code.cloudfoundry.org/cli/cf/commands/spacequota" 19 "code.cloudfoundry.org/cli/cf/flags" 20 . "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers" 21 . "github.com/onsi/ginkgo" 22 . "github.com/onsi/gomega" 23 ) 24 25 var _ = Describe("create-space-quota", func() { 26 var ( 27 ui *testterm.FakeUI 28 quotaRepo *spacequotasfakes.FakeSpaceQuotaRepository 29 config *coreconfigfakes.FakeRepository 30 31 loginReq *requirementsfakes.FakeRequirement 32 targetedOrgReq *requirementsfakes.FakeTargetedOrgRequirement 33 minApiVersionReq *requirementsfakes.FakeRequirement 34 reqFactory *requirementsfakes.FakeFactory 35 36 deps commandregistry.Dependency 37 cmd spacequota.CreateSpaceQuota 38 flagContext flags.FlagContext 39 ) 40 41 BeforeEach(func() { 42 ui = &testterm.FakeUI{} 43 quotaRepo = new(spacequotasfakes.FakeSpaceQuotaRepository) 44 config = new(coreconfigfakes.FakeRepository) 45 46 repoLocator := api.RepositoryLocator{} 47 repoLocator = repoLocator.SetSpaceQuotaRepository(quotaRepo) 48 49 deps = commandregistry.Dependency{ 50 UI: ui, 51 Config: config, 52 RepoLocator: repoLocator, 53 } 54 55 reqFactory = new(requirementsfakes.FakeFactory) 56 57 loginReq = new(requirementsfakes.FakeRequirement) 58 loginReq.ExecuteReturns(nil) 59 reqFactory.NewLoginRequirementReturns(loginReq) 60 61 targetedOrgReq = new(requirementsfakes.FakeTargetedOrgRequirement) 62 targetedOrgReq.ExecuteReturns(nil) 63 reqFactory.NewTargetedOrgRequirementReturns(targetedOrgReq) 64 65 minApiVersionReq = new(requirementsfakes.FakeRequirement) 66 minApiVersionReq.ExecuteReturns(nil) 67 reqFactory.NewMinAPIVersionRequirementReturns(minApiVersionReq) 68 69 cmd = spacequota.CreateSpaceQuota{} 70 flagContext = flags.NewFlagContext(cmd.MetaData().Flags) 71 }) 72 73 Describe("Requirements", func() { 74 BeforeEach(func() { 75 cmd.SetDependency(deps, false) 76 }) 77 Context("when not exactly one arg is provided", func() { 78 It("fails", func() { 79 flagContext.Parse() 80 _, err := cmd.Requirements(reqFactory, flagContext) 81 Expect(err).To(HaveOccurred()) 82 Expect(ui.Outputs()).To(ContainSubstrings( 83 []string{"FAILED"}, 84 []string{"Incorrect Usage. Requires an argument"}, 85 )) 86 }) 87 }) 88 89 Context("when provided exactly one arg", func() { 90 var actualRequirements []requirements.Requirement 91 var err error 92 93 Context("when no flags are provided", func() { 94 BeforeEach(func() { 95 flagContext.Parse("myquota") 96 actualRequirements, err = cmd.Requirements(reqFactory, flagContext) 97 Expect(err).NotTo(HaveOccurred()) 98 }) 99 100 It("returns a login requirement", func() { 101 Expect(reqFactory.NewLoginRequirementCallCount()).To(Equal(1)) 102 Expect(actualRequirements).To(ContainElement(loginReq)) 103 }) 104 105 It("returns a targeted org requirement", func() { 106 Expect(reqFactory.NewTargetedOrgRequirementCallCount()).To(Equal(1)) 107 Expect(actualRequirements).To(ContainElement(targetedOrgReq)) 108 }) 109 }) 110 }) 111 }) 112 113 Describe("Execute", func() { 114 var runCLIErr error 115 116 BeforeEach(func() { 117 orgFields := models.OrganizationFields{ 118 Name: "my-org", 119 GUID: "my-org-guid", 120 } 121 122 config.OrganizationFieldsReturns(orgFields) 123 config.UsernameReturns("my-user") 124 }) 125 126 JustBeforeEach(func() { 127 cmd.SetDependency(deps, false) 128 runCLIErr = cmd.Execute(flagContext) 129 }) 130 131 Context("when creating a quota succeeds", func() { 132 Context("without any flags", func() { 133 BeforeEach(func() { 134 flagContext.Parse("my-quota") 135 }) 136 137 It("creates a quota with a given name", func() { 138 Expect(quotaRepo.CreateArgsForCall(0).Name).To(Equal("my-quota")) 139 Expect(quotaRepo.CreateArgsForCall(0).OrgGUID).To(Equal("my-org-guid")) 140 Expect(ui.Outputs()).To(ContainSubstrings( 141 []string{"Creating space quota", "my-quota", "my-org", "my-user", "..."}, 142 []string{"OK"}, 143 )) 144 }) 145 146 It("sets the instance memory limit to unlimiited", func() { 147 Expect(quotaRepo.CreateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(-1))) 148 }) 149 150 It("sets the instance limit to unlimited", func() { 151 Expect(quotaRepo.CreateArgsForCall(0).AppInstanceLimit).To(Equal(resources.UnlimitedAppInstances)) 152 }) 153 154 It("defaults to not allowing paid service plans", func() { 155 Expect(quotaRepo.CreateArgsForCall(0).NonBasicServicesAllowed).To(BeFalse()) 156 }) 157 }) 158 159 Context("when the -m flag is provided with valid value", func() { 160 BeforeEach(func() { 161 flagContext.Parse("-m", "50G", "erryday makin fitty jeez") 162 }) 163 164 It("sets the memory limit", func() { 165 Expect(quotaRepo.CreateArgsForCall(0).MemoryLimit).To(Equal(int64(51200))) 166 }) 167 }) 168 169 Context("when the -i flag is provided with positive value", func() { 170 BeforeEach(func() { 171 flagContext.Parse("-i", "50G", "erryday makin fitty jeez") 172 }) 173 174 It("sets the memory limit", func() { 175 Expect(quotaRepo.CreateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(51200))) 176 }) 177 }) 178 179 Context("when the -i flag is provided with -1", func() { 180 BeforeEach(func() { 181 flagContext.Parse("-i", "-1", "wit mah hussle") 182 }) 183 184 It("accepts it as an appropriate value", func() { 185 Expect(quotaRepo.CreateArgsForCall(0).InstanceMemoryLimit).To(Equal(int64(-1))) 186 }) 187 }) 188 189 Context("when the -a flag is provided", func() { 190 BeforeEach(func() { 191 flagContext.Parse("-a", "50", "my special quota") 192 }) 193 194 It("sets the instance limit", func() { 195 Expect(quotaRepo.CreateArgsForCall(0).AppInstanceLimit).To(Equal(50)) 196 }) 197 }) 198 199 Context("when the -r flag is provided", func() { 200 BeforeEach(func() { 201 flagContext.Parse("-r", "12", "ecstatic") 202 }) 203 204 It("sets the route limit", func() { 205 Expect(quotaRepo.CreateArgsForCall(0).RoutesLimit).To(Equal(12)) 206 }) 207 }) 208 209 Context("when the -s flag is provided", func() { 210 BeforeEach(func() { 211 flagContext.Parse("-s", "42", "black star") 212 }) 213 214 It("sets the service instance limit", func() { 215 Expect(quotaRepo.CreateArgsForCall(0).ServicesLimit).To(Equal(42)) 216 }) 217 }) 218 219 Context("when the --reserved-route-ports flag is provided", func() { 220 BeforeEach(func() { 221 flagContext.Parse("--reserved-route-ports", "5", "square quota") 222 }) 223 224 It("sets the quotas TCP route limit", func() { 225 Expect(quotaRepo.CreateArgsForCall(0).ReservedRoutePortsLimit).To(Equal(json.Number("5"))) 226 }) 227 }) 228 229 Context("when requesting to allow paid service plans", func() { 230 BeforeEach(func() { 231 flagContext.Parse("--allow-paid-service-plans", "my-for-profit-quota") 232 }) 233 234 It("creates the quota with paid service plans allowed", func() { 235 Expect(quotaRepo.CreateArgsForCall(0).NonBasicServicesAllowed).To(BeTrue()) 236 }) 237 }) 238 }) 239 240 Context("when the -i flag is provided with invalid value", func() { 241 BeforeEach(func() { 242 flagContext.Parse("-i", "whoops", "yo", "12") 243 cmd.SetDependency(deps, false) 244 }) 245 246 It("alerts the user when parsing the memory limit fails", func() { 247 Expect(runCLIErr).To(HaveOccurred()) 248 runCLIErrStr := runCLIErr.Error() 249 Expect(runCLIErrStr).To(ContainSubstring("Invalid instance memory limit: whoops")) 250 Expect(runCLIErrStr).To(ContainSubstring("Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB")) 251 }) 252 }) 253 254 Context("when the -m flag is provided with invalid value", func() { 255 BeforeEach(func() { 256 flagContext.Parse("-m", "whoops", "wit mah hussle") 257 cmd.SetDependency(deps, false) 258 }) 259 260 It("alerts the user when parsing the memory limit fails", func() { 261 Expect(runCLIErr).To(HaveOccurred()) 262 runCLIErrStr := runCLIErr.Error() 263 Expect(runCLIErrStr).To(ContainSubstring("Invalid memory limit: whoops")) 264 Expect(runCLIErrStr).To(ContainSubstring("Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB")) 265 }) 266 }) 267 268 Context("when the request fails", func() { 269 BeforeEach(func() { 270 flagContext.Parse("my-quota") 271 quotaRepo.CreateReturns(errors.New("WHOOP THERE IT IS")) 272 cmd.SetDependency(deps, false) 273 }) 274 275 It("alets the user when creating the quota fails", func() { 276 Expect(runCLIErr).To(HaveOccurred()) 277 Expect(ui.Outputs()).To(ContainSubstrings( 278 []string{"Creating space quota", "my-quota", "my-org"}, 279 )) 280 Expect(runCLIErr.Error()).To(Equal("WHOOP THERE IT IS")) 281 }) 282 }) 283 284 Context("when the quota already exists", func() { 285 BeforeEach(func() { 286 flagContext.Parse("my-quota") 287 quotaRepo.CreateReturns(errors.NewHTTPError(400, errors.QuotaDefinitionNameTaken, "Quota Definition is taken: quota-sct")) 288 }) 289 290 It("warns the user", func() { 291 Expect(runCLIErr).NotTo(HaveOccurred()) 292 Expect(ui.Outputs()).ToNot(ContainSubstrings([]string{"FAILED"})) 293 Expect(ui.WarnOutputs).To(ContainSubstrings([]string{"already exists"})) 294 }) 295 }) 296 }) 297 })