blob: 1cb3badb9b1e0d120d2fd3c4ffd7554632fc72e1 [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"
Jon Perrittb6b1d022014-04-14 21:50:45 -05008 storage "github.com/rackspace/gophercloud/openstack/storage/v1"
9 "github.com/rackspace/gophercloud/openstack/storage/v1/accounts"
10 "github.com/rackspace/gophercloud/openstack/storage/v1/containers"
11 "github.com/rackspace/gophercloud/openstack/storage/v1/objects"
Jon Perritt5eb55b12014-08-18 14:48:23 -050012 "github.com/rackspace/gophercloud/openstack/utils"
Jon Perritta8c3b812014-08-19 22:02:31 -050013 "os"
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 Perritt5f4b5c22014-08-27 11:44:07 -050028 client, err := utils.NewClient(ao, utils.EndpointOpts{
Jon Perritta8c3b812014-08-19 22:02:31 -050029 Region: os.Getenv("OS_REGION_NAME"),
30 Type: "object-store",
Jon Perritt5eb55b12014-08-18 14:48:23 -050031 })
32 if err != nil {
33 return nil, err
34 }
35
36 return storage.NewClient(client.Endpoint, client.Authority, client.Options), nil
37}
38
Jon Perritt816d2a02014-03-11 20:49:46 -050039func TestAccount(t *testing.T) {
Jon Perritt5eb55b12014-08-18 14:48:23 -050040 client, err := newClient()
Jon Perritt816d2a02014-03-11 20:49:46 -050041 if err != nil {
42 t.Error(err)
43 return
44 }
45
Jon Perritt5eb55b12014-08-18 14:48:23 -050046 err = accounts.Update(client, accounts.UpdateOpts{
47 Metadata: metadata,
48 })
49 if err != nil {
50 t.Error(err)
51 return
52 }
53 defer func() {
54 tempMap := make(map[string]string)
55 for k := range metadata {
56 tempMap[k] = ""
Jon Perritt816d2a02014-03-11 20:49:46 -050057 }
Jon Perritt5eb55b12014-08-18 14:48:23 -050058 err = accounts.Update(client, accounts.UpdateOpts{
59 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -050060 })
61 if err != nil {
62 t.Error(err)
63 return
64 }
Jon Perritt5eb55b12014-08-18 14:48:23 -050065 }()
Jon Perritt816d2a02014-03-11 20:49:46 -050066
Jon Perritt5eb55b12014-08-18 14:48:23 -050067 gr, err := accounts.Get(client, accounts.GetOpts{})
68 if err != nil {
69 t.Error(err)
70 return
71 }
72 am := accounts.ExtractMetadata(gr)
73 for k := range metadata {
74 if am[k] != metadata[strings.Title(k)] {
75 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -050076 return
77 }
Jon Perritt816d2a02014-03-11 20:49:46 -050078 }
79}
80
81func TestContainers(t *testing.T) {
Jon Perritt5eb55b12014-08-18 14:48:23 -050082 client, err := newClient()
Jon Perritt816d2a02014-03-11 20:49:46 -050083 if err != nil {
84 t.Error(err)
85 return
86 }
87
Jon Perritt5eb55b12014-08-18 14:48:23 -050088 cNames := make([]string, numContainers)
89 for i := 0; i < numContainers; i++ {
90 cNames[i] = tools.RandomString("test-container-", 8)
91 }
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
Jon Perritt816d2a02014-03-11 20:49:46 -0500100 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500101 }
102 defer func() {
Jon Perritt816d2a02014-03-11 20:49:46 -0500103 for i := 0; i < len(cNames); i++ {
Jon Perritt5eb55b12014-08-18 14:48:23 -0500104 err = containers.Delete(client, containers.DeleteOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500105 Name: cNames[i],
106 })
107 if err != nil {
108 t.Error(err)
109 return
110 }
111 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500112 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500113
Jon Perritt5eb55b12014-08-18 14:48:23 -0500114 lr, err := containers.List(client, containers.ListOpts{
115 Full: false,
116 })
117 if err != nil {
118 t.Error(err)
119 return
120 }
121 cns, err := containers.ExtractNames(lr)
122 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 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500130
Jon Perritt5eb55b12014-08-18 14:48:23 -0500131 lr, err = containers.List(client, containers.ListOpts{
132 Full: true,
133 })
134 if err != nil {
135 t.Error(err)
136 return
137 }
138 cis, err := containers.ExtractInfo(lr)
139 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 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500147
Jon Perritt5eb55b12014-08-18 14:48:23 -0500148 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 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500161 err = containers.Update(client, containers.UpdateOpts{
162 Name: cNames[0],
Jon Perritt5eb55b12014-08-18 14:48:23 -0500163 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -0500164 })
165 if err != nil {
166 t.Error(err)
167 return
168 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500169 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500170
Jon Perritt5eb55b12014-08-18 14:48:23 -0500171 gr, err := containers.Get(client, containers.GetOpts{})
172 if err != nil {
173 t.Error(err)
174 return
175 }
176 cm := containers.ExtractMetadata(gr)
177 for k := range metadata {
178 if cm[k] != metadata[strings.Title(k)] {
179 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -0500180 return
181 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500182 }
183}
184
185func TestObjects(t *testing.T) {
Jon Perritt5eb55b12014-08-18 14:48:23 -0500186 client, err := newClient()
Jon Perritt816d2a02014-03-11 20:49:46 -0500187 if err != nil {
188 t.Error(err)
189 return
190 }
191
Jon Perritt5eb55b12014-08-18 14:48:23 -0500192 oNames := make([]string, numObjects)
193 for i := 0; i < len(oNames); i++ {
194 oNames[i] = tools.RandomString("test-object-", 8)
195 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500196
Jon Perritt5eb55b12014-08-18 14:48:23 -0500197 cName := tools.RandomString("test-container-", 8)
198 _, err = containers.Create(client, containers.CreateOpts{
199 Name: cName,
200 })
201 if err != nil {
202 t.Error(err)
203 return
204 }
205 defer func() {
206 err = containers.Delete(client, containers.DeleteOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500207 Name: cName,
208 })
209 if err != nil {
210 t.Error(err)
211 return
212 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500213 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500214
Jon Perritt5eb55b12014-08-18 14:48:23 -0500215 oContents := make([]*bytes.Buffer, numObjects)
216 for i := 0; i < numObjects; i++ {
217 oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
218 err = objects.Create(client, objects.CreateOpts{
219 Container: cName,
220 Name: oNames[i],
221 Content: oContents[i],
222 })
223 if err != nil {
224 t.Error(err)
225 return
226 }
227 }
228 defer func() {
Jon Perritt816d2a02014-03-11 20:49:46 -0500229 for i := 0; i < numObjects; i++ {
Jon Perritt5eb55b12014-08-18 14:48:23 -0500230 err = objects.Delete(client, objects.DeleteOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500231 Container: cName,
232 Name: oNames[i],
Jon Perritt816d2a02014-03-11 20:49:46 -0500233 })
Jon Perritt816d2a02014-03-11 20:49:46 -0500234 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500235 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500236
Jon Perritt5eb55b12014-08-18 14:48:23 -0500237 lr, err := objects.List(client, objects.ListOpts{
238 Full: false,
239 Container: cName,
240 })
241 if err != nil {
242 t.Error(err)
243 return
244 }
245 ons, err := objects.ExtractNames(lr)
246 if err != nil {
247 t.Error(err)
248 return
249 }
250 if len(ons) != len(oNames) {
251 t.Errorf("Expected %d names and got %d", len(oNames), len(ons))
252 return
253 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500254
Jon Perritt5eb55b12014-08-18 14:48:23 -0500255 lr, err = objects.List(client, objects.ListOpts{
256 Full: true,
257 Container: cName,
258 })
259 if err != nil {
260 t.Error(err)
261 return
262 }
263 ois, err := objects.ExtractInfo(lr)
264 if err != nil {
265 t.Error(err)
266 return
267 }
268 if len(ois) != len(oNames) {
269 t.Errorf("Expected %d containers and got %d", len(oNames), len(ois))
270 return
271 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500272
Jon Perritt5eb55b12014-08-18 14:48:23 -0500273 err = objects.Copy(client, objects.CopyOpts{
274 Container: cName,
275 Name: oNames[0],
276 NewContainer: cName,
277 NewName: oNames[1],
278 })
279 if err != nil {
280 t.Error(err)
281 return
282 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500283
Jon Perritt5eb55b12014-08-18 14:48:23 -0500284 dr, err := objects.Download(client, objects.DownloadOpts{
285 Container: cName,
286 Name: oNames[1],
287 })
288 if err != nil {
289 t.Error(err)
290 return
291 }
292 o2Content, err := objects.ExtractContent(dr)
293 if err != nil {
294 t.Error(err)
295 }
296 dr, err = objects.Download(client, objects.DownloadOpts{
297 Container: cName,
298 Name: oNames[0],
299 })
300 if err != nil {
301 t.Error(err)
302 return
303 }
304 o1Content, err := objects.ExtractContent(dr)
305 if err != nil {
306 t.Error(err)
307 return
308 }
309 if string(o2Content) != string(o1Content) {
310 t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content))
311 return
312 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500313
Jon Perritt5eb55b12014-08-18 14:48:23 -0500314 err = objects.Update(client, objects.UpdateOpts{
315 Container: cName,
316 Name: oNames[0],
317 Metadata: metadata,
318 })
319 if err != nil {
320 t.Error(err)
321 return
322 }
323 defer func() {
324 tempMap := make(map[string]string)
325 for k := range metadata {
326 tempMap[k] = ""
327 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500328 err = objects.Update(client, objects.UpdateOpts{
329 Container: cName,
330 Name: oNames[0],
Jon Perritt5eb55b12014-08-18 14:48:23 -0500331 Metadata: tempMap,
Jon Perritt816d2a02014-03-11 20:49:46 -0500332 })
333 if err != nil {
334 t.Error(err)
335 return
336 }
Jon Perritt5eb55b12014-08-18 14:48:23 -0500337 }()
Jon Perritt816d2a02014-03-11 20:49:46 -0500338
Jon Perritt5eb55b12014-08-18 14:48:23 -0500339 gr, err := objects.Get(client, objects.GetOpts{})
340 if err != nil {
341 t.Error(err)
342 return
343 }
344 om := objects.ExtractMetadata(gr)
345 for k := range metadata {
346 if om[k] != metadata[strings.Title(k)] {
347 t.Errorf("Expected custom metadata with key: %s", k)
Jon Perritt816d2a02014-03-11 20:49:46 -0500348 return
349 }
Jon Perritt816d2a02014-03-11 20:49:46 -0500350 }
351}