aboutsummaryrefslogtreecommitdiff
path: root/tests/object_test.go
blob: dd1b4c2691f2f87b366339b27ec5bc7dbd7a00f7 (plain)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/******************************************************************************
*
*  Copyright 2018 Stefan Majewsky <majewsky@gmx.net>
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*
******************************************************************************/

package tests

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"net/http"
	"strings"
	"testing"

	"go.xyrillian.de/schwift/v2"
)

func TestObjectLifecycle(t *testing.T) {
	testWithContainer(t, func(c *schwift.Container) {
		objectName := getRandomName()
		o := c.Object(objectName)

		expectString(t, o.Name(), objectName)
		expectString(t, o.FullName(), c.Name()+"/"+objectName)
		if o.Container() != c {
			t.Errorf("expected o.Container() = %#v, got %#v instead\n", c, o.Container())
		}
		expectObjectExistence(t, o, false)

		_, err := o.Headers(context.TODO())
		expectError(t, err, fmt.Sprintf("could not HEAD %q in Swift: expected 200 response, got 404 instead", o.FullName()))
		expectBool(t, schwift.Is(err, http.StatusNotFound), true)
		expectBool(t, schwift.Is(err, http.StatusNoContent), false)

		// DELETE should be idempotent and not return success on non-existence, but
		// OpenStack LOVES to be inconsistent with everything (including, notably, itself)
		err = o.Delete(context.TODO(), nil, nil)
		expectError(t, err, fmt.Sprintf("could not DELETE %q in Swift: expected 204 response, got 404 instead: <html><h1>Not Found</h1><p>The resource could not be found.</p></html>", o.FullName()))

		err = o.Upload(context.TODO(), bytes.NewReader([]byte("test")), nil, nil)
		expectSuccess(t, err)

		expectObjectExistence(t, o, true)

		err = o.Delete(context.TODO(), nil, nil)
		expectSuccess(t, err)
	})
}

func TestObjectUpload(t *testing.T) {
	testWithContainer(t, func(c *schwift.Container) {
		// test upload with bytes.Reader
		obj := c.Object("upload1")
		err := obj.Upload(context.TODO(), bytes.NewReader(objectExampleContent), nil, nil)
		expectSuccess(t, err)
		expectObjectContent(t, obj, objectExampleContent)

		// test upload with bytes.Buffer
		obj = c.Object("upload2")
		err = obj.Upload(context.TODO(), bytes.NewBuffer(objectExampleContent), nil, nil)
		expectSuccess(t, err)
		expectObjectContent(t, obj, objectExampleContent)

		// test upload with strings.Reader
		obj = c.Object("upload3")
		err = obj.Upload(context.TODO(), strings.NewReader(string(objectExampleContent)), nil, nil)
		expectSuccess(t, err)
		expectObjectContent(t, obj, objectExampleContent)

		// test upload with opaque io.Reader
		obj = c.Object("upload4")
		err = obj.Upload(context.TODO(), opaqueReader{bytes.NewReader(objectExampleContent)}, nil, nil)
		expectSuccess(t, err)
		expectObjectContent(t, obj, objectExampleContent)

		// test upload with io.Writer
		obj = c.Object("upload5")
		err = obj.UploadFromWriter(context.TODO(), nil, nil, func(w io.Writer) error {
			_, err := w.Write(objectExampleContent)
			return err
		})
		expectSuccess(t, err)
		expectObjectContent(t, obj, objectExampleContent)

		// test upload with empty reader (should create zero-byte-sized object)
		obj = c.Object("upload6")
		err = obj.Upload(context.TODO(), eofReader{}, nil, nil)
		expectSuccess(t, err)
		expectObjectContent(t, obj, nil)

		// test upload without reader (should create zero-byte-sized object)
		obj = c.Object("upload7")
		err = obj.Upload(context.TODO(), nil, nil, nil)
		expectSuccess(t, err)
		expectObjectContent(t, obj, nil)
	})
}

type eofReader struct{}

func (r eofReader) Read([]byte) (int, error) {
	return 0, io.EOF
}

type opaqueReader struct {
	b *bytes.Reader
}

func (r opaqueReader) Read(buf []byte) (int, error) {
	return r.b.Read(buf)
}

func TestObjectDownload(t *testing.T) {
	testWithContainer(t, func(c *schwift.Container) {
		// upload example object
		obj := c.Object("example")
		err := obj.Upload(context.TODO(), bytes.NewReader(objectExampleContent), nil, nil)
		expectSuccess(t, err)

		// test download as string
		str, err := obj.Download(context.TODO(), nil).AsString()
		expectSuccess(t, err)
		expectString(t, str, string(objectExampleContent))

		// test download as byte slice
		buf, err := obj.Download(context.TODO(), nil).AsByteSlice()
		expectSuccess(t, err)
		expectString(t, string(buf), string(objectExampleContent))

		// test download as io.ReadCloser slice
		reader, err := obj.Download(context.TODO(), nil).AsReadCloser()
		expectSuccess(t, err)
		buf = make([]byte, 4)
		_, err = reader.Read(buf)
		expectSuccess(t, err)
		expectString(t, string(buf), string(objectExampleContent[0:4]))
		_, err = reader.Read(buf)
		expectSuccess(t, err)
		expectString(t, string(buf), string(objectExampleContent[4:8]))
		buf, err = io.ReadAll(reader)
		expectSuccess(t, err)
		expectString(t, string(buf), string(objectExampleContent[8:]))
	})
}

