blob: 066a223a1298c7cb744f19bf1480ce89810895c6 [file] [log] [blame]
Jon Perrittf050a4c2014-09-11 15:01:17 -05001// +build acceptance
2
3package v1
4
5import (
6 "bytes"
7 "strings"
8 "testing"
9
10 "github.com/rackspace/gophercloud/acceptance/tools"
11 "github.com/rackspace/gophercloud/openstack/storage/v1/containers"
12 "github.com/rackspace/gophercloud/openstack/storage/v1/objects"
13 "github.com/rackspace/gophercloud/pagination"
14)
15
Jon Perrittb1eea512014-09-15 23:27:33 -050016// numObjects is the number of objects to create for testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050017var numObjects = 2
18
19func TestObjects(t *testing.T) {
Jon Perrittb1eea512014-09-15 23:27:33 -050020 // Create a provider client for executing the HTTP request.
21 // See common.go for more information.
Jon Perrittf050a4c2014-09-11 15:01:17 -050022 client, err := newClient()
23 if err != nil {
24 t.Error(err)
25 return
26 }
27
Jon Perrittb1eea512014-09-15 23:27:33 -050028 // Make a slice of length numObjects to hold the random object names.
Jon Perrittf050a4c2014-09-11 15:01:17 -050029 oNames := make([]string, numObjects)
30 for i := 0; i < len(oNames); i++ {
31 oNames[i] = tools.RandomString("test-object-", 8)
32 }
33
Jon Perrittb1eea512014-09-15 23:27:33 -050034 // Create a container to hold the test objects.
Jon Perrittf050a4c2014-09-11 15:01:17 -050035 cName := tools.RandomString("test-container-", 8)
36 _, err = containers.Create(client, containers.CreateOpts{
37 Name: cName,
38 })
39 if err != nil {
40 t.Error(err)
41 return
42 }
Jon Perrittb1eea512014-09-15 23:27:33 -050043 // Defer deletion of the container until after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050044 defer func() {
45 err = containers.Delete(client, containers.DeleteOpts{
46 Name: cName,
47 })
48 if err != nil {
49 t.Error(err)
50 return
51 }
52 }()
53
Jon Perrittb1eea512014-09-15 23:27:33 -050054 // Create a slice of buffers to hold the test object content.
Jon Perrittf050a4c2014-09-11 15:01:17 -050055 oContents := make([]*bytes.Buffer, numObjects)
56 for i := 0; i < numObjects; i++ {
57 oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
58 err = objects.Create(client, objects.CreateOpts{
59 Container: cName,
60 Name: oNames[i],
61 Content: oContents[i],
62 })
63 if err != nil {
64 t.Error(err)
65 return
66 }
67 }
Jon Perrittb1eea512014-09-15 23:27:33 -050068 // Delete the objects after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -050069 defer func() {
70 for i := 0; i < numObjects; i++ {
71 err = objects.Delete(client, objects.DeleteOpts{
72 Container: cName,
73 Name: oNames[i],
74 })
75 }
76 }()
77
78 pager := objects.List(client, objects.ListOpts{Full: false, Container: cName})
79 ons := make([]string, 0, len(oNames))
80 err = pager.EachPage(func(page pagination.Page) (bool, error) {
81 names, err := objects.ExtractNames(page)
82 if err != nil {
83 return false, err
84 }
85 ons = append(ons, names...)
86
87 return true, nil
88 })
89 if err != nil {
90 t.Error(err)
91 return
92 }
93 if len(ons) != len(oNames) {
94 t.Errorf("Expected %d names and got %d", len(oNames), len(ons))
95 return
96 }
97
98 pager = objects.List(client, objects.ListOpts{Full: true, Container: cName})
99 ois := make([]objects.Object, 0, len(oNames))
100 err = pager.EachPage(func(page pagination.Page) (bool, error) {
101 info, err := objects.ExtractInfo(page)
102 if err != nil {
103 return false, nil
104 }
105
106 ois = append(ois, info...)
107
108 return true, nil
109 })
110 if err != nil {
111 t.Error(err)
112 return
113 }
114 if len(ois) != len(oNames) {
115 t.Errorf("Expected %d containers and got %d", len(oNames), len(ois))
116 return
117 }
118
Jon Perrittb1eea512014-09-15 23:27:33 -0500119 // Copy the contents of one object to another.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500120 err = objects.Copy(client, objects.CopyOpts{
121 Container: cName,
122 Name: oNames[0],
123 NewContainer: cName,
124 NewName: oNames[1],
125 })
126 if err != nil {
127 t.Error(err)
128 return
129 }
130
Jon Perrittb1eea512014-09-15 23:27:33 -0500131 // Download one of the objects that was created above.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500132 dr, err := objects.Download(client, objects.DownloadOpts{
133 Container: cName,
134 Name: oNames[1],
135 })
136 if err != nil {
137 t.Error(err)
138 return
139 }
Jon Perrittb1eea512014-09-15 23:27:33 -0500140 // Extract the content from the 'Download' response of object.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500141 o2Content, err := objects.ExtractContent(dr)
142 if err != nil {
143 t.Error(err)
144 }
Jon Perrittb1eea512014-09-15 23:27:33 -0500145 // Download the another object that was create above.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500146 dr, err = objects.Download(client, objects.DownloadOpts{
147 Container: cName,
148 Name: oNames[0],
149 })
150 if err != nil {
151 t.Error(err)
152 return
153 }
Jon Perrittb1eea512014-09-15 23:27:33 -0500154 // Extract the content from the 'Download' response of other object.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500155 o1Content, err := objects.ExtractContent(dr)
156 if err != nil {
157 t.Error(err)
158 return
159 }
Jon Perrittb1eea512014-09-15 23:27:33 -0500160 // Compare the two object's contents to test that the copy worked.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500161 if string(o2Content) != string(o1Content) {
162 t.Errorf("Copy failed. Expected\n%s\nand got\n%s", string(o1Content), string(o2Content))
163 return
164 }
165
Jon Perrittb1eea512014-09-15 23:27:33 -0500166 // Update an object's metadata.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500167 err = objects.Update(client, objects.UpdateOpts{
168 Container: cName,
169 Name: oNames[0],
170 Metadata: metadata,
171 })
172 if err != nil {
173 t.Error(err)
174 return
175 }
Jon Perrittb1eea512014-09-15 23:27:33 -0500176 // Delete the object's metadata after testing.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500177 defer func() {
178 tempMap := make(map[string]string)
179 for k := range metadata {
180 tempMap[k] = ""
181 }
182 err = objects.Update(client, objects.UpdateOpts{
183 Container: cName,
184 Name: oNames[0],
185 Metadata: tempMap,
186 })
187 if err != nil {
188 t.Error(err)
189 return
190 }
191 }()
192
Jon Perrittb1eea512014-09-15 23:27:33 -0500193 // Retrieve an object's metadata.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500194 gr, err := objects.Get(client, objects.GetOpts{})
195 if err != nil {
196 t.Error(err)
197 return
198 }
Jon Perrittb1eea512014-09-15 23:27:33 -0500199 // Extract the custom metadata from the 'Get' response.
Jon Perrittf050a4c2014-09-11 15:01:17 -0500200 om := objects.ExtractMetadata(gr)
201 for k := range metadata {
202 if om[k] != metadata[strings.Title(k)] {
203 t.Errorf("Expected custom metadata with key: %s", k)
204 return
205 }
206 }
207}