github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v3/volumes/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 9 fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 10 ) 11 12 func MockListResponse(t *testing.T) { 13 th.Mux.HandleFunc("/volumes/detail", func(w http.ResponseWriter, r *http.Request) { 14 th.TestMethod(t, r, "GET") 15 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 16 17 w.Header().Add("Content-Type", "application/json") 18 w.WriteHeader(http.StatusOK) 19 20 if err := r.ParseForm(); err != nil { 21 t.Errorf("Failed to parse request form %v", err) 22 } 23 marker := r.Form.Get("marker") 24 switch marker { 25 case "": 26 fmt.Fprintf(w, ` 27 { 28 "volumes": [ 29 { 30 "volume_type": "lvmdriver-1", 31 "created_at": "2015-09-17T03:35:03.000000", 32 "bootable": "false", 33 "name": "vol-001", 34 "os-vol-mig-status-attr:name_id": null, 35 "consistencygroup_id": null, 36 "source_volid": null, 37 "os-volume-replication:driver_data": null, 38 "multiattach": false, 39 "snapshot_id": null, 40 "replication_status": "disabled", 41 "os-volume-replication:extended_status": null, 42 "encrypted": false, 43 "os-vol-host-attr:host": "host-001", 44 "availability_zone": "nova", 45 "attachments": [{ 46 "server_id": "83ec2e3b-4321-422b-8706-a84185f52a0a", 47 "attachment_id": "05551600-a936-4d4a-ba42-79a037c1-c91a", 48 "attached_at": "2016-08-06T14:48:20.000000", 49 "host_name": "foobar", 50 "volume_id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75", 51 "device": "/dev/vdc", 52 "id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75" 53 }], 54 "id": "289da7f8-6440-407c-9fb4-7db01ec49164", 55 "size": 75, 56 "user_id": "ff1ce52c03ab433aaba9108c2e3ef541", 57 "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459", 58 "os-vol-mig-status-attr:migstat": null, 59 "metadata": {"foo": "bar"}, 60 "status": "available", 61 "description": null 62 }, 63 { 64 "volume_type": "lvmdriver-1", 65 "created_at": "2015-09-17T03:32:29.000000", 66 "bootable": "false", 67 "name": "vol-002", 68 "os-vol-mig-status-attr:name_id": null, 69 "consistencygroup_id": null, 70 "source_volid": null, 71 "os-volume-replication:driver_data": null, 72 "multiattach": false, 73 "snapshot_id": null, 74 "replication_status": "disabled", 75 "os-volume-replication:extended_status": null, 76 "encrypted": false, 77 "os-vol-host-attr:host": null, 78 "availability_zone": "nova", 79 "attachments": [], 80 "id": "96c3bda7-c82a-4f50-be73-ca7621794835", 81 "size": 75, 82 "user_id": "ff1ce52c03ab433aaba9108c2e3ef541", 83 "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459", 84 "os-vol-mig-status-attr:migstat": null, 85 "metadata": {}, 86 "status": "available", 87 "description": null 88 } 89 ], 90 "volumes_links": [ 91 { 92 "href": "%s/volumes/detail?marker=1", 93 "rel": "next" 94 }] 95 } 96 `, th.Server.URL) 97 case "1": 98 fmt.Fprint(w, `{"volumes": []}`) 99 default: 100 t.Fatalf("Unexpected marker: [%s]", marker) 101 } 102 }) 103 } 104 105 func MockGetResponse(t *testing.T) { 106 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 107 th.TestMethod(t, r, "GET") 108 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 109 110 w.Header().Add("Content-Type", "application/json") 111 w.WriteHeader(http.StatusOK) 112 fmt.Fprint(w, ` 113 { 114 "volume": { 115 "volume_type": "lvmdriver-1", 116 "created_at": "2015-09-17T03:32:29.000000", 117 "bootable": "false", 118 "name": "vol-001", 119 "os-vol-mig-status-attr:name_id": null, 120 "consistencygroup_id": null, 121 "source_volid": null, 122 "os-volume-replication:driver_data": null, 123 "multiattach": false, 124 "snapshot_id": null, 125 "replication_status": "disabled", 126 "os-volume-replication:extended_status": null, 127 "encrypted": false, 128 "availability_zone": "nova", 129 "attachments": [{ 130 "server_id": "83ec2e3b-4321-422b-8706-a84185f52a0a", 131 "attachment_id": "05551600-a936-4d4a-ba42-79a037c1-c91a", 132 "attached_at": "2016-08-06T14:48:20.000000", 133 "host_name": "foobar", 134 "volume_id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75", 135 "device": "/dev/vdc", 136 "id": "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75" 137 }], 138 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 139 "size": 75, 140 "user_id": "ff1ce52c03ab433aaba9108c2e3ef541", 141 "os-vol-tenant-attr:tenant_id": "304dc00909ac4d0da6c62d816bcb3459", 142 "os-vol-mig-status-attr:migstat": null, 143 "metadata": {}, 144 "status": "available", 145 "volume_image_metadata": { 146 "container_format": "bare", 147 "image_name": "centos" 148 }, 149 "description": null 150 } 151 } 152 `) 153 }) 154 } 155 156 func MockCreateResponse(t *testing.T) { 157 th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) { 158 th.TestMethod(t, r, "POST") 159 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 160 th.TestHeader(t, r, "Content-Type", "application/json") 161 th.TestHeader(t, r, "Accept", "application/json") 162 th.TestJSONRequest(t, r, ` 163 { 164 "volume": { 165 "name": "vol-001", 166 "size": 75 167 } 168 } 169 `) 170 171 w.Header().Add("Content-Type", "application/json") 172 w.WriteHeader(http.StatusAccepted) 173 174 fmt.Fprint(w, ` 175 { 176 "volume": { 177 "size": 75, 178 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 179 "metadata": {}, 180 "created_at": "2015-09-17T03:32:29.044216", 181 "encrypted": false, 182 "bootable": "false", 183 "availability_zone": "nova", 184 "attachments": [], 185 "user_id": "ff1ce52c03ab433aaba9108c2e3ef541", 186 "status": "creating", 187 "description": null, 188 "volume_type": "lvmdriver-1", 189 "name": "vol-001", 190 "replication_status": "disabled", 191 "consistencygroup_id": null, 192 "source_volid": null, 193 "snapshot_id": null, 194 "multiattach": false 195 } 196 } 197 `) 198 }) 199 } 200 201 func MockDeleteResponse(t *testing.T) { 202 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 203 th.TestMethod(t, r, "DELETE") 204 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 205 w.WriteHeader(http.StatusAccepted) 206 }) 207 } 208 209 func MockUpdateResponse(t *testing.T) { 210 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) { 211 th.TestMethod(t, r, "PUT") 212 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 213 w.WriteHeader(http.StatusOK) 214 fmt.Fprint(w, ` 215 { 216 "volume": { 217 "name": "vol-002" 218 } 219 } 220 `) 221 }) 222 } 223 224 func MockCreateVolumeFromBackupResponse(t *testing.T) { 225 th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) { 226 th.TestMethod(t, r, "POST") 227 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 228 th.TestHeader(t, r, "Content-Type", "application/json") 229 th.TestHeader(t, r, "Accept", "application/json") 230 th.TestJSONRequest(t, r, ` 231 { 232 "volume": { 233 "name": "vol-001", 234 "backup_id": "20c792f0-bb03-434f-b653-06ef238e337e" 235 } 236 } 237 `) 238 239 w.Header().Add("Content-Type", "application/json") 240 w.WriteHeader(http.StatusAccepted) 241 242 fmt.Fprint(w, ` 243 { 244 "volume": { 245 "size": 30, 246 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22", 247 "metadata": {}, 248 "created_at": "2015-09-17T03:32:29.044216", 249 "encrypted": false, 250 "bootable": "false", 251 "availability_zone": "nova", 252 "attachments": [], 253 "user_id": "ff1ce52c03ab433aaba9108c2e3ef541", 254 "status": "creating", 255 "description": null, 256 "volume_type": "lvmdriver-1", 257 "name": "vol-001", 258 "replication_status": "disabled", 259 "consistencygroup_id": null, 260 "source_volid": null, 261 "snapshot_id": null, 262 "backup_id": "20c792f0-bb03-434f-b653-06ef238e337e", 263 "multiattach": false 264 } 265 }`) 266 }) 267 } 268 269 func MockAttachResponse(t *testing.T) { 270 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", 271 func(w http.ResponseWriter, r *http.Request) { 272 th.TestMethod(t, r, "POST") 273 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 274 th.TestHeader(t, r, "Content-Type", "application/json") 275 th.TestHeader(t, r, "Accept", "application/json") 276 th.TestJSONRequest(t, r, ` 277 { 278 "os-attach": 279 { 280 "mountpoint": "/mnt", 281 "mode": "rw", 282 "instance_uuid": "50902f4f-a974-46a0-85e9-7efc5e22dfdd" 283 } 284 } 285 `) 286 287 w.Header().Add("Content-Type", "application/json") 288 w.WriteHeader(http.StatusAccepted) 289 290 fmt.Fprint(w, `{}`) 291 }) 292 } 293 294 func MockBeginDetachingResponse(t *testing.T) { 295 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", 296 func(w http.ResponseWriter, r *http.Request) { 297 th.TestMethod(t, r, "POST") 298 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 299 th.TestHeader(t, r, "Content-Type", "application/json") 300 th.TestHeader(t, r, "Accept", "application/json") 301 th.TestJSONRequest(t, r, ` 302 { 303 "os-begin_detaching": {} 304 } 305 `) 306 307 w.Header().Add("Content-Type", "application/json") 308 w.WriteHeader(http.StatusAccepted) 309 310 fmt.Fprint(w, `{}`) 311 }) 312 } 313 314 func MockDetachResponse(t *testing.T) { 315 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", 316 func(w http.ResponseWriter, r *http.Request) { 317 th.TestMethod(t, r, "POST") 318 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 319 th.TestHeader(t, r, "Content-Type", "application/json") 320 th.TestHeader(t, r, "Accept", "application/json") 321 th.TestJSONRequest(t, r, ` 322 { 323 "os-detach": {} 324 } 325 `) 326 327 w.Header().Add("Content-Type", "application/json") 328 w.WriteHeader(http.StatusAccepted) 329 330 fmt.Fprint(w, `{}`) 331 }) 332 } 333 334 func MockUploadImageResponse(t *testing.T) { 335 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", 336 func(w http.ResponseWriter, r *http.Request) { 337 th.TestMethod(t, r, "POST") 338 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 339 th.TestHeader(t, r, "Content-Type", "application/json") 340 th.TestHeader(t, r, "Accept", "application/json") 341 th.TestJSONRequest(t, r, ` 342 { 343 "os-volume_upload_image": { 344 "container_format": "bare", 345 "force": true, 346 "image_name": "test", 347 "disk_format": "raw" 348 } 349 } 350 `) 351 352 w.Header().Add("Content-Type", "application/json") 353 w.WriteHeader(http.StatusAccepted) 354 355 fmt.Fprint(w, ` 356 { 357 "os-volume_upload_image": { 358 "container_format": "bare", 359 "display_description": null, 360 "id": "cd281d77-8217-4830-be95-9528227c105c", 361 "image_id": "ecb92d98-de08-45db-8235-bbafe317269c", 362 "image_name": "test", 363 "disk_format": "raw", 364 "size": 5, 365 "status": "uploading", 366 "updated_at": "2017-07-17T09:29:22.000000", 367 "volume_type": { 368 "created_at": "2016-05-04T08:54:14.000000", 369 "deleted": false, 370 "deleted_at": null, 371 "description": null, 372 "extra_specs": { 373 "volume_backend_name": "basic.ru-2a" 374 }, 375 "id": "b7133444-62f6-4433-8da3-70ac332229b7", 376 "is_public": true, 377 "name": "basic.ru-2a", 378 "updated_at": "2016-05-04T09:15:33.000000" 379 } 380 } 381 } 382 `) 383 }) 384 } 385 386 func MockReserveResponse(t *testing.T) { 387 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", 388 func(w http.ResponseWriter, r *http.Request) { 389 th.TestMethod(t, r, "POST") 390 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 391 th.TestHeader(t, r, "Content-Type", "application/json") 392 th.TestHeader(t, r, "Accept", "application/json") 393 th.TestJSONRequest(t, r, ` 394 { 395 "os-reserve": {} 396 } 397 `) 398 399 w.Header().Add("Content-Type", "application/json") 400 w.WriteHeader(http.StatusAccepted) 401 402 fmt.Fprint(w, `{}`) 403 }) 404 } 405 406 func MockUnreserveResponse(t *testing.T) { 407 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", 408 func(w http.ResponseWriter, r *http.Request) { 409 th.TestMethod(t, r, "POST") 410 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 411 th.TestHeader(t, r, "Content-Type", "application/json") 412 th.TestHeader(t, r, "Accept", "application/json") 413 th.TestJSONRequest(t, r, ` 414 { 415 "os-unreserve": {} 416 } 417 `) 418 419 w.Header().Add("Content-Type", "application/json") 420 w.WriteHeader(http.StatusAccepted) 421 422 fmt.Fprint(w, `{}`) 423 }) 424 } 425 426 func MockInitializeConnectionResponse(t *testing.T) { 427 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", 428 func(w http.ResponseWriter, r *http.Request) { 429 th.TestMethod(t, r, "POST") 430 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 431 th.TestHeader(t, r, "Content-Type", "application/json") 432 th.TestHeader(t, r, "Accept", "application/json") 433 th.TestJSONRequest(t, r, ` 434 { 435 "os-initialize_connection": 436 { 437 "connector": 438 { 439 "ip":"127.0.0.1", 440 "host":"stack", 441 "initiator":"iqn.1994-05.com.redhat:17cf566367d2", 442 "multipath": false, 443 "platform": "x86_64", 444 "os_type": "linux2" 445 } 446 } 447 } 448 `) 449 450 w.Header().Add("Content-Type", "application/json") 451 w.WriteHeader(http.StatusAccepted) 452 453 fmt.Fprint(w, `{ 454 "connection_info": { 455 "data": { 456 "target_portals": [ 457 "172.31.17.48:3260" 458 ], 459 "auth_method": "CHAP", 460 "auth_username": "5MLtcsTEmNN5jFVcT6ui", 461 "access_mode": "rw", 462 "target_lun": 0, 463 "volume_id": "cd281d77-8217-4830-be95-9528227c105c", 464 "target_luns": [ 465 0 466 ], 467 "target_iqns": [ 468 "iqn.2010-10.org.openstack:volume-cd281d77-8217-4830-be95-9528227c105c" 469 ], 470 "auth_password": "x854ZY5Re3aCkdNL", 471 "target_discovered": false, 472 "encrypted": false, 473 "qos_specs": null, 474 "target_iqn": "iqn.2010-10.org.openstack:volume-cd281d77-8217-4830-be95-9528227c105c", 475 "target_portal": "172.31.17.48:3260" 476 }, 477 "driver_volume_type": "iscsi" 478 } 479 }`) 480 }) 481 } 482 483 func MockTerminateConnectionResponse(t *testing.T) { 484 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", 485 func(w http.ResponseWriter, r *http.Request) { 486 th.TestMethod(t, r, "POST") 487 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 488 th.TestHeader(t, r, "Content-Type", "application/json") 489 th.TestHeader(t, r, "Accept", "application/json") 490 th.TestJSONRequest(t, r, ` 491 { 492 "os-terminate_connection": 493 { 494 "connector": 495 { 496 "ip":"127.0.0.1", 497 "host":"stack", 498 "initiator":"iqn.1994-05.com.redhat:17cf566367d2", 499 "multipath": true, 500 "platform": "x86_64", 501 "os_type": "linux2" 502 } 503 } 504 } 505 `) 506 507 w.Header().Add("Content-Type", "application/json") 508 w.WriteHeader(http.StatusAccepted) 509 510 fmt.Fprint(w, `{}`) 511 }) 512 } 513 514 func MockExtendSizeResponse(t *testing.T) { 515 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", 516 func(w http.ResponseWriter, r *http.Request) { 517 th.TestMethod(t, r, "POST") 518 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 519 th.TestHeader(t, r, "Content-Type", "application/json") 520 th.TestHeader(t, r, "Accept", "application/json") 521 th.TestJSONRequest(t, r, ` 522 { 523 "os-extend": 524 { 525 "new_size": 3 526 } 527 } 528 `) 529 530 w.Header().Add("Content-Type", "application/json") 531 w.WriteHeader(http.StatusAccepted) 532 533 fmt.Fprint(w, `{}`) 534 }) 535 } 536 537 func MockForceDeleteResponse(t *testing.T) { 538 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22/action", func(w http.ResponseWriter, r *http.Request) { 539 th.TestMethod(t, r, "POST") 540 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 541 th.TestBody(t, r, `{"os-force_delete":""}`) 542 w.WriteHeader(http.StatusAccepted) 543 }) 544 } 545 546 func MockSetImageMetadataResponse(t *testing.T) { 547 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", func(w http.ResponseWriter, r *http.Request) { 548 th.TestMethod(t, r, "POST") 549 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 550 th.TestHeader(t, r, "Content-Type", "application/json") 551 th.TestHeader(t, r, "Accept", "application/json") 552 th.TestJSONRequest(t, r, ` 553 { 554 "os-set_image_metadata": { 555 "metadata": { 556 "label": "test" 557 } 558 } 559 } 560 `) 561 w.Header().Add("Content-Type", "application/json") 562 w.WriteHeader(http.StatusOK) 563 564 fmt.Fprint(w, `{}`) 565 }) 566 } 567 568 func MockSetBootableResponse(t *testing.T) { 569 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", func(w http.ResponseWriter, r *http.Request) { 570 th.TestMethod(t, r, "POST") 571 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 572 th.TestHeader(t, r, "Content-Type", "application/json") 573 th.TestHeader(t, r, "Accept", "application/json") 574 th.TestJSONRequest(t, r, ` 575 { 576 "os-set_bootable": { 577 "bootable": true 578 } 579 } 580 `) 581 w.Header().Add("Content-Type", "application/json") 582 w.Header().Add("Content-Length", "0") 583 w.WriteHeader(http.StatusOK) 584 }) 585 } 586 587 func MockReImageResponse(t *testing.T) { 588 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", func(w http.ResponseWriter, r *http.Request) { 589 th.TestMethod(t, r, "POST") 590 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 591 th.TestHeader(t, r, "Content-Type", "application/json") 592 th.TestHeader(t, r, "Accept", "application/json") 593 th.TestJSONRequest(t, r, ` 594 { 595 "os-reimage": { 596 "image_id": "71543ced-a8af-45b6-a5c4-a46282108a90", 597 "reimage_reserved": false 598 } 599 } 600 `) 601 w.Header().Add("Content-Type", "application/json") 602 w.Header().Add("Content-Length", "0") 603 w.WriteHeader(http.StatusAccepted) 604 }) 605 } 606 607 func MockChangeTypeResponse(t *testing.T) { 608 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", 609 func(w http.ResponseWriter, r *http.Request) { 610 th.TestMethod(t, r, "POST") 611 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 612 th.TestHeader(t, r, "Content-Type", "application/json") 613 th.TestHeader(t, r, "Accept", "application/json") 614 th.TestJSONRequest(t, r, ` 615 { 616 "os-retype": 617 { 618 "new_type": "ssd", 619 "migration_policy": "on-demand" 620 } 621 } 622 `) 623 624 w.Header().Add("Content-Type", "application/json") 625 w.WriteHeader(http.StatusAccepted) 626 627 fmt.Fprint(w, `{}`) 628 }) 629 } 630 631 func MockResetStatusResponse(t *testing.T) { 632 th.Mux.HandleFunc("/volumes/cd281d77-8217-4830-be95-9528227c105c/action", 633 func(w http.ResponseWriter, r *http.Request) { 634 th.TestMethod(t, r, "POST") 635 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 636 th.TestHeader(t, r, "Content-Type", "application/json") 637 th.TestJSONRequest(t, r, ` 638 { 639 "os-reset_status": 640 { 641 "status": "error", 642 "attach_status": "detached", 643 "migration_status": "migrating" 644 } 645 } 646 `) 647 648 w.WriteHeader(http.StatusAccepted) 649 }) 650 }