blob: 9a9a65f1f5bdedc1fff2798202f8b880adbf0f09 [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
Jon Perritt5eb55b12014-08-18 14:48:23 -0500245 lr, err := objects.List(client, objects.ListOpts{
246 Full: false,
247 Container: cName,
248 })
249 if err != nil {
250 t.Error(err)
251 return
252 }
253 ons, err := objects.ExtractNames(lr)
254 if err != nil {
255 t.Error(err)
256 return
257 }
258 if len(ons) != len(oNames) {
259 t.Errorf("Expected %d names and got %d", len(oNames), len(ons))
260 return
261 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500262
Jon Perritt5eb55b12014-08-18 14:48:23 -0500263 lr, err = objects.List(client, objects.ListOpts{
264 Full: true,
265 Container: cName,
266 })
267 if err != nil {
268 t.Error(err)
269 return
270 }
271 ois, err := objects.ExtractInfo(lr)
272 if err != nil {
273 t.Error(err)
274 return
275 }
276 if len(ois) != len(oNames) {
277 t.Errorf("Expected %d containers and got %d", len(oNames), len(ois))
278 return
279 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500280
Jon Perritt5eb55b12014-08-18 14:48:23 -0500281 err = objects.Copy(client, objects.CopyOpts{
282 Container: cName,
283 Name: oNames[0],
284 NewContainer: cName,
285 NewName: oNames[1],
286 })
287 if err != nil {
288 t.Error(err)
289 return
290 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500291
Jon Perritt5eb55b12014-08-18 14:48:23 -0500292 dr, err := objects.Download(client, objects.DownloadOpts{
293 Container: cName,
294 Name: oNames[1],
295 })
296 if err != nil {
297 t.Error(err)
298 return
299 }
300 o2Content, err := objects.ExtractContent(dr)
301 if err != nil {
302 t.Error(err)
303 }
304 dr, err = objects.Download(client, objects.DownloadOpts{
305 Container: cName,
306 Name: oNames[0],
307 })
308 if err != nil {
309 t.Error(err)
310 return
311 }
312 o1Content, err := objects.ExtractContent(dr)
313 if err != nil {
314 t.Error(err)
315 return
316 }
317 if string(o2Content) != string(o1Content) {
318 t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content))
319 return
320 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500321
Jon Perritt5eb55b12014-08-18 14:48:23 -0500322 err = objects.Update(client, objects.UpdateOpts{
323 Container: cName,
324 Name: oNames[0],
325 Metadata: metadata,
326 })
327 if err != nil {
328 t.Error(err)
329 return
330 }
331 defer func() {
332 tempMap := make(map[string]string)
333 for k := range metadata {
334 tempMap[k] = ""
335 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500336 err = objects.Update(client, objects.UpdateOpts{
337 Container: cName,
338 Name: oNames[0],
Jon Perritt5eb55b12014-08-18 14:48:23 -0500339 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -0500340 })
341 if err != nil {
342 t.Error(err)
343 return
344 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500345 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500346
Jon Perritt5eb55b12014-08-18 14:48:23 -0500347 gr, err := objects.Get(client, objects.GetOpts{})
348 if err != nil {
349 t.Error(err)
350 return
351 }
352 om := objects.ExtractMetadata(gr)
353 for k := range metadata {
354 if om[k] != metadata[strings.Title(k)] {
355 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -0500356 return
357 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500358 }
359}