func TestObjectUpdate(t *testing.T) {
	testWithContainer(t, func(c *schwift.Container) {
		obj := c.Object("example")

		// test that metadata update fails for non-existing object
		newHeaders := schwift.NewObjectHeaders()
		newHeaders.ContentType().Set("application/json")
		err := obj.Update(context.TODO(), newHeaders, nil)
		expectBool(t, schwift.Is(err, http.StatusNotFound), true)
		expectError(t, err, fmt.Sprintf("could not POST %q in Swift: expected 202 response, got 404 instead: <html><h1>Not Found</h1><p>The resource could not be found.</p></html>", obj.FullName()))

		// create object
		err = obj.Upload(context.TODO(), nil, nil, nil)
		expectSuccess(t, err)

		hdr, err := obj.Headers(context.TODO())
		expectSuccess(t, err)
		expectString(t, hdr.ContentType().Get(), "application/octet-stream")

		// now the metadata update should work
		err = obj.Update(context.TODO(), newHeaders, nil)
		expectSuccess(t, err)
		obj.Invalidate()
		hdr, err = obj.Headers(context.TODO())
		expectSuccess(t, err)
		expectString(t, hdr.ContentType().Get(), "application/json")
	})
}

func TestObjectCopy(t *testing.T) {
	testWithContainer(t, func(c *schwift.Container) {
		obj1 := c.Object("location1")
		err := obj1.Upload(context.TODO(), bytes.NewReader(objectExampleContent), nil, nil)
		expectSuccess(t, err)
		expectObjectExistence(t, obj1, true)

		obj2 := c.Object("location2")
		expectSuccess(t, obj1.CopyTo(context.TODO(), obj2, nil, nil))
		expectObjectExistence(t, obj1, true)
		expectObjectExistence(t, obj2, true)
		expectObjectContent(t, obj2, objectExampleContent)
	})
}

func TestSymlinkOperations(t *testing.T) {
	testWithContainer(t, func(c *schwift.Container) {
		// create a test object that we can link to
		obj1 := c.Object("target")
		err := obj1.Upload(context.TODO(), bytes.NewReader(objectExampleContent), nil, nil)
		expectSuccess(t, err)
		expectObjectExistence(t, obj1, true)

		// create a symlink
		obj2 := c.Object("symlink")
		expectSuccess(t, obj2.SymlinkTo(context.TODO(), obj1, nil, nil))
		expectObjectExistence(t, obj2, true)
		expectObjectSymlink(t, obj2, obj1)
		expectObjectContent(t, obj2, objectExampleContent)

		// overwrite symlink with normal object
		otherContent := []byte("abc")
		expectSuccess(t, obj2.Upload(context.TODO(), bytes.NewReader(otherContent), nil, nil))
		expectObjectExistence(t, obj2, true)
		expectObjectSymlink(t, obj2, nil)
		expectObjectContent(t, obj2, otherContent)

		// overwrite normal object with symlink
		expectSuccess(t, obj2.SymlinkTo(context.TODO(), obj1, nil, nil))
		expectObjectExistence(t, obj2, true)
		expectObjectSymlink(t, obj2, obj1)
		expectObjectContent(t, obj2, objectExampleContent)

		// deep-copy symlink
		obj3 := c.Object("copy")
		expectSuccess(t, obj2.CopyTo(context.TODO(), obj3, nil, nil))
		expectObjectExistence(t, obj3, true)
		expectObjectSymlink(t, obj3, nil)
		expectObjectContent(t, obj3, objectExampleContent)

		// shallow-copy symlink
		expectSuccess(t, obj2.CopyTo(context.TODO(), obj3, &schwift.CopyOptions{
			ShallowCopySymlinks: true,
		}, nil))
		expectObjectExistence(t, obj3, true)
		expectObjectSymlink(t, obj3, obj1)
		expectObjectContent(t, obj3, objectExampleContent)

		// delete symlink
		expectSuccess(t, obj2.Delete(context.TODO(), nil, nil))
		expectObjectExistence(t, obj2, false)
	})
}

////////////////////////////////////////////////////////////////////////////////
// helpers

func expectObjectExistence(t *testing.T, obj *schwift.Object, expectedExists bool) {
	t.Helper()
	obj.Invalidate()
	actualExists, err := obj.Exists(context.TODO())
	expectSuccess(t, err)
	expectBool(t, actualExists, expectedExists)
}

func expectObjectContent(t *testing.T, obj *schwift.Object, expected []byte) {
	t.Helper()
	str, err := obj.Download(context.TODO(), nil).AsString()
	expectSuccess(t, err)
	expectString(t, str, string(expected))
	obj.Invalidate()
	hdr, err := obj.Headers(context.TODO())
	expectSuccess(t, err)
	if !hdr.IsLargeObject() {
		expectString(t, hdr.Etag().Get(), etagOf(expected))
	}
}

func expectObjectSymlink(t *testing.T, source, expectedTarget *schwift.Object) {
	t.Helper()
	_, target, err := source.SymlinkHeaders(context.TODO())
	if expectedTarget == nil {
		switch err {
		case nil:
			if target != nil {
				t.Errorf("expected %s to not be a symlink, but found symlink to %s\n",
					source.FullName(), target.FullName())
			}
		default:
			t.Errorf("got unexpected error from Object.SymlinkHeaders() for %s: %s\n",
				source.FullName(), err.Error())
		}
	} else {
		if err != nil {
			t.Errorf("expected %s to be a symlink to %s, but Object.SymlinkHeaders() returned error: %s\n",
				source.FullName(), expectedTarget.FullName(), err.Error())
		} else if target.FullName() != expectedTarget.FullName() {
			t.Errorf("expected %s to be a symlink to %s, but got target %s\n",
				source.FullName(), expectedTarget.FullName(), target.FullName())
		}
	}
}