| Joe Topjian | 68bed5f | 2016-08-10 15:30:57 -0600 | [diff] [blame] | 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 |  | 
| Krzysztof Szukiełojć | 3f41d08 | 2017-05-07 14:43:06 +0200 | [diff] [blame] | 9 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" | 
| Krzysztof Szukiełojć | 24a29ce | 2017-05-07 14:24:02 +0200 | [diff] [blame] | 10 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/acceptance/tools" | 
|  | 11 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/blockstorage/v1/snapshots" | 
|  | 12 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/blockstorage/v1/volumes" | 
|  | 13 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/blockstorage/v1/volumetypes" | 
| Joe Topjian | 68bed5f | 2016-08-10 15:30:57 -0600 | [diff] [blame] | 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 | t.Logf("Attempting to create volume: %s", volumeName) | 
|  | 54 |  | 
|  | 55 | createOpts := volumes.CreateOpts{ | 
|  | 56 | Size: 1, | 
|  | 57 | Name: volumeName, | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | volume, err := volumes.Create(client, createOpts).Extract() | 
|  | 61 | if err != nil { | 
|  | 62 | return volume, err | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | err = volumes.WaitForStatus(client, volume.ID, "available", 60) | 
|  | 66 | if err != nil { | 
|  | 67 | return volume, err | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | return volume, nil | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | // CreateVolumeType will create a volume type with a random name. An error will | 
|  | 74 | // be returned if the volume type was unable to be created. | 
|  | 75 | func CreateVolumeType(t *testing.T, client *gophercloud.ServiceClient) (*volumetypes.VolumeType, error) { | 
|  | 76 | volumeTypeName := tools.RandomString("ACPTTEST", 16) | 
|  | 77 | t.Logf("Attempting to create volume type: %s", volumeTypeName) | 
|  | 78 |  | 
|  | 79 | createOpts := volumetypes.CreateOpts{ | 
|  | 80 | Name: volumeTypeName, | 
|  | 81 | ExtraSpecs: map[string]interface{}{ | 
|  | 82 | "capabilities": "ssd", | 
|  | 83 | "priority":     3, | 
|  | 84 | }, | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | volumeType, err := volumetypes.Create(client, createOpts).Extract() | 
|  | 88 | if err != nil { | 
|  | 89 | return volumeType, err | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | return volumeType, nil | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | // DeleteSnapshot will delete a snapshot. A fatal error will occur if the | 
|  | 96 | // snapshot failed to be deleted. This works best when used as a deferred | 
|  | 97 | // function. | 
|  | 98 | func DeleteSnapshotshot(t *testing.T, client *gophercloud.ServiceClient, snapshot *snapshots.Snapshot) { | 
|  | 99 | err := snapshots.Delete(client, snapshot.ID).ExtractErr() | 
|  | 100 | if err != nil { | 
|  | 101 | t.Fatalf("Unable to delete snapshot %s: %v", snapshot.ID, err) | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | // Volumes can't be deleted until their snapshots have been, | 
|  | 105 | // so block up to 120 seconds for the snapshot to delete. | 
|  | 106 | err = gophercloud.WaitFor(120, func() (bool, error) { | 
|  | 107 | _, err := snapshots.Get(client, snapshot.ID).Extract() | 
|  | 108 | if err != nil { | 
|  | 109 | return true, nil | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | return false, nil | 
|  | 113 | }) | 
|  | 114 | if err != nil { | 
|  | 115 | t.Fatalf("Unable to wait for snapshot to delete: %v", err) | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | t.Logf("Deleted snapshot: %s", snapshot.ID) | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | // DeleteVolume will delete a volume. A fatal error will occur if the volume | 
|  | 122 | // failed to be deleted. This works best when used as a deferred function. | 
|  | 123 | func DeleteVolume(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) { | 
|  | 124 | err := volumes.Delete(client, volume.ID).ExtractErr() | 
|  | 125 | if err != nil { | 
|  | 126 | t.Fatalf("Unable to delete volume %s: %v", volume.ID, err) | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | t.Logf("Deleted volume: %s", volume.ID) | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | // DeleteVolumeType will delete a volume type. A fatal error will occur if the | 
|  | 133 | // volume type failed to be deleted. This works best when used as a deferred | 
|  | 134 | // function. | 
|  | 135 | func DeleteVolumeType(t *testing.T, client *gophercloud.ServiceClient, volumeType *volumetypes.VolumeType) { | 
|  | 136 | err := volumetypes.Delete(client, volumeType.ID).ExtractErr() | 
|  | 137 | if err != nil { | 
|  | 138 | t.Fatalf("Unable to delete volume type %s: %v", volumeType.ID, err) | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | t.Logf("Deleted volume type: %s", volumeType.ID) | 
|  | 142 | } |