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" |
| 9 | "github.com/gophercloud/gophercloud/acceptance/tools" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes" |
| 11 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach" |
| 12 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 13 | ) |
| 14 | |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 15 | func TestVolumeAttachAttachment(t *testing.T) { |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 16 | if testing.Short() { |
| 17 | t.Skip("Skipping test that requires server creation in short mode.") |
| 18 | } |
| 19 | |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 20 | client, err := newClient() |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 21 | if err != nil { |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 22 | t.Fatalf("Unable to create a compute client: %v", err) |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 25 | choices, err := ComputeChoicesFromEnv() |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 30 | blockClient, err := newBlockClient() |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 31 | if err != nil { |
| 32 | t.Fatalf("Unable to create a blockstorage client: %v", err) |
| 33 | } |
| 34 | |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 35 | server, err := createServer(t, client, choices) |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 36 | if err != nil { |
| 37 | t.Fatalf("Unable to create server: %v", err) |
| 38 | } |
| 39 | |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 40 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 41 | t.Fatalf("Unable to wait for server: %v", err) |
| 42 | } |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 43 | defer deleteServer(t, client, server) |
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 | volume, err := createVolume(t, blockClient) |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 46 | if err != nil { |
| 47 | t.Fatalf("Unable to create volume: %v", err) |
| 48 | } |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 49 | |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 50 | if err = volumes.WaitForStatus(blockClient, volume.ID, "available", 60); err != nil { |
| 51 | t.Fatalf("Unable to wait for volume: %v", err) |
| 52 | } |
| 53 | defer deleteVolume(t, blockClient, volume) |
Joe Topjian | d1d730f | 2015-02-07 19:02:00 +0000 | [diff] [blame] | 54 | |
Joe Topjian | ab883ea | 2016-07-26 01:52:01 +0000 | [diff] [blame] | 55 | volumeAttachment, err := createVolumeAttachment(t, client, blockClient, server, volume) |
| 56 | if err != nil { |
| 57 | t.Fatalf("Unable to attach volume: %v", err) |
| 58 | } |
| 59 | defer deleteVolumeAttachment(t, client, blockClient, server, volumeAttachment) |
| 60 | |
| 61 | printVolumeAttachment(t, volumeAttachment) |
| 62 | |
| 63 | } |
| 64 | |
| 65 | func createVolume(t *testing.T, blockClient *gophercloud.ServiceClient) (*volumes.Volume, error) { |
| 66 | volumeName := tools.RandomString("ACPTTEST", 16) |
| 67 | createOpts := volumes.CreateOpts{ |
| 68 | Size: 1, |
| 69 | Name: volumeName, |
| 70 | } |
| 71 | |
| 72 | volume, err := volumes.Create(blockClient, createOpts).Extract() |
| 73 | if err != nil { |
| 74 | return volume, err |
| 75 | } |
| 76 | |
| 77 | t.Logf("Created volume: %s", volume.ID) |
| 78 | return volume, nil |
| 79 | } |
| 80 | |
| 81 | func deleteVolume(t *testing.T, blockClient *gophercloud.ServiceClient, volume *volumes.Volume) { |
| 82 | err := volumes.Delete(blockClient, volume.ID).ExtractErr() |
| 83 | if err != nil { |
| 84 | t.Fatalf("Unable to delete volume: %v", err) |
| 85 | } |
| 86 | |
| 87 | t.Logf("Deleted volume: %s", volume.ID) |
| 88 | } |
| 89 | |
| 90 | func createVolumeAttachment(t *testing.T, client *gophercloud.ServiceClient, blockClient *gophercloud.ServiceClient, server *servers.Server, volume *volumes.Volume) (*volumeattach.VolumeAttachment, error) { |
| 91 | volumeAttachOptions := volumeattach.CreateOpts{ |
| 92 | VolumeID: volume.ID, |
| 93 | } |
| 94 | |
| 95 | t.Logf("Attempting to attach volume %s to server %s", volume.ID, server.ID) |
| 96 | volumeAttachment, err := volumeattach.Create(client, server.ID, volumeAttachOptions).Extract() |
| 97 | if err != nil { |
| 98 | return volumeAttachment, err |
| 99 | } |
| 100 | |
| 101 | if err = volumes.WaitForStatus(blockClient, volume.ID, "in-use", 60); err != nil { |
| 102 | return volumeAttachment, err |
| 103 | } |
| 104 | |
| 105 | return volumeAttachment, nil |
| 106 | } |
| 107 | |
| 108 | func deleteVolumeAttachment(t *testing.T, client *gophercloud.ServiceClient, blockClient *gophercloud.ServiceClient, server *servers.Server, volumeAttachment *volumeattach.VolumeAttachment) { |
| 109 | |
| 110 | err := volumeattach.Delete(client, server.ID, volumeAttachment.VolumeID).ExtractErr() |
| 111 | if err != nil { |
| 112 | t.Fatalf("Unable to detach volume: %v", err) |
| 113 | } |
| 114 | |
| 115 | if err = volumes.WaitForStatus(blockClient, volumeAttachment.ID, "available", 60); err != nil { |
| 116 | t.Fatalf("Unable to wait for volume: %v", err) |
| 117 | } |
| 118 | t.Logf("Deleted volume: %s", volumeAttachment.VolumeID) |
| 119 | } |
| 120 | |
| 121 | func printVolumeAttachment(t *testing.T, volumeAttachment *volumeattach.VolumeAttachment) { |
| 122 | t.Logf("ID: %s", volumeAttachment.ID) |
| 123 | t.Logf("Device: %s", volumeAttachment.Device) |
| 124 | t.Logf("VolumeID: %s", volumeAttachment.VolumeID) |
| 125 | t.Logf("ServerID: %s", volumeAttachment.ServerID) |
Joe Topjian | 6c69fa6 | 2015-02-07 18:35:02 +0000 | [diff] [blame] | 126 | } |