blob: e007c2a3c5a573d970924b29dc5b815321e70589 [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"
10 "github.com/gophercloud/gophercloud/acceptance/tools"
11 "github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
12)
13
14// CreateVolume will create a volume with a random name and size of 1GB. An
15// error will be returned if the volume was unable to be created.
16func CreateVolume(t *testing.T, client *gophercloud.ServiceClient) (*volumes.Volume, error) {
17 if testing.Short() {
18 t.Skip("Skipping test that requires volume creation in short mode.")
19 }
20
21 volumeName := tools.RandomString("ACPTTEST", 16)
22 t.Logf("Attempting to create volume: %s", volumeName)
23
24 createOpts := volumes.CreateOpts{
25 Size: 1,
26 Name: volumeName,
27 }
28
29 volume, err := volumes.Create(client, createOpts).Extract()
30 if err != nil {
31 return volume, err
32 }
33
34 err = volumes.WaitForStatus(client, volume.ID, "available", 60)
35 if err != nil {
36 return volume, err
37 }
38
39 return volume, nil
40}
41
42// DeleteVolume will delete a volume. A fatal error will occur if the volume
43// failed to be deleted. This works best when used as a deferred function.
44func DeleteVolume(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) {
45 err := volumes.Delete(client, volume.ID).ExtractErr()
46 if err != nil {
47 t.Fatalf("Unable to delete volume %s: %v", volume.ID, err)
48 }
49
50 t.Logf("Deleted volume: %s", volume.ID)
51}
52
53// PrintVolume will print a volume and all of its attributes.
54func PrintVolume(t *testing.T, volume *volumes.Volume) {
55 t.Logf("ID: %s", volume.ID)
56 t.Logf("Status: %s", volume.Status)
57 t.Logf("Size: %d", volume.Size)
58 t.Logf("AvailabilityZone: %s", volume.AvailabilityZone)
59 t.Logf("CreatedAt: %v", volume.CreatedAt)
60 t.Logf("UpdatedAt: %v", volume.CreatedAt)
61 t.Logf("Attachments: %#v", volume.Attachments)
62 t.Logf("Name: %s", volume.Name)
63 t.Logf("Description: %s", volume.Description)
64 t.Logf("VolumeType: %s", volume.VolumeType)
65 t.Logf("SnapshotID: %s", volume.SnapshotID)
66 t.Logf("SourceVolID: %s", volume.SourceVolID)
67 t.Logf("Metadata: %#v", volume.Metadata)
68 t.Logf("UserID: %s", volume.UserID)
69 t.Logf("Bootable: %s", volume.Bootable)
70 t.Logf("Encrypted: %s", volume.Encrypted)
71 t.Logf("ReplicationStatus: %s", volume.ReplicationStatus)
72 t.Logf("ConsistencyGroupID: %s", volume.ConsistencyGroupID)
73 t.Logf("Multiattach: %t", volume.Multiattach)
74}