github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v3/volumes/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/vnpaycloud-console/gophercloud/v2" 9 "github.com/vnpaycloud-console/gophercloud/v2/openstack/blockstorage/v3/volumes" 10 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 11 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 12 "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 13 ) 14 15 func TestListWithExtensions(t *testing.T) { 16 th.SetupHTTP() 17 defer th.TeardownHTTP() 18 19 MockListResponse(t) 20 21 count := 0 22 23 err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 24 count++ 25 actual, err := volumes.ExtractVolumes(page) 26 if err != nil { 27 t.Errorf("Failed to extract volumes: %v", err) 28 return false, err 29 } 30 31 expected := []volumes.Volume{ 32 { 33 ID: "289da7f8-6440-407c-9fb4-7db01ec49164", 34 Name: "vol-001", 35 Attachments: []volumes.Attachment{{ 36 ServerID: "83ec2e3b-4321-422b-8706-a84185f52a0a", 37 AttachmentID: "05551600-a936-4d4a-ba42-79a037c1-c91a", 38 AttachedAt: time.Date(2016, 8, 6, 14, 48, 20, 0, time.UTC), 39 HostName: "foobar", 40 VolumeID: "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75", 41 Device: "/dev/vdc", 42 ID: "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75", 43 }}, 44 AvailabilityZone: "nova", 45 Bootable: "false", 46 ConsistencyGroupID: "", 47 CreatedAt: time.Date(2015, 9, 17, 3, 35, 3, 0, time.UTC), 48 Description: "", 49 Encrypted: false, 50 Host: "host-001", 51 Metadata: map[string]string{"foo": "bar"}, 52 Multiattach: false, 53 TenantID: "304dc00909ac4d0da6c62d816bcb3459", 54 //ReplicationDriverData: "", 55 //ReplicationExtendedStatus: "", 56 ReplicationStatus: "disabled", 57 Size: 75, 58 SnapshotID: "", 59 SourceVolID: "", 60 Status: "available", 61 UserID: "ff1ce52c03ab433aaba9108c2e3ef541", 62 VolumeType: "lvmdriver-1", 63 }, 64 { 65 ID: "96c3bda7-c82a-4f50-be73-ca7621794835", 66 Name: "vol-002", 67 Attachments: []volumes.Attachment{}, 68 AvailabilityZone: "nova", 69 Bootable: "false", 70 ConsistencyGroupID: "", 71 CreatedAt: time.Date(2015, 9, 17, 3, 32, 29, 0, time.UTC), 72 Description: "", 73 Encrypted: false, 74 Metadata: map[string]string{}, 75 Multiattach: false, 76 TenantID: "304dc00909ac4d0da6c62d816bcb3459", 77 //ReplicationDriverData: "", 78 //ReplicationExtendedStatus: "", 79 ReplicationStatus: "disabled", 80 Size: 75, 81 SnapshotID: "", 82 SourceVolID: "", 83 Status: "available", 84 UserID: "ff1ce52c03ab433aaba9108c2e3ef541", 85 VolumeType: "lvmdriver-1", 86 }, 87 } 88 89 th.CheckDeepEquals(t, expected, actual) 90 91 return true, nil 92 }) 93 th.AssertNoErr(t, err) 94 95 if count != 1 { 96 t.Errorf("Expected 1 page, got %d", count) 97 } 98 } 99 100 func TestListAllWithExtensions(t *testing.T) { 101 th.SetupHTTP() 102 defer th.TeardownHTTP() 103 104 MockListResponse(t) 105 106 allPages, err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).AllPages(context.TODO()) 107 th.AssertNoErr(t, err) 108 109 var actual []volumes.Volume 110 err = volumes.ExtractVolumesInto(allPages, &actual) 111 th.AssertNoErr(t, err) 112 th.AssertEquals(t, 2, len(actual)) 113 th.AssertEquals(t, "host-001", actual[0].Host) 114 th.AssertEquals(t, "", actual[1].Host) 115 th.AssertEquals(t, "304dc00909ac4d0da6c62d816bcb3459", actual[0].TenantID) 116 } 117 118 func TestListAll(t *testing.T) { 119 th.SetupHTTP() 120 defer th.TeardownHTTP() 121 122 MockListResponse(t) 123 124 allPages, err := volumes.List(client.ServiceClient(), &volumes.ListOpts{}).AllPages(context.TODO()) 125 th.AssertNoErr(t, err) 126 actual, err := volumes.ExtractVolumes(allPages) 127 th.AssertNoErr(t, err) 128 129 expected := []volumes.Volume{ 130 { 131 ID: "289da7f8-6440-407c-9fb4-7db01ec49164", 132 Name: "vol-001", 133 Attachments: []volumes.Attachment{{ 134 ServerID: "83ec2e3b-4321-422b-8706-a84185f52a0a", 135 AttachmentID: "05551600-a936-4d4a-ba42-79a037c1-c91a", 136 AttachedAt: time.Date(2016, 8, 6, 14, 48, 20, 0, time.UTC), 137 HostName: "foobar", 138 VolumeID: "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75", 139 Device: "/dev/vdc", 140 ID: "d6cacb1a-8b59-4c88-ad90-d70ebb82bb75", 141 }}, 142 AvailabilityZone: "nova", 143 Bootable: "false", 144 ConsistencyGroupID: "", 145 CreatedAt: time.Date(2015, 9, 17, 3, 35, 3, 0, time.UTC), 146 Description: "", 147 Encrypted: false, 148 Host: "host-001", 149 Metadata: map[string]string{"foo": "bar"}, 150 Multiattach: false, 151 TenantID: "304dc00909ac4d0da6c62d816bcb3459", 152 //ReplicationDriverData: "", 153 //ReplicationExtendedStatus: "", 154 ReplicationStatus: "disabled", 155 Size: 75, 156 SnapshotID: "", 157 SourceVolID: "", 158 Status: "available", 159 UserID: "ff1ce52c03ab433aaba9108c2e3ef541", 160 VolumeType: "lvmdriver-1", 161 }, 162 { 163 ID: "96c3bda7-c82a-4f50-be73-ca7621794835", 164 Name: "vol-002", 165 Attachments: []volumes.Attachment{}, 166 AvailabilityZone: "nova", 167 Bootable: "false", 168 ConsistencyGroupID: "", 169 CreatedAt: time.Date(2015, 9, 17, 3, 32, 29, 0, time.UTC), 170 Description: "", 171 Encrypted: false, 172 Metadata: map[string]string{}, 173 Multiattach: false, 174 TenantID: "304dc00909ac4d0da6c62d816bcb3459", 175 //ReplicationDriverData: "", 176 //ReplicationExtendedStatus: "", 177 ReplicationStatus: "disabled", 178 Size: 75, 179 SnapshotID: "", 180 SourceVolID: "", 181 Status: "available", 182 UserID: "ff1ce52c03ab433aaba9108c2e3ef541", 183 VolumeType: "lvmdriver-1", 184 }, 185 } 186 187 th.CheckDeepEquals(t, expected, actual) 188 189 } 190 191 func TestGet(t *testing.T) { 192 th.SetupHTTP() 193 defer th.TeardownHTTP() 194 195 MockGetResponse(t) 196 197 v, err := volumes.Get(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() 198 th.AssertNoErr(t, err) 199 200 th.AssertEquals(t, v.Name, "vol-001") 201 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 202 } 203 204 func TestCreate(t *testing.T) { 205 th.SetupHTTP() 206 defer th.TeardownHTTP() 207 208 MockCreateResponse(t) 209 210 options := &volumes.CreateOpts{Size: 75, Name: "vol-001"} 211 n, err := volumes.Create(context.TODO(), client.ServiceClient(), options, nil).Extract() 212 th.AssertNoErr(t, err) 213 214 th.AssertEquals(t, n.Size, 75) 215 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 216 } 217 218 func TestCreateSchedulerHints(t *testing.T) { 219 base := volumes.SchedulerHintOpts{ 220 DifferentHost: []string{ 221 "a0cf03a5-d921-4877-bb5c-86d26cf818e1", 222 "8c19174f-4220-44f0-824a-cd1eeef10287", 223 }, 224 SameHost: []string{ 225 "a0cf03a5-d921-4877-bb5c-86d26cf818e1", 226 "8c19174f-4220-44f0-824a-cd1eeef10287", 227 }, 228 LocalToInstance: "0ffb2c1b-d621-4fc1-9ae4-88d99c088ff6", 229 AdditionalProperties: map[string]any{"mark": "a0cf03a5-d921-4877-bb5c-86d26cf818e1"}, 230 } 231 expected := ` 232 { 233 "OS-SCH-HNT:scheduler_hints": { 234 "different_host": [ 235 "a0cf03a5-d921-4877-bb5c-86d26cf818e1", 236 "8c19174f-4220-44f0-824a-cd1eeef10287" 237 ], 238 "same_host": [ 239 "a0cf03a5-d921-4877-bb5c-86d26cf818e1", 240 "8c19174f-4220-44f0-824a-cd1eeef10287" 241 ], 242 "local_to_instance": "0ffb2c1b-d621-4fc1-9ae4-88d99c088ff6", 243 "mark": "a0cf03a5-d921-4877-bb5c-86d26cf818e1" 244 } 245 } 246 ` 247 actual, err := base.ToSchedulerHintsMap() 248 th.AssertNoErr(t, err) 249 th.CheckJSONEquals(t, expected, actual) 250 } 251 252 func TestDelete(t *testing.T) { 253 th.SetupHTTP() 254 defer th.TeardownHTTP() 255 256 MockDeleteResponse(t) 257 258 res := volumes.Delete(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", volumes.DeleteOpts{}) 259 th.AssertNoErr(t, res.Err) 260 } 261 262 func TestUpdate(t *testing.T) { 263 th.SetupHTTP() 264 defer th.TeardownHTTP() 265 266 MockUpdateResponse(t) 267 268 var name = "vol-002" 269 options := volumes.UpdateOpts{Name: &name} 270 v, err := volumes.Update(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract() 271 th.AssertNoErr(t, err) 272 th.CheckEquals(t, "vol-002", v.Name) 273 } 274 275 func TestGetWithExtensions(t *testing.T) { 276 th.SetupHTTP() 277 defer th.TeardownHTTP() 278 279 MockGetResponse(t) 280 281 var v volumes.Volume 282 err := volumes.Get(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").ExtractInto(&v) 283 th.AssertNoErr(t, err) 284 th.AssertEquals(t, "304dc00909ac4d0da6c62d816bcb3459", v.TenantID) 285 th.AssertEquals(t, "centos", v.VolumeImageMetadata["image_name"]) 286 287 err = volumes.Get(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").ExtractInto(v) 288 if err == nil { 289 t.Errorf("Expected error when providing non-pointer struct") 290 } 291 } 292 293 func TestCreateFromBackup(t *testing.T) { 294 th.SetupHTTP() 295 defer th.TeardownHTTP() 296 297 MockCreateVolumeFromBackupResponse(t) 298 299 options := volumes.CreateOpts{ 300 Name: "vol-001", 301 BackupID: "20c792f0-bb03-434f-b653-06ef238e337e", 302 } 303 v, err := volumes.Create(context.TODO(), client.ServiceClient(), options, nil).Extract() 304 305 th.AssertNoErr(t, err) 306 th.AssertEquals(t, v.Size, 30) 307 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 308 th.AssertEquals(t, *v.BackupID, "20c792f0-bb03-434f-b653-06ef238e337e") 309 } 310 311 func TestAttach(t *testing.T) { 312 th.SetupHTTP() 313 defer th.TeardownHTTP() 314 315 MockAttachResponse(t) 316 317 options := &volumes.AttachOpts{ 318 MountPoint: "/mnt", 319 Mode: "rw", 320 InstanceUUID: "50902f4f-a974-46a0-85e9-7efc5e22dfdd", 321 } 322 err := volumes.Attach(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr() 323 th.AssertNoErr(t, err) 324 } 325 326 func TestBeginDetaching(t *testing.T) { 327 th.SetupHTTP() 328 defer th.TeardownHTTP() 329 330 MockBeginDetachingResponse(t) 331 332 err := volumes.BeginDetaching(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c").ExtractErr() 333 th.AssertNoErr(t, err) 334 } 335 336 func TestDetach(t *testing.T) { 337 th.SetupHTTP() 338 defer th.TeardownHTTP() 339 340 MockDetachResponse(t) 341 342 err := volumes.Detach(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", &volumes.DetachOpts{}).ExtractErr() 343 th.AssertNoErr(t, err) 344 } 345 346 func TestUploadImage(t *testing.T) { 347 th.SetupHTTP() 348 defer th.TeardownHTTP() 349 MockUploadImageResponse(t) 350 options := &volumes.UploadImageOpts{ 351 ContainerFormat: "bare", 352 DiskFormat: "raw", 353 ImageName: "test", 354 Force: true, 355 } 356 357 actual, err := volumes.UploadImage(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).Extract() 358 th.AssertNoErr(t, err) 359 360 expected := volumes.VolumeImage{ 361 VolumeID: "cd281d77-8217-4830-be95-9528227c105c", 362 ContainerFormat: "bare", 363 DiskFormat: "raw", 364 Description: "", 365 ImageID: "ecb92d98-de08-45db-8235-bbafe317269c", 366 ImageName: "test", 367 Size: 5, 368 Status: "uploading", 369 UpdatedAt: time.Date(2017, 7, 17, 9, 29, 22, 0, time.UTC), 370 VolumeType: volumes.ImageVolumeType{ 371 ID: "b7133444-62f6-4433-8da3-70ac332229b7", 372 Name: "basic.ru-2a", 373 Description: "", 374 IsPublic: true, 375 ExtraSpecs: map[string]any{"volume_backend_name": "basic.ru-2a"}, 376 QosSpecsID: "", 377 Deleted: false, 378 DeletedAt: time.Time{}, 379 CreatedAt: time.Date(2016, 5, 4, 8, 54, 14, 0, time.UTC), 380 UpdatedAt: time.Date(2016, 5, 4, 9, 15, 33, 0, time.UTC), 381 }, 382 } 383 th.AssertDeepEquals(t, expected, actual) 384 } 385 386 func TestReserve(t *testing.T) { 387 th.SetupHTTP() 388 defer th.TeardownHTTP() 389 390 MockReserveResponse(t) 391 392 err := volumes.Reserve(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c").ExtractErr() 393 th.AssertNoErr(t, err) 394 } 395 396 func TestUnreserve(t *testing.T) { 397 th.SetupHTTP() 398 defer th.TeardownHTTP() 399 400 MockUnreserveResponse(t) 401 402 err := volumes.Unreserve(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c").ExtractErr() 403 th.AssertNoErr(t, err) 404 } 405 406 func TestInitializeConnection(t *testing.T) { 407 th.SetupHTTP() 408 defer th.TeardownHTTP() 409 410 MockInitializeConnectionResponse(t) 411 412 options := &volumes.InitializeConnectionOpts{ 413 IP: "127.0.0.1", 414 Host: "stack", 415 Initiator: "iqn.1994-05.com.redhat:17cf566367d2", 416 Multipath: gophercloud.Disabled, 417 Platform: "x86_64", 418 OSType: "linux2", 419 } 420 _, err := volumes.InitializeConnection(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).Extract() 421 th.AssertNoErr(t, err) 422 } 423 424 func TestTerminateConnection(t *testing.T) { 425 th.SetupHTTP() 426 defer th.TeardownHTTP() 427 428 MockTerminateConnectionResponse(t) 429 430 options := &volumes.TerminateConnectionOpts{ 431 IP: "127.0.0.1", 432 Host: "stack", 433 Initiator: "iqn.1994-05.com.redhat:17cf566367d2", 434 Multipath: gophercloud.Enabled, 435 Platform: "x86_64", 436 OSType: "linux2", 437 } 438 err := volumes.TerminateConnection(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr() 439 th.AssertNoErr(t, err) 440 } 441 442 func TestExtendSize(t *testing.T) { 443 th.SetupHTTP() 444 defer th.TeardownHTTP() 445 446 MockExtendSizeResponse(t) 447 448 options := &volumes.ExtendSizeOpts{ 449 NewSize: 3, 450 } 451 452 err := volumes.ExtendSize(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr() 453 th.AssertNoErr(t, err) 454 } 455 456 func TestForceDelete(t *testing.T) { 457 th.SetupHTTP() 458 defer th.TeardownHTTP() 459 460 MockForceDeleteResponse(t) 461 462 res := volumes.ForceDelete(context.TODO(), client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") 463 th.AssertNoErr(t, res.Err) 464 } 465 466 func TestSetImageMetadata(t *testing.T) { 467 th.SetupHTTP() 468 defer th.TeardownHTTP() 469 470 MockSetImageMetadataResponse(t) 471 472 options := &volumes.ImageMetadataOpts{ 473 Metadata: map[string]string{ 474 "label": "test", 475 }, 476 } 477 478 err := volumes.SetImageMetadata(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr() 479 th.AssertNoErr(t, err) 480 } 481 482 func TestSetBootable(t *testing.T) { 483 th.SetupHTTP() 484 defer th.TeardownHTTP() 485 486 MockSetBootableResponse(t) 487 488 options := volumes.BootableOpts{ 489 Bootable: true, 490 } 491 492 err := volumes.SetBootable(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr() 493 th.AssertNoErr(t, err) 494 } 495 496 func TestReImage(t *testing.T) { 497 th.SetupHTTP() 498 defer th.TeardownHTTP() 499 500 MockReImageResponse(t) 501 502 options := volumes.ReImageOpts{ 503 ImageID: "71543ced-a8af-45b6-a5c4-a46282108a90", 504 ReImageReserved: false, 505 } 506 507 err := volumes.ReImage(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr() 508 th.AssertNoErr(t, err) 509 } 510 511 func TestChangeType(t *testing.T) { 512 th.SetupHTTP() 513 defer th.TeardownHTTP() 514 515 MockChangeTypeResponse(t) 516 517 options := &volumes.ChangeTypeOpts{ 518 NewType: "ssd", 519 MigrationPolicy: "on-demand", 520 } 521 522 err := volumes.ChangeType(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr() 523 th.AssertNoErr(t, err) 524 } 525 526 func TestResetStatus(t *testing.T) { 527 th.SetupHTTP() 528 defer th.TeardownHTTP() 529 530 MockResetStatusResponse(t) 531 532 options := &volumes.ResetStatusOpts{ 533 Status: "error", 534 AttachStatus: "detached", 535 MigrationStatus: "migrating", 536 } 537 538 err := volumes.ResetStatus(context.TODO(), client.ServiceClient(), "cd281d77-8217-4830-be95-9528227c105c", options).ExtractErr() 539 th.AssertNoErr(t, err) 540 }