github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/api/quotas/quotas_test.go (about) 1 package quotas_test 2 3 import ( 4 "net/http" 5 "time" 6 7 "code.cloudfoundry.org/cli/cf/api/quotas" 8 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 9 "code.cloudfoundry.org/cli/cf/models" 10 "code.cloudfoundry.org/cli/cf/net" 11 12 "code.cloudfoundry.org/cli/cf/terminal/terminalfakes" 13 testconfig "code.cloudfoundry.org/cli/cf/util/testhelpers/configuration" 14 15 "github.com/onsi/gomega/ghttp" 16 17 "encoding/json" 18 19 "code.cloudfoundry.org/cli/cf/trace/tracefakes" 20 . "github.com/onsi/ginkgo" 21 . "github.com/onsi/gomega" 22 ) 23 24 var _ = Describe("CloudControllerQuotaRepository", func() { 25 var ( 26 ccServer *ghttp.Server 27 configRepo coreconfig.ReadWriter 28 repo quotas.CloudControllerQuotaRepository 29 ) 30 31 BeforeEach(func() { 32 ccServer = ghttp.NewServer() 33 configRepo = testconfig.NewRepositoryWithDefaults() 34 configRepo.SetAPIEndpoint(ccServer.URL()) 35 gateway := net.NewCloudControllerGateway(configRepo, time.Now, new(terminalfakes.FakeUI), new(tracefakes.FakePrinter), "") 36 repo = quotas.NewCloudControllerQuotaRepository(configRepo, gateway) 37 }) 38 39 AfterEach(func() { 40 ccServer.Close() 41 }) 42 43 Describe("FindByName", func() { 44 BeforeEach(func() { 45 ccServer.AppendHandlers( 46 ghttp.CombineHandlers( 47 ghttp.VerifyRequest("GET", "/v2/quota_definitions"), 48 ghttp.RespondWith(http.StatusOK, `{ 49 "next_url": "/v2/quota_definitions?page=2", 50 "resources": [ 51 { 52 "metadata": { "guid": "my-quota-guid" }, 53 "entity": { 54 "name": "my-remote-quota", 55 "memory_limit": 1024, 56 "instance_memory_limit": -1, 57 "total_routes": 123, 58 "total_services": 321, 59 "non_basic_services_allowed": true, 60 "app_instance_limit": 7, 61 "total_reserved_route_ports": 5 62 } 63 } 64 ] 65 }`), 66 ), 67 ghttp.CombineHandlers( 68 ghttp.VerifyRequest("GET", "/v2/quota_definitions", "page=2"), 69 ghttp.RespondWith(http.StatusOK, `{ 70 "resources": [ 71 { 72 "metadata": { "guid": "my-quota-guid2" }, 73 "entity": { "name": "my-remote-quota2", "memory_limit": 1024 } 74 }, 75 { 76 "metadata": { "guid": "my-quota-guid3" }, 77 "entity": { "name": "my-remote-quota3", "memory_limit": 1024 } 78 } 79 ] 80 }`), 81 ), 82 ) 83 }) 84 85 It("Finds Quota definitions by name", func() { 86 quota, err := repo.FindByName("my-remote-quota") 87 Expect(err).NotTo(HaveOccurred()) 88 Expect(ccServer.ReceivedRequests()).To(HaveLen(2)) 89 Expect(quota).To(Equal(models.QuotaFields{ 90 GUID: "my-quota-guid", 91 Name: "my-remote-quota", 92 MemoryLimit: 1024, 93 InstanceMemoryLimit: -1, 94 RoutesLimit: 123, 95 ServicesLimit: 321, 96 NonBasicServicesAllowed: true, 97 AppInstanceLimit: 7, 98 ReservedRoutePorts: "5", 99 })) 100 }) 101 }) 102 103 Describe("FindAll", func() { 104 var ( 105 quotas []models.QuotaFields 106 err error 107 ) 108 109 BeforeEach(func() { 110 ccServer.AppendHandlers( 111 ghttp.CombineHandlers( 112 ghttp.VerifyRequest("GET", "/v2/quota_definitions"), 113 ghttp.RespondWith(http.StatusOK, `{ 114 "next_url": "/v2/quota_definitions?page=2", 115 "resources": [ 116 { 117 "metadata": { "guid": "my-quota-guid" }, 118 "entity": { 119 "name": "my-remote-quota", 120 "memory_limit": 1024, 121 "instance_memory_limit": -1, 122 "total_routes": 123, 123 "total_services": 321, 124 "non_basic_services_allowed": true, 125 "app_instance_limit": 7, 126 "total_reserved_route_ports": 3 127 } 128 } 129 ] 130 }`), 131 ), 132 ghttp.CombineHandlers( 133 ghttp.VerifyRequest("GET", "/v2/quota_definitions", "page=2"), 134 ghttp.RespondWith(http.StatusOK, `{ 135 "resources": [ 136 { 137 "metadata": { "guid": "my-quota-guid2" }, 138 "entity": { "name": "my-remote-quota2", "memory_limit": 1024 } 139 }, 140 { 141 "metadata": { "guid": "my-quota-guid3" }, 142 "entity": { "name": "my-remote-quota3", "memory_limit": 1024 } 143 } 144 ] 145 }`), 146 ), 147 ) 148 }) 149 150 JustBeforeEach(func() { 151 quotas, err = repo.FindAll() 152 Expect(err).NotTo(HaveOccurred()) 153 }) 154 155 It("finds all Quota definitions", func() { 156 Expect(ccServer.ReceivedRequests()).To(HaveLen(2)) 157 Expect(quotas).To(HaveLen(3)) 158 Expect(quotas[0].GUID).To(Equal("my-quota-guid")) 159 Expect(quotas[0].Name).To(Equal("my-remote-quota")) 160 Expect(quotas[0].MemoryLimit).To(Equal(int64(1024))) 161 Expect(quotas[0].RoutesLimit).To(Equal(123)) 162 Expect(quotas[0].ServicesLimit).To(Equal(321)) 163 Expect(quotas[0].AppInstanceLimit).To(Equal(7)) 164 Expect(quotas[0].ReservedRoutePorts).To(Equal(json.Number("3"))) 165 166 Expect(quotas[1].GUID).To(Equal("my-quota-guid2")) 167 Expect(quotas[2].GUID).To(Equal("my-quota-guid3")) 168 }) 169 170 It("defaults missing app instance limit to -1 (unlimited)", func() { 171 Expect(quotas[1].AppInstanceLimit).To(Equal(-1)) 172 }) 173 174 It("defaults missing reserved route ports to be empty", func() { 175 Expect(quotas[1].ReservedRoutePorts).To(BeEmpty()) 176 }) 177 }) 178 179 Describe("AssignQuotaToOrg", func() { 180 BeforeEach(func() { 181 ccServer.AppendHandlers( 182 ghttp.CombineHandlers( 183 ghttp.VerifyRequest("PUT", "/v2/organizations/my-org-guid"), 184 ghttp.VerifyJSON(`{"quota_definition_guid":"my-quota-guid"}`), 185 ghttp.RespondWith(http.StatusCreated, nil), 186 ), 187 ) 188 }) 189 190 It("sets the quota for an organization", func() { 191 err := repo.AssignQuotaToOrg("my-org-guid", "my-quota-guid") 192 Expect(ccServer.ReceivedRequests()).To(HaveLen(1)) 193 Expect(err).NotTo(HaveOccurred()) 194 }) 195 }) 196 197 Describe("Create", func() { 198 BeforeEach(func() { 199 ccServer.AppendHandlers( 200 ghttp.CombineHandlers( 201 ghttp.VerifyRequest("POST", "/v2/quota_definitions"), 202 ghttp.VerifyJSON(`{ 203 "name": "not-so-strict", 204 "non_basic_services_allowed": false, 205 "total_services": 1, 206 "total_routes": 12, 207 "memory_limit": 123, 208 "app_instance_limit": 42, 209 "instance_memory_limit": 0, 210 "total_reserved_route_ports": 10 211 }`), 212 ghttp.RespondWith(http.StatusCreated, nil), 213 ), 214 ) 215 }) 216 217 It("creates a new quota with the given name", func() { 218 quota := models.QuotaFields{ 219 Name: "not-so-strict", 220 ServicesLimit: 1, 221 RoutesLimit: 12, 222 MemoryLimit: 123, 223 AppInstanceLimit: 42, 224 ReservedRoutePorts: "10", 225 } 226 227 err := repo.Create(quota) 228 Expect(err).NotTo(HaveOccurred()) 229 Expect(ccServer.ReceivedRequests()).To(HaveLen(1)) 230 }) 231 }) 232 233 Describe("Update", func() { 234 BeforeEach(func() { 235 ccServer.AppendHandlers( 236 ghttp.CombineHandlers( 237 238 ghttp.VerifyRequest("PUT", "/v2/quota_definitions/my-quota-guid"), 239 ghttp.VerifyJSON(`{ 240 "guid": "my-quota-guid", 241 "non_basic_services_allowed": false, 242 "name": "amazing-quota", 243 "total_services": 1, 244 "total_routes": 12, 245 "memory_limit": 123, 246 "app_instance_limit": 42, 247 "instance_memory_limit": 0, 248 "total_reserved_route_ports": 10 249 }`), 250 ghttp.RespondWith(http.StatusOK, nil), 251 ), 252 ) 253 }) 254 255 It("updates an existing quota", func() { 256 quota := models.QuotaFields{ 257 GUID: "my-quota-guid", 258 Name: "amazing-quota", 259 ServicesLimit: 1, 260 RoutesLimit: 12, 261 MemoryLimit: 123, 262 AppInstanceLimit: 42, 263 ReservedRoutePorts: "10", 264 } 265 266 err := repo.Update(quota) 267 Expect(err).NotTo(HaveOccurred()) 268 Expect(ccServer.ReceivedRequests()).To(HaveLen(1)) 269 }) 270 }) 271 272 Describe("Delete", func() { 273 BeforeEach(func() { 274 ccServer.AppendHandlers( 275 ghttp.CombineHandlers( 276 ghttp.VerifyRequest("DELETE", "/v2/quota_definitions/my-quota-guid"), 277 ghttp.RespondWith(http.StatusNoContent, nil), 278 ), 279 ) 280 }) 281 282 It("deletes the quota with the given name", func() { 283 err := repo.Delete("my-quota-guid") 284 Expect(err).NotTo(HaveOccurred()) 285 Expect(ccServer.ReceivedRequests()).To(HaveLen(1)) 286 }) 287 }) 288 })