blob: 979233556fe573bd6e3e4cd45cf0aec35c93bf07 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001// +build acceptance
2
3package openstack
4
5import (
6 "bytes"
Jon Perrittb6b1d022014-04-14 21:50:45 -05007 storage "github.com/rackspace/gophercloud/openstack/storage/v1"
8 "github.com/rackspace/gophercloud/openstack/storage/v1/accounts"
9 "github.com/rackspace/gophercloud/openstack/storage/v1/containers"
10 "github.com/rackspace/gophercloud/openstack/storage/v1/objects"
Jon Perritt816d2a02014-03-11 20:49:46 -050011 "os"
12 "strings"
13 "testing"
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -070014 "github.com/rackspace/gophercloud/acceptance/tools"
Jon Perritt816d2a02014-03-11 20:49:46 -050015)
16
17var objectStorage = "object-store"
18var metadata = map[string]string{"gopher": "cloud"}
19var numContainers = 2
20var numObjects = 2
21
22func TestAccount(t *testing.T) {
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -070023 ts, err := tools.SetupForList(objectStorage)
Jon Perritt816d2a02014-03-11 20:49:46 -050024 if err != nil {
25 t.Error(err)
26 return
27 }
28
29 region := os.Getenv("OS_REGION_NAME")
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -070030 for _, ep := range ts.EPs {
Jon Perritt816d2a02014-03-11 20:49:46 -050031 if (region != "") && (region != ep.Region) {
32 continue
33 }
34
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -070035 client := storage.NewClient(ep.PublicURL, ts.A, ts.O)
Jon Perritt816d2a02014-03-11 20:49:46 -050036
37 err := accounts.Update(client, accounts.UpdateOpts{
38 Metadata: metadata,
39 })
40 if err != nil {
41 t.Error(err)
42 return
43 }
44 defer func() {
45 tempMap := make(map[string]string)
46 for k := range metadata {
47 tempMap[k] = ""
48 }
49 err = accounts.Update(client, accounts.UpdateOpts{
50 Metadata: tempMap,
51 })
52 if err != nil {
53 t.Error(err)
54 return
55 }
56 }()
57
58 gr, err := accounts.Get(client, accounts.GetOpts{})
59 if err != nil {
60 t.Error(err)
61 return
62 }
Jon Perritteb575642014-04-24 15:16:31 -050063 am := accounts.ExtractMetadata(gr)
Jon Perritt816d2a02014-03-11 20:49:46 -050064 for k := range metadata {
65 if am[k] != metadata[strings.Title(k)] {
66 t.Errorf("Expected custom metadata with key: %s", k)
67 return
68 }
69 }
70 }
71}
72
73func TestContainers(t *testing.T) {
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -070074 ts, err := tools.SetupForList(objectStorage)
Jon Perritt816d2a02014-03-11 20:49:46 -050075 if err != nil {
76 t.Error(err)
77 return
78 }
79
80 region := os.Getenv("OS_REGION_NAME")
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -070081 for _, ep := range ts.EPs {
Jon Perritt816d2a02014-03-11 20:49:46 -050082 if (region != "") && (region != ep.Region) {
83 continue
84 }
85
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -070086 client := storage.NewClient(ep.PublicURL, ts.A, ts.O)
Jon Perritt816d2a02014-03-11 20:49:46 -050087
88 cNames := make([]string, numContainers)
89 for i := 0; i < numContainers; i++ {
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -070090 cNames[i] = tools.RandomString("test-container-", 8)
Jon Perritt816d2a02014-03-11 20:49:46 -050091 }
92
93 for i := 0; i < len(cNames); i++ {
94 _, err := containers.Create(client, containers.CreateOpts{
95 Name: cNames[i],
96 })
97 if err != nil {
98 t.Error(err)
99 return
100 }
101 }
102 defer func() {
103 for i := 0; i < len(cNames); i++ {
104 err = containers.Delete(client, containers.DeleteOpts{
105 Name: cNames[i],
106 })
107 if err != nil {
108 t.Error(err)
109 return
110 }
111 }
112 }()
113
114 lr, err := containers.List(client, containers.ListOpts{
115 Full: false,
116 })
117 if err != nil {
118 t.Error(err)
119 return
120 }
Jon Perritteb575642014-04-24 15:16:31 -0500121 cns, err := containers.ExtractNames(lr)
Jon Perritt816d2a02014-03-11 20:49:46 -0500122 if err != nil {
123 t.Error(err)
124 return
125 }
126 if len(cns) != len(cNames) {
127 t.Errorf("Expected %d names and got %d", len(cNames), len(cns))
128 return
129 }
130
131 lr, err = containers.List(client, containers.ListOpts{
132 Full: true,
133 })
134 if err != nil {
135 t.Error(err)
136 return
137 }
Jon Perritteb575642014-04-24 15:16:31 -0500138 cis, err := containers.ExtractInfo(lr)
Jon Perritt816d2a02014-03-11 20:49:46 -0500139 if err != nil {
140 t.Error(err)
141 return
142 }
143 if len(cis) != len(cNames) {
144 t.Errorf("Expected %d containers and got %d", len(cNames), len(cis))
145 return
146 }
147
148 err = containers.Update(client, containers.UpdateOpts{
149 Name: cNames[0],
150 Metadata: metadata,
151 })
152 if err != nil {
153 t.Error(err)
154 return
155 }
156 defer func() {
157 tempMap := make(map[string]string)
158 for k := range metadata {
159 tempMap[k] = ""
160 }
161 err = containers.Update(client, containers.UpdateOpts{
162 Name: cNames[0],
163 Metadata: tempMap,
164 })
165 if err != nil {
166 t.Error(err)
167 return
168 }
169 }()
170
171 gr, err := containers.Get(client, containers.GetOpts{})
172 if err != nil {
173 t.Error(err)
174 return
175 }
Jon Perritteb575642014-04-24 15:16:31 -0500176 cm := containers.ExtractMetadata(gr)
Jon Perritt816d2a02014-03-11 20:49:46 -0500177 for k := range metadata {
178 if cm[k] != metadata[strings.Title(k)] {
179 t.Errorf("Expected custom metadata with key: %s", k)
180 return
181 }
182 }
183 }
184}
185
186func TestObjects(t *testing.T) {
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -0700187 ts, err := tools.SetupForList(objectStorage)
Jon Perritt816d2a02014-03-11 20:49:46 -0500188 if err != nil {
189 t.Error(err)
190 return
191 }
192
193 region := os.Getenv("OS_REGION_NAME")
194
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -0700195 for _, ep := range ts.EPs {
Jon Perritt816d2a02014-03-11 20:49:46 -0500196 if (region != "") && (region != ep.Region) {
197 continue
198 }
199
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -0700200 client := storage.NewClient(ep.PublicURL, ts.A, ts.O)
Jon Perritt816d2a02014-03-11 20:49:46 -0500201
202 oNames := make([]string, numObjects)
203 for i := 0; i < len(oNames); i++ {
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -0700204 oNames[i] = tools.RandomString("test-object-", 8)
Jon Perritt816d2a02014-03-11 20:49:46 -0500205 }
206
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -0700207 cName := tools.RandomString("test-container-", 8)
Jon Perritt816d2a02014-03-11 20:49:46 -0500208 _, err := containers.Create(client, containers.CreateOpts{
209 Name: cName,
210 })
211 if err != nil {
212 t.Error(err)
213 return
214 }
215 defer func() {
216 err = containers.Delete(client, containers.DeleteOpts{
217 Name: cName,
218 })
219 if err != nil {
220 t.Error(err)
221 return
222 }
223 }()
224
225 oContents := make([]*bytes.Buffer, numObjects)
226 for i := 0; i < numObjects; i++ {
Samuel A. Falvo II28cadc22014-07-31 14:35:40 -0700227 oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
Jon Perritt816d2a02014-03-11 20:49:46 -0500228 err = objects.Create(client, objects.CreateOpts{
229 Container: cName,
230 Name: oNames[i],
231 Content: oContents[i],
232 })
233 if err != nil {
234 t.Error(err)
235 return
236 }
237 }
238 defer func() {
239 for i := 0; i < numObjects; i++ {
240 err = objects.Delete(client, objects.DeleteOpts{
241 Container: cName,
242 Name: oNames[i],
243 })
244 }
245 }()
246
247 lr, err := objects.List(client, objects.ListOpts{
248 Full: false,
249 Container: cName,
250 })
251 if err != nil {
252 t.Error(err)
253 return
254 }
Jon Perritteb575642014-04-24 15:16:31 -0500255 ons, err := objects.ExtractNames(lr)
256 if err != nil {
257 t.Error(err)
258 return
259 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500260 if len(ons) != len(oNames) {
261 t.Errorf("Expected %d names and got %d", len(oNames), len(ons))
262 return
263 }
264
265 lr, err = objects.List(client, objects.ListOpts{
266 Full: true,
267 Container: cName,
268 })
269 if err != nil {
270 t.Error(err)
271 return
272 }
Jon Perritteb575642014-04-24 15:16:31 -0500273 ois, err := objects.ExtractInfo(lr)
Jon Perritt816d2a02014-03-11 20:49:46 -0500274 if err != nil {
275 t.Error(err)
276 return
277 }
278 if len(ois) != len(oNames) {
279 t.Errorf("Expected %d containers and got %d", len(oNames), len(ois))
280 return
281 }
282
283 err = objects.Copy(client, objects.CopyOpts{
284 Container: cName,
285 Name: oNames[0],
286 NewContainer: cName,
287 NewName: oNames[1],
288 })
289 if err != nil {
290 t.Error(err)
291 return
292 }
293
294 dr, err := objects.Download(client, objects.DownloadOpts{
295 Container: cName,
296 Name: oNames[1],
297 })
298 if err != nil {
299 t.Error(err)
300 return
301 }
Jon Perritteb575642014-04-24 15:16:31 -0500302 o2Content, err := objects.ExtractContent(dr)
303 if err != nil {
304 t.Error(err)
305 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500306 dr, err = objects.Download(client, objects.DownloadOpts{
307 Container: cName,
308 Name: oNames[0],
309 })
310 if err != nil {
311 t.Error(err)
312 return
313 }
Jon Perritteb575642014-04-24 15:16:31 -0500314 o1Content, err := objects.ExtractContent(dr)
315 if err != nil {
316 t.Error(err)
317 return
318 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500319 if string(o2Content) != string(o1Content) {
320 t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content))
321 return
322 }
323
324 err = objects.Update(client, objects.UpdateOpts{
325 Container: cName,
326 Name: oNames[0],
327 Metadata: metadata,
328 })
329 if err != nil {
330 t.Error(err)
331 return
332 }
333 defer func() {
334 tempMap := make(map[string]string)
335 for k := range metadata {
336 tempMap[k] = ""
337 }
338 err = objects.Update(client, objects.UpdateOpts{
339 Container: cName,
340 Name: oNames[0],
341 Metadata: tempMap,
342 })
343 if err != nil {
344 t.Error(err)
345 return
346 }
347 }()
348
349 gr, err := objects.Get(client, objects.GetOpts{})
350 if err != nil {
351 t.Error(err)
352 return
353 }
Jon Perritteb575642014-04-24 15:16:31 -0500354 om := objects.ExtractMetadata(gr)
Jon Perritt816d2a02014-03-11 20:49:46 -0500355 for k := range metadata {
356 if om[k] != metadata[strings.Title(k)] {
357 t.Errorf("Expected custom metadata with key: %s", k)
358 return
359 }
360 }
361 }
362}