blob: aaabac10859cf7af53aef7a01a40ae4b9e44e834 [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001// +build acceptance
2
3package openstack
4
5import (
Jon Perritt60c3b2e2014-05-27 18:38:33 -05006 "fmt"
Jon Perrittee6074f2014-04-30 18:42:32 -05007 blockstorage "github.com/rackspace/gophercloud/openstack/blockstorage/v1"
Jon Perritt687c7d02014-05-05 18:44:54 -05008 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/snapshots"
Jon Perrittee6074f2014-04-30 18:42:32 -05009 "github.com/rackspace/gophercloud/openstack/blockstorage/v1/volumes"
Jon Perritt60c3b2e2014-05-27 18:38:33 -050010 identity "github.com/rackspace/gophercloud/openstack/identity/v2"
Jon Perrittee6074f2014-04-30 18:42:32 -050011 "github.com/rackspace/gophercloud/openstack/utils"
12 "os"
Jon Perritt94963ad2014-05-05 12:14:39 -050013 "strconv"
Jon Perrittee6074f2014-04-30 18:42:32 -050014 "testing"
Jon Perritt94963ad2014-05-05 12:14:39 -050015 "time"
Jon Perrittee6074f2014-04-30 18:42:32 -050016)
17
Jon Perritt94963ad2014-05-05 12:14:39 -050018var numVols = 2
19
Jon Perrittee6074f2014-04-30 18:42:32 -050020func getClient() (*blockstorage.Client, error) {
21 ao, err := utils.AuthOptions()
22 if err != nil {
23 return nil, err
24 }
25
26 r, err := identity.Authenticate(ao)
27 if err != nil {
28 return nil, err
29 }
30
31 sc, err := identity.GetServiceCatalog(r)
32 if err != nil {
33 return nil, err
34 }
35
36 ces, err := sc.CatalogEntries()
37 if err != nil {
38 return nil, err
39 }
40
41 var eps []identity.Endpoint
42 for _, ce := range ces {
43 if ce.Type == "volume" {
44 eps = ce.Endpoints
45 }
46 }
47
48 region := os.Getenv("OS_REGION_NAME")
49 rep := ""
50 for _, ep := range eps {
51 if ep.Region == region {
52 rep = ep.PublicURL
53 }
54 }
55
56 client := blockstorage.NewClient(rep, r, ao)
57
58 return client, nil
59
60}
61
62func TestVolumes(t *testing.T) {
63 client, err := getClient()
64 if err != nil {
65 t.Error(err)
66 return
67 }
Jon Perritt70dd47d2014-05-01 13:51:53 -050068
Jon Perritt94963ad2014-05-05 12:14:39 -050069 var cv volumes.Volume
70 for i := 0; i < numVols; i++ {
71 cv, err := volumes.Create(client, volumes.CreateOpts{
72 "size": 1,
73 "display_name": "test-volume" + strconv.Itoa(i),
Jon Perritte77b9b22014-05-01 13:11:12 -050074 })
Jon Perritt94963ad2014-05-05 12:14:39 -050075 if err != nil {
76 t.Error(err)
77 return
78 }
79 defer func() {
80 time.Sleep(10000 * time.Millisecond)
81 err = volumes.Delete(client, volumes.DeleteOpts{
82 "id": cv.Id,
83 })
84 if err != nil {
85 t.Error(err)
86 return
87 }
88 }()
89 }
Jon Perritt70dd47d2014-05-01 13:51:53 -050090
Jon Perritt94963ad2014-05-05 12:14:39 -050091 vols, err := volumes.List(client, volumes.ListOpts{
92 "full": true,
Jon Perritt70dd47d2014-05-01 13:51:53 -050093 })
94 if err != nil {
95 t.Error(err)
96 return
97 }
Jon Perritt94963ad2014-05-05 12:14:39 -050098 if len(vols) != numVols {
99 t.Errorf("Expected %d volumes, got %d", numVols, len(vols))
100 return
101 }
102
103 vols, err = volumes.List(client, volumes.ListOpts{
104 "full": false,
105 })
106 if err != nil {
107 t.Error(err)
108 return
109 }
110 if len(vols) != numVols {
111 t.Errorf("Expected %d volumes, got %d", numVols, len(vols))
112 return
113 }
114
115 _, err = volumes.Get(client, volumes.GetOpts{
116 "id": cv.Id,
117 })
118 if err != nil {
119 t.Error(err)
120 return
121 }
122
Jon Perrittee6074f2014-04-30 18:42:32 -0500123}
Jon Perritt687c7d02014-05-05 18:44:54 -0500124
125func TestSnapshots(t *testing.T) {
126 client, err := getClient()
127 if err != nil {
128 t.Error(err)
129 return
130 }
131
Jon Perritte3af81d2014-05-06 14:52:28 -0500132 var css snapshots.Snapshot
133
Jon Perritt687c7d02014-05-05 18:44:54 -0500134 cv, err := volumes.Create(client, volumes.CreateOpts{
135 "size": 1,
136 "display_name": "test-volume",
137 })
138 if err != nil {
139 t.Error(err)
140 return
141 }
142 defer func() {
Jon Perritte3af81d2014-05-06 14:52:28 -0500143 for i := 0; i < 60; i++ {
144 gss, _ := snapshots.Get(client, snapshots.GetOpts{
145 "id": css.Id,
146 })
147 if gss.Status == "" {
148 err = volumes.Delete(client, volumes.DeleteOpts{
149 "id": cv.Id,
150 })
151 if err != nil {
152 t.Error(err)
153 return
154 }
155 break
156 }
157 time.Sleep(5000 * time.Millisecond)
Jon Perritt687c7d02014-05-05 18:44:54 -0500158 }
159 }()
160
161 for i := 0; i < 60; i++ {
162 gv, err := volumes.Get(client, volumes.GetOpts{
163 "id": cv.Id,
164 })
165 if err != nil {
166 t.Error(err)
167 return
168 }
169 if gv.Status == "available" {
170 break
171 }
172 time.Sleep(2000 * time.Millisecond)
173 }
174
Jon Perritt687c7d02014-05-05 18:44:54 -0500175 css, err = snapshots.Create(client, snapshots.CreateOpts{
Jon Perritt982c86d2014-05-05 21:13:54 -0500176 "volume_id": cv.Id,
177 "display_name": "test-snapshot",
Jon Perritt687c7d02014-05-05 18:44:54 -0500178 })
179 if err != nil {
180 t.Error(err)
181 return
182 }
Jon Perritt982c86d2014-05-05 21:13:54 -0500183 defer func() {
Jon Perritte3af81d2014-05-06 14:52:28 -0500184 for i := 0; i < 60; i++ {
185 gss, err := snapshots.Get(client, snapshots.GetOpts{
186 "id": css.Id,
187 })
188 if err != nil {
189 t.Error(err)
190 return
191 }
192 if gss.Status == "available" {
193 err = snapshots.Delete(client, snapshots.DeleteOpts{
194 "id": css.Id,
195 })
196 if err != nil {
197 t.Error(err)
198 return
199 }
200 break
201 }
202 time.Sleep(2000 * time.Millisecond)
Jon Perritt982c86d2014-05-05 21:13:54 -0500203 }
204 }()
Jon Perritt60c3b2e2014-05-27 18:38:33 -0500205
206 lss, err := snapshots.List(client, snapshots.ListOpts{
207 Full: true,
208 })
209 if err != nil {
210 t.Error(err)
211 return
212 }
Jon Perritt687c7d02014-05-05 18:44:54 -0500213}