blob: a1b10a95a6b754f736a82ebd112a5c181e545be6 [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"
9 "github.com/gophercloud/gophercloud/acceptance/tools"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "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 Topjian6c69fa62015-02-07 18:35:02 +000013)
14
Joe Topjianab883ea2016-07-26 01:52:01 +000015func TestVolumeAttachAttachment(t *testing.T) {
Joe Topjian6c69fa62015-02-07 18:35:02 +000016 if testing.Short() {
17 t.Skip("Skipping test that requires server creation in short mode.")
18 }
19
Joe Topjianab883ea2016-07-26 01:52:01 +000020 client, err := newClient()
Joe Topjian6c69fa62015-02-07 18:35:02 +000021 if err != nil {
Joe Topjianab883ea2016-07-26 01:52:01 +000022 t.Fatalf("Unable to create a compute client: %v", err)
Joe Topjian6c69fa62015-02-07 18:35:02 +000023 }
24
Joe Topjian6c69fa62015-02-07 18:35:02 +000025 choices, err := ComputeChoicesFromEnv()
26 if err != nil {
27 t.Fatal(err)
28 }
29
Joe Topjianab883ea2016-07-26 01:52:01 +000030 blockClient, err := newBlockClient()
Joe Topjian6c69fa62015-02-07 18:35:02 +000031 if err != nil {
32 t.Fatalf("Unable to create a blockstorage client: %v", err)
33 }
34
Joe Topjianab883ea2016-07-26 01:52:01 +000035 server, err := createServer(t, client, choices)
Joe Topjian6c69fa62015-02-07 18:35:02 +000036 if err != nil {
37 t.Fatalf("Unable to create server: %v", err)
38 }
39
Joe Topjianab883ea2016-07-26 01:52:01 +000040 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
Joe Topjian6c69fa62015-02-07 18:35:02 +000041 t.Fatalf("Unable to wait for server: %v", err)
42 }
Joe Topjianab883ea2016-07-26 01:52:01 +000043 defer deleteServer(t, client, server)
Joe Topjian6c69fa62015-02-07 18:35:02 +000044
Joe Topjianab883ea2016-07-26 01:52:01 +000045 volume, err := createVolume(t, blockClient)
Joe Topjian6c69fa62015-02-07 18:35:02 +000046 if err != nil {
47 t.Fatalf("Unable to create volume: %v", err)
48 }
Joe Topjian6c69fa62015-02-07 18:35:02 +000049
Joe Topjianab883ea2016-07-26 01:52:01 +000050 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 Topjiand1d730f2015-02-07 19:02:00 +000054
Joe Topjianab883ea2016-07-26 01:52:01 +000055 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
65func 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
81func 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
90func 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
108func 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
121func 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 Topjian6c69fa62015-02-07 18:35:02 +0000126}