Joe Topjian | 68bed5f | 2016-08-10 15:30:57 -0600 | [diff] [blame] | 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 | |
| 6 | import ( |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/gophercloud/gophercloud" |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/acceptance/clients" |
Joe Topjian | 68bed5f | 2016-08-10 15:30:57 -0600 | [diff] [blame] | 11 | "github.com/gophercloud/gophercloud/acceptance/tools" |
| 12 | "github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes" |
| 13 | ) |
| 14 | |
| 15 | // CreateVolume will create a volume with a random name and size of 1GB. An |
| 16 | // error will be returned if the volume was unable to be created. |
| 17 | func CreateVolume(t *testing.T, client *gophercloud.ServiceClient) (*volumes.Volume, error) { |
| 18 | if testing.Short() { |
| 19 | t.Skip("Skipping test that requires volume creation in short mode.") |
| 20 | } |
| 21 | |
| 22 | volumeName := tools.RandomString("ACPTTEST", 16) |
| 23 | t.Logf("Attempting to create volume: %s", volumeName) |
| 24 | |
| 25 | createOpts := volumes.CreateOpts{ |
| 26 | Size: 1, |
| 27 | Name: volumeName, |
| 28 | } |
| 29 | |
| 30 | volume, err := volumes.Create(client, createOpts).Extract() |
| 31 | if err != nil { |
| 32 | return volume, err |
| 33 | } |
| 34 | |
| 35 | err = volumes.WaitForStatus(client, volume.ID, "available", 60) |
| 36 | if err != nil { |
| 37 | return volume, err |
| 38 | } |
| 39 | |
| 40 | return volume, nil |
| 41 | } |
| 42 | |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 43 | // CreateVolumeFromImage will create a volume from with a random name and size of |
| 44 | // 1GB. An error will be returned if the volume was unable to be created. |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 45 | func CreateVolumeFromImage(t *testing.T, client *gophercloud.ServiceClient) (*volumes.Volume, error) { |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 46 | if testing.Short() { |
| 47 | t.Skip("Skipping test that requires volume creation in short mode.") |
| 48 | } |
| 49 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 50 | choices, err := clients.AcceptanceTestChoicesFromEnv() |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | |
Joe Topjian | f1f4041 | 2016-10-13 17:42:25 -0600 | [diff] [blame] | 55 | volumeName := tools.RandomString("ACPTTEST", 16) |
| 56 | t.Logf("Attempting to create volume: %s", volumeName) |
| 57 | |
| 58 | createOpts := volumes.CreateOpts{ |
| 59 | Size: 1, |
| 60 | Name: volumeName, |
| 61 | ImageID: choices.ImageID, |
| 62 | } |
| 63 | |
| 64 | volume, err := volumes.Create(client, createOpts).Extract() |
| 65 | if err != nil { |
| 66 | return volume, err |
| 67 | } |
| 68 | |
| 69 | err = volumes.WaitForStatus(client, volume.ID, "available", 60) |
| 70 | if err != nil { |
| 71 | return volume, err |
| 72 | } |
| 73 | |
| 74 | return volume, nil |
| 75 | } |
| 76 | |
Joe Topjian | 68bed5f | 2016-08-10 15:30:57 -0600 | [diff] [blame] | 77 | // DeleteVolume will delete a volume. A fatal error will occur if the volume |
| 78 | // failed to be deleted. This works best when used as a deferred function. |
| 79 | func DeleteVolume(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) { |
| 80 | err := volumes.Delete(client, volume.ID).ExtractErr() |
| 81 | if err != nil { |
| 82 | t.Fatalf("Unable to delete volume %s: %v", volume.ID, err) |
| 83 | } |
| 84 | |
| 85 | t.Logf("Deleted volume: %s", volume.ID) |
| 86 | } |