blob: 78d85a9bfc37cfc365b95104e320763559476999 [file] [log] [blame]
Joe Topjianab883ea2016-07-26 01:52:01 +00001// +build acceptance compute volumeattach
Joe Topjian6c69fa62015-02-07 18:35:02 +00002
3package v2
4
5import (
Joe Topjian6c69fa62015-02-07 18:35:02 +00006 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
Joe Topjian1c15e3f2016-08-08 10:48:38 -06009 "github.com/gophercloud/gophercloud/acceptance/clients"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/acceptance/tools"
Jon Perritt27249f42016-02-18 10:35:59 -060011 "github.com/gophercloud/gophercloud/openstack/blockstorage/v1/volumes"
Joe Topjian6c69fa62015-02-07 18:35:02 +000012)
13
Joe Topjianab883ea2016-07-26 01:52:01 +000014func TestVolumeAttachAttachment(t *testing.T) {
Joe Topjian6c69fa62015-02-07 18:35:02 +000015 if testing.Short() {
16 t.Skip("Skipping test that requires server creation in short mode.")
17 }
18
Joe Topjian1c15e3f2016-08-08 10:48:38 -060019 client, err := clients.NewComputeV2Client()
Joe Topjian6c69fa62015-02-07 18:35:02 +000020 if err != nil {
Joe Topjianab883ea2016-07-26 01:52:01 +000021 t.Fatalf("Unable to create a compute client: %v", err)
Joe Topjian6c69fa62015-02-07 18:35:02 +000022 }
23
Joe Topjian1c15e3f2016-08-08 10:48:38 -060024 blockClient, err := clients.NewBlockStorageV1Client()
Joe Topjian6c69fa62015-02-07 18:35:02 +000025 if err != nil {
26 t.Fatalf("Unable to create a blockstorage client: %v", err)
27 }
28
Joe Topjian66a046c2017-01-19 22:07:26 -070029 server, err := CreateServer(t, client)
Joe Topjian6c69fa62015-02-07 18:35:02 +000030 if err != nil {
31 t.Fatalf("Unable to create server: %v", err)
32 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060033 defer DeleteServer(t, client, server)
Joe Topjian6c69fa62015-02-07 18:35:02 +000034
Joe Topjianab883ea2016-07-26 01:52:01 +000035 volume, err := createVolume(t, blockClient)
Joe Topjian6c69fa62015-02-07 18:35:02 +000036 if err != nil {
37 t.Fatalf("Unable to create volume: %v", err)
38 }
Joe Topjian6c69fa62015-02-07 18:35:02 +000039
Joe Topjianab883ea2016-07-26 01:52:01 +000040 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 Topjiand1d730f2015-02-07 19:02:00 +000044
Joe Topjian1c15e3f2016-08-08 10:48:38 -060045 volumeAttachment, err := CreateVolumeAttachment(t, client, blockClient, server, volume)
Joe Topjianab883ea2016-07-26 01:52:01 +000046 if err != nil {
47 t.Fatalf("Unable to attach volume: %v", err)
48 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060049 defer DeleteVolumeAttachment(t, client, blockClient, server, volumeAttachment)
Joe Topjianab883ea2016-07-26 01:52:01 +000050
Joe Topjian66a046c2017-01-19 22:07:26 -070051 tools.PrintResource(t, volumeAttachment)
Joe Topjianab883ea2016-07-26 01:52:01 +000052
53}
54
55func 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
71func 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}