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