github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/spacequota/set_space_quota_test.go (about) 1 package spacequota_test 2 3 import ( 4 "code.cloudfoundry.org/cli/cf/api/spacequotas/spacequotasfakes" 5 "code.cloudfoundry.org/cli/cf/api/spaces/spacesfakes" 6 "code.cloudfoundry.org/cli/cf/commands/spacequota" 7 "code.cloudfoundry.org/cli/cf/configuration/coreconfig/coreconfigfakes" 8 "code.cloudfoundry.org/cli/cf/errors" 9 "code.cloudfoundry.org/cli/cf/flags" 10 "code.cloudfoundry.org/cli/cf/models" 11 "code.cloudfoundry.org/cli/cf/requirements" 12 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 13 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 14 15 "code.cloudfoundry.org/cli/cf/commandregistry" 16 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 ) 20 21 var _ = Describe("set-space-quota command", func() { 22 var ( 23 ui *testterm.FakeUI 24 spaceRepo *spacesfakes.FakeSpaceRepository 25 quotaRepo *spacequotasfakes.FakeSpaceQuotaRepository 26 requirementsFactory *requirementsfakes.FakeFactory 27 configRepo *coreconfigfakes.FakeRepository 28 deps commandregistry.Dependency 29 cmd spacequota.SetSpaceQuota 30 flagContext flags.FlagContext 31 loginReq requirements.Requirement 32 orgReq *requirementsfakes.FakeTargetedOrgRequirement 33 ) 34 35 BeforeEach(func() { 36 requirementsFactory = new(requirementsfakes.FakeFactory) 37 38 loginReq = requirements.Passing{Type: "login"} 39 requirementsFactory.NewLoginRequirementReturns(loginReq) 40 orgReq = new(requirementsfakes.FakeTargetedOrgRequirement) 41 requirementsFactory.NewTargetedOrgRequirementReturns(orgReq) 42 43 ui = new(testterm.FakeUI) 44 configRepo = new(coreconfigfakes.FakeRepository) 45 deps = commandregistry.Dependency{ 46 UI: ui, 47 Config: configRepo, 48 } 49 quotaRepo = new(spacequotasfakes.FakeSpaceQuotaRepository) 50 deps.RepoLocator = deps.RepoLocator.SetSpaceQuotaRepository(quotaRepo) 51 spaceRepo = new(spacesfakes.FakeSpaceRepository) 52 deps.RepoLocator = deps.RepoLocator.SetSpaceRepository(spaceRepo) 53 54 flagContext = flags.NewFlagContext(cmd.MetaData().Flags) 55 56 cmd = spacequota.SetSpaceQuota{} 57 cmd.SetDependency(deps, false) 58 59 configRepo.UsernameReturns("my-user") 60 }) 61 62 Describe("Requirements", func() { 63 Context("when provided a quota and space", func() { 64 var reqs []requirements.Requirement 65 66 BeforeEach(func() { 67 var err error 68 69 flagContext.Parse("space", "space-quota") 70 reqs, err = cmd.Requirements(requirementsFactory, flagContext) 71 Expect(err).NotTo(HaveOccurred()) 72 }) 73 74 It("returns a LoginRequirement", func() { 75 Expect(reqs).To(ContainElement(loginReq)) 76 }) 77 78 It("requires the user to target an org", func() { 79 Expect(reqs).To(ContainElement(orgReq)) 80 }) 81 }) 82 83 Context("when not provided a quota and space", func() { 84 BeforeEach(func() { 85 flagContext.Parse("") 86 }) 87 88 It("fails with usage", func() { 89 _, err := cmd.Requirements(requirementsFactory, flagContext) 90 Expect(err).To(HaveOccurred()) 91 Expect(ui.Outputs()).To(ContainSubstrings( 92 []string{"Incorrect Usage. Requires", "as arguments"}, 93 )) 94 }) 95 }) 96 }) 97 98 Describe("Execute", func() { 99 var executeErr error 100 101 JustBeforeEach(func() { 102 flagContext.Parse("my-space", "quota-name") 103 executeErr = cmd.Execute(flagContext) 104 }) 105 106 Context("when the space and quota both exist", func() { 107 BeforeEach(func() { 108 quotaRepo.FindByNameReturns( 109 models.SpaceQuota{ 110 Name: "quota-name", 111 GUID: "quota-guid", 112 MemoryLimit: 1024, 113 InstanceMemoryLimit: 512, 114 RoutesLimit: 111, 115 ServicesLimit: 222, 116 NonBasicServicesAllowed: true, 117 OrgGUID: "my-org-guid", 118 }, nil) 119 120 spaceRepo.FindByNameReturns( 121 models.Space{ 122 SpaceFields: models.SpaceFields{ 123 Name: "my-space", 124 GUID: "my-space-guid", 125 }, 126 SpaceQuotaGUID: "", 127 }, nil) 128 }) 129 130 Context("when the space quota was not previously assigned to a space", func() { 131 It("associates the provided space with the provided space quota", func() { 132 Expect(executeErr).NotTo(HaveOccurred()) 133 spaceGUID, quotaGUID := quotaRepo.AssociateSpaceWithQuotaArgsForCall(0) 134 135 Expect(spaceGUID).To(Equal("my-space-guid")) 136 Expect(quotaGUID).To(Equal("quota-guid")) 137 Expect(ui.Outputs()).To(ContainSubstrings( 138 []string{"Assigning space quota", "to space", "my-user"}, 139 []string{"OK"}, 140 )) 141 }) 142 }) 143 144 Context("when the space quota was previously assigned to a space", func() { 145 BeforeEach(func() { 146 spaceRepo.FindByNameReturns( 147 models.Space{ 148 SpaceFields: models.SpaceFields{ 149 Name: "my-space", 150 GUID: "my-space-guid", 151 }, 152 SpaceQuotaGUID: "another-quota", 153 }, nil) 154 }) 155 156 It("warns the user that the operation was not performed", func() { 157 Expect(quotaRepo.UpdateCallCount()).To(Equal(0)) 158 Expect(ui.Outputs()).To(ContainSubstrings( 159 []string{"Assigning space quota", "to space", "my-user"}, 160 )) 161 Expect(executeErr).To(HaveOccurred()) 162 Expect(executeErr.Error()).To(Equal("This space already has an assigned space quota.")) 163 }) 164 }) 165 }) 166 167 Context("when an error occurs fetching the space", func() { 168 var spaceError error 169 170 BeforeEach(func() { 171 spaceError = errors.New("space-repo-err") 172 spaceRepo.FindByNameReturns(models.Space{}, spaceError) 173 }) 174 175 It("prints an error", func() { 176 Expect(ui.Outputs()).To(ContainSubstrings( 177 []string{"Assigning space quota", "to space", "my-user"}, 178 )) 179 Expect(executeErr).To(Equal(spaceError)) 180 }) 181 }) 182 183 Context("when an error occurs fetching the quota", func() { 184 var quotaErr error 185 186 BeforeEach(func() { 187 spaceRepo.FindByNameReturns( 188 models.Space{ 189 SpaceFields: models.SpaceFields{ 190 Name: "my-space", 191 GUID: "my-space-guid", 192 }, 193 SpaceQuotaGUID: "", 194 }, nil) 195 196 quotaErr = errors.New("I can't find my quota name!") 197 quotaRepo.FindByNameReturns(models.SpaceQuota{}, quotaErr) 198 }) 199 200 It("prints an error", func() { 201 Expect(ui.Outputs()).To(ContainSubstrings( 202 []string{"Assigning space quota", "to space", "my-user"}, 203 )) 204 Expect(executeErr).To(Equal(quotaErr)) 205 }) 206 }) 207 }) 208 })