blob: 555bdcd2485e8525cce736431198606372ff5a3e [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.
45func CreateVolumeFromImage(t *testing.T, client *gophercloud.ServiceClient, choices *clients.AcceptanceTestChoices) (*volumes.Volume, error) {
46 if testing.Short() {
47 t.Skip("Skipping test that requires volume creation in short mode.")
48 }
49
50 volumeName := tools.RandomString("ACPTTEST", 16)
51 t.Logf("Attempting to create volume: %s", volumeName)
52
53 createOpts := volumes.CreateOpts{
54 Size: 1,
55 Name: volumeName,
56 ImageID: choices.ImageID,
57 }
58
59 volume, err := volumes.Create(client, createOpts).Extract()
60 if err != nil {
61 return volume, err
62 }
63
64 err = volumes.WaitForStatus(client, volume.ID, "available", 60)
65 if err != nil {
66 return volume, err
67 }
68
69 return volume, nil
70}
71
Joe Topjian68bed5f2016-08-10 15:30:57 -060072// DeleteVolume will delete a volume. A fatal error will occur if the volume
73// failed to be deleted. This works best when used as a deferred function.
74func DeleteVolume(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) {
75 err := volumes.Delete(client, volume.ID).ExtractErr()
76 if err != nil {
77 t.Fatalf("Unable to delete volume %s: %v", volume.ID, err)
78 }
79
80 t.Logf("Deleted volume: %s", volume.ID)
81}
82
83// PrintVolume will print a volume and all of its attributes.
84func PrintVolume(t *testing.T, volume *volumes.Volume) {
85 t.Logf("ID: %s", volume.ID)
86 t.Logf("Status: %s", volume.Status)
87 t.Logf("Size: %d", volume.Size)
88 t.Logf("AvailabilityZone: %s", volume.AvailabilityZone)
89 t.Logf("CreatedAt: %v", volume.CreatedAt)
90 t.Logf("UpdatedAt: %v", volume.CreatedAt)
91 t.Logf("Attachments: %#v", volume.Attachments)
92 t.Logf("Name: %s", volume.Name)
93 t.Logf("Description: %s", volume.Description)
94 t.Logf("VolumeType: %s", volume.VolumeType)
95 t.Logf("SnapshotID: %s", volume.SnapshotID)
96 t.Logf("SourceVolID: %s", volume.SourceVolID)
97 t.Logf("Metadata: %#v", volume.Metadata)
98 t.Logf("UserID: %s", volume.UserID)
99 t.Logf("Bootable: %s", volume.Bootable)
100 t.Logf("Encrypted: %s", volume.Encrypted)
101 t.Logf("ReplicationStatus: %s", volume.ReplicationStatus)
102 t.Logf("ConsistencyGroupID: %s", volume.ConsistencyGroupID)
103 t.Logf("Multiattach: %t", volume.Multiattach)
104}