PageRenderTime 89ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

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