blob: 29f63120b6a53b84f02096f2ed110314d843edd9 [file] [log] [blame]
Jon Perrittf050a4c2014-09-11 15:01:17 -05001// +build acceptance
2
3package v1
4
5import (
6 "strings"
7 "testing"
8
9 "github.com/rackspace/gophercloud/acceptance/tools"
10 "github.com/rackspace/gophercloud/openstack/storage/v1/containers"
11)
12
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050013// numContainers is the number of containers to create for testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050014var numContainers = 2
15
16func TestContainers(t *testing.T) {
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050017 // Create a new client to execute the HTTP requests. See common.go for newClient body.
Jon Perrittf050a4c2014-09-11 15:01:17 -050018 client, err := newClient()
19 if err != nil {
20 t.Error(err)
21 return
22 }
23
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050024 // Create a slice of random container names.
Jon Perrittf050a4c2014-09-11 15:01:17 -050025 cNames := make([]string, numContainers)
26 for i := 0; i < numContainers; i++ {
27 cNames[i] = tools.RandomString("gophercloud-test-container-", 8)
28 }
29
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050030 // Create numContainers containers.
Jon Perrittf050a4c2014-09-11 15:01:17 -050031 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 Perritt4cf2ecf2014-09-15 13:59:16 -050040 // Delete the numContainers containers after function completion.
Jon Perrittf050a4c2014-09-11 15:01:17 -050041 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 Perritt4cf2ecf2014-09-15 13:59:16 -050053 // List the numContainer names that were just created. To just list those,
54 // the 'prefix' parameter is used.
Jon Perrittf050a4c2014-09-11 15:01:17 -050055 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 Perritt4cf2ecf2014-09-15 13:59:16 -050065 // Extract the names from the 'List' response.
Jon Perrittf050a4c2014-09-11 15:01:17 -050066 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 Perritt4cf2ecf2014-09-15 13:59:16 -050076 // List the info for the numContainer containers that were created.
Jon Perrittf050a4c2014-09-11 15:01:17 -050077 lr, err = containers.List(client, containers.ListOpts{
78 Full: true,
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050079 Params: map[string]string{
80 "prefix": "gophercloud-test-container-",
81 },
Jon Perrittf050a4c2014-09-11 15:01:17 -050082 })
83 if err != nil {
84 t.Error(err)
85 return
86 }
Jon Perritt4cf2ecf2014-09-15 13:59:16 -050087 // Extract the info from the 'List' response.
Jon Perrittf050a4c2014-09-11 15:01:17 -050088 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 Perritt4cf2ecf2014-09-15 13:59:16 -050097
98 // Update one of the numContainer container metadata.
Jon Perrittf050a4c2014-09-11 15:01:17 -050099 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 Perritt4cf2ecf2014-09-15 13:59:16 -0500107 // After the tests are done, delete the metadata that was set.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500108 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 Perritt4cf2ecf2014-09-15 13:59:16 -0500123 // Retrieve a container's metadata.
124 gr, err := containers.Get(client, containers.GetOpts{
125 Name: cNames[0],
126 })
Jon Perrittf050a4c2014-09-11 15:01:17 -0500127 if err != nil {
128 t.Error(err)
129 return
130 }
Jon Perritt4cf2ecf2014-09-15 13:59:16 -0500131 // Extract the metadata from the 'Get' response.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500132 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}