blob: 34634c9d2f8b031b201e6f46cfa840938a29db58 [file] [log] [blame]
Joe Topjian6c69fa62015-02-07 18:35:02 +00001// +build acceptance compute servers
2
3package v2
4
5import (
6 "os"
7 "testing"
8
9 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/acceptance/tools"
11 "github.com/rackspace/gophercloud/openstack"
12 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
13 "github.com/rackspace/gophercloud/openstack/compute/v2/extensions/volumeattach"
14 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
15 th "github.com/rackspace/gophercloud/testhelper"
16)
17
18func newBlockClient(t *testing.T) (*gophercloud.ServiceClient, error) {
19 ao, err := openstack.AuthOptionsFromEnv()
20 th.AssertNoErr(t, err)
21
22 client, err := openstack.AuthenticatedClient(ao)
23 th.AssertNoErr(t, err)
24
25 return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{
26 Region: os.Getenv("OS_REGION_NAME"),
27 })
28}
29
30func createVAServer(t *testing.T, computeClient *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
31 if testing.Short() {
32 t.Skip("Skipping test that requires server creation in short mode.")
33 }
34
35 name := tools.RandomString("ACPTTEST", 16)
36 t.Logf("Attempting to create server: %s\n", name)
37
38 pwd := tools.MakeNewPassword("")
39
40 server, err := servers.Create(computeClient, servers.CreateOpts{
41 Name: name,
42 FlavorRef: choices.FlavorID,
43 ImageRef: choices.ImageID,
44 AdminPass: pwd,
45 }).Extract()
46 if err != nil {
47 t.Fatalf("Unable to create server: %v", err)
48 }
49
50 th.AssertEquals(t, pwd, server.AdminPass)
51
52 return server, err
53}
54
55func createVAVolume(t *testing.T, blockClient *gophercloud.ServiceClient) (*volumes.Volume, error) {
56 volume, err := volumes.Create(blockClient, &volumes.CreateOpts{
57 Size: 1,
58 Name: "gophercloud-test-volume",
59 }).Extract()
60 th.AssertNoErr(t, err)
61 defer func() {
62 err = volumes.WaitForStatus(blockClient, volume.ID, "available", 60)
63 th.AssertNoErr(t, err)
64 }()
65
66 return volume, err
67}
68
69func createVolumeAttachment(t *testing.T, computeClient *gophercloud.ServiceClient, blockClient *gophercloud.ServiceClient, serverId string, volumeId string) {
70 va, err := volumeattach.Create(computeClient, serverId, &volumeattach.CreateOpts{
71 VolumeID: volumeId,
72 }).Extract()
73 th.AssertNoErr(t, err)
74 defer func() {
75 err = volumes.WaitForStatus(blockClient, volumeId, "in-use", 60)
76 th.AssertNoErr(t, err)
77 err = volumeattach.Delete(computeClient, serverId, va.ID).ExtractErr()
78 th.AssertNoErr(t, err)
79 err = volumes.WaitForStatus(blockClient, volumeId, "available", 60)
80 th.AssertNoErr(t, err)
81 }()
82}
83
84func TestAttachVolume(t *testing.T) {
85 choices, err := ComputeChoicesFromEnv()
86 if err != nil {
87 t.Fatal(err)
88 }
89
90 computeClient, err := newClient()
91 if err != nil {
92 t.Fatalf("Unable to create a compute client: %v", err)
93 }
94
95 blockClient, err := newBlockClient(t)
96 if err != nil {
97 t.Fatalf("Unable to create a blockstorage client: %v", err)
98 }
99
100 server, err := createVAServer(t, computeClient, choices)
101 if err != nil {
102 t.Fatalf("Unable to create server: %v", err)
103 }
Joe Topjiand1d730f2015-02-07 19:02:00 +0000104 defer func() {
105 servers.Delete(computeClient, server.ID)
106 t.Logf("Server deleted.")
107 }()
Joe Topjian6c69fa62015-02-07 18:35:02 +0000108
109 if err = waitForStatus(computeClient, server, "ACTIVE"); err != nil {
110 t.Fatalf("Unable to wait for server: %v", err)
111 }
112
113 volume, err := createVAVolume(t, blockClient)
114 if err != nil {
115 t.Fatalf("Unable to create volume: %v", err)
116 }
Joe Topjian6c69fa62015-02-07 18:35:02 +0000117 defer func() {
118 err = volumes.Delete(blockClient, volume.ID).ExtractErr()
119 th.AssertNoErr(t, err)
120 t.Logf("Volume deleted.")
121 }()
122
Joe Topjiand1d730f2015-02-07 19:02:00 +0000123 createVolumeAttachment(t, computeClient, blockClient, server.ID, volume.ID)
124
Joe Topjian6c69fa62015-02-07 18:35:02 +0000125}