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 | b6b1d02 | 2014-04-14 21:50:45 -0500 | [diff] [blame] | 7 | storage "github.com/rackspace/gophercloud/openstack/storage/v1" |
| 8 | "github.com/rackspace/gophercloud/openstack/storage/v1/accounts" |
| 9 | "github.com/rackspace/gophercloud/openstack/storage/v1/containers" |
| 10 | "github.com/rackspace/gophercloud/openstack/storage/v1/objects" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 11 | "os" |
| 12 | "strings" |
| 13 | "testing" |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 14 | "github.com/rackspace/gophercloud/acceptance/tools" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | var objectStorage = "object-store" |
| 18 | var metadata = map[string]string{"gopher": "cloud"} |
| 19 | var numContainers = 2 |
| 20 | var numObjects = 2 |
| 21 | |
| 22 | func TestAccount(t *testing.T) { |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 23 | ts, err := tools.SetupForList(objectStorage) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 24 | if err != nil { |
| 25 | t.Error(err) |
| 26 | return |
| 27 | } |
| 28 | |
| 29 | region := os.Getenv("OS_REGION_NAME") |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 30 | for _, ep := range ts.EPs { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 31 | if (region != "") && (region != ep.Region) { |
| 32 | continue |
| 33 | } |
| 34 | |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 35 | client := storage.NewClient(ep.PublicURL, ts.A, ts.O) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 36 | |
| 37 | err := accounts.Update(client, accounts.UpdateOpts{ |
| 38 | Metadata: metadata, |
| 39 | }) |
| 40 | if err != nil { |
| 41 | t.Error(err) |
| 42 | return |
| 43 | } |
| 44 | defer func() { |
| 45 | tempMap := make(map[string]string) |
| 46 | for k := range metadata { |
| 47 | tempMap[k] = "" |
| 48 | } |
| 49 | err = accounts.Update(client, accounts.UpdateOpts{ |
| 50 | Metadata: tempMap, |
| 51 | }) |
| 52 | if err != nil { |
| 53 | t.Error(err) |
| 54 | return |
| 55 | } |
| 56 | }() |
| 57 | |
| 58 | gr, err := accounts.Get(client, accounts.GetOpts{}) |
| 59 | if err != nil { |
| 60 | t.Error(err) |
| 61 | return |
| 62 | } |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 63 | am := accounts.ExtractMetadata(gr) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 64 | for k := range metadata { |
| 65 | if am[k] != metadata[strings.Title(k)] { |
| 66 | t.Errorf("Expected custom metadata with key: %s", k) |
| 67 | return |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestContainers(t *testing.T) { |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 74 | ts, err := tools.SetupForList(objectStorage) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 75 | if err != nil { |
| 76 | t.Error(err) |
| 77 | return |
| 78 | } |
| 79 | |
| 80 | region := os.Getenv("OS_REGION_NAME") |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 81 | for _, ep := range ts.EPs { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 82 | if (region != "") && (region != ep.Region) { |
| 83 | continue |
| 84 | } |
| 85 | |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 86 | client := storage.NewClient(ep.PublicURL, ts.A, ts.O) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 87 | |
| 88 | cNames := make([]string, numContainers) |
| 89 | for i := 0; i < numContainers; i++ { |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 90 | cNames[i] = tools.RandomString("test-container-", 8) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 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 |
| 100 | } |
| 101 | } |
| 102 | defer func() { |
| 103 | for i := 0; i < len(cNames); i++ { |
| 104 | err = containers.Delete(client, containers.DeleteOpts{ |
| 105 | Name: cNames[i], |
| 106 | }) |
| 107 | if err != nil { |
| 108 | t.Error(err) |
| 109 | return |
| 110 | } |
| 111 | } |
| 112 | }() |
| 113 | |
| 114 | lr, err := containers.List(client, containers.ListOpts{ |
| 115 | Full: false, |
| 116 | }) |
| 117 | if err != nil { |
| 118 | t.Error(err) |
| 119 | return |
| 120 | } |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 121 | cns, err := containers.ExtractNames(lr) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 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 | } |
| 130 | |
| 131 | lr, err = containers.List(client, containers.ListOpts{ |
| 132 | Full: true, |
| 133 | }) |
| 134 | if err != nil { |
| 135 | t.Error(err) |
| 136 | return |
| 137 | } |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 138 | cis, err := containers.ExtractInfo(lr) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 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 | } |
| 147 | |
| 148 | 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 | } |
| 161 | err = containers.Update(client, containers.UpdateOpts{ |
| 162 | Name: cNames[0], |
| 163 | Metadata: tempMap, |
| 164 | }) |
| 165 | if err != nil { |
| 166 | t.Error(err) |
| 167 | return |
| 168 | } |
| 169 | }() |
| 170 | |
| 171 | gr, err := containers.Get(client, containers.GetOpts{}) |
| 172 | if err != nil { |
| 173 | t.Error(err) |
| 174 | return |
| 175 | } |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 176 | cm := containers.ExtractMetadata(gr) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 177 | for k := range metadata { |
| 178 | if cm[k] != metadata[strings.Title(k)] { |
| 179 | t.Errorf("Expected custom metadata with key: %s", k) |
| 180 | return |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func TestObjects(t *testing.T) { |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 187 | ts, err := tools.SetupForList(objectStorage) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 188 | if err != nil { |
| 189 | t.Error(err) |
| 190 | return |
| 191 | } |
| 192 | |
| 193 | region := os.Getenv("OS_REGION_NAME") |
| 194 | |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 195 | for _, ep := range ts.EPs { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 196 | if (region != "") && (region != ep.Region) { |
| 197 | continue |
| 198 | } |
| 199 | |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 200 | client := storage.NewClient(ep.PublicURL, ts.A, ts.O) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 201 | |
| 202 | oNames := make([]string, numObjects) |
| 203 | for i := 0; i < len(oNames); i++ { |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 204 | oNames[i] = tools.RandomString("test-object-", 8) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 205 | } |
| 206 | |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 207 | cName := tools.RandomString("test-container-", 8) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 208 | _, err := containers.Create(client, containers.CreateOpts{ |
| 209 | Name: cName, |
| 210 | }) |
| 211 | if err != nil { |
| 212 | t.Error(err) |
| 213 | return |
| 214 | } |
| 215 | defer func() { |
| 216 | err = containers.Delete(client, containers.DeleteOpts{ |
| 217 | Name: cName, |
| 218 | }) |
| 219 | if err != nil { |
| 220 | t.Error(err) |
| 221 | return |
| 222 | } |
| 223 | }() |
| 224 | |
| 225 | oContents := make([]*bytes.Buffer, numObjects) |
| 226 | for i := 0; i < numObjects; i++ { |
Samuel A. Falvo II | 28cadc2 | 2014-07-31 14:35:40 -0700 | [diff] [blame] | 227 | oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10))) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 228 | err = objects.Create(client, objects.CreateOpts{ |
| 229 | Container: cName, |
| 230 | Name: oNames[i], |
| 231 | Content: oContents[i], |
| 232 | }) |
| 233 | if err != nil { |
| 234 | t.Error(err) |
| 235 | return |
| 236 | } |
| 237 | } |
| 238 | defer func() { |
| 239 | for i := 0; i < numObjects; i++ { |
| 240 | err = objects.Delete(client, objects.DeleteOpts{ |
| 241 | Container: cName, |
| 242 | Name: oNames[i], |
| 243 | }) |
| 244 | } |
| 245 | }() |
| 246 | |
| 247 | lr, err := objects.List(client, objects.ListOpts{ |
| 248 | Full: false, |
| 249 | Container: cName, |
| 250 | }) |
| 251 | if err != nil { |
| 252 | t.Error(err) |
| 253 | return |
| 254 | } |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 255 | ons, err := objects.ExtractNames(lr) |
| 256 | if err != nil { |
| 257 | t.Error(err) |
| 258 | return |
| 259 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 260 | if len(ons) != len(oNames) { |
| 261 | t.Errorf("Expected %d names and got %d", len(oNames), len(ons)) |
| 262 | return |
| 263 | } |
| 264 | |
| 265 | lr, err = objects.List(client, objects.ListOpts{ |
| 266 | Full: true, |
| 267 | Container: cName, |
| 268 | }) |
| 269 | if err != nil { |
| 270 | t.Error(err) |
| 271 | return |
| 272 | } |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 273 | ois, err := objects.ExtractInfo(lr) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 274 | if err != nil { |
| 275 | t.Error(err) |
| 276 | return |
| 277 | } |
| 278 | if len(ois) != len(oNames) { |
| 279 | t.Errorf("Expected %d containers and got %d", len(oNames), len(ois)) |
| 280 | return |
| 281 | } |
| 282 | |
| 283 | err = objects.Copy(client, objects.CopyOpts{ |
| 284 | Container: cName, |
| 285 | Name: oNames[0], |
| 286 | NewContainer: cName, |
| 287 | NewName: oNames[1], |
| 288 | }) |
| 289 | if err != nil { |
| 290 | t.Error(err) |
| 291 | return |
| 292 | } |
| 293 | |
| 294 | dr, err := objects.Download(client, objects.DownloadOpts{ |
| 295 | Container: cName, |
| 296 | Name: oNames[1], |
| 297 | }) |
| 298 | if err != nil { |
| 299 | t.Error(err) |
| 300 | return |
| 301 | } |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 302 | o2Content, err := objects.ExtractContent(dr) |
| 303 | if err != nil { |
| 304 | t.Error(err) |
| 305 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 306 | dr, err = objects.Download(client, objects.DownloadOpts{ |
| 307 | Container: cName, |
| 308 | Name: oNames[0], |
| 309 | }) |
| 310 | if err != nil { |
| 311 | t.Error(err) |
| 312 | return |
| 313 | } |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 314 | o1Content, err := objects.ExtractContent(dr) |
| 315 | if err != nil { |
| 316 | t.Error(err) |
| 317 | return |
| 318 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 319 | if string(o2Content) != string(o1Content) { |
| 320 | t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content)) |
| 321 | return |
| 322 | } |
| 323 | |
| 324 | err = objects.Update(client, objects.UpdateOpts{ |
| 325 | Container: cName, |
| 326 | Name: oNames[0], |
| 327 | Metadata: metadata, |
| 328 | }) |
| 329 | if err != nil { |
| 330 | t.Error(err) |
| 331 | return |
| 332 | } |
| 333 | defer func() { |
| 334 | tempMap := make(map[string]string) |
| 335 | for k := range metadata { |
| 336 | tempMap[k] = "" |
| 337 | } |
| 338 | err = objects.Update(client, objects.UpdateOpts{ |
| 339 | Container: cName, |
| 340 | Name: oNames[0], |
| 341 | Metadata: tempMap, |
| 342 | }) |
| 343 | if err != nil { |
| 344 | t.Error(err) |
| 345 | return |
| 346 | } |
| 347 | }() |
| 348 | |
| 349 | gr, err := objects.Get(client, objects.GetOpts{}) |
| 350 | if err != nil { |
| 351 | t.Error(err) |
| 352 | return |
| 353 | } |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 354 | om := objects.ExtractMetadata(gr) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 355 | for k := range metadata { |
| 356 | if om[k] != metadata[strings.Title(k)] { |
| 357 | t.Errorf("Expected custom metadata with key: %s", k) |
| 358 | return |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | } |