blob: d791992068cab918c43a16110296ec1928beefde [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 choices, err :=clients.AcceptanceTestChoicesFromEnv()
Joe Topjian6c69fa62015-02-07 18:35:02 +000025 if err != nil {
26 t.Fatal(err)
27 }
28
Joe Topjian1c15e3f2016-08-08 10:48:38 -060029 blockClient, err := clients.NewBlockStorageV1Client()
Joe Topjian6c69fa62015-02-07 18:35:02 +000030 if err != nil {
31 t.Fatalf("Unable to create a blockstorage client: %v", err)
32 }
33
Joe Topjian1c15e3f2016-08-08 10:48:38 -060034 server, err := CreateServer(t, client, choices)
Joe Topjian6c69fa62015-02-07 18:35:02 +000035 if err != nil {
36 t.Fatalf("Unable to create server: %v", err)
37 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060038 defer DeleteServer(t, client, server)
Joe Topjian6c69fa62015-02-07 18:35:02 +000039
Joe Topjianab883ea2016-07-26 01:52:01 +000040 volume, err := createVolume(t, blockClient)
Joe Topjian6c69fa62015-02-07 18:35:02 +000041 if err != nil {
42 t.Fatalf("Unable to create volume: %v", err)
43 }
Joe Topjian6c69fa62015-02-07 18:35:02 +000044
Joe Topjianab883ea2016-07-26 01:52:01 +000045 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 Topjiand1d730f2015-02-07 19:02:00 +000049
Joe Topjian1c15e3f2016-08-08 10:48:38 -060050 volumeAttachment, err := CreateVolumeAttachment(t, client, blockClient, server, volume)
Joe Topjianab883ea2016-07-26 01:52:01 +000051 if err != nil {
52 t.Fatalf("Unable to attach volume: %v", err)
53 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060054 defer DeleteVolumeAttachment(t, client, blockClient, server, volumeAttachment)
Joe Topjianab883ea2016-07-26 01:52:01 +000055
Joe Topjian1c15e3f2016-08-08 10:48:38 -060056 PrintVolumeAttachment(t, volumeAttachment)
Joe Topjianab883ea2016-07-26 01:52:01 +000057
58}
59
60func 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
76func 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}