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 | fdac6e5 | 2014-09-29 19:43:45 -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 | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 32 | _, err := containers.Create(client, cNames[i], containers.CreateOpts{}) |
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 | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 40 | err = containers.Delete(client, cNames[i]) |
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 | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 49 | pager := containers.List(client, containers.ListOpts{Full: true, Prefix: "gophercloud-test-container-"}) |
| 50 | if pager.Err != nil { |
| 51 | t.Error(err) |
| 52 | return |
| 53 | } |
| 54 | err = pager.EachPage(func(page pagination.Page) (bool, error) { |
| 55 | containerList, err := containers.ExtractInfo(page) |
| 56 | if err != nil { |
| 57 | t.Error(err) |
| 58 | } |
| 59 | for _, n := range containerList { |
| 60 | t.Logf("Container: Name [%s] Count [%d] Bytes [%d]", |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame^] | 61 | n.Name, n.Count, n.Bytes) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | return true, nil |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 65 | }) |
| 66 | if err != nil { |
| 67 | t.Error(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 70 | // List the info for the numContainer containers that were created. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 71 | pager = containers.List(client, containers.ListOpts{Full: false, Prefix: "gophercloud-test-container-"}) |
| 72 | err = pager.EachPage(func(page pagination.Page) (bool, error) { |
| 73 | containerList, err := containers.ExtractNames(page) |
| 74 | if err != nil { |
| 75 | return false, err |
| 76 | } |
| 77 | for _, n := range containerList { |
| 78 | t.Logf("Container: Name [%s]", n) |
| 79 | } |
| 80 | |
| 81 | return true, nil |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 82 | }) |
| 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 | |
| 87 | // Update one of the numContainer container metadata. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 88 | err = containers.Update(client, cNames[0], containers.UpdateOpts{Metadata: metadata}) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 89 | if err != nil { |
| 90 | t.Error(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 91 | } |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 92 | // After the tests are done, delete the metadata that was set. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 93 | defer func() { |
| 94 | tempMap := make(map[string]string) |
| 95 | for k := range metadata { |
| 96 | tempMap[k] = "" |
| 97 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 98 | err = containers.Update(client, cNames[0], containers.UpdateOpts{Metadata: tempMap}) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 99 | if err != nil { |
| 100 | t.Error(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 101 | } |
| 102 | }() |
| 103 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame] | 104 | // Retrieve a container's metadata. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 105 | cm, err := containers.Get(client, cNames[0]).ExtractMetadata() |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 106 | if err != nil { |
| 107 | t.Error(err) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 108 | } |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 109 | for k := range metadata { |
| 110 | if cm[k] != metadata[strings.Title(k)] { |
| 111 | t.Errorf("Expected custom metadata with key: %s", k) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |