github.com/gophercloud/gophercloud@v1.11.0/openstack/placement/v1/resourceproviders/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/gophercloud/gophercloud/openstack/placement/v1/resourceproviders" 9 10 th "github.com/gophercloud/gophercloud/testhelper" 11 fake "github.com/gophercloud/gophercloud/testhelper/client" 12 ) 13 14 const ResourceProviderTestID = "99c09379-6e52-4ef8-9a95-b9ce6f68452e" 15 16 const ResourceProvidersBody = ` 17 { 18 "resource_providers": [ 19 { 20 "generation": 1, 21 "uuid": "99c09379-6e52-4ef8-9a95-b9ce6f68452e", 22 "links": [ 23 { 24 "href": "/resource_providers/99c09379-6e52-4ef8-9a95-b9ce6f68452e", 25 "rel": "self" 26 } 27 ], 28 "name": "vgr.localdomain", 29 "parent_provider_uuid": "542df8ed-9be2-49b9-b4db-6d3183ff8ec8", 30 "root_provider_uuid": "542df8ed-9be2-49b9-b4db-6d3183ff8ec8" 31 }, 32 { 33 "generation": 2, 34 "uuid": "d0b381e9-8761-42de-8e6c-bba99a96d5f5", 35 "links": [ 36 { 37 "href": "/resource_providers/d0b381e9-8761-42de-8e6c-bba99a96d5f5", 38 "rel": "self" 39 } 40 ], 41 "name": "pony1", 42 "parent_provider_uuid": null, 43 "root_provider_uuid": "d0b381e9-8761-42de-8e6c-bba99a96d5f5" 44 } 45 ] 46 } 47 ` 48 49 const ResourceProviderCreateBody = ` 50 { 51 "generation": 1, 52 "uuid": "99c09379-6e52-4ef8-9a95-b9ce6f68452e", 53 "links": [ 54 { 55 "href": "/resource_providers/99c09379-6e52-4ef8-9a95-b9ce6f68452e", 56 "rel": "self" 57 } 58 ], 59 "name": "vgr.localdomain", 60 "parent_provider_uuid": "542df8ed-9be2-49b9-b4db-6d3183ff8ec8", 61 "root_provider_uuid": "542df8ed-9be2-49b9-b4db-6d3183ff8ec8" 62 } 63 ` 64 65 const ResourceProviderUpdateResponse = ` 66 { 67 "generation": 1, 68 "uuid": "4e8e5957-649f-477b-9e5b-f1f75b21c03c", 69 "links": [ 70 { 71 "href": "/resource_providers/4e8e5957-649f-477b-9e5b-f1f75b21c03c", 72 "rel": "self" 73 } 74 ], 75 "name": "new_name", 76 "parent_provider_uuid": "b99b3ab4-3aa6-4fba-b827-69b88b9c544a", 77 "root_provider_uuid": "542df8ed-9be2-49b9-b4db-6d3183ff8ec8" 78 } 79 ` 80 81 const ResourceProviderUpdateRequest = ` 82 { 83 "name": "new_name", 84 "parent_provider_uuid": "b99b3ab4-3aa6-4fba-b827-69b88b9c544a" 85 } 86 ` 87 88 const UsagesBody = ` 89 { 90 "resource_provider_generation": 1, 91 "usages": { 92 "DISK_GB": 1, 93 "MEMORY_MB": 512, 94 "VCPU": 1 95 } 96 } 97 ` 98 99 const InventoriesBody = ` 100 { 101 "inventories": { 102 "DISK_GB": { 103 "allocation_ratio": 1.0, 104 "max_unit": 35, 105 "min_unit": 1, 106 "reserved": 0, 107 "step_size": 1, 108 "total": 35 109 }, 110 "MEMORY_MB": { 111 "allocation_ratio": 1.5, 112 "max_unit": 5825, 113 "min_unit": 1, 114 "reserved": 512, 115 "step_size": 1, 116 "total": 5825 117 }, 118 "VCPU": { 119 "allocation_ratio": 16.0, 120 "max_unit": 4, 121 "min_unit": 1, 122 "reserved": 0, 123 "step_size": 1, 124 "total": 4 125 } 126 }, 127 "resource_provider_generation": 7 128 } 129 ` 130 131 const AllocationsBody = ` 132 { 133 "allocations": { 134 "56785a3f-6f1c-4fec-af0b-0faf075b1fcb": { 135 "resources": { 136 "MEMORY_MB": 256, 137 "VCPU": 1 138 } 139 }, 140 "9afd5aeb-d6b9-4dea-a588-1e6327a91834": { 141 "resources": { 142 "MEMORY_MB": 512, 143 "VCPU": 2 144 } 145 }, 146 "9d16a611-e7f9-4ef3-be26-c61ed01ecefb": { 147 "resources": { 148 "MEMORY_MB": 1024, 149 "VCPU": 1 150 } 151 } 152 }, 153 "resource_provider_generation": 12 154 } 155 ` 156 157 const TraitsBody = ` 158 { 159 "resource_provider_generation": 1, 160 "traits": [ 161 "CUSTOM_HW_FPGA_CLASS1", 162 "CUSTOM_HW_FPGA_CLASS3" 163 ] 164 } 165 ` 166 167 var ExpectedResourceProvider1 = resourceproviders.ResourceProvider{ 168 Generation: 1, 169 UUID: "99c09379-6e52-4ef8-9a95-b9ce6f68452e", 170 Links: []resourceproviders.ResourceProviderLinks{ 171 { 172 Href: "/resource_providers/99c09379-6e52-4ef8-9a95-b9ce6f68452e", 173 Rel: "self", 174 }, 175 }, 176 Name: "vgr.localdomain", 177 ParentProviderUUID: "542df8ed-9be2-49b9-b4db-6d3183ff8ec8", 178 RootProviderUUID: "542df8ed-9be2-49b9-b4db-6d3183ff8ec8", 179 } 180 181 var ExpectedResourceProvider2 = resourceproviders.ResourceProvider{ 182 Generation: 2, 183 UUID: "d0b381e9-8761-42de-8e6c-bba99a96d5f5", 184 Links: []resourceproviders.ResourceProviderLinks{ 185 { 186 Href: "/resource_providers/d0b381e9-8761-42de-8e6c-bba99a96d5f5", 187 Rel: "self", 188 }, 189 }, 190 Name: "pony1", 191 ParentProviderUUID: "", 192 RootProviderUUID: "d0b381e9-8761-42de-8e6c-bba99a96d5f5", 193 } 194 195 var ExpectedResourceProviders = []resourceproviders.ResourceProvider{ 196 ExpectedResourceProvider1, 197 ExpectedResourceProvider2, 198 } 199 200 var ExpectedUsages = resourceproviders.ResourceProviderUsage{ 201 ResourceProviderGeneration: 1, 202 Usages: map[string]int{ 203 "DISK_GB": 1, 204 "MEMORY_MB": 512, 205 "VCPU": 1, 206 }, 207 } 208 209 var ExpectedInventories = resourceproviders.ResourceProviderInventories{ 210 ResourceProviderGeneration: 7, 211 Inventories: map[string]resourceproviders.Inventory{ 212 "DISK_GB": { 213 AllocationRatio: 1.0, 214 MaxUnit: 35, 215 MinUnit: 1, 216 Reserved: 0, 217 StepSize: 1, 218 Total: 35, 219 }, 220 "MEMORY_MB": { 221 AllocationRatio: 1.5, 222 MaxUnit: 5825, 223 MinUnit: 1, 224 Reserved: 512, 225 StepSize: 1, 226 Total: 5825, 227 }, 228 "VCPU": { 229 AllocationRatio: 16.0, 230 MaxUnit: 4, 231 MinUnit: 1, 232 Reserved: 0, 233 StepSize: 1, 234 Total: 4, 235 }, 236 }, 237 } 238 239 var ExpectedAllocations = resourceproviders.ResourceProviderAllocations{ 240 ResourceProviderGeneration: 12, 241 Allocations: map[string]resourceproviders.Allocation{ 242 "56785a3f-6f1c-4fec-af0b-0faf075b1fcb": { 243 Resources: map[string]int{ 244 "MEMORY_MB": 256, 245 "VCPU": 1, 246 }, 247 }, 248 "9afd5aeb-d6b9-4dea-a588-1e6327a91834": { 249 Resources: map[string]int{ 250 "MEMORY_MB": 512, 251 "VCPU": 2, 252 }, 253 }, 254 "9d16a611-e7f9-4ef3-be26-c61ed01ecefb": { 255 Resources: map[string]int{ 256 "MEMORY_MB": 1024, 257 "VCPU": 1, 258 }, 259 }, 260 }, 261 } 262 263 var ExpectedTraits = resourceproviders.ResourceProviderTraits{ 264 ResourceProviderGeneration: 1, 265 Traits: []string{ 266 "CUSTOM_HW_FPGA_CLASS1", 267 "CUSTOM_HW_FPGA_CLASS3", 268 }, 269 } 270 271 func HandleResourceProviderList(t *testing.T) { 272 th.Mux.HandleFunc("/resource_providers", 273 func(w http.ResponseWriter, r *http.Request) { 274 th.TestMethod(t, r, "GET") 275 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 276 277 w.Header().Add("Content-Type", "application/json") 278 w.WriteHeader(http.StatusOK) 279 280 fmt.Fprintf(w, ResourceProvidersBody) 281 }) 282 } 283 284 func HandleResourceProviderCreate(t *testing.T) { 285 th.Mux.HandleFunc("/resource_providers", func(w http.ResponseWriter, r *http.Request) { 286 th.TestMethod(t, r, "POST") 287 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 288 289 w.Header().Add("Content-Type", "application/json") 290 w.WriteHeader(http.StatusOK) 291 292 fmt.Fprintf(w, ResourceProviderCreateBody) 293 }) 294 } 295 296 func HandleResourceProviderGet(t *testing.T) { 297 th.Mux.HandleFunc("/resource_providers/99c09379-6e52-4ef8-9a95-b9ce6f68452e", func(w http.ResponseWriter, r *http.Request) { 298 th.TestMethod(t, r, "GET") 299 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 300 301 w.Header().Add("Content-Type", "application/json") 302 w.WriteHeader(http.StatusOK) 303 304 fmt.Fprintf(w, ResourceProviderCreateBody) 305 }) 306 } 307 308 func HandleResourceProviderDelete(t *testing.T) { 309 th.Mux.HandleFunc("/resource_providers/b99b3ab4-3aa6-4fba-b827-69b88b9c544a", func(w http.ResponseWriter, r *http.Request) { 310 th.TestMethod(t, r, "DELETE") 311 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 312 w.WriteHeader(http.StatusNoContent) 313 }) 314 } 315 316 func HandleResourceProviderUpdate(t *testing.T) { 317 th.Mux.HandleFunc("/resource_providers/4e8e5957-649f-477b-9e5b-f1f75b21c03c", func(w http.ResponseWriter, r *http.Request) { 318 th.TestMethod(t, r, "PUT") 319 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 320 th.TestHeader(t, r, "Content-Type", "application/json") 321 th.TestHeader(t, r, "Accept", "application/json") 322 th.TestJSONRequest(t, r, ResourceProviderUpdateRequest) 323 324 w.Header().Add("Content-Type", "application/json") 325 w.WriteHeader(http.StatusOK) 326 327 fmt.Fprintf(w, ResourceProviderUpdateResponse) 328 }) 329 } 330 331 func HandleResourceProviderGetUsages(t *testing.T) { 332 usageTestUrl := fmt.Sprintf("/resource_providers/%s/usages", ResourceProviderTestID) 333 334 th.Mux.HandleFunc(usageTestUrl, 335 func(w http.ResponseWriter, r *http.Request) { 336 th.TestMethod(t, r, "GET") 337 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 338 339 w.Header().Add("Content-Type", "application/json") 340 w.WriteHeader(http.StatusOK) 341 342 fmt.Fprintf(w, UsagesBody) 343 }) 344 } 345 346 func HandleResourceProviderGetInventories(t *testing.T) { 347 inventoriesTestUrl := fmt.Sprintf("/resource_providers/%s/inventories", ResourceProviderTestID) 348 349 th.Mux.HandleFunc(inventoriesTestUrl, 350 func(w http.ResponseWriter, r *http.Request) { 351 th.TestMethod(t, r, "GET") 352 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 353 354 w.Header().Add("Content-Type", "application/json") 355 w.WriteHeader(http.StatusOK) 356 357 fmt.Fprintf(w, InventoriesBody) 358 }) 359 } 360 361 func HandleResourceProviderGetAllocations(t *testing.T) { 362 allocationsTestUrl := fmt.Sprintf("/resource_providers/%s/allocations", ResourceProviderTestID) 363 364 th.Mux.HandleFunc(allocationsTestUrl, 365 func(w http.ResponseWriter, r *http.Request) { 366 th.TestMethod(t, r, "GET") 367 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 368 369 w.Header().Add("Content-Type", "application/json") 370 w.WriteHeader(http.StatusOK) 371 372 fmt.Fprintf(w, AllocationsBody) 373 }) 374 } 375 376 func HandleResourceProviderGetTraits(t *testing.T) { 377 traitsTestUrl := fmt.Sprintf("/resource_providers/%s/traits", ResourceProviderTestID) 378 379 th.Mux.HandleFunc(traitsTestUrl, 380 func(w http.ResponseWriter, r *http.Request) { 381 th.TestMethod(t, r, "GET") 382 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 383 384 w.Header().Add("Content-Type", "application/json") 385 w.WriteHeader(http.StatusOK) 386 387 fmt.Fprintf(w, TraitsBody) 388 }) 389 }