blob: cf5e3cd0a2ccaa2d06829024510d030df8053b40 [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
13var numContainers = 2
14
15func TestContainers(t *testing.T) {
16 client, err := newClient()
17 if err != nil {
18 t.Error(err)
19 return
20 }
21
22 cNames := make([]string, numContainers)
23 for i := 0; i < numContainers; i++ {
24 cNames[i] = tools.RandomString("gophercloud-test-container-", 8)
25 }
26
27 for i := 0; i < len(cNames); i++ {
28 _, err := containers.Create(client, containers.CreateOpts{
29 Name: cNames[i],
30 })
31 if err != nil {
32 t.Error(err)
33 return
34 }
35 }
36 defer func() {
37 for i := 0; i < len(cNames); i++ {
38 err = containers.Delete(client, containers.DeleteOpts{
39 Name: cNames[i],
40 })
41 if err != nil {
42 t.Error(err)
43 return
44 }
45 }
46 }()
47
48 lr, err := containers.List(client, containers.ListOpts{
49 Full: false,
50 Params: map[string]string{
51 "prefix": "gophercloud-test-container-",
52 },
53 })
54 if err != nil {
55 t.Error(err)
56 return
57 }
58 cns, err := containers.ExtractNames(lr)
59 if err != nil {
60 t.Error(err)
61 return
62 }
63 if len(cns) != len(cNames) {
64 t.Errorf("Expected %d names and got %d:\nExpected:%v\nActual:%v", len(cNames), len(cns), cNames, cns)
65 return
66 }
67
68 lr, err = containers.List(client, containers.ListOpts{
69 Full: true,
70 })
71 if err != nil {
72 t.Error(err)
73 return
74 }
75 cis, err := containers.ExtractInfo(lr)
76 if err != nil {
77 t.Error(err)
78 return
79 }
80 if len(cis) != len(cNames) {
81 t.Errorf("Expected %d containers and got %d", len(cNames), len(cis))
82 return
83 }
84 err = containers.Update(client, containers.UpdateOpts{
85 Name: cNames[0],
86 Metadata: metadata,
87 })
88 if err != nil {
89 t.Error(err)
90 return
91 }
92 defer func() {
93 tempMap := make(map[string]string)
94 for k := range metadata {
95 tempMap[k] = ""
96 }
97 err = containers.Update(client, containers.UpdateOpts{
98 Name: cNames[0],
99 Metadata: tempMap,
100 })
101 if err != nil {
102 t.Error(err)
103 return
104 }
105 }()
106
107 gr, err := containers.Get(client, containers.GetOpts{})
108 if err != nil {
109 t.Error(err)
110 return
111 }
112 cm := containers.ExtractMetadata(gr)
113 for k := range metadata {
114 if cm[k] != metadata[strings.Title(k)] {
115 t.Errorf("Expected custom metadata with key: %s", k)
116 return
117 }
118 }
119}