blob: 3d4c9f1c9ebfbc56719f06ee5aa8859b68645b05 [file] [log] [blame]
Joe Topjian68bed5f2016-08-10 15:30:57 -06001// 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.
4package v1
5
6import (
7 "testing"
8
9 "github.com/gophercloud/gophercloud"
10 "github.com/gophercloud/gophercloud/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.
19func 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.
47func 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.
75func 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.
98func 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.
123func 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.
135func 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}
143
144// PrintVolume will print a volume and all of its attributes.
145func PrintVolume(t *testing.T, volume *volumes.Volume) {
146 t.Logf("ID: %s", volume.ID)
147 t.Logf("Status: %s", volume.Status)
148 t.Logf("Name: %s", volume.Name)
149 t.Logf("AvailabilityZone: %s", volume.AvailabilityZone)
150 t.Logf("Bootable: %s", volume.Bootable)
151 t.Logf("CreatedAt: %v", volume.CreatedAt)
152 t.Logf("Description: %s", volume.Description)
153 t.Logf("VolumeType: %s", volume.VolumeType)
154 t.Logf("SnapshotID: %s", volume.SnapshotID)
155 t.Logf("SourceVolID: %s", volume.SourceVolID)
156 t.Logf("Size: %d", volume.Size)
157 t.Logf("Metadata: %#v", volume.Metadata)
158 t.Logf("Attachments: %#v", volume.Attachments)
159}
160
161// PrintVolumeType will print a volume type and all of its attributes.
162func PrintVolumeType(t *testing.T, volumeType *volumetypes.VolumeType) {
163 t.Logf("ID: %s", volumeType.ID)
164 t.Logf("Name: %s", volumeType.Name)
165 t.Logf("ExtraSpecs: %#v", volumeType.ExtraSpecs)
166}
167
168// PrintSnapshot will print a snapshot and all of its attributes.
169func PrintSnapshot(t *testing.T, snapshot *snapshots.Snapshot) {
170 t.Logf("ID: %s", snapshot.ID)
171 t.Logf("Status: %s", snapshot.Status)
172 t.Logf("Name: %s", snapshot.Name)
173 t.Logf("AvailabilityZone: %s", snapshot.AvailabilityZone)
174 t.Logf("Bootable: %s", snapshot.Bootable)
175 t.Logf("CreatedAt: %v", snapshot.CreatedAt)
176 t.Logf("Description: %s", snapshot.Description)
177 t.Logf("VolumeType: %s", snapshot.VolumeType)
178 t.Logf("SnapshotID: %s", snapshot.SnapshotID)
179 t.Logf("VolumeID: %s", snapshot.VolumeID)
180 t.Logf("Size: %d", snapshot.Size)
181 t.Logf("Metadata: %#v", snapshot.Metadata)
182 t.Logf("Attachments: %#v", snapshot.Attachments)
183}