github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/api/space_quotas/space_quotas_test.go (about) 1 package space_quotas_test 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "time" 7 8 testapi "github.com/cloudfoundry/cli/cf/api/fakes" 9 "github.com/cloudfoundry/cli/cf/configuration/core_config" 10 "github.com/cloudfoundry/cli/cf/errors" 11 "github.com/cloudfoundry/cli/cf/models" 12 "github.com/cloudfoundry/cli/cf/net" 13 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 14 testnet "github.com/cloudfoundry/cli/testhelpers/net" 15 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 16 17 . "github.com/cloudfoundry/cli/cf/api/space_quotas" 18 . "github.com/cloudfoundry/cli/testhelpers/matchers" 19 . "github.com/onsi/ginkgo" 20 . "github.com/onsi/gomega" 21 ) 22 23 var _ = Describe("CloudControllerQuotaRepository", func() { 24 var ( 25 testServer *httptest.Server 26 testHandler *testnet.TestHandler 27 configRepo core_config.ReadWriter 28 repo CloudControllerSpaceQuotaRepository 29 ) 30 31 setupTestServer := func(reqs ...testnet.TestRequest) { 32 testServer, testHandler = testnet.NewServer(reqs) 33 configRepo.SetApiEndpoint(testServer.URL) 34 } 35 36 BeforeEach(func() { 37 configRepo = testconfig.NewRepositoryWithDefaults() 38 gateway := net.NewCloudControllerGateway(configRepo, time.Now, &testterm.FakeUI{}) 39 repo = NewCloudControllerSpaceQuotaRepository(configRepo, gateway) 40 }) 41 42 AfterEach(func() { 43 testServer.Close() 44 }) 45 46 Describe("FindByName", func() { 47 BeforeEach(func() { 48 setupTestServer(firstSpaceQuotaRequest, secondSpaceQuotaRequest) 49 }) 50 51 It("Finds Quota definitions by name", func() { 52 quota, err := repo.FindByName("my-remote-quota") 53 54 Expect(err).NotTo(HaveOccurred()) 55 Expect(testHandler).To(HaveAllRequestsCalled()) 56 Expect(quota).To(Equal(models.SpaceQuota{ 57 Guid: "my-quota-guid", 58 Name: "my-remote-quota", 59 MemoryLimit: 1024, 60 RoutesLimit: 123, 61 ServicesLimit: 321, 62 NonBasicServicesAllowed: true, 63 OrgGuid: "my-org-guid", 64 })) 65 }) 66 67 It("Returns an error if the quota cannot be found", func() { 68 _, err := repo.FindByName("totally-not-a-quota") 69 70 Expect(testHandler).To(HaveAllRequestsCalled()) 71 Expect(err.(*errors.ModelNotFoundError)).NotTo(BeNil()) 72 }) 73 }) 74 75 Describe("FindByOrg", func() { 76 BeforeEach(func() { 77 setupTestServer(firstSpaceQuotaRequest, secondSpaceQuotaRequest) 78 }) 79 80 It("finds all quota definitions by org guid", func() { 81 quotas, err := repo.FindByOrg("my-org-guid") 82 83 Expect(err).NotTo(HaveOccurred()) 84 Expect(testHandler).To(HaveAllRequestsCalled()) 85 Expect(len(quotas)).To(Equal(3)) 86 Expect(quotas[0].Guid).To(Equal("my-quota-guid")) 87 Expect(quotas[0].Name).To(Equal("my-remote-quota")) 88 Expect(quotas[0].MemoryLimit).To(Equal(int64(1024))) 89 Expect(quotas[0].RoutesLimit).To(Equal(123)) 90 Expect(quotas[0].ServicesLimit).To(Equal(321)) 91 Expect(quotas[0].OrgGuid).To(Equal("my-org-guid")) 92 93 Expect(quotas[1].Guid).To(Equal("my-quota-guid2")) 94 Expect(quotas[1].OrgGuid).To(Equal("my-org-guid")) 95 Expect(quotas[2].Guid).To(Equal("my-quota-guid3")) 96 Expect(quotas[2].OrgGuid).To(Equal("my-org-guid")) 97 }) 98 }) 99 100 Describe("FindByGuid", func() { 101 BeforeEach(func() { 102 setupTestServer(firstSpaceQuotaRequest, secondSpaceQuotaRequest) 103 }) 104 105 It("Finds Quota definitions by Guid", func() { 106 quota, err := repo.FindByGuid("my-quota-guid") 107 108 Expect(err).NotTo(HaveOccurred()) 109 Expect(testHandler).To(HaveAllRequestsCalled()) 110 Expect(quota).To(Equal(models.SpaceQuota{ 111 Guid: "my-quota-guid", 112 Name: "my-remote-quota", 113 MemoryLimit: 1024, 114 RoutesLimit: 123, 115 ServicesLimit: 321, 116 NonBasicServicesAllowed: true, 117 OrgGuid: "my-org-guid", 118 })) 119 }) 120 It("Returns an error if the quota cannot be found", func() { 121 _, err := repo.FindByGuid("totally-not-a-quota-guid") 122 123 Expect(testHandler).To(HaveAllRequestsCalled()) 124 Expect(err.(*errors.ModelNotFoundError)).NotTo(BeNil()) 125 }) 126 }) 127 128 Describe("AssociateSpaceWithQuota", func() { 129 It("sets the quota for a space", func() { 130 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 131 Method: "PUT", 132 Path: "/v2/space_quota_definitions/my-quota-guid/spaces/my-space-guid", 133 Response: testnet.TestResponse{Status: http.StatusCreated}, 134 }) 135 136 setupTestServer(req) 137 138 err := repo.AssociateSpaceWithQuota("my-space-guid", "my-quota-guid") 139 Expect(testHandler).To(HaveAllRequestsCalled()) 140 Expect(err).NotTo(HaveOccurred()) 141 }) 142 }) 143 144 Describe("UnassignQuotaFromSpace", func() { 145 It("deletes the association between the quota and the space", func() { 146 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 147 Method: "DELETE", 148 Path: "/v2/space_quota_definitions/my-quota-guid/spaces/my-space-guid", 149 Response: testnet.TestResponse{Status: http.StatusNoContent}, 150 }) 151 setupTestServer(req) 152 153 err := repo.UnassignQuotaFromSpace("my-space-guid", "my-quota-guid") 154 Expect(err).NotTo(HaveOccurred()) 155 Expect(testHandler).To(HaveAllRequestsCalled()) 156 }) 157 }) 158 159 Describe("Create", func() { 160 It("creates a new quota with the given name", func() { 161 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 162 Method: "POST", 163 Path: "/v2/space_quota_definitions", 164 Matcher: testnet.RequestBodyMatcher(`{ 165 "name": "not-so-strict", 166 "non_basic_services_allowed": false, 167 "total_services": 1, 168 "total_routes": 12, 169 "memory_limit": 123, 170 "instance_memory_limit": 0, 171 "organization_guid": "my-org-guid" 172 }`), 173 Response: testnet.TestResponse{Status: http.StatusCreated}, 174 }) 175 setupTestServer(req) 176 177 quota := models.SpaceQuota{ 178 Name: "not-so-strict", 179 ServicesLimit: 1, 180 RoutesLimit: 12, 181 MemoryLimit: 123, 182 OrgGuid: "my-org-guid", 183 } 184 err := repo.Create(quota) 185 Expect(err).NotTo(HaveOccurred()) 186 Expect(testHandler).To(HaveAllRequestsCalled()) 187 }) 188 }) 189 190 Describe("Update", func() { 191 It("updates an existing quota", func() { 192 setupTestServer(testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 193 Method: "PUT", 194 Path: "/v2/space_quota_definitions/my-quota-guid", 195 Matcher: testnet.RequestBodyMatcher(`{ 196 "guid": "my-quota-guid", 197 "non_basic_services_allowed": false, 198 "name": "amazing-quota", 199 "total_services": 1, 200 "total_routes": 12, 201 "memory_limit": 123, 202 "instance_memory_limit": 1234, 203 "organization_guid": "myorgguid" 204 }`), 205 })) 206 207 quota := models.SpaceQuota{ 208 Guid: "my-quota-guid", 209 Name: "amazing-quota", 210 NonBasicServicesAllowed: false, 211 ServicesLimit: 1, 212 RoutesLimit: 12, 213 MemoryLimit: 123, 214 InstanceMemoryLimit: 1234, 215 OrgGuid: "myorgguid", 216 } 217 218 err := repo.Update(quota) 219 Expect(err).NotTo(HaveOccurred()) 220 Expect(testHandler).To(HaveAllRequestsCalled()) 221 }) 222 }) 223 224 Describe("Delete", func() { 225 It("deletes the quota with the given name", func() { 226 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 227 Method: "DELETE", 228 Path: "/v2/space_quota_definitions/my-quota-guid", 229 Response: testnet.TestResponse{Status: http.StatusNoContent}, 230 }) 231 setupTestServer(req) 232 233 err := repo.Delete("my-quota-guid") 234 Expect(err).NotTo(HaveOccurred()) 235 Expect(testHandler).To(HaveAllRequestsCalled()) 236 }) 237 }) 238 }) 239 240 var firstQuotaRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 241 Method: "GET", 242 Path: "/v2/space_quota_definitions", 243 Response: testnet.TestResponse{ 244 Status: http.StatusOK, 245 Body: `{ 246 "next_url": "/v2/quota_definitions?page=2", 247 "resources": [ 248 { 249 "metadata": { "guid": "my-quota-guid" }, 250 "entity": { 251 "name": "my-remote-quota", 252 "memory_limit": 1024, 253 "total_routes": 123, 254 "total_services": 321, 255 "non_basic_services_allowed": true, 256 } 257 } 258 ]}`, 259 }, 260 }) 261 262 var secondQuotaRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 263 Method: "GET", 264 Path: "/v2/space_quota_definitions?page=2", 265 Response: testnet.TestResponse{ 266 Status: http.StatusOK, 267 Body: `{ 268 "resources": [ 269 { 270 "metadata": { "guid": "my-quota-guid2" }, 271 "entity": { "name": "my-remote-quota2", "memory_limit": 1024 } 272 }, 273 { 274 "metadata": { "guid": "my-quota-guid3" }, 275 "entity": { "name": "my-remote-quota3", "memory_limit": 1024 } 276 } 277 ]}`, 278 }, 279 }) 280 281 var firstSpaceQuotaRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 282 Method: "GET", 283 Path: "/v2/organizations/my-org-guid/space_quota_definitions", 284 Response: testnet.TestResponse{ 285 Status: http.StatusOK, 286 Body: `{ 287 "next_url": "/v2/organizations/my-org-guid/space_quota_definitions?page=2", 288 "resources": [ 289 { 290 "metadata": { "guid": "my-quota-guid" }, 291 "entity": { 292 "name": "my-remote-quota", 293 "memory_limit": 1024, 294 "total_routes": 123, 295 "total_services": 321, 296 "non_basic_services_allowed": true, 297 "organization_guid": "my-org-guid" 298 } 299 } 300 ]}`, 301 }, 302 }) 303 304 var secondSpaceQuotaRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 305 Method: "GET", 306 Path: "/v2/organizations/my-org-guid/space_quota_definitions?page=2", 307 Response: testnet.TestResponse{ 308 Status: http.StatusOK, 309 Body: `{ 310 "resources": [ 311 { 312 "metadata": { "guid": "my-quota-guid2" }, 313 "entity": { "name": "my-remote-quota2", "memory_limit": 1024, "organization_guid": "my-org-guid" } 314 }, 315 { 316 "metadata": { "guid": "my-quota-guid3" }, 317 "entity": { "name": "my-remote-quota3", "memory_limit": 1024, "organization_guid": "my-org-guid" } 318 } 319 ]}`, 320 }, 321 })