PageRenderTime 186ms CodeModel.GetById 36ms app.highlight 84ms RepoModel.GetById 35ms app.codeStats 0ms

/vendor/github.com/gophercloud/gophercloud/acceptance/openstack/blockstorage/v3/blockstorage.go

https://gitlab.com/unofficial-mirrors/openshift-origin
Go | 107 lines | 74 code | 20 blank | 13 comment | 18 complexity | e185431926215d32aae9ecf07f3b61ad MD5 | raw file
  1// Package v3 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 v3
  5
  6import (
  7	"testing"
  8
  9	"github.com/gophercloud/gophercloud"
 10	"github.com/gophercloud/gophercloud/acceptance/tools"
 11	"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/snapshots"
 12	"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/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
 43// DeleteVolume will delete a volume. A fatal error will occur if the volume
 44// failed to be deleted. This works best when used as a deferred function.
 45func DeleteVolume(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) {
 46	err := volumes.Delete(client, volume.ID).ExtractErr()
 47	if err != nil {
 48		t.Fatalf("Unable to delete volume %s: %v", volume.ID, err)
 49	}
 50
 51	t.Logf("Deleted volume: %s", volume.ID)
 52}
 53
 54// CreateSnapshot will create a snapshot of the specified volume.
 55// Snapshot will be assigned a random name and description.
 56func CreateSnapshot(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) (*snapshots.Snapshot, error) {
 57	if testing.Short() {
 58		t.Skip("Skipping test that requires snapshot creation in short mode.")
 59	}
 60
 61	snapshotName := tools.RandomString("ACPTTEST", 16)
 62	snapshotDescription := tools.RandomString("ACPTTEST", 16)
 63	t.Logf("Attempting to create snapshot: %s", snapshotName)
 64
 65	createOpts := snapshots.CreateOpts{
 66		VolumeID:    volume.ID,
 67		Name:        snapshotName,
 68		Description: snapshotDescription,
 69	}
 70
 71	snapshot, err := snapshots.Create(client, createOpts).Extract()
 72	if err != nil {
 73		return snapshot, err
 74	}
 75
 76	err = snapshots.WaitForStatus(client, snapshot.ID, "available", 60)
 77	if err != nil {
 78		return snapshot, err
 79	}
 80
 81	return snapshot, nil
 82}
 83
 84// DeleteSnapshot will delete a snapshot. A fatal error will occur if the
 85// snapshot failed to be deleted.
 86func DeleteSnapshot(t *testing.T, client *gophercloud.ServiceClient, snapshot *snapshots.Snapshot) {
 87	err := snapshots.Delete(client, snapshot.ID).ExtractErr()
 88	if err != nil {
 89		t.Fatalf("Unable to delete snapshot %s: %+v", snapshot.ID, err)
 90	}
 91
 92	// Volumes can't be deleted until their snapshots have been,
 93	// so block up to 120 seconds for the snapshot to delete.
 94	err = gophercloud.WaitFor(120, func() (bool, error) {
 95		_, err := snapshots.Get(client, snapshot.ID).Extract()
 96		if err != nil {
 97			return true, nil
 98		}
 99
100		return false, nil
101	})
102	if err != nil {
103		t.Fatalf("Error waiting for snapshot to delete: %v", err)
104	}
105
106	t.Logf("Deleted snapshot: %s", snapshot.ID)
107}