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 | a8c3b81 | 2014-08-19 22:02:31 -0500 | [diff] [blame] | 14 | "os" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 15 | "strings" |
| 16 | "testing" |
| 17 | ) |
| 18 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 19 | var metadata = map[string]string{"gopher": "cloud"} |
| 20 | var numContainers = 2 |
| 21 | var numObjects = 2 |
| 22 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 23 | func newClient() (*storage.Client, error) { |
| 24 | ao, err := utils.AuthOptions() |
| 25 | if err != nil { |
| 26 | return nil, err |
| 27 | } |
| 28 | |
Jon Perritt | 6e89878 | 2014-08-19 15:58:11 -0500 | [diff] [blame] | 29 | client, err := ao.NewClient(identity.EndpointOpts{ |
Jon Perritt | a8c3b81 | 2014-08-19 22:02:31 -0500 | [diff] [blame] | 30 | Region: os.Getenv("OS_REGION_NAME"), |
| 31 | Type: "object-store", |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 32 | }) |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | |
| 37 | return storage.NewClient(client.Endpoint, client.Authority, client.Options), nil |
| 38 | } |
| 39 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 40 | func TestAccount(t *testing.T) { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 41 | client, err := newClient() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 42 | if err != nil { |
| 43 | t.Error(err) |
| 44 | return |
| 45 | } |
| 46 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 47 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 58 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 59 | err = accounts.Update(client, accounts.UpdateOpts{ |
| 60 | Metadata: tempMap, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 61 | }) |
| 62 | if err != nil { |
| 63 | t.Error(err) |
| 64 | return |
| 65 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 66 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 67 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 68 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 77 | return |
| 78 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestContainers(t *testing.T) { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 83 | client, err := newClient() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 84 | if err != nil { |
| 85 | t.Error(err) |
| 86 | return |
| 87 | } |
| 88 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 89 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 101 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 102 | } |
| 103 | defer func() { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 104 | for i := 0; i < len(cNames); i++ { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 105 | err = containers.Delete(client, containers.DeleteOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 106 | Name: cNames[i], |
| 107 | }) |
| 108 | if err != nil { |
| 109 | t.Error(err) |
| 110 | return |
| 111 | } |
| 112 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 113 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 114 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 115 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 131 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 132 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 148 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 149 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 162 | err = containers.Update(client, containers.UpdateOpts{ |
| 163 | Name: cNames[0], |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 164 | Metadata: tempMap, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 165 | }) |
| 166 | if err != nil { |
| 167 | t.Error(err) |
| 168 | return |
| 169 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 170 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 171 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 172 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 181 | return |
| 182 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | func TestObjects(t *testing.T) { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 187 | client, err := newClient() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 188 | if err != nil { |
| 189 | t.Error(err) |
| 190 | return |
| 191 | } |
| 192 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 193 | oNames := make([]string, numObjects) |
| 194 | for i := 0; i < len(oNames); i++ { |
| 195 | oNames[i] = tools.RandomString("test-object-", 8) |
| 196 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 197 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 198 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 208 | Name: cName, |
| 209 | }) |
| 210 | if err != nil { |
| 211 | t.Error(err) |
| 212 | return |
| 213 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 214 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 215 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 216 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 230 | for i := 0; i < numObjects; i++ { |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 231 | err = objects.Delete(client, objects.DeleteOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 232 | Container: cName, |
| 233 | Name: oNames[i], |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -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 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 237 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 238 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 255 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 256 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 273 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 274 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 284 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 285 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 314 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 315 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 329 | err = objects.Update(client, objects.UpdateOpts{ |
| 330 | Container: cName, |
| 331 | Name: oNames[0], |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 332 | Metadata: tempMap, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 333 | }) |
| 334 | if err != nil { |
| 335 | t.Error(err) |
| 336 | return |
| 337 | } |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 338 | }() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 339 | |
Jon Perritt | 5eb55b1 | 2014-08-18 14:48:23 -0500 | [diff] [blame] | 340 | 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 349 | return |
| 350 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 351 | } |
| 352 | } |