blob: 0cd3c12fd2f3906e1e1948ca76b2217ffee94d2c [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001// +build acceptance
2
3package openstack
4
5import (
6 "bytes"
Jon Perritt5eb55b12014-08-18 14:48:23 -05007 "github.com/rackspace/gophercloud/acceptance/tools"
8 identity "github.com/rackspace/gophercloud/openstack/identity/v2"
Jon Perrittb6b1d022014-04-14 21:50:45 -05009 storage "github.com/rackspace/gophercloud/openstack/storage/v1"
10 "github.com/rackspace/gophercloud/openstack/storage/v1/accounts"
11 "github.com/rackspace/gophercloud/openstack/storage/v1/containers"
12 "github.com/rackspace/gophercloud/openstack/storage/v1/objects"
Jon Perritt5eb55b12014-08-18 14:48:23 -050013 "github.com/rackspace/gophercloud/openstack/utils"
Jon Perritt816d2a02014-03-11 20:49:46 -050014 "strings"
15 "testing"
16)
17
Jon Perritt816d2a02014-03-11 20:49:46 -050018var metadata = map[string]string{"gopher": "cloud"}
19var numContainers = 2
20var numObjects = 2
21
Jon Perritt5eb55b12014-08-18 14:48:23 -050022func newClient() (*storage.Client, error) {
23 ao, err := utils.AuthOptions()
24 if err != nil {
25 return nil, err
26 }
27
Jon Perritt6e898782014-08-19 15:58:11 -050028 client, err := ao.NewClient(identity.EndpointOpts{
Jon Perritt5eb55b12014-08-18 14:48:23 -050029 Type: "object-store",
30 })
31 if err != nil {
32 return nil, err
33 }
34
35 return storage.NewClient(client.Endpoint, client.Authority, client.Options), nil
36}
37
Jon Perritt816d2a02014-03-11 20:49:46 -050038func TestAccount(t *testing.T) {
Jon Perritt5eb55b12014-08-18 14:48:23 -050039 client, err := newClient()
Jon Perritt816d2a02014-03-11 20:49:46 -050040 if err != nil {
41 t.Error(err)
42 return
43 }
44
Jon Perritt5eb55b12014-08-18 14:48:23 -050045 err = accounts.Update(client, accounts.UpdateOpts{
46 Metadata: metadata,
47 })
48 if err != nil {
49 t.Error(err)
50 return
51 }
52 defer func() {
53 tempMap := make(map[string]string)
54 for k := range metadata {
55 tempMap[k] = ""
Jon Perritt816d2a02014-03-11 20:49:46 -050056 }
Jon Perritt5eb55b12014-08-18 14:48:23 -050057 err = accounts.Update(client, accounts.UpdateOpts{
58 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -050059 })
60 if err != nil {
61 t.Error(err)
62 return
63 }
Jon Perritt5eb55b12014-08-18 14:48:23 -050064 }()
Jon Perritt816d2a02014-03-11 20:49:46 -050065
Jon Perritt5eb55b12014-08-18 14:48:23 -050066 gr, err := accounts.Get(client, accounts.GetOpts{})
67 if err != nil {
68 t.Error(err)
69 return
70 }
71 am := accounts.ExtractMetadata(gr)
72 for k := range metadata {
73 if am[k] != metadata[strings.Title(k)] {
74 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -050075 return
76 }
Jon Perritt816d2a02014-03-11 20:49:46 -050077 }
78}
79
80func TestContainers(t *testing.T) {
Jon Perritt5eb55b12014-08-18 14:48:23 -050081 client, err := newClient()
Jon Perritt816d2a02014-03-11 20:49:46 -050082 if err != nil {
83 t.Error(err)
84 return
85 }
86
Jon Perritt5eb55b12014-08-18 14:48:23 -050087 cNames := make([]string, numContainers)
88 for i := 0; i < numContainers; i++ {
89 cNames[i] = tools.RandomString("test-container-", 8)
90 }
91
92 for i := 0; i < len(cNames); i++ {
93 _, err := containers.Create(client, containers.CreateOpts{
94 Name: cNames[i],
95 })
96 if err != nil {
97 t.Error(err)
98 return
Jon Perritt816d2a02014-03-11 20:49:46 -050099 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500100 }
101 defer func() {
Jon Perritt816d2a02014-03-11 20:49:46 -0500102 for i := 0; i < len(cNames); i++ {
Jon Perritt5eb55b12014-08-18 14:48:23 -0500103 err = containers.Delete(client, containers.DeleteOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500104 Name: cNames[i],
105 })
106 if err != nil {
107 t.Error(err)
108 return
109 }
110 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500111 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500112
Jon Perritt5eb55b12014-08-18 14:48:23 -0500113 lr, err := containers.List(client, containers.ListOpts{
114 Full: false,
115 })
116 if err != nil {
117 t.Error(err)
118 return
119 }
120 cns, err := containers.ExtractNames(lr)
121 if err != nil {
122 t.Error(err)
123 return
124 }
125 if len(cns) != len(cNames) {
126 t.Errorf("Expected %d names and got %d", len(cNames), len(cns))
127 return
128 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500129
Jon Perritt5eb55b12014-08-18 14:48:23 -0500130 lr, err = containers.List(client, containers.ListOpts{
131 Full: true,
132 })
133 if err != nil {
134 t.Error(err)
135 return
136 }
137 cis, err := containers.ExtractInfo(lr)
138 if err != nil {
139 t.Error(err)
140 return
141 }
142 if len(cis) != len(cNames) {
143 t.Errorf("Expected %d containers and got %d", len(cNames), len(cis))
144 return
145 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500146
Jon Perritt5eb55b12014-08-18 14:48:23 -0500147 err = containers.Update(client, containers.UpdateOpts{
148 Name: cNames[0],
149 Metadata: metadata,
150 })
151 if err != nil {
152 t.Error(err)
153 return
154 }
155 defer func() {
156 tempMap := make(map[string]string)
157 for k := range metadata {
158 tempMap[k] = ""
159 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500160 err = containers.Update(client, containers.UpdateOpts{
161 Name: cNames[0],
Jon Perritt5eb55b12014-08-18 14:48:23 -0500162 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -0500163 })
164 if err != nil {
165 t.Error(err)
166 return
167 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500168 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500169
Jon Perritt5eb55b12014-08-18 14:48:23 -0500170 gr, err := containers.Get(client, containers.GetOpts{})
171 if err != nil {
172 t.Error(err)
173 return
174 }
175 cm := containers.ExtractMetadata(gr)
176 for k := range metadata {
177 if cm[k] != metadata[strings.Title(k)] {
178 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -0500179 return
180 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500181 }
182}
183
184func TestObjects(t *testing.T) {
Jon Perritt5eb55b12014-08-18 14:48:23 -0500185 client, err := newClient()
Jon Perritt816d2a02014-03-11 20:49:46 -0500186 if err != nil {
187 t.Error(err)
188 return
189 }
190
Jon Perritt5eb55b12014-08-18 14:48:23 -0500191 oNames := make([]string, numObjects)
192 for i := 0; i < len(oNames); i++ {
193 oNames[i] = tools.RandomString("test-object-", 8)
194 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500195
Jon Perritt5eb55b12014-08-18 14:48:23 -0500196 cName := tools.RandomString("test-container-", 8)
197 _, err = containers.Create(client, containers.CreateOpts{
198 Name: cName,
199 })
200 if err != nil {
201 t.Error(err)
202 return
203 }
204 defer func() {
205 err = containers.Delete(client, containers.DeleteOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500206 Name: cName,
207 })
208 if err != nil {
209 t.Error(err)
210 return
211 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500212 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500213
Jon Perritt5eb55b12014-08-18 14:48:23 -0500214 oContents := make([]*bytes.Buffer, numObjects)
215 for i := 0; i < numObjects; i++ {
216 oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
217 err = objects.Create(client, objects.CreateOpts{
218 Container: cName,
219 Name: oNames[i],
220 Content: oContents[i],
221 })
222 if err != nil {
223 t.Error(err)
224 return
225 }
226 }
227 defer func() {
Jon Perritt816d2a02014-03-11 20:49:46 -0500228 for i := 0; i < numObjects; i++ {
Jon Perritt5eb55b12014-08-18 14:48:23 -0500229 err = objects.Delete(client, objects.DeleteOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500230 Container: cName,
231 Name: oNames[i],
Jon Perritt816d2a02014-03-11 20:49:46 -0500232 })
Jon Perritt816d2a02014-03-11 20:49:46 -0500233 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500234 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500235
Jon Perritt5eb55b12014-08-18 14:48:23 -0500236 lr, err := objects.List(client, objects.ListOpts{
237 Full: false,
238 Container: cName,
239 })
240 if err != nil {
241 t.Error(err)
242 return
243 }
244 ons, err := objects.ExtractNames(lr)
245 if err != nil {
246 t.Error(err)
247 return
248 }
249 if len(ons) != len(oNames) {
250 t.Errorf("Expected %d names and got %d", len(oNames), len(ons))
251 return
252 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500253
Jon Perritt5eb55b12014-08-18 14:48:23 -0500254 lr, err = objects.List(client, objects.ListOpts{
255 Full: true,
256 Container: cName,
257 })
258 if err != nil {
259 t.Error(err)
260 return
261 }
262 ois, err := objects.ExtractInfo(lr)
263 if err != nil {
264 t.Error(err)
265 return
266 }
267 if len(ois) != len(oNames) {
268 t.Errorf("Expected %d containers and got %d", len(oNames), len(ois))
269 return
270 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500271
Jon Perritt5eb55b12014-08-18 14:48:23 -0500272 err = objects.Copy(client, objects.CopyOpts{
273 Container: cName,
274 Name: oNames[0],
275 NewContainer: cName,
276 NewName: oNames[1],
277 })
278 if err != nil {
279 t.Error(err)
280 return
281 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500282
Jon Perritt5eb55b12014-08-18 14:48:23 -0500283 dr, err := objects.Download(client, objects.DownloadOpts{
284 Container: cName,
285 Name: oNames[1],
286 })
287 if err != nil {
288 t.Error(err)
289 return
290 }
291 o2Content, err := objects.ExtractContent(dr)
292 if err != nil {
293 t.Error(err)
294 }
295 dr, err = objects.Download(client, objects.DownloadOpts{
296 Container: cName,
297 Name: oNames[0],
298 })
299 if err != nil {
300 t.Error(err)
301 return
302 }
303 o1Content, err := objects.ExtractContent(dr)
304 if err != nil {
305 t.Error(err)
306 return
307 }
308 if string(o2Content) != string(o1Content) {
309 t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content))
310 return
311 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500312
Jon Perritt5eb55b12014-08-18 14:48:23 -0500313 err = objects.Update(client, objects.UpdateOpts{
314 Container: cName,
315 Name: oNames[0],
316 Metadata: metadata,
317 })
318 if err != nil {
319 t.Error(err)
320 return
321 }
322 defer func() {
323 tempMap := make(map[string]string)
324 for k := range metadata {
325 tempMap[k] = ""
326 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500327 err = objects.Update(client, objects.UpdateOpts{
328 Container: cName,
329 Name: oNames[0],
Jon Perritt5eb55b12014-08-18 14:48:23 -0500330 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -0500331 })
332 if err != nil {
333 t.Error(err)
334 return
335 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500336 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500337
Jon Perritt5eb55b12014-08-18 14:48:23 -0500338 gr, err := objects.Get(client, objects.GetOpts{})
339 if err != nil {
340 t.Error(err)
341 return
342 }
343 om := objects.ExtractMetadata(gr)
344 for k := range metadata {
345 if om[k] != metadata[strings.Title(k)] {
346 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -0500347 return
348 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500349 }
350}