Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
| 3 | package openstack |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 7 | "github.com/rackspace/gophercloud/acceptance/tools" |
| 8 | identity "github.com/rackspace/gophercloud/openstack/identity/v2" |
Jon Perritt | b6b1d02 | 2014-04-14 21:50:45 -0500 | [diff] [blame] | 9 | 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 Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 13 | "github.com/rackspace/gophercloud/openstack/utils" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 14 | "strings" |
| 15 | "testing" |
| 16 | ) |
| 17 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 18 | var metadata = map[string]string{"gopher": "cloud"} |
| 19 | var numContainers = 2 |
| 20 | var numObjects = 2 |
| 21 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 22 | func newClient() (*storage.Client, error) { |
| 23 | ao, err := utils.AuthOptions() |
| 24 | if err != nil { |
| 25 | return nil, err |
| 26 | } |
| 27 | |
| 28 | client, err := ao.NewClient(identity.ClientOpts{ |
| 29 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 38 | func TestAccount(t *testing.T) { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 39 | client, err := newClient() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 40 | if err != nil { |
| 41 | t.Error(err) |
| 42 | return |
| 43 | } |
| 44 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 45 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 56 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 57 | err = accounts.Update(client, accounts.UpdateOpts{ |
| 58 | Metadata: tempMap, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 59 | }) |
| 60 | if err != nil { |
| 61 | t.Error(err) |
| 62 | return |
| 63 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 64 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 65 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 66 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 75 | return |
| 76 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | func TestContainers(t *testing.T) { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 81 | client, err := newClient() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 82 | if err != nil { |
| 83 | t.Error(err) |
| 84 | return |
| 85 | } |
| 86 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 87 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 99 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 100 | } |
| 101 | defer func() { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 102 | for i := 0; i < len(cNames); i++ { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 103 | err = containers.Delete(client, containers.DeleteOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 104 | Name: cNames[i], |
| 105 | }) |
| 106 | if err != nil { |
| 107 | t.Error(err) |
| 108 | return |
| 109 | } |
| 110 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 111 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 112 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 113 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 129 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 130 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 146 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 147 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 160 | err = containers.Update(client, containers.UpdateOpts{ |
| 161 | Name: cNames[0], |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 162 | Metadata: tempMap, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 163 | }) |
| 164 | if err != nil { |
| 165 | t.Error(err) |
| 166 | return |
| 167 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 168 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 169 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 170 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 179 | return |
| 180 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | func TestObjects(t *testing.T) { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 185 | client, err := newClient() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 186 | if err != nil { |
| 187 | t.Error(err) |
| 188 | return |
| 189 | } |
| 190 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 191 | oNames := make([]string, numObjects) |
| 192 | for i := 0; i < len(oNames); i++ { |
| 193 | oNames[i] = tools.RandomString("test-object-", 8) |
| 194 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 195 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 196 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 206 | Name: cName, |
| 207 | }) |
| 208 | if err != nil { |
| 209 | t.Error(err) |
| 210 | return |
| 211 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 212 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 213 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 214 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 228 | for i := 0; i < numObjects; i++ { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 229 | err = objects.Delete(client, objects.DeleteOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 230 | Container: cName, |
| 231 | Name: oNames[i], |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 232 | }) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 233 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 234 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 235 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 236 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 253 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 254 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 271 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 272 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 282 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 283 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 312 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 313 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 327 | err = objects.Update(client, objects.UpdateOpts{ |
| 328 | Container: cName, |
| 329 | Name: oNames[0], |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 330 | Metadata: tempMap, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 331 | }) |
| 332 | if err != nil { |
| 333 | t.Error(err) |
| 334 | return |
| 335 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 336 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 337 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame^] | 338 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 347 | return |
| 348 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 349 | } |
| 350 | } |