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