-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathconditional_put_test.go
More file actions
379 lines (322 loc) · 10.2 KB
/
Copy pathconditional_put_test.go
File metadata and controls
379 lines (322 loc) · 10.2 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package gofakes3_test
import (
"bytes"
"context"
"io/ioutil"
"os"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/spf13/afero"
bolt "go.etcd.io/bbolt"
"github.com/johannesboyne/gofakes3"
"github.com/johannesboyne/gofakes3/backend/s3afero"
"github.com/johannesboyne/gofakes3/backend/s3bolt"
"github.com/johannesboyne/gofakes3/backend/s3mem"
)
// backendTestCase represents a backend configuration for testing
type backendTestCase struct {
name string
backend gofakes3.Backend
cleanup func() // Optional cleanup function
}
// createAllBackends creates instances of all available backends for testing
func createAllBackends(t *testing.T) []backendTestCase {
t.Helper()
var backends []backendTestCase
// 1. s3mem backend
backends = append(backends, backendTestCase{
name: "s3mem",
backend: s3mem.New(s3mem.WithTimeSource(gofakes3.FixedTimeSource(defaultDate))),
cleanup: nil,
})
// 2. s3bolt backend
tempBoltFile, err := ioutil.TempFile("", "gofakes3-test-*.db")
if err != nil {
t.Fatal("Failed to create temp bolt file:", err)
}
tempBoltFile.Close()
boltDB, err := bolt.Open(tempBoltFile.Name(), 0600, nil)
if err != nil {
os.Remove(tempBoltFile.Name())
t.Fatal("Failed to open bolt database:", err)
}
boltBackend := s3bolt.New(boltDB, s3bolt.WithTimeSource(gofakes3.FixedTimeSource(defaultDate)))
backends = append(backends, backendTestCase{
name: "s3bolt",
backend: boltBackend,
cleanup: func() {
boltDB.Close()
os.Remove(tempBoltFile.Name())
},
})
// 3. s3afero SingleBucket backend (use defaultBucket name to match test expectations)
singleBackend, err := s3afero.SingleBucket(defaultBucket, afero.NewMemMapFs(), nil)
if err != nil {
t.Fatal("Failed to create s3afero single backend:", err)
}
backends = append(backends, backendTestCase{
name: "s3afero-single",
backend: singleBackend,
cleanup: nil,
})
// 4. s3afero MultiBucket backend
multiBackend, err := s3afero.MultiBucket(afero.NewMemMapFs())
if err != nil {
t.Fatal("Failed to create s3afero multi backend:", err)
}
backends = append(backends, backendTestCase{
name: "s3afero-multi",
backend: multiBackend,
cleanup: nil,
})
return backends
}
// runWithAllBackends runs a test function against all backend implementations
func runWithAllBackends(t *testing.T, testFunc func(*testing.T, *testServer)) {
backends := createAllBackends(t)
for _, backendCase := range backends {
t.Run(backendCase.name, func(t *testing.T) {
// Ensure cleanup happens even if test fails
if backendCase.cleanup != nil {
defer backendCase.cleanup()
}
// Create test server with this backend
var ts *testServer
if backendCase.name == "s3afero-single" {
// SingleBucket backend already has the bucket, don't try to create it
ts = newTestServer(t, withBackend(backendCase.backend), withoutInitialBuckets())
} else {
ts = newTestServer(t, withBackend(backendCase.backend))
}
defer ts.Close()
// Run the test function
testFunc(t, ts)
})
}
}
func TestConditionalPutIfNoneMatch(t *testing.T) {
runWithAllBackends(t, testConditionalPutIfNoneMatch)
}
func testConditionalPutIfNoneMatch(t *testing.T, ts *testServer) {
svc := ts.s3Client()
bucket := aws.String(defaultBucket)
key := aws.String("test-object")
body := []byte("test content")
// Test 1: If-None-Match with * should succeed when object doesn't exist
_, err := svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: bucket,
Key: key,
Body: bytes.NewReader(body),
IfNoneMatch: aws.String("*"),
})
if err != nil {
t.Fatal("Expected success when object doesn't exist:", err)
}
// Test 2: If-None-Match with * should fail when object exists
_, err = svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: bucket,
Key: key,
Body: bytes.NewReader([]byte("new content")),
IfNoneMatch: aws.String("*"),
})
if err == nil {
t.Fatal("Expected failure when object exists")
}
if !hasErrorCode(err, gofakes3.ErrPreconditionFailed) {
t.Fatal("Expected PreconditionFailed error, got:", err)
}
// Verify original content is unchanged
obj := ts.backendGetString(defaultBucket, "test-object", nil)
if obj != "test content" {
t.Fatal("Object content was modified when it shouldn't have been")
}
}
func TestConditionalPutIfMatch(t *testing.T) {
runWithAllBackends(t, testConditionalPutIfMatch)
}
func testConditionalPutIfMatch(t *testing.T, ts *testServer) {
svc := ts.s3Client()
bucket := aws.String(defaultBucket)
key := aws.String("test-object")
body := []byte("test content")
// Create initial object
putResp, err := svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: bucket,
Key: key,
Body: bytes.NewReader(body),
})
if err != nil {
t.Fatal("Failed to create initial object:", err)
}
etag := *putResp.ETag
// Test 1: If-Match with correct ETag should succeed
_, err = svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: bucket,
Key: key,
Body: bytes.NewReader([]byte("updated content")),
IfMatch: aws.String(etag),
})
if err != nil {
t.Fatal("Expected success with matching ETag:", err)
}
// Test 2: If-Match with wrong ETag should fail
_, err = svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: bucket,
Key: key,
Body: bytes.NewReader([]byte("another update")),
IfMatch: aws.String(`"wrong-etag"`),
})
if err == nil {
t.Fatal("Expected failure with wrong ETag")
}
if !hasErrorCode(err, gofakes3.ErrPreconditionFailed) {
t.Fatal("Expected PreconditionFailed error, got:", err)
}
// Verify content is the updated content, not the failed update
obj := ts.backendGetString(defaultBucket, "test-object", nil)
if obj != "updated content" {
t.Fatal("Object content is not the expected updated content")
}
}
func TestConditionalPutNonExistentObject(t *testing.T) {
runWithAllBackends(t, testConditionalPutNonExistentObject)
}
func testConditionalPutNonExistentObject(t *testing.T, ts *testServer) {
svc := ts.s3Client()
bucket := aws.String(defaultBucket)
key := aws.String("nonexistent-object")
// Test: If-Match on non-existent object should fail
_, err := svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: bucket,
Key: key,
Body: bytes.NewReader([]byte("content")),
IfMatch: aws.String(`"some-etag"`),
})
if err == nil {
t.Fatal("Expected failure when object doesn't exist")
}
if !hasErrorCode(err, gofakes3.ErrPreconditionFailed) {
t.Fatal("Expected PreconditionFailed error, got:", err)
}
// Verify object was not created
if exists, _ := ts.backendObjectExists(defaultBucket, "nonexistent-object"); exists {
t.Fatal("Object should not have been created")
}
}
func TestConditionalPutMultipleConditions(t *testing.T) {
runWithAllBackends(t, testConditionalPutMultipleConditions)
}
func testConditionalPutMultipleConditions(t *testing.T, ts *testServer) {
svc := ts.s3Client()
bucket := aws.String(defaultBucket)
key := aws.String("test-object")
body := []byte("test content")
// Create initial object
putResp, err := svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: bucket,
Key: key,
Body: bytes.NewReader(body),
})
if err != nil {
t.Fatal("Failed to create initial object:", err)
}
etag := *putResp.ETag
// Test: If-Match condition should pass
_, err = svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: bucket,
Key: key,
Body: bytes.NewReader([]byte("updated content")),
IfMatch: aws.String(etag),
})
if err != nil {
t.Fatal("Expected success with If-Match condition:", err)
}
// Verify content was updated
obj := ts.backendGetString(defaultBucket, "test-object", nil)
if obj != "updated content" {
t.Fatal("Object content was not updated")
}
}
func TestConditionalPutCopyOperation(t *testing.T) {
runWithAllBackends(t, testConditionalPutCopyOperation)
}
func testConditionalPutCopyOperation(t *testing.T, ts *testServer) {
svc := ts.s3Client()
bucket := aws.String(defaultBucket)
sourceKey := aws.String("source-object")
destKey := aws.String("dest-object")
// Create source object
_, err := svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: bucket,
Key: sourceKey,
Body: bytes.NewReader([]byte("source content")),
})
if err != nil {
t.Fatal("Failed to create source object:", err)
}
// Test: Copy operation should not be affected by conditional headers
// (copy operations always pass nil conditions to backend)
_, err = svc.CopyObject(context.TODO(), &s3.CopyObjectInput{
Bucket: bucket,
Key: destKey,
CopySource: aws.String(defaultBucket + "/" + *sourceKey),
})
if err != nil {
t.Fatal("Copy operation failed:", err)
}
// Verify copy succeeded
obj := ts.backendGetString(defaultBucket, "dest-object", nil)
if obj != "source content" {
t.Fatal("Copy operation did not work correctly")
}
}
func TestConditionalPutETagComparison(t *testing.T) {
runWithAllBackends(t, testConditionalPutETagComparison)
}
func testConditionalPutETagComparison(t *testing.T, ts *testServer) {
svc := ts.s3Client()
bucket := aws.String(defaultBucket)
key := aws.String("test-object")
body := []byte("test content")
// Create initial object
putResp, err := svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: bucket,
Key: key,
Body: bytes.NewReader(body),
})
if err != nil {
t.Fatal("Failed to create initial object:", err)
}
etag := *putResp.ETag
// Test 1: ETag with quotes should work
_, err = svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: bucket,
Key: key,
Body: bytes.NewReader([]byte("updated content 1")),
IfMatch: aws.String(etag), // ETag already has quotes
})
if err != nil {
t.Fatal("Expected success with quoted ETag:", err)
}
// Get new ETag
getResp, err := svc.GetObject(context.TODO(), &s3.GetObjectInput{
Bucket: bucket,
Key: key,
})
if err != nil {
t.Fatal("Failed to get object:", err)
}
newEtag := *getResp.ETag
// Test 2: ETag without quotes should also work
unquotedEtag := newEtag[1 : len(newEtag)-1] // Remove quotes
_, err = svc.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: bucket,
Key: key,
Body: bytes.NewReader([]byte("updated content 2")),
IfMatch: aws.String(unquotedEtag),
})
if err != nil {
t.Fatal("Expected success with unquoted ETag:", err)
}
}