github.com/gophercloud/gophercloud@v1.11.0/internal/acceptance/openstack/blockstorage/v2/blockstorage.go (about)

     1  // 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.
     4  package v2
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/gophercloud/gophercloud"
    10  	"github.com/gophercloud/gophercloud/internal/acceptance/clients"
    11  	"github.com/gophercloud/gophercloud/internal/acceptance/tools"
    12  	"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/snapshots"
    13  	"github.com/gophercloud/gophercloud/openstack/blockstorage/v2/volumes"
    14  	th "github.com/gophercloud/gophercloud/testhelper"
    15  )
    16  
    17  // CreateSnapshot will create a snapshot of the specified volume.
    18  // Snapshot will be assigned a random name and description.
    19  func CreateSnapshot(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) (*snapshots.Snapshot, error) {
    20  	snapshotName := tools.RandomString("ACPTTEST", 16)
    21  	snapshotDescription := tools.RandomString("ACPTTEST", 16)
    22  	t.Logf("Attempting to create snapshot: %s", snapshotName)
    23  
    24  	createOpts := snapshots.CreateOpts{
    25  		VolumeID:    volume.ID,
    26  		Name:        snapshotName,
    27  		Description: snapshotDescription,
    28  	}
    29  
    30  	snapshot, err := snapshots.Create(client, createOpts).Extract()
    31  	if err != nil {
    32  		return snapshot, err
    33  	}
    34  
    35  	err = snapshots.WaitForStatus(client, snapshot.ID, "available", 60)
    36  	if err != nil {
    37  		return snapshot, err
    38  	}
    39  
    40  	t.Logf("Successfully created snapshot: %s", snapshot.ID)
    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.
    47  func CreateVolume(t *testing.T, client *gophercloud.ServiceClient) (*volumes.Volume, error) {
    48  	volumeName := tools.RandomString("ACPTTEST", 16)
    49  	volumeDescription := tools.RandomString("ACPTTEST-DESC", 16)
    50  	t.Logf("Attempting to create volume: %s", volumeName)
    51  
    52  	createOpts := volumes.CreateOpts{
    53  		Size:        1,
    54  		Name:        volumeName,
    55  		Description: volumeDescription,
    56  	}
    57  
    58  	volume, err := volumes.Create(client, createOpts).Extract()
    59  	if err != nil {
    60  		return volume, err
    61  	}
    62  
    63  	err = volumes.WaitForStatus(client, volume.ID, "available", 60)
    64  	if err != nil {
    65  		return volume, err
    66  	}
    67  
    68  	tools.PrintResource(t, volume)
    69  	th.AssertEquals(t, volume.Name, volumeName)
    70  	th.AssertEquals(t, volume.Description, volumeDescription)
    71  	th.AssertEquals(t, volume.Size, 1)
    72  
    73  	t.Logf("Successfully created volume: %s", volume.ID)
    74  
    75  	return volume, nil
    76  }
    77  
    78  // CreateVolumeFromImage will create a volume from with a random name and size of
    79  // 1GB. An error will be returned if the volume was unable to be created.
    80  func CreateVolumeFromImage(t *testing.T, client *gophercloud.ServiceClient) (*volumes.Volume, error) {
    81  	choices, err := clients.AcceptanceTestChoicesFromEnv()
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  
    86  	volumeName := tools.RandomString("ACPTTEST", 16)
    87  	t.Logf("Attempting to create volume: %s", volumeName)
    88  
    89  	createOpts := volumes.CreateOpts{
    90  		Size:    1,
    91  		Name:    volumeName,
    92  		ImageID: choices.ImageID,
    93  	}
    94  
    95  	volume, err := volumes.Create(client, createOpts).Extract()
    96  	if err != nil {
    97  		return volume, err
    98  	}
    99  
   100  	err = volumes.WaitForStatus(client, volume.ID, "available", 60)
   101  	if err != nil {
   102  		return volume, err
   103  	}
   104  
   105  	newVolume, err := volumes.Get(client, volume.ID).Extract()
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	th.AssertEquals(t, newVolume.Name, volumeName)
   111  	th.AssertEquals(t, newVolume.Size, 1)
   112  
   113  	t.Logf("Successfully created volume from image: %s", newVolume.ID)
   114  
   115  	return newVolume, nil
   116  }
   117  
   118  // DeleteVolume will delete a volume. A fatal error will occur if the volume
   119  // failed to be deleted. This works best when used as a deferred function.
   120  func DeleteVolume(t *testing.T, client *gophercloud.ServiceClient, volume *volumes.Volume) {
   121  	t.Logf("Attempting to delete volume: %s", volume.ID)
   122  
   123  	err := volumes.Delete(client, volume.ID, volumes.DeleteOpts{}).ExtractErr()
   124  	if err != nil {
   125  		t.Fatalf("Unable to delete volume %s: %v", volume.ID, err)
   126  	}
   127  
   128  	t.Logf("Successfully deleted volume: %s", volume.ID)
   129  }
   130  
   131  // DeleteSnapshot will delete a snapshot. A fatal error will occur if the
   132  // snapshot failed to be deleted.
   133  func DeleteSnapshot(t *testing.T, client *gophercloud.ServiceClient, snapshot *snapshots.Snapshot) {
   134  	t.Logf("Attempting to delete snapshot: %s", snapshot.ID)
   135  
   136  	err := snapshots.Delete(client, snapshot.ID).ExtractErr()
   137  	if err != nil {
   138  		t.Fatalf("Unable to delete snapshot %s: %+v", snapshot.ID, err)
   139  	}
   140  
   141  	// Volumes can't be deleted until their snapshots have been,
   142  	// so block until the snapshot is deleted.
   143  	err = tools.WaitFor(func() (bool, error) {
   144  		_, err := snapshots.Get(client, snapshot.ID).Extract()
   145  		if err != nil {
   146  			return true, nil
   147  		}
   148  
   149  		return false, nil
   150  	})
   151  	if err != nil {
   152  		t.Fatalf("Error waiting for snapshot to delete: %v", err)
   153  	}
   154  
   155  	t.Logf("Successfully deleted snapshot: %s", snapshot.ID)
   156  }