PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 173 lines | 113 code | 42 blank | 18 comment | 29 complexity | ba44e9c540a95662dba4a7e4d0f5d47d MD5 | raw file
  1. // Package extensions contains common functions for creating block storage
  2. // resources that are extensions of the block storage API. See the `*_test.go`
  3. // files for example usages.
  4. package extensions
  5. import (
  6. "testing"
  7. "github.com/gophercloud/gophercloud"
  8. "github.com/gophercloud/gophercloud/acceptance/tools"
  9. "github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumeactions"
  10. "github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
  11. "github.com/gophercloud/gophercloud/openstack/compute/v2/images"
  12. "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
  13. )
  14. // CreateUploadImage will upload volume it as volume-baked image. An name of new image or err will be
  15. // returned
  16. func CreateUploadImage(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) (volumeactions.VolumeImage, error) {
  17. if testing.Short() {
  18. t.Skip("Skipping test that requires volume-backed image uploading in short mode.")
  19. }
  20. imageName := tools.RandomString("ACPTTEST", 16)
  21. uploadImageOpts := volumeactions.UploadImageOpts{
  22. ImageName: imageName,
  23. Force: true,
  24. }
  25. volumeImage, err := volumeactions.UploadImage(client, volume.ID, uploadImageOpts).Extract()
  26. if err != nil {
  27. return volumeImage, err
  28. }
  29. t.Logf("Uploading volume %s as volume-backed image %s", volume.ID, imageName)
  30. if err := volumes.WaitForStatus(client, volume.ID, "available", 60); err != nil {
  31. return volumeImage, err
  32. }
  33. t.Logf("Uploaded volume %s as volume-backed image %s", volume.ID, imageName)
  34. return volumeImage, nil
  35. }
  36. // DeleteUploadedImage deletes uploaded image. An error will be returned
  37. // if the deletion request failed.
  38. func DeleteUploadedImage(t *testing.T, client *gophercloud.ServiceClient, imageName string) error {
  39. if testing.Short() {
  40. t.Skip("Skipping test that requires volume-backed image removing in short mode.")
  41. }
  42. t.Logf("Getting image id for image name %s", imageName)
  43. imageID, err := images.IDFromName(client, imageName)
  44. if err != nil {
  45. return err
  46. }
  47. t.Logf("Removing image %s", imageID)
  48. err = images.Delete(client, imageID).ExtractErr()
  49. if err != nil {
  50. return err
  51. }
  52. return nil
  53. }
  54. // CreateVolumeAttach will attach a volume to an instance. An error will be
  55. // returned if the attachment failed.
  56. func CreateVolumeAttach(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume, server *servers.Server) error {
  57. if testing.Short() {
  58. t.Skip("Skipping test that requires volume attachment in short mode.")
  59. }
  60. attachOpts := volumeactions.AttachOpts{
  61. MountPoint: "/mnt",
  62. Mode: "rw",
  63. InstanceUUID: server.ID,
  64. }
  65. t.Logf("Attempting to attach volume %s to server %s", volume.ID, server.ID)
  66. if err := volumeactions.Attach(client, volume.ID, attachOpts).ExtractErr(); err != nil {
  67. return err
  68. }
  69. if err := volumes.WaitForStatus(client, volume.ID, "in-use", 60); err != nil {
  70. return err
  71. }
  72. t.Logf("Attached volume %s to server %s", volume.ID, server.ID)
  73. return nil
  74. }
  75. // CreateVolumeReserve creates a volume reservation. An error will be returned
  76. // if the reservation failed.
  77. func CreateVolumeReserve(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) error {
  78. if testing.Short() {
  79. t.Skip("Skipping test that requires volume reservation in short mode.")
  80. }
  81. t.Logf("Attempting to reserve volume %s", volume.ID)
  82. if err := volumeactions.Reserve(client, volume.ID).ExtractErr(); err != nil {
  83. return err
  84. }
  85. t.Logf("Reserved volume %s", volume.ID)
  86. return nil
  87. }
  88. // DeleteVolumeAttach will detach a volume from an instance. A fatal error will
  89. // occur if the snapshot failed to be deleted. This works best when used as a
  90. // deferred function.
  91. func DeleteVolumeAttach(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) {
  92. t.Logf("Attepting to detach volume volume: %s", volume.ID)
  93. detachOpts := volumeactions.DetachOpts{
  94. AttachmentID: volume.Attachments[0].AttachmentID,
  95. }
  96. if err := volumeactions.Detach(client, volume.ID, detachOpts).ExtractErr(); err != nil {
  97. t.Fatalf("Unable to detach volume %s: %v", volume.ID, err)
  98. }
  99. if err := volumes.WaitForStatus(client, volume.ID, "available", 60); err != nil {
  100. t.Fatalf("Volume %s failed to become unavailable in 60 seconds: %v", volume.ID, err)
  101. }
  102. t.Logf("Detached volume: %s", volume.ID)
  103. }
  104. // DeleteVolumeReserve deletes a volume reservation. A fatal error will occur
  105. // if the deletion request failed. This works best when used as a deferred
  106. // function.
  107. func DeleteVolumeReserve(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) {
  108. if testing.Short() {
  109. t.Skip("Skipping test that requires volume reservation in short mode.")
  110. }
  111. t.Logf("Attempting to unreserve volume %s", volume.ID)
  112. if err := volumeactions.Unreserve(client, volume.ID).ExtractErr(); err != nil {
  113. t.Fatalf("Unable to unreserve volume %s: %v", volume.ID, err)
  114. }
  115. t.Logf("Unreserved volume %s", volume.ID)
  116. }
  117. // ExtendVolumeSize will extend the size of a volume.
  118. func ExtendVolumeSize(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) error {
  119. t.Logf("Attempting to extend the size of volume %s", volume.ID)
  120. extendOpts := volumeactions.ExtendSizeOpts{
  121. NewSize: 2,
  122. }
  123. err := volumeactions.ExtendSize(client, volume.ID, extendOpts).ExtractErr()
  124. if err != nil {
  125. return err
  126. }
  127. if err := volumes.WaitForStatus(client, volume.ID, "available", 60); err != nil {
  128. return err
  129. }
  130. return nil
  131. }