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