blob: e7907d1dd8f2760237ba2c6f417d7f8eb3e57660 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001// +build acceptance
2
3package openstack
4
5import (
6 "bytes"
Ash Wilsona26eea02014-09-10 16:21:49 -04007 "os"
8 "strings"
9 "testing"
10
11 "github.com/rackspace/gophercloud"
Jon Perritt5eb55b12014-08-18 14:48:23 -050012 "github.com/rackspace/gophercloud/acceptance/tools"
Ash Wilsona26eea02014-09-10 16:21:49 -040013 "github.com/rackspace/gophercloud/openstack"
Jon Perrittb6b1d022014-04-14 21:50:45 -050014 "github.com/rackspace/gophercloud/openstack/storage/v1/accounts"
15 "github.com/rackspace/gophercloud/openstack/storage/v1/containers"
16 "github.com/rackspace/gophercloud/openstack/storage/v1/objects"
Jon Perritt5eb55b12014-08-18 14:48:23 -050017 "github.com/rackspace/gophercloud/openstack/utils"
Ash Wilsonda7c09f2014-09-16 15:20:44 -040018 "github.com/rackspace/gophercloud/pagination"
Jon Perritt816d2a02014-03-11 20:49:46 -050019)
20
Jon Perritt816d2a02014-03-11 20:49:46 -050021var metadata = map[string]string{"gopher": "cloud"}
22var numContainers = 2
23var numObjects = 2
24
Ash Wilsona26eea02014-09-10 16:21:49 -040025func newClient() (*gophercloud.ServiceClient, error) {
Jon Perritt5eb55b12014-08-18 14:48:23 -050026 ao, err := utils.AuthOptions()
27 if err != nil {
28 return nil, err
29 }
30
Ash Wilsona26eea02014-09-10 16:21:49 -040031 client, err := openstack.AuthenticatedClient(ao)
Jon Perritt5eb55b12014-08-18 14:48:23 -050032 if err != nil {
33 return nil, err
34 }
35
Ash Wilsond88f32b2014-09-10 16:59:58 -040036 return openstack.NewStorageV1(client, gophercloud.EndpointOpts{
37 Region: os.Getenv("OS_REGION_NAME"),
38 })
Jon Perritt5eb55b12014-08-18 14:48:23 -050039}
40
Jon Perritt816d2a02014-03-11 20:49:46 -050041func TestAccount(t *testing.T) {
Jon Perritt5eb55b12014-08-18 14:48:23 -050042 client, err := newClient()
Jon Perritt816d2a02014-03-11 20:49:46 -050043 if err != nil {
44 t.Error(err)
45 return
46 }
47
Jon Perritt5eb55b12014-08-18 14:48:23 -050048 err = accounts.Update(client, accounts.UpdateOpts{
49 Metadata: metadata,
50 })
51 if err != nil {
52 t.Error(err)
53 return
54 }
55 defer func() {
56 tempMap := make(map[string]string)
57 for k := range metadata {
58 tempMap[k] = ""
Jon Perritt816d2a02014-03-11 20:49:46 -050059 }
Jon Perritt5eb55b12014-08-18 14:48:23 -050060 err = accounts.Update(client, accounts.UpdateOpts{
61 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -050062 })
63 if err != nil {
64 t.Error(err)
65 return
66 }
Jon Perritt5eb55b12014-08-18 14:48:23 -050067 }()
Jon Perritt816d2a02014-03-11 20:49:46 -050068
Jon Perritt5eb55b12014-08-18 14:48:23 -050069 gr, err := accounts.Get(client, accounts.GetOpts{})
70 if err != nil {
71 t.Error(err)
72 return
73 }
74 am := accounts.ExtractMetadata(gr)
75 for k := range metadata {
76 if am[k] != metadata[strings.Title(k)] {
77 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -050078 return
79 }
Jon Perritt816d2a02014-03-11 20:49:46 -050080 }
81}
82
83func TestContainers(t *testing.T) {
Jon Perritt5eb55b12014-08-18 14:48:23 -050084 client, err := newClient()
Jon Perritt816d2a02014-03-11 20:49:46 -050085 if err != nil {
86 t.Error(err)
87 return
88 }
89
Jon Perritt5eb55b12014-08-18 14:48:23 -050090 cNames := make([]string, numContainers)
91 for i := 0; i < numContainers; i++ {
92 cNames[i] = tools.RandomString("test-container-", 8)
93 }
94
95 for i := 0; i < len(cNames); i++ {
96 _, err := containers.Create(client, containers.CreateOpts{
97 Name: cNames[i],
98 })
99 if err != nil {
100 t.Error(err)
101 return
Jon Perritt816d2a02014-03-11 20:49:46 -0500102 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500103 }
104 defer func() {
Jon Perritt816d2a02014-03-11 20:49:46 -0500105 for i := 0; i < len(cNames); i++ {
Jon Perritt5eb55b12014-08-18 14:48:23 -0500106 err = containers.Delete(client, containers.DeleteOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500107 Name: cNames[i],
108 })
109 if err != nil {
110 t.Error(err)
111 return
112 }
113 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500114 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500115
Ash Wilsonda7c09f2014-09-16 15:20:44 -0400116 cns := make([]string, 0, numContainers)
117 pager := containers.List(client, containers.ListOpts{Full: false})
118 err = pager.EachPage(func(page pagination.Page) (bool, error) {
119 names, err := containers.ExtractNames(page)
120 if err != nil {
121 return false, err
122 }
123
124 cns = append(cns, names...)
125
126 return true, nil
Jon Perritt5eb55b12014-08-18 14:48:23 -0500127 })
128 if err != nil {
Ash Wilsonda7c09f2014-09-16 15:20:44 -0400129 t.Fatal(err)
Jon Perritt5eb55b12014-08-18 14:48:23 -0500130 return
131 }
Ash Wilsonda7c09f2014-09-16 15:20:44 -0400132
Jon Perritt5eb55b12014-08-18 14:48:23 -0500133 if len(cns) != len(cNames) {
134 t.Errorf("Expected %d names and got %d", len(cNames), len(cns))
135 return
136 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500137
Ash Wilsonda7c09f2014-09-16 15:20:44 -0400138 cis := make([]containers.Container, 0, numContainers)
139 pager = containers.List(client, containers.ListOpts{Full: true})
140 err = pager.EachPage(func(page pagination.Page) (bool, error) {
141 cisPage, err := containers.ExtractInfo(page)
142 if err != nil {
143 return false, err
144 }
145
146 cis = append(cis, cisPage...)
147
148 return true, nil
Jon Perritt5eb55b12014-08-18 14:48:23 -0500149 })
Ash Wilsonda7c09f2014-09-16 15:20:44 -0400150
Jon Perritt5eb55b12014-08-18 14:48:23 -0500151 if len(cis) != len(cNames) {
152 t.Errorf("Expected %d containers and got %d", len(cNames), len(cis))
153 return
154 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500155
Jon Perritt5eb55b12014-08-18 14:48:23 -0500156 err = containers.Update(client, containers.UpdateOpts{
157 Name: cNames[0],
158 Metadata: metadata,
159 })
160 if err != nil {
161 t.Error(err)
162 return
163 }
164 defer func() {
165 tempMap := make(map[string]string)
166 for k := range metadata {
167 tempMap[k] = ""
168 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500169 err = containers.Update(client, containers.UpdateOpts{
170 Name: cNames[0],
Jon Perritt5eb55b12014-08-18 14:48:23 -0500171 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -0500172 })
173 if err != nil {
174 t.Error(err)
175 return
176 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500177 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500178
Jon Perritt5eb55b12014-08-18 14:48:23 -0500179 gr, err := containers.Get(client, containers.GetOpts{})
180 if err != nil {
181 t.Error(err)
182 return
183 }
184 cm := containers.ExtractMetadata(gr)
185 for k := range metadata {
186 if cm[k] != metadata[strings.Title(k)] {
187 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -0500188 return
189 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500190 }
191}
192
193func TestObjects(t *testing.T) {
Jon Perritt5eb55b12014-08-18 14:48:23 -0500194 client, err := newClient()
Jon Perritt816d2a02014-03-11 20:49:46 -0500195 if err != nil {
196 t.Error(err)
197 return
198 }
199
Jon Perritt5eb55b12014-08-18 14:48:23 -0500200 oNames := make([]string, numObjects)
201 for i := 0; i < len(oNames); i++ {
202 oNames[i] = tools.RandomString("test-object-", 8)
203 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500204
Jon Perritt5eb55b12014-08-18 14:48:23 -0500205 cName := tools.RandomString("test-container-", 8)
206 _, err = containers.Create(client, containers.CreateOpts{
207 Name: cName,
208 })
209 if err != nil {
210 t.Error(err)
211 return
212 }
213 defer func() {
214 err = containers.Delete(client, containers.DeleteOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500215 Name: cName,
216 })
217 if err != nil {
218 t.Error(err)
219 return
220 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500221 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500222
Jon Perritt5eb55b12014-08-18 14:48:23 -0500223 oContents := make([]*bytes.Buffer, numObjects)
224 for i := 0; i < numObjects; i++ {
225 oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
226 err = objects.Create(client, objects.CreateOpts{
227 Container: cName,
228 Name: oNames[i],
229 Content: oContents[i],
230 })
231 if err != nil {
232 t.Error(err)
233 return
234 }
235 }
236 defer func() {
Jon Perritt816d2a02014-03-11 20:49:46 -0500237 for i := 0; i < numObjects; i++ {
Jon Perritt5eb55b12014-08-18 14:48:23 -0500238 err = objects.Delete(client, objects.DeleteOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500239 Container: cName,
240 Name: oNames[i],
Jon Perritt816d2a02014-03-11 20:49:46 -0500241 })
Jon Perritt816d2a02014-03-11 20:49:46 -0500242 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500243 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500244
Ash Wilsond88186c2014-09-16 15:44:13 -0400245 pager := objects.List(client, objects.ListOpts{Full: false, Container: cName})
246 ons := make([]string, 0, len(oNames))
247 err = pager.EachPage(func(page pagination.Page) (bool, error) {
248 names, err := objects.ExtractNames(page)
249 if err != nil {
250 return false, err
251 }
252 ons = append(ons, names...)
253
254 return true, nil
Jon Perritt5eb55b12014-08-18 14:48:23 -0500255 })
256 if err != nil {
257 t.Error(err)
258 return
259 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500260 if len(ons) != len(oNames) {
261 t.Errorf("Expected %d names and got %d", len(oNames), len(ons))
262 return
263 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500264
Ash Wilsond88186c2014-09-16 15:44:13 -0400265 pager = objects.List(client, objects.ListOpts{Full: true, Container: cName})
266 ois := make([]objects.Object, 0, len(oNames))
267 err = pager.EachPage(func(page pagination.Page) (bool, error) {
268 info, err := objects.ExtractInfo(page)
269 if err != nil {
270 return false, nil
271 }
272
273 ois = append(ois, info...)
274
275 return true, nil
Jon Perritt5eb55b12014-08-18 14:48:23 -0500276 })
277 if err != nil {
278 t.Error(err)
279 return
280 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500281 if len(ois) != len(oNames) {
282 t.Errorf("Expected %d containers and got %d", len(oNames), len(ois))
283 return
284 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500285
Jon Perritt5eb55b12014-08-18 14:48:23 -0500286 err = objects.Copy(client, objects.CopyOpts{
287 Container: cName,
288 Name: oNames[0],
289 NewContainer: cName,
290 NewName: oNames[1],
291 })
292 if err != nil {
293 t.Error(err)
294 return
295 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500296
Jon Perritt5eb55b12014-08-18 14:48:23 -0500297 dr, err := objects.Download(client, objects.DownloadOpts{
298 Container: cName,
299 Name: oNames[1],
300 })
301 if err != nil {
302 t.Error(err)
303 return
304 }
305 o2Content, err := objects.ExtractContent(dr)
306 if err != nil {
307 t.Error(err)
308 }
309 dr, err = objects.Download(client, objects.DownloadOpts{
310 Container: cName,
311 Name: oNames[0],
312 })
313 if err != nil {
314 t.Error(err)
315 return
316 }
317 o1Content, err := objects.ExtractContent(dr)
318 if err != nil {
319 t.Error(err)
320 return
321 }
322 if string(o2Content) != string(o1Content) {
323 t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content))
324 return
325 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500326
Jon Perritt5eb55b12014-08-18 14:48:23 -0500327 err = objects.Update(client, objects.UpdateOpts{
328 Container: cName,
329 Name: oNames[0],
330 Metadata: metadata,
331 })
332 if err != nil {
333 t.Error(err)
334 return
335 }
336 defer func() {
337 tempMap := make(map[string]string)
338 for k := range metadata {
339 tempMap[k] = ""
340 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500341 err = objects.Update(client, objects.UpdateOpts{
342 Container: cName,
343 Name: oNames[0],
Jon Perritt5eb55b12014-08-18 14:48:23 -0500344 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -0500345 })
346 if err != nil {
347 t.Error(err)
348 return
349 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500350 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500351
Jon Perritt5eb55b12014-08-18 14:48:23 -0500352 gr, err := objects.Get(client, objects.GetOpts{})
353 if err != nil {
354 t.Error(err)
355 return
356 }
357 om := objects.ExtractMetadata(gr)
358 for k := range metadata {
359 if om[k] != metadata[strings.Title(k)] {
360 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -0500361 return
362 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500363 }
364}