/
extensions.go
254 lines (195 loc) · 7.67 KB
/
extensions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Package extensions contains common functions for creating block storage
// resources that are extensions of the block storage API. See the `*_test.go`
// files for example usages.
package extensions
import (
"testing"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/backups"
"github.com/gophercloud/gophercloud/openstack/blockstorage/extensions/volumeactions"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
"github.com/gophercloud/gophercloud/openstack/compute/v2/images"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
th "github.com/gophercloud/gophercloud/testhelper"
)
// CreateUploadImage will upload volume it as volume-baked image. An name of new image or err will be
// returned
func CreateUploadImage(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) (volumeactions.VolumeImage, error) {
if testing.Short() {
t.Skip("Skipping test that requires volume-backed image uploading in short mode.")
}
imageName := tools.RandomString("ACPTTEST", 16)
uploadImageOpts := volumeactions.UploadImageOpts{
ImageName: imageName,
Force: true,
}
volumeImage, err := volumeactions.UploadImage(client, volume.ID, uploadImageOpts).Extract()
if err != nil {
return volumeImage, err
}
t.Logf("Uploading volume %s as volume-backed image %s", volume.ID, imageName)
if err := volumes.WaitForStatus(client, volume.ID, "available", 60); err != nil {
return volumeImage, err
}
t.Logf("Uploaded volume %s as volume-backed image %s", volume.ID, imageName)
return volumeImage, nil
}
// DeleteUploadedImage deletes uploaded image. An error will be returned
// if the deletion request failed.
func DeleteUploadedImage(t *testing.T, client *gophercloud.ServiceClient, imageName string) error {
if testing.Short() {
t.Skip("Skipping test that requires volume-backed image removing in short mode.")
}
t.Logf("Getting image id for image name %s", imageName)
imageID, err := images.IDFromName(client, imageName)
if err != nil {
return err
}
t.Logf("Removing image %s", imageID)
err = images.Delete(client, imageID).ExtractErr()
if err != nil {
return err
}
return nil
}
// CreateVolumeAttach will attach a volume to an instance. An error will be
// returned if the attachment failed.
func CreateVolumeAttach(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume, server *servers.Server) error {
if testing.Short() {
t.Skip("Skipping test that requires volume attachment in short mode.")
}
attachOpts := volumeactions.AttachOpts{
MountPoint: "/mnt",
Mode: "rw",
InstanceUUID: server.ID,
}
t.Logf("Attempting to attach volume %s to server %s", volume.ID, server.ID)
if err := volumeactions.Attach(client, volume.ID, attachOpts).ExtractErr(); err != nil {
return err
}
if err := volumes.WaitForStatus(client, volume.ID, "in-use", 60); err != nil {
return err
}
t.Logf("Attached volume %s to server %s", volume.ID, server.ID)
return nil
}
// CreateVolumeReserve creates a volume reservation. An error will be returned
// if the reservation failed.
func CreateVolumeReserve(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) error {
if testing.Short() {
t.Skip("Skipping test that requires volume reservation in short mode.")
}
t.Logf("Attempting to reserve volume %s", volume.ID)
if err := volumeactions.Reserve(client, volume.ID).ExtractErr(); err != nil {
return err
}
t.Logf("Reserved volume %s", volume.ID)
return nil
}
// DeleteVolumeAttach will detach a volume from an instance. A fatal error will
// occur if the snapshot failed to be deleted. This works best when used as a
// deferred function.
func DeleteVolumeAttach(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) {
t.Logf("Attepting to detach volume volume: %s", volume.ID)
detachOpts := volumeactions.DetachOpts{
AttachmentID: volume.Attachments[0].AttachmentID,
}
if err := volumeactions.Detach(client, volume.ID, detachOpts).ExtractErr(); err != nil {
t.Fatalf("Unable to detach volume %s: %v", volume.ID, err)
}
if err := volumes.WaitForStatus(client, volume.ID, "available", 60); err != nil {
t.Fatalf("Volume %s failed to become unavailable in 60 seconds: %v", volume.ID, err)
}
t.Logf("Detached volume: %s", volume.ID)
}
// DeleteVolumeReserve deletes a volume reservation. A fatal error will occur
// if the deletion request failed. This works best when used as a deferred
// function.
func DeleteVolumeReserve(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) {
if testing.Short() {
t.Skip("Skipping test that requires volume reservation in short mode.")
}
t.Logf("Attempting to unreserve volume %s", volume.ID)
if err := volumeactions.Unreserve(client, volume.ID).ExtractErr(); err != nil {
t.Fatalf("Unable to unreserve volume %s: %v", volume.ID, err)
}
t.Logf("Unreserved volume %s", volume.ID)
}
// ExtendVolumeSize will extend the size of a volume.
func ExtendVolumeSize(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) error {
t.Logf("Attempting to extend the size of volume %s", volume.ID)
extendOpts := volumeactions.ExtendSizeOpts{
NewSize: 2,
}
err := volumeactions.ExtendSize(client, volume.ID, extendOpts).ExtractErr()
if err != nil {
return err
}
if err := volumes.WaitForStatus(client, volume.ID, "available", 60); err != nil {
return err
}
return nil
}
// SetImageMetadata will apply the metadata to a volume.
func SetImageMetadata(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) error {
t.Logf("Attempting to apply image metadata to volume %s", volume.ID)
imageMetadataOpts := volumeactions.ImageMetadataOpts{
Metadata: map[string]string{
"image_name": "testimage",
},
}
err := volumeactions.SetImageMetadata(client, volume.ID, imageMetadataOpts).ExtractErr()
if err != nil {
return err
}
return nil
}
// CreateBackup will create a backup based on a volume. An error will be
// will be returned if the backup could not be created.
func CreateBackup(t *testing.T, client *gophercloud.ServiceClient, volumeID string) (*backups.Backup, error) {
t.Logf("Attempting to create a backup of volume %s", volumeID)
backupName := tools.RandomString("ACPTTEST", 16)
createOpts := backups.CreateOpts{
VolumeID: volumeID,
Name: backupName,
}
backup, err := backups.Create(client, createOpts).Extract()
if err != nil {
return nil, err
}
err = WaitForBackupStatus(client, backup.ID, "available", 120)
if err != nil {
return nil, err
}
backup, err = backups.Get(client, backup.ID).Extract()
if err != nil {
return nil, err
}
t.Logf("Successfully created backup %s", backup.ID)
tools.PrintResource(t, backup)
th.AssertEquals(t, backup.Name, backupName)
return backup, nil
}
// DeleteBackup will delete a backup. A fatal error will occur if the backup
// could not be deleted. This works best when used as a deferred function.
func DeleteBackup(t *testing.T, client *gophercloud.ServiceClient, backupID string) {
if err := backups.Delete(client, backupID).ExtractErr(); err != nil {
t.Fatalf("Unable to delete backup %s: %s", backupID, err)
}
t.Logf("Deleted backup %s", backupID)
}
// WaitForBackupStatus will continually poll a backup, checking for a particular
// status. It will do this for the amount of seconds defined.
func WaitForBackupStatus(client *gophercloud.ServiceClient, id, status string, secs int) error {
return gophercloud.WaitFor(secs, func() (bool, error) {
current, err := backups.Get(client, id).Extract()
if err != nil {
return false, err
}
if current.Status == status {
return true, nil
}
return false, nil
})
}