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" |
| 10 | "github.com/rackspace/gophercloud/openstack/storage/v1/containers" |
| 11 | ) |
| 12 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame^] | 13 | // numContainers is the number of containers to create for testing. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 14 | var numContainers = 2 |
| 15 | |
| 16 | func TestContainers(t *testing.T) { |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame^] | 17 | // 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] | 18 | client, err := newClient() |
| 19 | if err != nil { |
| 20 | t.Error(err) |
| 21 | return |
| 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++ { |
| 32 | _, err := containers.Create(client, containers.CreateOpts{ |
| 33 | Name: cNames[i], |
| 34 | }) |
| 35 | if err != nil { |
| 36 | t.Error(err) |
| 37 | return |
| 38 | } |
| 39 | } |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame^] | 40 | // Delete the numContainers containers after function completion. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 41 | defer func() { |
| 42 | for i := 0; i < len(cNames); i++ { |
| 43 | err = containers.Delete(client, containers.DeleteOpts{ |
| 44 | Name: cNames[i], |
| 45 | }) |
| 46 | if err != nil { |
| 47 | t.Error(err) |
| 48 | return |
| 49 | } |
| 50 | } |
| 51 | }() |
| 52 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame^] | 53 | // List the numContainer names that were just created. To just list those, |
| 54 | // the 'prefix' parameter is used. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 55 | lr, err := containers.List(client, containers.ListOpts{ |
| 56 | Full: false, |
| 57 | Params: map[string]string{ |
| 58 | "prefix": "gophercloud-test-container-", |
| 59 | }, |
| 60 | }) |
| 61 | if err != nil { |
| 62 | t.Error(err) |
| 63 | return |
| 64 | } |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame^] | 65 | // Extract the names from the 'List' response. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 66 | cns, err := containers.ExtractNames(lr) |
| 67 | if err != nil { |
| 68 | t.Error(err) |
| 69 | return |
| 70 | } |
| 71 | if len(cns) != len(cNames) { |
| 72 | t.Errorf("Expected %d names and got %d:\nExpected:%v\nActual:%v", len(cNames), len(cns), cNames, cns) |
| 73 | return |
| 74 | } |
| 75 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame^] | 76 | // List the info for the numContainer containers that were created. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 77 | lr, err = containers.List(client, containers.ListOpts{ |
| 78 | Full: true, |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame^] | 79 | Params: map[string]string{ |
| 80 | "prefix": "gophercloud-test-container-", |
| 81 | }, |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 82 | }) |
| 83 | if err != nil { |
| 84 | t.Error(err) |
| 85 | return |
| 86 | } |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame^] | 87 | // Extract the info from the 'List' response. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 88 | cis, err := containers.ExtractInfo(lr) |
| 89 | if err != nil { |
| 90 | t.Error(err) |
| 91 | return |
| 92 | } |
| 93 | if len(cis) != len(cNames) { |
| 94 | t.Errorf("Expected %d containers and got %d", len(cNames), len(cis)) |
| 95 | return |
| 96 | } |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame^] | 97 | |
| 98 | // Update one of the numContainer container metadata. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 99 | err = containers.Update(client, containers.UpdateOpts{ |
| 100 | Name: cNames[0], |
| 101 | Metadata: metadata, |
| 102 | }) |
| 103 | if err != nil { |
| 104 | t.Error(err) |
| 105 | return |
| 106 | } |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame^] | 107 | // After the tests are done, delete the metadata that was set. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 108 | defer func() { |
| 109 | tempMap := make(map[string]string) |
| 110 | for k := range metadata { |
| 111 | tempMap[k] = "" |
| 112 | } |
| 113 | err = containers.Update(client, containers.UpdateOpts{ |
| 114 | Name: cNames[0], |
| 115 | Metadata: tempMap, |
| 116 | }) |
| 117 | if err != nil { |
| 118 | t.Error(err) |
| 119 | return |
| 120 | } |
| 121 | }() |
| 122 | |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame^] | 123 | // Retrieve a container's metadata. |
| 124 | gr, err := containers.Get(client, containers.GetOpts{ |
| 125 | Name: cNames[0], |
| 126 | }) |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 127 | if err != nil { |
| 128 | t.Error(err) |
| 129 | return |
| 130 | } |
Jon Perritt | 4cf2ecf | 2014-09-15 13:59:16 -0500 | [diff] [blame^] | 131 | // Extract the metadata from the 'Get' response. |
Jon Perritt | f050a4c | 2014-09-11 15:01:17 -0500 | [diff] [blame] | 132 | cm := containers.ExtractMetadata(gr) |
| 133 | for k := range metadata { |
| 134 | if cm[k] != metadata[strings.Title(k)] { |
| 135 | t.Errorf("Expected custom metadata with key: %s", k) |
| 136 | return |
| 137 | } |
| 138 | } |
| 139 | } |