github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/api/quotas/quotas_test.go (about) 1 package 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/models" 11 "github.com/cloudfoundry/cli/cf/net" 12 testconfig "github.com/cloudfoundry/cli/testhelpers/configuration" 13 testnet "github.com/cloudfoundry/cli/testhelpers/net" 14 testterm "github.com/cloudfoundry/cli/testhelpers/terminal" 15 16 . "github.com/cloudfoundry/cli/cf/api/quotas" 17 . "github.com/cloudfoundry/cli/testhelpers/matchers" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 ) 21 22 var _ = Describe("CloudControllerQuotaRepository", func() { 23 var ( 24 testServer *httptest.Server 25 testHandler *testnet.TestHandler 26 configRepo core_config.ReadWriter 27 repo CloudControllerQuotaRepository 28 ) 29 30 BeforeEach(func() { 31 configRepo = testconfig.NewRepositoryWithDefaults() 32 gateway := net.NewCloudControllerGateway(configRepo, time.Now, &testterm.FakeUI{}) 33 repo = NewCloudControllerQuotaRepository(configRepo, gateway) 34 }) 35 36 AfterEach(func() { 37 testServer.Close() 38 }) 39 40 setupTestServer := func(reqs ...testnet.TestRequest) { 41 testServer, testHandler = testnet.NewServer(reqs) 42 configRepo.SetApiEndpoint(testServer.URL) 43 } 44 45 Describe("FindByName", func() { 46 BeforeEach(func() { 47 setupTestServer(firstQuotaRequest, secondQuotaRequest) 48 }) 49 50 It("Finds Quota definitions by name", func() { 51 quota, err := repo.FindByName("my-remote-quota") 52 53 Expect(err).NotTo(HaveOccurred()) 54 Expect(testHandler).To(HaveAllRequestsCalled()) 55 Expect(quota).To(Equal(models.QuotaFields{ 56 Guid: "my-quota-guid", 57 Name: "my-remote-quota", 58 MemoryLimit: 1024, 59 InstanceMemoryLimit: -1, 60 RoutesLimit: 123, 61 ServicesLimit: 321, 62 NonBasicServicesAllowed: true, 63 })) 64 }) 65 }) 66 67 Describe("FindAll", func() { 68 BeforeEach(func() { 69 setupTestServer(firstQuotaRequest, secondQuotaRequest) 70 }) 71 72 It("finds all Quota definitions", func() { 73 quotas, err := repo.FindAll() 74 75 Expect(err).NotTo(HaveOccurred()) 76 Expect(testHandler).To(HaveAllRequestsCalled()) 77 Expect(len(quotas)).To(Equal(3)) 78 Expect(quotas[0].Guid).To(Equal("my-quota-guid")) 79 Expect(quotas[0].Name).To(Equal("my-remote-quota")) 80 Expect(quotas[0].MemoryLimit).To(Equal(int64(1024))) 81 Expect(quotas[0].RoutesLimit).To(Equal(123)) 82 Expect(quotas[0].ServicesLimit).To(Equal(321)) 83 84 Expect(quotas[1].Guid).To(Equal("my-quota-guid2")) 85 Expect(quotas[2].Guid).To(Equal("my-quota-guid3")) 86 }) 87 }) 88 89 Describe("AssignQuotaToOrg", func() { 90 It("sets the quota for an organization", func() { 91 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 92 Method: "PUT", 93 Path: "/v2/organizations/my-org-guid", 94 Matcher: testnet.RequestBodyMatcher(`{"quota_definition_guid":"my-quota-guid"}`), 95 Response: testnet.TestResponse{Status: http.StatusCreated}, 96 }) 97 98 setupTestServer(req) 99 100 err := repo.AssignQuotaToOrg("my-org-guid", "my-quota-guid") 101 Expect(testHandler).To(HaveAllRequestsCalled()) 102 Expect(err).NotTo(HaveOccurred()) 103 }) 104 }) 105 106 Describe("Create", func() { 107 It("creates a new quota with the given name", func() { 108 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 109 Method: "POST", 110 Path: "/v2/quota_definitions", 111 Matcher: testnet.RequestBodyMatcher(`{ 112 "name": "not-so-strict", 113 "non_basic_services_allowed": false, 114 "total_services": 1, 115 "total_routes": 12, 116 "memory_limit": 123, 117 "instance_memory_limit": 0 118 }`), 119 Response: testnet.TestResponse{Status: http.StatusCreated}, 120 }) 121 setupTestServer(req) 122 123 quota := models.QuotaFields{ 124 Name: "not-so-strict", 125 ServicesLimit: 1, 126 RoutesLimit: 12, 127 MemoryLimit: 123, 128 } 129 err := repo.Create(quota) 130 Expect(err).NotTo(HaveOccurred()) 131 Expect(testHandler).To(HaveAllRequestsCalled()) 132 }) 133 }) 134 135 Describe("Update", func() { 136 It("updates an existing quota", func() { 137 setupTestServer(testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 138 Method: "PUT", 139 Path: "/v2/quota_definitions/my-quota-guid", 140 Matcher: testnet.RequestBodyMatcher(`{ 141 "guid": "my-quota-guid", 142 "non_basic_services_allowed": false, 143 "name": "amazing-quota", 144 "total_services": 1, 145 "total_routes": 12, 146 "memory_limit": 123, 147 "instance_memory_limit": 0 148 }`), 149 })) 150 151 quota := models.QuotaFields{ 152 Guid: "my-quota-guid", 153 Name: "amazing-quota", 154 ServicesLimit: 1, 155 RoutesLimit: 12, 156 MemoryLimit: 123, 157 } 158 159 err := repo.Update(quota) 160 Expect(err).NotTo(HaveOccurred()) 161 Expect(testHandler).To(HaveAllRequestsCalled()) 162 }) 163 }) 164 165 Describe("Delete", func() { 166 It("deletes the quota with the given name", func() { 167 req := testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 168 Method: "DELETE", 169 Path: "/v2/quota_definitions/my-quota-guid", 170 Response: testnet.TestResponse{Status: http.StatusNoContent}, 171 }) 172 setupTestServer(req) 173 174 err := repo.Delete("my-quota-guid") 175 Expect(err).NotTo(HaveOccurred()) 176 Expect(testHandler).To(HaveAllRequestsCalled()) 177 }) 178 }) 179 }) 180 181 var firstQuotaRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 182 Method: "GET", 183 Path: "/v2/quota_definitions", 184 Response: testnet.TestResponse{ 185 Status: http.StatusOK, 186 Body: `{ 187 "next_url": "/v2/quota_definitions?page=2", 188 "resources": [ 189 { 190 "metadata": { "guid": "my-quota-guid" }, 191 "entity": { 192 "name": "my-remote-quota", 193 "memory_limit": 1024, 194 "instance_memory_limit": -1, 195 "total_routes": 123, 196 "total_services": 321, 197 "non_basic_services_allowed": true 198 } 199 } 200 ]}`, 201 }, 202 }) 203 204 var secondQuotaRequest = testapi.NewCloudControllerTestRequest(testnet.TestRequest{ 205 Method: "GET", 206 Path: "/v2/quota_definitions?page=2", 207 Response: testnet.TestResponse{ 208 Status: http.StatusOK, 209 Body: `{ 210 "resources": [ 211 { 212 "metadata": { "guid": "my-quota-guid2" }, 213 "entity": { "name": "my-remote-quota2", "memory_limit": 1024 } 214 }, 215 { 216 "metadata": { "guid": "my-quota-guid3" }, 217 "entity": { "name": "my-remote-quota3", "memory_limit": 1024 } 218 } 219 ]}`, 220 }, 221 })