Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 1 | // +build acceptance compute volumeattach |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 6 | "testing" |
| 7 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud" |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/acceptance/clients" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/acceptance/tools" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 11 | "github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes" |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 12 | ) |
| 13 | |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 14 | func TestVolumeAttachAttachment(t *testing.T) { |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 15 | if testing.Short() { |
| 16 | t.Skip("Skipping test that requires server creation in short mode.") |
| 17 | } |
| 18 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 19 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 20 | if err != nil { |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 21 | t.Fatalf("Unable to create a compute client: %v", err) |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 24 | blockClient, err := clients.NewBlockStorageV1Client() |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 25 | if err != nil { |
| 26 | t.Fatalf("Unable to create a blockstorage client: %v", err) |
| 27 | } |
| 28 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 29 | server, err := CreateServer(t, client) |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 30 | if err != nil { |
| 31 | t.Fatalf("Unable to create server: %v", err) |
| 32 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 33 | defer DeleteServer(t, client, server) |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 34 | |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 35 | volume, err := createVolume(t, blockClient) |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 36 | if err != nil { |
| 37 | t.Fatalf("Unable to create volume: %v", err) |
| 38 | } |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 39 | |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 40 | if err = volumes.WaitForStatus(blockClient, volume.ID, "available", 60); err != nil { |
| 41 | t.Fatalf("Unable to wait for volume: %v", err) |
| 42 | } |
| 43 | defer deleteVolume(t, blockClient, volume) |
Joe Topjian | d1d730f | 2015-02-07 19:02:00 +0000 | [diff] [blame] | 44 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 45 | volumeAttachment, err := CreateVolumeAttachment(t, client, blockClient, server, volume) |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 46 | if err != nil { |
| 47 | t.Fatalf("Unable to attach volume: %v", err) |
| 48 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 49 | defer DeleteVolumeAttachment(t, client, blockClient, server, volumeAttachment) |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 50 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 51 | tools.PrintResource(t, volumeAttachment) |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 52 | |
| 53 | } |
| 54 | |
| 55 | func createVolume(t *testing.T, blockClient *gophercloud.ServiceClient) (*volumes.Volume, error) { |
| 56 | volumeName := tools.RandomString("ACPTTEST", 16) |
| 57 | createOpts := volumes.CreateOpts{ |
| 58 | Size: 1, |
| 59 | Name: volumeName, |
| 60 | } |
| 61 | |
| 62 | volume, err := volumes.Create(blockClient, createOpts).Extract() |
| 63 | if err != nil { |
| 64 | return volume, err |
| 65 | } |
| 66 | |
| 67 | t.Logf("Created volume: %s", volume.ID) |
| 68 | return volume, nil |
| 69 | } |
| 70 | |
| 71 | func deleteVolume(t *testing.T, blockClient *gophercloud.ServiceClient, volume *volumes.Volume) { |
| 72 | err := volumes.Delete(blockClient, volume.ID).ExtractErr() |
| 73 | if err != nil { |
| 74 | t.Fatalf("Unable to delete volume: %v", err) |
| 75 | } |
| 76 | |
| 77 | t.Logf("Deleted volume: %s", volume.ID) |
| 78 | } |