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 | choices, err :=clients.AcceptanceTestChoicesFromEnv() |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 29 | blockClient, err := clients.NewBlockStorageV1Client() |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 30 | if err != nil { |
| 31 | t.Fatalf("Unable to create a blockstorage client: %v", err) |
| 32 | } |
| 33 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 34 | server, err := CreateServer(t, client, choices) |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 35 | if err != nil { |
| 36 | t.Fatalf("Unable to create server: %v", err) |
| 37 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 38 | defer DeleteServer(t, client, server) |
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 | volume, err := createVolume(t, blockClient) |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 41 | if err != nil { |
| 42 | t.Fatalf("Unable to create volume: %v", err) |
| 43 | } |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 44 | |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 45 | if err = volumes.WaitForStatus(blockClient, volume.ID, "available", 60); err != nil { |
| 46 | t.Fatalf("Unable to wait for volume: %v", err) |
| 47 | } |
| 48 | defer deleteVolume(t, blockClient, volume) |
Joe Topjian | d1d730f | 2015-02-07 19:02:00 +0000 | [diff] [blame] | 49 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 50 | volumeAttachment, err := CreateVolumeAttachment(t, client, blockClient, server, volume) |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 51 | if err != nil { |
| 52 | t.Fatalf("Unable to attach volume: %v", err) |
| 53 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 54 | defer DeleteVolumeAttachment(t, client, blockClient, server, volumeAttachment) |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 55 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 56 | PrintVolumeAttachment(t, volumeAttachment) |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 57 | |
| 58 | } |
| 59 | |
| 60 | func createVolume(t *testing.T, blockClient *gophercloud.ServiceClient) (*volumes.Volume, error) { |
| 61 | volumeName := tools.RandomString("ACPTTEST", 16) |
| 62 | createOpts := volumes.CreateOpts{ |
| 63 | Size: 1, |
| 64 | Name: volumeName, |
| 65 | } |
| 66 | |
| 67 | volume, err := volumes.Create(blockClient, createOpts).Extract() |
| 68 | if err != nil { |
| 69 | return volume, err |
| 70 | } |
| 71 | |
| 72 | t.Logf("Created volume: %s", volume.ID) |
| 73 | return volume, nil |
| 74 | } |
| 75 | |
| 76 | func deleteVolume(t *testing.T, blockClient *gophercloud.ServiceClient, volume *volumes.Volume) { |
| 77 | err := volumes.Delete(blockClient, volume.ID).ExtractErr() |
| 78 | if err != nil { |
| 79 | t.Fatalf("Unable to delete volume: %v", err) |
| 80 | } |
| 81 | |
| 82 | t.Logf("Deleted volume: %s", volume.ID) |
| 83 | } |