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

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