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. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 20 | client, err := newClient() |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame^] | 21 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 22 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 23 | // Create a slice of random container names. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 24 | cNames := make([]string, numContainers) |
| 25 | for i := 0; i < numContainers; i++ { |
| 26 | cNames[i] = tools.RandomString("gophercloud-test-container-", 8) |
| 27 | } |
| 28 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 29 | // Create numContainers containers. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 30 | for i := 0; i < len(cNames); i++ { |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame^] | 31 | res := containers.Create(client, cNames[i], nil) |
| 32 | th.AssertNoErr(res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 33 | } |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 34 | // Delete the numContainers containers after function completion. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 35 | defer func() { |
| 36 | for i := 0; i < len(cNames); i++ { |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame^] | 37 | res = containers.Delete(client, cNames[i]) |
| 38 | th.AssertNoErr(res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 39 | } |
| 40 | }() |
| 41 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 42 | // List the numContainer names that were just created. To just list those, |
| 43 | // the 'prefix' parameter is used. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 44 | 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] | 45 | containerList, err := containers.ExtractInfo(page) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame^] | 46 | th.AssertNoErr(err) |
| 47 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 48 | for _, n := range containerList { |
| 49 | t.Logf("Container: Name [%s] Count [%d] Bytes [%d]", |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 50 | n.Name, n.Count, n.Bytes) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | return true, nil |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 54 | }) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame^] | 55 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 56 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 57 | // List the info for the numContainer containers that were created. |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 58 | 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] | 59 | containerList, err := containers.ExtractNames(page) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame^] | 60 | th.AssertNoErr(err) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 61 | for _, n := range containerList { |
| 62 | t.Logf("Container: Name [%s]", n) |
| 63 | } |
| 64 | |
| 65 | return true, nil |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 66 | }) |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame^] | 67 | th.AssertNoErr(err) |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 68 | |
| 69 | // Update one of the numContainer container metadata. |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame^] | 70 | res = containers.Update(client, cNames[0], &containers.UpdateOpts{Metadata: metadata}) |
| 71 | th.AssertNoErr(res.Err) |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 72 | // After the tests are done, delete the metadata that was set. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 73 | defer func() { |
| 74 | tempMap := make(map[string]string) |
| 75 | for k := range metadata { |
| 76 | tempMap[k] = "" |
| 77 | } |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame^] | 78 | res = containers.Update(client, cNames[0], &containers.UpdateOpts{Metadata: tempMap}) |
| 79 | th.AssertNoErr(res.Err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 80 | }() |
| 81 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 82 | // Retrieve a container's metadata. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 83 | cm, err := containers.Get(client, cNames[0]).ExtractMetadata() |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame^] | 84 | th.AssertNoErr(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 85 | for k := range metadata { |
| 86 | if cm[k] != metadata[strings.Title(k)] { |
| 87 | t.Errorf("Expected custom metadata with key: %s", k) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |