forked from s3tester/s3tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations_test.go
400 lines (297 loc) · 10.5 KB
/
operations_test.go
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package main
import (
"context"
"fmt"
"io/ioutil"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
)
// MockS3Client is used for mocking Amazon S3 for testing purposes
type MockS3Client struct {
s3iface.S3API
S3OpHandler func(interface{}) interface{}
}
// NewMockS3Client creates a new mock S3 client; suitable for use in unit tests
func NewMockS3Client(handler func(interface{}) interface{}) *MockS3Client {
return &MockS3Client{S3OpHandler: handler}
}
func (m *MockS3Client) PutObject(in *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
m.S3OpHandler(in)
return &s3.PutObjectOutput{}, nil
}
func TestPutOp(t *testing.T) {
var numBytes int64 = 100
md5 := "+M5KlcqLv/LqWGVzA4hI/A=="
handler := func(in interface{}) interface{} {
i := in.(*s3.PutObjectInput)
if *i.Bucket != "b" {
t.Errorf("Expected bucket: %s but got: %s", "b", *i.Bucket)
}
if *i.Key != "k1" {
t.Errorf("Expected key: %s but got: %s", "k1", *i.Key)
}
if *i.ContentLength != numBytes {
t.Errorf("Expected object size: %d but got: %d", numBytes, *i.ContentLength)
}
if *i.ContentMD5 != md5 {
t.Errorf("Expected content md5: %s, but got %s", md5, *i.ContentMD5)
}
data, err := ioutil.ReadAll(i.Body)
if err != nil {
t.Errorf("Failed to read %d expected bytes from objects. Error: %v", *i.ContentLength, err)
}
if int64(len(data)) != *i.ContentLength {
t.Fatalf("Failed to read %d expected bytes from objects. Error: %v", *i.ContentLength, err)
}
return in
}
svc := NewMockS3Client(handler)
Put(svc, "b", "k1", "", numBytes, map[string]*string{})
}
func TestPutWithTagsOp(t *testing.T) {
var numBytes int64 = 394
tags := "tag1=blue&tag2=red"
handler := func(in interface{}) interface{} {
i := in.(*s3.PutObjectInput)
if *i.Bucket != "b" {
t.Fatalf("Expected bucket: %s but got: %s", "b", *i.Bucket)
}
if *i.Key != "k1" {
t.Fatalf("Expected key: %s but got: %s", "k1", *i.Key)
}
if *i.ContentLength != numBytes {
t.Fatalf("Expected object size: %d but got: %d", numBytes, *i.ContentLength)
}
data, err := ioutil.ReadAll(i.Body)
if err != nil {
t.Fatalf("Failed to read %d expected bytes from objects. Error: %v", *i.ContentLength, err)
}
if int64(len(data)) != *i.ContentLength {
t.Fatalf("Failed to read %d expected bytes from objects. Error: %v", *i.ContentLength, err)
}
if *i.Tagging != tags {
t.Fatalf("Expected tags: %s but got: %s", tags, *i.Tagging)
}
return in
}
svc := NewMockS3Client(handler)
err := Put(svc, "b", "k1", tags, numBytes, map[string]*string{})
if err != nil {
t.Fatalf("Failed PUT operation with error: %v", err)
}
}
func (m *MockS3Client) PutObjectTagging(in *s3.PutObjectTaggingInput) (*s3.PutObjectTaggingOutput, error) {
m.S3OpHandler(in)
return &s3.PutObjectTaggingOutput{}, nil
}
func TestPutTaggingOp(t *testing.T) {
tags := "tag1=blue&tag2=red"
handler := func(in interface{}) interface{} {
i := in.(*s3.PutObjectTaggingInput)
if *i.Bucket != "b" {
t.Fatalf("Expected bucket: %s but got: %s", "b", *i.Bucket)
}
if *i.Key != "k1" {
t.Fatalf("Expected key: %s but got: %s", "k1", *i.Key)
}
if i.Tagging.String() != parseTags(tags).String() {
t.Fatalf("Expected tags: %s but got: %s", tags, *i.Tagging)
}
return in
}
svc := NewMockS3Client(handler)
err := PutTagging(svc, "b", "k1", tags)
if err != nil {
t.Fatalf("Failed PUT with tags operation with error: %v", err)
}
}
func (m *MockS3Client) CopyObject(in *s3.CopyObjectInput) (*s3.CopyObjectOutput, error) {
m.S3OpHandler(in)
return &s3.CopyObjectOutput{}, nil
}
func TestUpdateMetadataOp(t *testing.T) {
v1, v2, v3 := "val1", "val2", "val3"
metadata := map[string]*string{"attribute1": &v1, "attribute2": &v2, "attribute3": &v3}
handler := func(in interface{}) interface{} {
i := in.(*s3.CopyObjectInput)
if *i.Bucket != "b" {
t.Fatalf("Expected bucket: %s but got: %s", "b", *i.Bucket)
}
if *i.Key != "k1" {
t.Fatalf("Expected key: %s but got: %s", "k1", *i.Key)
}
if *i.MetadataDirective != directiveReplace {
t.Fatalf("For metadata updates metadata directive must be REPLACE but got: %s", *i.MetadataDirective)
}
for k, v := range metadata {
if *i.Metadata[k] != *v {
t.Fatalf("Expected meta key/value pair: %s %s but got: %s %s", k, *v, k, *i.Metadata[k])
}
}
return in
}
svc := NewMockS3Client(handler)
err := UpdateMetadata(svc, "b", "k1", metadata)
if err != nil {
t.Fatalf("Failed update metadata operation with error: %v", err)
}
}
func TestCopyOperation(t *testing.T) {
metadataVal1, metadataVal2 := "val1", "val2"
testData := []struct {
copySourceBucket string
destinationBucket string
objectKey string
copySource string
taggingDirective string
tags string
metadataDirective string
metadata map[string]*string
}{
// No tag, No metadata
{copySourceBucket: "cs", destinationBucket: "b", objectKey: "k", copySource: "cs/k",
taggingDirective: directiveCopy, tags: "", metadataDirective: directiveCopy, metadata: nil},
// Only metadata
{copySourceBucket: "cs", destinationBucket: "b", objectKey: "k", copySource: "cs/k",
taggingDirective: directiveCopy, tags: "",
metadataDirective: directiveReplace, metadata: map[string]*string{"key1": &metadataVal1, "key2": &metadataVal2}},
// Only tag
{copySourceBucket: "cs", destinationBucket: "b", objectKey: "k", copySource: "cs/k",
taggingDirective: directiveReplace, tags: "tag1=blue&tag2=red", metadataDirective: directiveCopy, metadata: nil},
// Both tag and metadata
{copySourceBucket: "cs", destinationBucket: "b", objectKey: "k", copySource: "cs/k",
taggingDirective: directiveReplace, tags: "tag1=blue&tag2=red",
metadataDirective: directiveReplace, metadata: map[string]*string{"key1": &metadataVal1, "key2": &metadataVal2}},
}
for i, test := range testData {
testcase := fmt.Sprintf("%d/tag: %s, metadata: %s", i, test.taggingDirective, test.metadataDirective)
t.Run(testcase, func(t *testing.T) {
handler := func(in interface{}) interface{} {
i := in.(*s3.CopyObjectInput)
if *i.CopySource != test.copySource {
t.Errorf("Expected copySource: %s but got: %s", test.copySource, *i.CopySource)
}
if *i.Bucket != test.destinationBucket {
t.Errorf("Expected bucket: %s but got: %s", test.destinationBucket, *i.Bucket)
}
if *i.Key != test.objectKey {
t.Errorf("Expected key: %s but got: %s", test.objectKey, *i.Key)
}
if *i.TaggingDirective != test.taggingDirective {
t.Errorf("Expected taggingDirective: %s but got: %s", test.taggingDirective, *i.TaggingDirective)
}
if *i.Tagging != test.tags {
t.Errorf("Expected tags: %s but got: %s", test.tags, *i.Tagging)
}
if *i.MetadataDirective != test.metadataDirective {
t.Errorf("Expected metadataDirective: %s but got: %s", test.metadataDirective, *i.MetadataDirective)
}
for k, v := range test.metadata {
if *i.Metadata[k] != *v {
t.Errorf("Expected meta key/value pair: %s %s but got: %s %s", k, *v, k, *i.Metadata[k])
}
}
return in
}
svc := NewMockS3Client(handler)
err := Copy(svc, test.copySourceBucket, test.destinationBucket, test.objectKey,
test.tags, test.taggingDirective, test.metadata, test.metadataDirective)
if err != nil {
t.Errorf("Failed COPY operation with error: %v", err)
}
})
}
}
func (m *MockS3Client) HeadObject(in *s3.HeadObjectInput) (*s3.HeadObjectOutput, error) {
m.S3OpHandler(in)
return &s3.HeadObjectOutput{}, nil
}
func TestHeadObjectOp(t *testing.T) {
handler := func(in interface{}) interface{} {
i := in.(*s3.HeadObjectInput)
if *i.Bucket != "b" {
t.Fatalf("Expected bucket: %s but got: %s", "b", *i.Bucket)
}
if *i.Key != "k1" {
t.Fatalf("Expected key: %s but got: %s", "k1", *i.Key)
}
return in
}
svc := NewMockS3Client(handler)
err := Head(svc, "b", "k1")
if err != nil {
t.Fatalf("Failed HEAD operation with error: %v", err)
}
}
func (m *MockS3Client) DeleteObject(in *s3.DeleteObjectInput) (*s3.DeleteObjectOutput, error) {
m.S3OpHandler(in)
return &s3.DeleteObjectOutput{}, nil
}
func TestDeleteObjectOp(t *testing.T) {
handler := func(in interface{}) interface{} {
i := in.(*s3.DeleteObjectInput)
if *i.Bucket != "b" {
t.Fatalf("Expected bucket: %s but got: %s", "b", *i.Bucket)
}
if *i.Key != "k1" {
t.Fatalf("Expected key: %s but got: %s", "k1", *i.Key)
}
return in
}
svc := NewMockS3Client(handler)
err := Delete(svc, "b", "k1")
if err != nil {
t.Fatalf("Failed DELETE operation with error: %v", err)
}
}
func TestParseMetadataString(t *testing.T) {
validString := []string{"k1=v1", "ColfaX3.0-#@our=blue", "a=0&b=1&b=3&fajng47q^($%_02hg=()**#!%CLAr3"}
for _, m := range validString {
parseMetadataString(m)
}
}
func (m *MockS3Client) CreateMultipartUploadWithContext(ctx context.Context, in *s3.CreateMultipartUploadInput, opts ...request.Option) (*s3.CreateMultipartUploadOutput, error) {
m.S3OpHandler(in)
return &s3.CreateMultipartUploadOutput{UploadId: aws.String("VXBsb2FkIElEIGZvciA2aWWpbmcncyBteS1tb3ZpZS5tMnRzIHVwbG9hZA")}, nil
}
func (m *MockS3Client) UploadPartWithContext(ctx context.Context, in *s3.UploadPartInput, opts ...request.Option) (*s3.UploadPartOutput, error) {
m.S3OpHandler(in)
eTag := ""
return &s3.UploadPartOutput{ETag: &eTag}, nil
}
func (m *MockS3Client) CompleteMultipartUploadWithContext(ctx context.Context, in *s3.CompleteMultipartUploadInput, opts ...request.Option) (*s3.CompleteMultipartUploadOutput, error) {
m.S3OpHandler(in)
return &s3.CompleteMultipartUploadOutput{}, nil
}
func TestMultipartPutWithTagging(t *testing.T) {
tags := "tag1=blue&tag2=red"
handler := func(in interface{}) interface{} {
fmt.Print(in)
i, ok := in.(*s3.CreateMultipartUploadInput)
if !ok {
return in
}
if *i.Bucket != "b" {
t.Fatalf("Expected bucket: %s but got: %s", "b", *i.Bucket)
}
if *i.Key != "k1" {
t.Fatalf("Expected key: %s but got: %s", "k1", *i.Key)
}
if *i.Tagging != tags {
t.Fatalf("Expected tags: %s but got: %s", tags, *i.Tagging)
}
if len(i.Metadata) != 0 {
t.Fatalf("Expected empty metadata but got metadata with length: %d", len(i.Metadata))
}
return in
}
svc := NewMockS3Client(handler)
mockSysInterruptHandler := NewSyscallParams(*NewParameters())
err := MultipartPut(context.Background(), svc, "b", "k1", 1, 1, tags, map[string]*string{}, mockSysInterruptHandler)
if err != nil {
t.Fatalf("Failed MultipartPut operation with error: %v", err)
}
}