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