blob: bd12a33eaed198fb70de465c105a07b1d9addfea [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 Perritta8c3b812014-08-19 22:02:31 -050014 "os"
Jon Perritt816d2a02014-03-11 20:49:46 -050015 "strings"
16 "testing"
17)
18
Jon Perritt816d2a02014-03-11 20:49:46 -050019var metadata = map[string]string{"gopher": "cloud"}
20var numContainers = 2
21var numObjects = 2
22
Jon Perritt5eb55b12014-08-18 14:48:23 -050023func newClient() (*storage.Client, error) {
24 ao, err := utils.AuthOptions()
25 if err != nil {
26 return nil, err
27 }
28
Jon Perritt6e898782014-08-19 15:58:11 -050029 client, err := ao.NewClient(identity.EndpointOpts{
Jon Perritta8c3b812014-08-19 22:02:31 -050030 Region: os.Getenv("OS_REGION_NAME"),
31 Type: "object-store",
Jon Perritt5eb55b12014-08-18 14:48:23 -050032 })
33 if err != nil {
34 return nil, err
35 }
36
37 return storage.NewClient(client.Endpoint, client.Authority, client.Options), nil
38}
39
Jon Perritt816d2a02014-03-11 20:49:46 -050040func TestAccount(t *testing.T) {
Jon Perritt5eb55b12014-08-18 14:48:23 -050041 client, err := newClient()
Jon Perritt816d2a02014-03-11 20:49:46 -050042 if err != nil {
43 t.Error(err)
44 return
45 }
46
Jon Perritt5eb55b12014-08-18 14:48:23 -050047 err = accounts.Update(client, accounts.UpdateOpts{
48 Metadata: metadata,
49 })
50 if err != nil {
51 t.Error(err)
52 return
53 }
54 defer func() {
55 tempMap := make(map[string]string)
56 for k := range metadata {
57 tempMap[k] = ""
Jon Perritt816d2a02014-03-11 20:49:46 -050058 }
Jon Perritt5eb55b12014-08-18 14:48:23 -050059 err = accounts.Update(client, accounts.UpdateOpts{
60 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -050061 })
62 if err != nil {
63 t.Error(err)
64 return
65 }
Jon Perritt5eb55b12014-08-18 14:48:23 -050066 }()
Jon Perritt816d2a02014-03-11 20:49:46 -050067
Jon Perritt5eb55b12014-08-18 14:48:23 -050068 gr, err := accounts.Get(client, accounts.GetOpts{})
69 if err != nil {
70 t.Error(err)
71 return
72 }
73 am := accounts.ExtractMetadata(gr)
74 for k := range metadata {
75 if am[k] != metadata[strings.Title(k)] {
76 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -050077 return
78 }
Jon Perritt816d2a02014-03-11 20:49:46 -050079 }
80}
81
82func TestContainers(t *testing.T) {
Jon Perritt5eb55b12014-08-18 14:48:23 -050083 client, err := newClient()
Jon Perritt816d2a02014-03-11 20:49:46 -050084 if err != nil {
85 t.Error(err)
86 return
87 }
88
Jon Perritt5eb55b12014-08-18 14:48:23 -050089 cNames := make([]string, numContainers)
90 for i := 0; i < numContainers; i++ {
91 cNames[i] = tools.RandomString("test-container-", 8)
92 }
93
94 for i := 0; i < len(cNames); i++ {
95 _, err := containers.Create(client, containers.CreateOpts{
96 Name: cNames[i],
97 })
98 if err != nil {
99 t.Error(err)
100 return
Jon Perritt816d2a02014-03-11 20:49:46 -0500101 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500102 }
103 defer func() {
Jon Perritt816d2a02014-03-11 20:49:46 -0500104 for i := 0; i < len(cNames); i++ {
Jon Perritt5eb55b12014-08-18 14:48:23 -0500105 err = containers.Delete(client, containers.DeleteOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500106 Name: cNames[i],
107 })
108 if err != nil {
109 t.Error(err)
110 return
111 }
112 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500113 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500114
Jon Perritt5eb55b12014-08-18 14:48:23 -0500115 lr, err := containers.List(client, containers.ListOpts{
116 Full: false,
117 })
118 if err != nil {
119 t.Error(err)
120 return
121 }
122 cns, err := containers.ExtractNames(lr)
123 if err != nil {
124 t.Error(err)
125 return
126 }
127 if len(cns) != len(cNames) {
128 t.Errorf("Expected %d names and got %d", len(cNames), len(cns))
129 return
130 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500131
Jon Perritt5eb55b12014-08-18 14:48:23 -0500132 lr, err = containers.List(client, containers.ListOpts{
133 Full: true,
134 })
135 if err != nil {
136 t.Error(err)
137 return
138 }
139 cis, err := containers.ExtractInfo(lr)
140 if err != nil {
141 t.Error(err)
142 return
143 }
144 if len(cis) != len(cNames) {
145 t.Errorf("Expected %d containers and got %d", len(cNames), len(cis))
146 return
147 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500148
Jon Perritt5eb55b12014-08-18 14:48:23 -0500149 err = containers.Update(client, containers.UpdateOpts{
150 Name: cNames[0],
151 Metadata: metadata,
152 })
153 if err != nil {
154 t.Error(err)
155 return
156 }
157 defer func() {
158 tempMap := make(map[string]string)
159 for k := range metadata {
160 tempMap[k] = ""
161 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500162 err = containers.Update(client, containers.UpdateOpts{
163 Name: cNames[0],
Jon Perritt5eb55b12014-08-18 14:48:23 -0500164 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -0500165 })
166 if err != nil {
167 t.Error(err)
168 return
169 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500170 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500171
Jon Perritt5eb55b12014-08-18 14:48:23 -0500172 gr, err := containers.Get(client, containers.GetOpts{})
173 if err != nil {
174 t.Error(err)
175 return
176 }
177 cm := containers.ExtractMetadata(gr)
178 for k := range metadata {
179 if cm[k] != metadata[strings.Title(k)] {
180 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -0500181 return
182 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500183 }
184}
185
186func TestObjects(t *testing.T) {
Jon Perritt5eb55b12014-08-18 14:48:23 -0500187 client, err := newClient()
Jon Perritt816d2a02014-03-11 20:49:46 -0500188 if err != nil {
189 t.Error(err)
190 return
191 }
192
Jon Perritt5eb55b12014-08-18 14:48:23 -0500193 oNames := make([]string, numObjects)
194 for i := 0; i < len(oNames); i++ {
195 oNames[i] = tools.RandomString("test-object-", 8)
196 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500197
Jon Perritt5eb55b12014-08-18 14:48:23 -0500198 cName := tools.RandomString("test-container-", 8)
199 _, err = containers.Create(client, containers.CreateOpts{
200 Name: cName,
201 })
202 if err != nil {
203 t.Error(err)
204 return
205 }
206 defer func() {
207 err = containers.Delete(client, containers.DeleteOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500208 Name: cName,
209 })
210 if err != nil {
211 t.Error(err)
212 return
213 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500214 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500215
Jon Perritt5eb55b12014-08-18 14:48:23 -0500216 oContents := make([]*bytes.Buffer, numObjects)
217 for i := 0; i < numObjects; i++ {
218 oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
219 err = objects.Create(client, objects.CreateOpts{
220 Container: cName,
221 Name: oNames[i],
222 Content: oContents[i],
223 })
224 if err != nil {
225 t.Error(err)
226 return
227 }
228 }
229 defer func() {
Jon Perritt816d2a02014-03-11 20:49:46 -0500230 for i := 0; i < numObjects; i++ {
Jon Perritt5eb55b12014-08-18 14:48:23 -0500231 err = objects.Delete(client, objects.DeleteOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500232 Container: cName,
233 Name: oNames[i],
Jon Perritt816d2a02014-03-11 20:49:46 -0500234 })
Jon Perritt816d2a02014-03-11 20:49:46 -0500235 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500236 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500237
Jon Perritt5eb55b12014-08-18 14:48:23 -0500238 lr, err := objects.List(client, objects.ListOpts{
239 Full: false,
240 Container: cName,
241 })
242 if err != nil {
243 t.Error(err)
244 return
245 }
246 ons, err := objects.ExtractNames(lr)
247 if err != nil {
248 t.Error(err)
249 return
250 }
251 if len(ons) != len(oNames) {
252 t.Errorf("Expected %d names and got %d", len(oNames), len(ons))
253 return
254 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500255
Jon Perritt5eb55b12014-08-18 14:48:23 -0500256 lr, err = objects.List(client, objects.ListOpts{
257 Full: true,
258 Container: cName,
259 })
260 if err != nil {
261 t.Error(err)
262 return
263 }
264 ois, err := objects.ExtractInfo(lr)
265 if err != nil {
266 t.Error(err)
267 return
268 }
269 if len(ois) != len(oNames) {
270 t.Errorf("Expected %d containers and got %d", len(oNames), len(ois))
271 return
272 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500273
Jon Perritt5eb55b12014-08-18 14:48:23 -0500274 err = objects.Copy(client, objects.CopyOpts{
275 Container: cName,
276 Name: oNames[0],
277 NewContainer: cName,
278 NewName: oNames[1],
279 })
280 if err != nil {
281 t.Error(err)
282 return
283 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500284
Jon Perritt5eb55b12014-08-18 14:48:23 -0500285 dr, err := objects.Download(client, objects.DownloadOpts{
286 Container: cName,
287 Name: oNames[1],
288 })
289 if err != nil {
290 t.Error(err)
291 return
292 }
293 o2Content, err := objects.ExtractContent(dr)
294 if err != nil {
295 t.Error(err)
296 }
297 dr, err = objects.Download(client, objects.DownloadOpts{
298 Container: cName,
299 Name: oNames[0],
300 })
301 if err != nil {
302 t.Error(err)
303 return
304 }
305 o1Content, err := objects.ExtractContent(dr)
306 if err != nil {
307 t.Error(err)
308 return
309 }
310 if string(o2Content) != string(o1Content) {
311 t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content))
312 return
313 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500314
Jon Perritt5eb55b12014-08-18 14:48:23 -0500315 err = objects.Update(client, objects.UpdateOpts{
316 Container: cName,
317 Name: oNames[0],
318 Metadata: metadata,
319 })
320 if err != nil {
321 t.Error(err)
322 return
323 }
324 defer func() {
325 tempMap := make(map[string]string)
326 for k := range metadata {
327 tempMap[k] = ""
328 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500329 err = objects.Update(client, objects.UpdateOpts{
330 Container: cName,
331 Name: oNames[0],
Jon Perritt5eb55b12014-08-18 14:48:23 -0500332 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -0500333 })
334 if err != nil {
335 t.Error(err)
336 return
337 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500338 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500339
Jon Perritt5eb55b12014-08-18 14:48:23 -0500340 gr, err := objects.Get(client, objects.GetOpts{})
341 if err != nil {
342 t.Error(err)
343 return
344 }
345 om := objects.ExtractMetadata(gr)
346 for k := range metadata {
347 if om[k] != metadata[strings.Title(k)] {
348 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -0500349 return
350 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500351 }
352}