github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/api/cloudcontroller/ccv3/organization_quota_test.go (about) 1 package ccv3_test 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "code.cloudfoundry.org/cli/types" 8 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 10 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 . "github.com/onsi/gomega/ghttp" 14 ) 15 16 var _ = Describe("Organization Quotas", func() { 17 var client *Client 18 var executeErr error 19 var warnings Warnings 20 var orgQuotas []OrgQuota 21 var query Query 22 23 BeforeEach(func() { 24 client, _ = NewTestClient() 25 }) 26 27 Describe("GetOrganizationQuotas", func() { 28 JustBeforeEach(func() { 29 orgQuotas, warnings, executeErr = client.GetOrganizationQuotas(query) 30 }) 31 32 When("the cloud controller returns without errors", func() { 33 BeforeEach(func() { 34 response1 := fmt.Sprintf(`{ 35 "pagination": { 36 "total_results": 1, 37 "total_pages": 1, 38 "first": { 39 "href": "%s/v3/organization_quotas?page=1&per_page=1" 40 }, 41 "last": { 42 "href": "%s/v3/organization_quotas?page=2&per_page=1" 43 }, 44 "next": { 45 "href": "%s/v3/organization_quotas?page=2&per_page=1" 46 }, 47 "previous": null 48 }, 49 "resources": [ 50 { 51 "guid": "quota-guid", 52 "created_at": "2016-05-04T17:00:41Z", 53 "updated_at": "2016-05-04T17:00:41Z", 54 "name": "don-quixote", 55 "apps": { 56 "total_memory_in_mb": 5120, 57 "per_process_memory_in_mb": 1024, 58 "total_instances": 10, 59 "per_app_tasks": 5 60 }, 61 "services": { 62 "paid_services_allowed": true, 63 "total_service_instances": 10, 64 "total_service_keys": 20 65 }, 66 "routes": { 67 "total_routes": 8, 68 "total_reserved_ports": 4 69 }, 70 "domains": { 71 "total_private_domains": 7 72 }, 73 "relationships": { 74 "organizations": { 75 "data": [ 76 { "guid": "org-guid1" }, 77 { "guid": "org-guid2" } 78 ] 79 } 80 }, 81 "links": { 82 "self": { "href": "%s/v3/organization_quotas/quota-guid" } 83 } 84 } 85 ] 86 }`, server.URL(), server.URL(), server.URL(), server.URL()) 87 88 response2 := fmt.Sprintf(`{ 89 "pagination": { 90 "next": null 91 }, 92 "resources": [ 93 { 94 "guid": "quota-2-guid", 95 "created_at": "2017-05-04T17:00:41Z", 96 "updated_at": "2017-05-04T17:00:41Z", 97 "name": "sancho-panza", 98 "apps": { 99 "total_memory_in_mb": 10240, 100 "per_process_memory_in_mb": 1024, 101 "total_instances": 8, 102 "per_app_tasks": 5 103 }, 104 "services": { 105 "paid_services_allowed": false, 106 "total_service_instances": 8, 107 "total_service_keys": 20 108 }, 109 "routes": { 110 "total_routes": 10, 111 "total_reserved_ports": 5 112 }, 113 "domains": { 114 "total_private_domains": 7 115 }, 116 "relationships": { 117 "organizations": { 118 "data": [] 119 } 120 }, 121 "links": { 122 "self": { "href": "%s/v3/organization_quotas/quota-2-guid" } 123 } 124 } 125 ] 126 }`, server.URL()) 127 128 server.AppendHandlers( 129 CombineHandlers( 130 VerifyRequest(http.MethodGet, "/v3/organization_quotas"), 131 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"page1 warning"}}), 132 ), 133 ) 134 135 server.AppendHandlers( 136 CombineHandlers( 137 VerifyRequest(http.MethodGet, "/v3/organization_quotas", "page=2&per_page=1"), 138 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"page2 warning"}}), 139 ), 140 ) 141 }) 142 143 It("returns org quotas and warnings", func() { 144 Expect(executeErr).NotTo(HaveOccurred()) 145 Expect(warnings).To(ConsistOf("page1 warning", "page2 warning")) 146 Expect(orgQuotas).To(ConsistOf( 147 OrgQuota{ 148 GUID: "quota-guid", 149 Name: "don-quixote", 150 Apps: AppLimit{ 151 TotalMemory: types.NullInt{Value: 5120, IsSet: true}, 152 InstanceMemory: types.NullInt{Value: 1024, IsSet: true}, 153 TotalAppInstances: types.NullInt{Value: 10, IsSet: true}, 154 }, 155 Services: ServiceLimit{ 156 TotalServiceInstances: types.NullInt{Value: 10, IsSet: true}, 157 PaidServicePlans: true, 158 }, 159 Routes: RouteLimit{ 160 TotalRoutes: types.NullInt{Value: 8, IsSet: true}, 161 TotalRoutePorts: types.NullInt{Value: 4, IsSet: true}, 162 }, 163 }, 164 OrgQuota{ 165 GUID: "quota-2-guid", 166 Name: "sancho-panza", 167 Apps: AppLimit{ 168 TotalMemory: types.NullInt{Value: 10240, IsSet: true}, 169 InstanceMemory: types.NullInt{Value: 1024, IsSet: true}, 170 TotalAppInstances: types.NullInt{Value: 8, IsSet: true}, 171 }, 172 Services: ServiceLimit{ 173 TotalServiceInstances: types.NullInt{Value: 8, IsSet: true}, 174 PaidServicePlans: false, 175 }, 176 Routes: RouteLimit{ 177 TotalRoutes: types.NullInt{Value: 10, IsSet: true}, 178 TotalRoutePorts: types.NullInt{Value: 5, IsSet: true}, 179 }, 180 }, 181 )) 182 }) 183 }) 184 185 When("requesting quotas by name", func() { 186 BeforeEach(func() { 187 query = Query{ 188 Key: NameFilter, 189 Values: []string{"sancho-panza"}, 190 } 191 192 response := fmt.Sprintf(`{ 193 "pagination": { 194 "next": null 195 }, 196 "resources": [ 197 { 198 "guid": "quota-2-guid", 199 "created_at": "2017-05-04T17:00:41Z", 200 "updated_at": "2017-05-04T17:00:41Z", 201 "name": "sancho-panza", 202 "apps": { 203 "total_memory_in_mb": 10240, 204 "per_process_memory_in_mb": 1024, 205 "total_instances": 8, 206 "per_app_tasks": 5 207 }, 208 "services": { 209 "paid_services_allowed": false, 210 "total_service_instances": 8, 211 "total_service_keys": 20 212 }, 213 "routes": { 214 "total_routes": 10, 215 "total_reserved_ports": 5 216 }, 217 "domains": { 218 "total_private_domains": 7 219 }, 220 "relationships": { 221 "organizations": { 222 "data": [] 223 } 224 }, 225 "links": { 226 "self": { "href": "%s/v3/organization_quotas/quota-2-guid" } 227 } 228 } 229 ] 230 }`, server.URL()) 231 232 server.AppendHandlers( 233 CombineHandlers( 234 VerifyRequest(http.MethodGet, "/v3/organization_quotas", "names=sancho-panza"), 235 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"page1 warning"}}), 236 ), 237 ) 238 }) 239 240 It("queries the API with the given name", func() { 241 Expect(executeErr).NotTo(HaveOccurred()) 242 Expect(warnings).To(ConsistOf("page1 warning")) 243 Expect(orgQuotas).To(ConsistOf( 244 OrgQuota{ 245 GUID: "quota-2-guid", 246 Name: "sancho-panza", 247 Apps: AppLimit{ 248 TotalMemory: types.NullInt{Value: 10240, IsSet: true}, 249 InstanceMemory: types.NullInt{Value: 1024, IsSet: true}, 250 TotalAppInstances: types.NullInt{Value: 8, IsSet: true}, 251 }, 252 Services: ServiceLimit{ 253 TotalServiceInstances: types.NullInt{Value: 8, IsSet: true}, 254 PaidServicePlans: false, 255 }, 256 Routes: RouteLimit{ 257 TotalRoutes: types.NullInt{Value: 10, IsSet: true}, 258 TotalRoutePorts: types.NullInt{Value: 5, IsSet: true}, 259 }, 260 }, 261 )) 262 }) 263 }) 264 265 When("the cloud controller returns errors and warnings", func() { 266 BeforeEach(func() { 267 response := `{ 268 "errors": [ 269 { 270 "code": 10008, 271 "detail": "The request is semantically invalid: command presence", 272 "title": "CF-UnprocessableEntity" 273 }, 274 { 275 "code": 10010, 276 "detail": "App not found", 277 "title": "CF-ResourceNotFound" 278 } 279 ] 280 }` 281 server.AppendHandlers( 282 CombineHandlers( 283 VerifyRequest(http.MethodGet, "/v3/organization_quotas"), 284 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 285 ), 286 ) 287 }) 288 289 It("returns the error and all warnings", func() { 290 Expect(executeErr).To(MatchError(ccerror.MultiError{ 291 ResponseCode: http.StatusTeapot, 292 Errors: []ccerror.V3Error{ 293 { 294 Code: 10008, 295 Detail: "The request is semantically invalid: command presence", 296 Title: "CF-UnprocessableEntity", 297 }, 298 { 299 Code: 10010, 300 Detail: "App not found", 301 Title: "CF-ResourceNotFound", 302 }, 303 }, 304 })) 305 Expect(warnings).To(ConsistOf("this is a warning")) 306 }) 307 }) 308 }) 309 })