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