blob: 39fb7dab04eb4a47a4ea30e602e543f81fb31c9c [file] [log] [blame]
Joe Topjian68bed5f2016-08-10 15:30:57 -06001// 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.
4package v2
5
6import (
7 "testing"
8
9 "github.com/gophercloud/gophercloud"
Joe Topjianf1f40412016-10-13 17:42:25 -060010 "github.com/gophercloud/gophercloud/acceptance/clients"
Joe Topjian68bed5f2016-08-10 15:30:57 -060011 "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.
17func 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 Topjianf1f40412016-10-13 17:42:25 -060043// 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 Topjian66a046c2017-01-19 22:07:26 -070045func CreateVolumeFromImage(t *testing.T, client *gophercloud.ServiceClient) (*volumes.Volume, error) {
Joe Topjianf1f40412016-10-13 17:42:25 -060046 if testing.Short() {
47 t.Skip("Skipping test that requires volume creation in short mode.")
48 }
49
Joe Topjian66a046c2017-01-19 22:07:26 -070050 choices, err := clients.AcceptanceTestChoicesFromEnv()
51 if err != nil {
52 t.Fatal(err)
53 }
54
Joe Topjianf1f40412016-10-13 17:42:25 -060055 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 Topjian68bed5f2016-08-10 15:30:57 -060077// 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.
79func 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}