File: objects_test.go

package info (click to toggle)
golang-github-gophercloud-gophercloud 0.0~git20180917.45f1c769-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,768 kB
  • sloc: sh: 98; makefile: 14
file content (263 lines) | stat: -rw-r--r-- 7,164 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// +build acceptance

package v1

import (
	"bytes"
	"strings"
	"testing"

	"github.com/gophercloud/gophercloud/acceptance/clients"
	"github.com/gophercloud/gophercloud/acceptance/tools"
	"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
	"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/objects"
	th "github.com/gophercloud/gophercloud/testhelper"
)

// numObjects is the number of objects to create for testing.
var numObjects = 2

func TestObjects(t *testing.T) {
	client, err := clients.NewObjectStorageV1Client()
	if err != nil {
		t.Fatalf("Unable to create client: %v", err)
	}

	// Make a slice of length numObjects to hold the random object names.
	oNames := make([]string, numObjects)
	for i := 0; i < len(oNames); i++ {
		oNames[i] = tools.RandomString("test-object-", 8)
	}

	// Create a container to hold the test objects.
	cName := tools.RandomString("test-container-", 8)
	header, err := containers.Create(client, cName, nil).Extract()
	th.AssertNoErr(t, err)
	t.Logf("Create object headers: %+v\n", header)

	// Defer deletion of the container until after testing.
	defer func() {
		res := containers.Delete(client, cName)
		th.AssertNoErr(t, res.Err)
	}()

	// Create a slice of buffers to hold the test object content.
	oContents := make([]*bytes.Buffer, numObjects)
	for i := 0; i < numObjects; i++ {
		oContents[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
		createOpts := objects.CreateOpts{
			Content: oContents[i],
		}
		res := objects.Create(client, cName, oNames[i], createOpts)
		th.AssertNoErr(t, res.Err)
	}
	// Delete the objects after testing.
	defer func() {
		for i := 0; i < numObjects; i++ {
			res := objects.Delete(client, cName, oNames[i], nil)
			th.AssertNoErr(t, res.Err)
		}
	}()

	// List all created objects
	listOpts := objects.ListOpts{
		Full:   true,
		Prefix: "test-object-",
	}

	allPages, err := objects.List(client, cName, listOpts).AllPages()
	if err != nil {
		t.Fatalf("Unable to list objects: %v", err)
	}

	ons, err := objects.ExtractNames(allPages)
	if err != nil {
		t.Fatalf("Unable to extract objects: %v", err)
	}
	th.AssertEquals(t, len(ons), len(oNames))

	ois, err := objects.ExtractInfo(allPages)
	if err != nil {
		t.Fatalf("Unable to extract object info: %v", err)
	}
	th.AssertEquals(t, len(ois), len(oNames))

	// Copy the contents of one object to another.
	copyOpts := objects.CopyOpts{
		Destination: cName + "/" + oNames[1],
	}
	copyres := objects.Copy(client, cName, oNames[0], copyOpts)
	th.AssertNoErr(t, copyres.Err)

	// Download one of the objects that was created above.
	downloadres := objects.Download(client, cName, oNames[0], nil)
	th.AssertNoErr(t, downloadres.Err)

	o1Content, err := downloadres.ExtractContent()
	th.AssertNoErr(t, err)

	// Download the another object that was create above.
	downloadOpts := objects.DownloadOpts{
		Newest: true,
	}
	downloadres = objects.Download(client, cName, oNames[1], downloadOpts)
	th.AssertNoErr(t, downloadres.Err)
	o2Content, err := downloadres.ExtractContent()
	th.AssertNoErr(t, err)

	// Compare the two object's contents to test that the copy worked.
	th.AssertEquals(t, string(o2Content), string(o1Content))

	// Update an object's metadata.
	metadata := map[string]string{
		"Gophercloud-Test": "objects",
	}

	updateOpts := objects.UpdateOpts{
		Metadata: metadata,
	}
	updateres := objects.Update(client, cName, oNames[0], updateOpts)
	th.AssertNoErr(t, updateres.Err)

	// Delete the object's metadata after testing.
	defer func() {
		tempMap := make(map[string]string)
		for k := range metadata {
			tempMap[k] = ""
		}
		res := objects.Update(client, cName, oNames[0], &objects.UpdateOpts{Metadata: tempMap})
		th.AssertNoErr(t, res.Err)
	}()

	// Retrieve an object's metadata.
	getOpts := objects.GetOpts{
		Newest: true,
	}
	om, err := objects.Get(client, cName, oNames[0], getOpts).ExtractMetadata()
	th.AssertNoErr(t, err)
	for k := range metadata {
		if om[k] != metadata[strings.Title(k)] {
			t.Errorf("Expected custom metadata with key: %s", k)
			return
		}
	}
}

func TestObjectsListSubdir(t *testing.T) {
	client, err := clients.NewObjectStorageV1Client()
	if err != nil {
		t.Fatalf("Unable to create client: %v", err)
	}

	// Create a random subdirectory name.
	cSubdir1 := tools.RandomString("test-subdir-", 8)
	cSubdir2 := tools.RandomString("test-subdir-", 8)

	// Make a slice of length numObjects to hold the random object names.
	oNames1 := make([]string, numObjects)
	for i := 0; i < len(oNames1); i++ {
		oNames1[i] = cSubdir1 + "/" + tools.RandomString("test-object-", 8)
	}

	oNames2 := make([]string, numObjects)
	for i := 0; i < len(oNames2); i++ {
		oNames2[i] = cSubdir2 + "/" + tools.RandomString("test-object-", 8)
	}

	// Create a container to hold the test objects.
	cName := tools.RandomString("test-container-", 8)
	_, err = containers.Create(client, cName, nil).Extract()
	th.AssertNoErr(t, err)

	// Defer deletion of the container until after testing.
	defer func() {
		t.Logf("Deleting container %s", cName)
		res := containers.Delete(client, cName)
		th.AssertNoErr(t, res.Err)
	}()

	// Create a slice of buffers to hold the test object content.
	oContents1 := make([]*bytes.Buffer, numObjects)
	for i := 0; i < numObjects; i++ {
		oContents1[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
		createOpts := objects.CreateOpts{
			Content: oContents1[i],
		}
		res := objects.Create(client, cName, oNames1[i], createOpts)
		th.AssertNoErr(t, res.Err)
	}
	// Delete the objects after testing.
	defer func() {
		for i := 0; i < numObjects; i++ {
			t.Logf("Deleting object %s", oNames1[i])
			res := objects.Delete(client, cName, oNames1[i], nil)
			th.AssertNoErr(t, res.Err)
		}
	}()

	oContents2 := make([]*bytes.Buffer, numObjects)
	for i := 0; i < numObjects; i++ {
		oContents2[i] = bytes.NewBuffer([]byte(tools.RandomString("", 10)))
		createOpts := objects.CreateOpts{
			Content: oContents2[i],
		}
		res := objects.Create(client, cName, oNames2[i], createOpts)
		th.AssertNoErr(t, res.Err)
	}
	// Delete the objects after testing.
	defer func() {
		for i := 0; i < numObjects; i++ {
			t.Logf("Deleting object %s", oNames2[i])
			res := objects.Delete(client, cName, oNames2[i], nil)
			th.AssertNoErr(t, res.Err)
		}
	}()

	listOpts := objects.ListOpts{
		Full:      true,
		Delimiter: "/",
	}

	allPages, err := objects.List(client, cName, listOpts).AllPages()
	if err != nil {
		t.Fatal(err)
	}

	allObjects, err := objects.ExtractNames(allPages)
	if err != nil {
		t.Fatal(err)
	}

	t.Logf("%#v\n", allObjects)
	expected := []string{cSubdir1, cSubdir2}
	for _, e := range expected {
		var valid bool
		for _, a := range allObjects {
			if e+"/" == a {
				valid = true
			}
		}
		if !valid {
			t.Fatalf("could not find %s in results", e)
		}
	}

	listOpts = objects.ListOpts{
		Full:      true,
		Delimiter: "/",
		Prefix:    cSubdir2,
	}

	allPages, err = objects.List(client, cName, listOpts).AllPages()
	if err != nil {
		t.Fatal(err)
	}

	allObjects, err = objects.ExtractNames(allPages)
	if err != nil {
		t.Fatal(err)
	}

	th.AssertEquals(t, allObjects[0], cSubdir2+"/")
	t.Logf("%#v\n", allObjects)
}