Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 1 | // +build acceptance |
| 2 | |
| 3 | package v1 |
| 4 | |
| 5 | import ( |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud/acceptance/tools" |
Jon Perritt | a9caabd | 2014-09-30 13:02:34 -0500 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/openstack/objectstorage/v1/containers" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 12 | th "github.com/rackspace/gophercloud/testhelper" |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 13 | ) |
| 14 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 15 | // numContainers is the number of containers to create for testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 16 | var numContainers = 2 |
| 17 | |
| 18 | func TestContainers(t *testing.T) { |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 19 | // Create a new client to execute the HTTP requests. See common.go for newClient body. |
Ash Wilson | 0036cbf | 2014-10-24 15:29:48 -0400 | [diff] [blame^] | 20 | client := newClient(t) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 21 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 22 | // Create a slice of random container names. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 23 | cNames := make([]string, numContainers) |
| 24 | for i := 0; i < numContainers; i++ { |
| 25 | cNames[i] = tools.RandomString("gophercloud-test-container-", 8) |
| 26 | } |
| 27 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 28 | // Create numContainers containers. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 29 | for i := 0; i < len(cNames); i++ { |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 30 | res := containers.Create(client, cNames[i], nil) |
| 31 | th.AssertNoErr(res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 32 | } |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 33 | // Delete the numContainers containers after function completion. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 34 | defer func() { |
| 35 | for i := 0; i < len(cNames); i++ { |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 36 | res = containers.Delete(client, cNames[i]) |
| 37 | th.AssertNoErr(res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 38 | } |
| 39 | }() |
| 40 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 41 | // List the numContainer names that were just created. To just list those, |
| 42 | // the 'prefix' parameter is used. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 43 | err = containers.List(client, &containers.ListOpts{Full: true, Prefix: "gophercloud-test-container-"}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 44 | containerList, err := containers.ExtractInfo(page) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 45 | th.AssertNoErr(err) |
| 46 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 47 | for _, n := range containerList { |
| 48 | t.Logf("Container: Name [%s] Count [%d] Bytes [%d]", |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 49 | n.Name, n.Count, n.Bytes) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return true, nil |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 53 | }) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 54 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 55 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 56 | // List the info for the numContainer containers that were created. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 57 | err = containers.List(client, &containers.ListOpts{Full: false, Prefix: "gophercloud-test-container-"}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 58 | containerList, err := containers.ExtractNames(page) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 59 | th.AssertNoErr(err) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 60 | for _, n := range containerList { |
| 61 | t.Logf("Container: Name [%s]", n) |
| 62 | } |
| 63 | |
| 64 | return true, nil |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 65 | }) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 66 | th.AssertNoErr(err) |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 67 | |
| 68 | // Update one of the numContainer container metadata. |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 69 | res = containers.Update(client, cNames[0], &containers.UpdateOpts{Metadata: metadata}) |
| 70 | th.AssertNoErr(res.Err) |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 71 | // After the tests are done, delete the metadata that was set. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 72 | defer func() { |
| 73 | tempMap := make(map[string]string) |
| 74 | for k := range metadata { |
| 75 | tempMap[k] = "" |
| 76 | } |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 77 | res = containers.Update(client, cNames[0], &containers.UpdateOpts{Metadata: tempMap}) |
| 78 | th.AssertNoErr(res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 79 | }() |
| 80 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 81 | // Retrieve a container's metadata. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 82 | cm, err := containers.Get(client, cNames[0]).ExtractMetadata() |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 83 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 84 | for k := range metadata { |
| 85 | if cm[k] != metadata[strings.Title(k)] { |
| 86 | t.Errorf("Expected custom metadata with key: %s", k) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |