PageRenderTime 537ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 142 lines | 100 code | 27 blank | 15 comment | 25 complexity | d437d041ff456fec790368385970fb2c MD5 | raw file
  1. // Package v2 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 v2
  5. import (
  6. "testing"
  7. "github.com/gophercloud/gophercloud"
  8. "github.com/gophercloud/gophercloud/acceptance/clients"
  9. "github.com/gophercloud/gophercloud/acceptance/tools"
  10. "github.com/gophercloud/gophercloud/openstack/blockstorage/v2/snapshots"
  11. "github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
  12. )
  13. // CreateVolume will create a volume with a random name and size of 1GB. An
  14. // error will be returned if the volume was unable to be created.
  15. func CreateVolume(t *testing.T, client *gophercloud.ServiceClient) (*volumes.Volume, error) {
  16. if testing.Short() {
  17. t.Skip("Skipping test that requires volume creation in short mode.")
  18. }
  19. volumeName := tools.RandomString("ACPTTEST", 16)
  20. t.Logf("Attempting to create volume: %s", volumeName)
  21. createOpts := volumes.CreateOpts{
  22. Size: 1,
  23. Name: volumeName,
  24. }
  25. volume, err := volumes.Create(client, createOpts).Extract()
  26. if err != nil {
  27. return volume, err
  28. }
  29. err = volumes.WaitForStatus(client, volume.ID, "available", 60)
  30. if err != nil {
  31. return volume, err
  32. }
  33. return volume, nil
  34. }
  35. // CreateVolumeFromImage will create a volume from with a random name and size of
  36. // 1GB. An error will be returned if the volume was unable to be created.
  37. func CreateVolumeFromImage(t *testing.T, client *gophercloud.ServiceClient) (*volumes.Volume, error) {
  38. if testing.Short() {
  39. t.Skip("Skipping test that requires volume creation in short mode.")
  40. }
  41. choices, err := clients.AcceptanceTestChoicesFromEnv()
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. volumeName := tools.RandomString("ACPTTEST", 16)
  46. t.Logf("Attempting to create volume: %s", volumeName)
  47. createOpts := volumes.CreateOpts{
  48. Size: 1,
  49. Name: volumeName,
  50. ImageID: choices.ImageID,
  51. }
  52. volume, err := volumes.Create(client, createOpts).Extract()
  53. if err != nil {
  54. return volume, err
  55. }
  56. err = volumes.WaitForStatus(client, volume.ID, "available", 60)
  57. if err != nil {
  58. return volume, err
  59. }
  60. return volume, nil
  61. }
  62. // DeleteVolume will delete a volume. A fatal error will occur if the volume
  63. // failed to be deleted. This works best when used as a deferred function.
  64. func DeleteVolume(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) {
  65. err := volumes.Delete(client, volume.ID).ExtractErr()
  66. if err != nil {
  67. t.Fatalf("Unable to delete volume %s: %v", volume.ID, err)
  68. }
  69. t.Logf("Deleted volume: %s", volume.ID)
  70. }
  71. // CreateSnapshot will create a snapshot of the specified volume.
  72. // Snapshot will be assigned a random name and description.
  73. func CreateSnapshot(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) (*snapshots.Snapshot, error) {
  74. if testing.Short() {
  75. t.Skip("Skipping test that requires snapshot creation in short mode.")
  76. }
  77. snapshotName := tools.RandomString("ACPTTEST", 16)
  78. snapshotDescription := tools.RandomString("ACPTTEST", 16)
  79. t.Logf("Attempting to create snapshot: %s", snapshotName)
  80. createOpts := snapshots.CreateOpts{
  81. VolumeID: volume.ID,
  82. Name: snapshotName,
  83. Description: snapshotDescription,
  84. }
  85. snapshot, err := snapshots.Create(client, createOpts).Extract()
  86. if err != nil {
  87. return snapshot, err
  88. }
  89. err = snapshots.WaitForStatus(client, snapshot.ID, "available", 60)
  90. if err != nil {
  91. return snapshot, err
  92. }
  93. return snapshot, nil
  94. }
  95. // DeleteSnapshot will delete a snapshot. A fatal error will occur if the
  96. // snapshot failed to be deleted.
  97. func DeleteSnapshot(t *testing.T, client *gophercloud.ServiceClient, snapshot *snapshots.Snapshot) {
  98. err := snapshots.Delete(client, snapshot.ID).ExtractErr()
  99. if err != nil {
  100. t.Fatalf("Unable to delete snapshot %s: %+v", snapshot.ID, err)
  101. }
  102. // Volumes can't be deleted until their snapshots have been,
  103. // so block up to 120 seconds for the snapshot to delete.
  104. err = gophercloud.WaitFor(120, func() (bool, error) {
  105. _, err := snapshots.Get(client, snapshot.ID).Extract()
  106. if err != nil {
  107. return true, nil
  108. }
  109. return false, nil
  110. })
  111. if err != nil {
  112. t.Fatalf("Error waiting for snapshot to delete: %v", err)
  113. }
  114. t.Logf("Deleted snapshot: %s", snapshot.ID)
  115. }