blob: 0cd1d566f4c9d9615d8411cf8a6549a45395fa88 [file] [log] [blame]
Jon Perrittee6074f2014-04-30 18:42:32 -05001// +build acceptance
2
3package openstack
4
5import (
Jon Perritt687c7d02014-05-05 18:44:54 -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"
10 "github.com/rackspace/gophercloud/openstack/identity"
11 "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
132 cv, err := volumes.Create(client, volumes.CreateOpts{
133 "size": 1,
134 "display_name": "test-volume",
135 })
136 if err != nil {
137 t.Error(err)
138 return
139 }
140 defer func() {
141 err = volumes.Delete(client, volumes.DeleteOpts{
142 "id": cv.Id,
143 })
144 if err != nil {
145 t.Error(err)
146 return
147 }
148 }()
149
150 for i := 0; i < 60; i++ {
151 gv, err := volumes.Get(client, volumes.GetOpts{
152 "id": cv.Id,
153 })
154 if err != nil {
155 t.Error(err)
156 return
157 }
158 if gv.Status == "available" {
159 break
160 }
161 time.Sleep(2000 * time.Millisecond)
162 }
163
164 var css snapshots.Snapshot
165 css, err = snapshots.Create(client, snapshots.CreateOpts{
Jon Perritt982c86d2014-05-05 21:13:54 -0500166 "volume_id": cv.Id,
167 "display_name": "test-snapshot",
Jon Perritt687c7d02014-05-05 18:44:54 -0500168 })
169 if err != nil {
170 t.Error(err)
171 return
172 }
Jon Perritt982c86d2014-05-05 21:13:54 -0500173 time.Sleep(20000 * time.Millisecond)
174 defer func() {
175 err = snapshots.Delete(client, snapshots.DeleteOpts{
176 "id": cv.Id,
177 })
178 if err != nil {
179 t.Error(err)
180 return
181 }
182 }()
Jon Perritt687c7d02014-05-05 18:44:54 -0500183 fmt.Printf("%+v\n", css)
184}