PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/github.com/gophercloud/gophercloud/acceptance/openstack/blockstorage/v3/blockstorage.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 107 lines | 74 code | 20 blank | 13 comment | 18 complexity | e185431926215d32aae9ecf07f3b61ad MD5 | raw file
  1. // Package v3 contains common functions for creating block storage based
  2. // resources for use in acceptance tests. See the `*_test.go` files for
  3. // example usages.
  4. package v3
  5. import (
  6. "testing"
  7. "github.com/gophercloud/gophercloud"
  8. "github.com/gophercloud/gophercloud/acceptance/tools"
  9. "github.com/gophercloud/gophercloud/openstack/blockstorage/v3/snapshots"
  10. "github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
  11. )
  12. // CreateVolume will create a volume with a random name and size of 1GB. An
  13. // error will be returned if the volume was unable to be created.
  14. func CreateVolume(t *testing.T, client *gophercloud.ServiceClient) (*volumes.Volume, error) {
  15. if testing.Short() {
  16. t.Skip("Skipping test that requires volume creation in short mode.")
  17. }
  18. volumeName := tools.RandomString("ACPTTEST", 16)
  19. t.Logf("Attempting to create volume: %s", volumeName)
  20. createOpts := volumes.CreateOpts{
  21. Size: 1,
  22. Name: volumeName,
  23. }
  24. volume, err := volumes.Create(client, createOpts).Extract()
  25. if err != nil {
  26. return volume, err
  27. }
  28. err = volumes.WaitForStatus(client, volume.ID, "available", 60)
  29. if err != nil {
  30. return volume, err
  31. }
  32. return volume, nil
  33. }
  34. // DeleteVolume will delete a volume. A fatal error will occur if the volume
  35. // failed to be deleted. This works best when used as a deferred function.
  36. func DeleteVolume(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) {
  37. err := volumes.Delete(client, volume.ID).ExtractErr()
  38. if err != nil {
  39. t.Fatalf("Unable to delete volume %s: %v", volume.ID, err)
  40. }
  41. t.Logf("Deleted volume: %s", volume.ID)
  42. }
  43. // CreateSnapshot will create a snapshot of the specified volume.
  44. // Snapshot will be assigned a random name and description.
  45. func CreateSnapshot(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) (*snapshots.Snapshot, error) {
  46. if testing.Short() {
  47. t.Skip("Skipping test that requires snapshot creation in short mode.")
  48. }
  49. snapshotName := tools.RandomString("ACPTTEST", 16)
  50. snapshotDescription := tools.RandomString("ACPTTEST", 16)
  51. t.Logf("Attempting to create snapshot: %s", snapshotName)
  52. createOpts := snapshots.CreateOpts{
  53. VolumeID: volume.ID,
  54. Name: snapshotName,
  55. Description: snapshotDescription,
  56. }
  57. snapshot, err := snapshots.Create(client, createOpts).Extract()
  58. if err != nil {
  59. return snapshot, err
  60. }
  61. err = snapshots.WaitForStatus(client, snapshot.ID, "available", 60)
  62. if err != nil {
  63. return snapshot, err
  64. }
  65. return snapshot, nil
  66. }
  67. // DeleteSnapshot will delete a snapshot. A fatal error will occur if the
  68. // snapshot failed to be deleted.
  69. func DeleteSnapshot(t *testing.T, client *gophercloud.ServiceClient, snapshot *snapshots.Snapshot) {
  70. err := snapshots.Delete(client, snapshot.ID).ExtractErr()
  71. if err != nil {
  72. t.Fatalf("Unable to delete snapshot %s: %+v", snapshot.ID, err)
  73. }
  74. // Volumes can't be deleted until their snapshots have been,
  75. // so block up to 120 seconds for the snapshot to delete.
  76. err = gophercloud.WaitFor(120, func() (bool, error) {
  77. _, err := snapshots.Get(client, snapshot.ID).Extract()
  78. if err != nil {
  79. return true, nil
  80. }
  81. return false, nil
  82. })
  83. if err != nil {
  84. t.Fatalf("Error waiting for snapshot to delete: %v", err)
  85. }
  86. t.Logf("Deleted snapshot: %s", snapshot.ID)
  87. }