-
Notifications
You must be signed in to change notification settings - Fork 772
Expand file tree
/
Copy pathapi_op_UploadObject_test.go
More file actions
1281 lines (1069 loc) · 36.3 KB
/
api_op_UploadObject_test.go
File metadata and controls
1281 lines (1069 loc) · 36.3 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package transfermanager
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"reflect"
"regexp"
"slices"
"strconv"
"strings"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
s3testing "github.com/aws/aws-sdk-go-v2/feature/s3/transfermanager/internal/testing"
"github.com/aws/aws-sdk-go-v2/internal/awstesting"
"github.com/aws/aws-sdk-go-v2/internal/sdk"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
)
// getReaderLength discards the bytes from reader and returns the length
func getReaderLength(r io.Reader) int64 {
n, _ := io.Copy(ioutil.Discard, r)
return n
}
func TestUploadOrderMulti(t *testing.T) {
c, invocations, args := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
resp, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key - value"),
Body: bytes.NewReader(buf20MB),
ServerSideEncryption: "aws:kms",
SSEKMSKeyID: aws.String("KmsId"),
ContentType: aws.String("content/type"),
})
if err != nil {
t.Errorf("Expected no error but received %v", err)
}
if diff := cmpDiff([]string{"CreateMultipartUpload", "UploadPart", "UploadPart", "UploadPart", "CompleteMultipartUpload"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
if "UPLOAD-ID" != aws.ToString(resp.UploadID) {
t.Errorf("expect %q, got %q", "UPLOAD-ID", aws.ToString(resp.UploadID))
}
if "VERSION-ID" != aws.ToString(resp.VersionID) {
t.Errorf("expect %q, got %q", "VERSION-ID", aws.ToString(resp.VersionID))
}
// Validate input values
// UploadPart
for i := 1; i < 4; i++ {
v := aws.ToString((*args)[i].(*s3.UploadPartInput).UploadId)
if "UPLOAD-ID" != v {
t.Errorf("Expected %q, but received %q", "UPLOAD-ID", v)
}
}
// CompleteMultipartUpload
v := aws.ToString((*args)[4].(*s3.CompleteMultipartUploadInput).UploadId)
if "UPLOAD-ID" != v {
t.Errorf("Expected %q, but received %q", "UPLOAD-ID", v)
}
parts := (*args)[4].(*s3.CompleteMultipartUploadInput).MultipartUpload.Parts
for i := range 3 {
num := parts[i].PartNumber
etag := aws.ToString(parts[i].ETag)
if int32(i+1) != aws.ToInt32(num) {
t.Errorf("expect %d, got %d", i+1, num)
}
if matched, err := regexp.MatchString(`^ETAG\d+$`, etag); !matched || err != nil {
t.Errorf("Failed regexp expression `^ETAG\\d+$`")
}
}
// Custom headers
cmu := (*args)[0].(*s3.CreateMultipartUploadInput)
if e, a := types.ServerSideEncryption("aws:kms"), cmu.ServerSideEncryption; e != a {
t.Errorf("expect %q, got %q", e, a)
}
if e, a := "KmsId", aws.ToString(cmu.SSEKMSKeyId); e != a {
t.Errorf("expect %q, got %q", e, a)
}
if e, a := "content/type", aws.ToString(cmu.ContentType); e != a {
t.Errorf("expect %q, got %q", e, a)
}
}
func TestUploadOrderMultiTriggerredBySinglePartSize(t *testing.T) {
c, invocations, args := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
resp, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key - value"),
Body: bytes.NewReader(make([]byte, 8*1024*1024)),
ServerSideEncryption: "aws:kms",
SSEKMSKeyID: aws.String("KmsId"),
ContentType: aws.String("content/type"),
})
if err != nil {
t.Errorf("Expected no error but received %v", err)
}
if diff := cmpDiff([]string{"CreateMultipartUpload", "UploadPart", "CompleteMultipartUpload"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
if "UPLOAD-ID" != aws.ToString(resp.UploadID) {
t.Errorf("expect %q, got %q", "UPLOAD-ID", aws.ToString(resp.UploadID))
}
if "VERSION-ID" != aws.ToString(resp.VersionID) {
t.Errorf("expect %q, got %q", "VERSION-ID", aws.ToString(resp.VersionID))
}
// Validate input values
v := aws.ToString((*args)[1].(*s3.UploadPartInput).UploadId)
if "UPLOAD-ID" != v {
t.Errorf("Expected %q, but received %q", "UPLOAD-ID", v)
}
v = aws.ToString((*args)[2].(*s3.CompleteMultipartUploadInput).UploadId)
if "UPLOAD-ID" != v {
t.Errorf("Expected %q, but received %q", "UPLOAD-ID", v)
}
parts := (*args)[2].(*s3.CompleteMultipartUploadInput).MultipartUpload.Parts
num := parts[0].PartNumber
etag := aws.ToString(parts[0].ETag)
if aws.ToInt32(num) != 1 {
t.Errorf("expect part number to be 1, got %d", num)
}
if matched, err := regexp.MatchString(`^ETAG\d+$`, etag); !matched || err != nil {
t.Errorf("Failed regexp expression `^ETAG\\d+$`, got %s", etag)
}
// Custom headers
cmu := (*args)[0].(*s3.CreateMultipartUploadInput)
if e, a := types.ServerSideEncryption("aws:kms"), cmu.ServerSideEncryption; e != a {
t.Errorf("expect %q, got %q", e, a)
}
if e, a := "KmsId", aws.ToString(cmu.SSEKMSKeyId); e != a {
t.Errorf("expect %q, got %q", e, a)
}
if e, a := "content/type", aws.ToString(cmu.ContentType); e != a {
t.Errorf("expect %q, got %q", e, a)
}
}
func TestUploadOrderMultiJustExceedSinglePart(t *testing.T) {
c, invocations, args := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
resp, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key - value"),
Body: bytes.NewReader(make([]byte, 8*1024*1024+1)),
ServerSideEncryption: "aws:kms",
SSEKMSKeyID: aws.String("KmsId"),
ContentType: aws.String("content/type"),
})
if err != nil {
t.Errorf("Expected no error but received %v", err)
}
if diff := cmpDiff([]string{"CreateMultipartUpload", "UploadPart", "UploadPart",
"CompleteMultipartUpload"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
if "UPLOAD-ID" != aws.ToString(resp.UploadID) {
t.Errorf("expect %q, got %q", "UPLOAD-ID", aws.ToString(resp.UploadID))
}
if "VERSION-ID" != aws.ToString(resp.VersionID) {
t.Errorf("expect %q, got %q", "VERSION-ID", aws.ToString(resp.VersionID))
}
// Validate input values
// UploadPart
for i := 1; i < 3; i++ {
v := aws.ToString((*args)[i].(*s3.UploadPartInput).UploadId)
if "UPLOAD-ID" != v {
t.Errorf("Expected %q, but received %q", "UPLOAD-ID", v)
}
}
// CompleteMultipartUpload
v := aws.ToString((*args)[3].(*s3.CompleteMultipartUploadInput).UploadId)
if "UPLOAD-ID" != v {
t.Errorf("Expected %q, but received %q", "UPLOAD-ID", v)
}
parts := (*args)[3].(*s3.CompleteMultipartUploadInput).MultipartUpload.Parts
for i := range 2 {
num := parts[i].PartNumber
etag := aws.ToString(parts[i].ETag)
if int32(i+1) != aws.ToInt32(num) {
t.Errorf("expect %d, got %d", i+1, num)
}
if matched, err := regexp.MatchString(`^ETAG\d+$`, etag); !matched || err != nil {
t.Errorf("Failed regexp expression `^ETAG\\d+$`")
}
}
// Custom headers
cmu := (*args)[0].(*s3.CreateMultipartUploadInput)
if e, a := types.ServerSideEncryption("aws:kms"), cmu.ServerSideEncryption; e != a {
t.Errorf("expect %q, got %q", e, a)
}
if e, a := "KmsId", aws.ToString(cmu.SSEKMSKeyId); e != a {
t.Errorf("expect %q, got %q", e, a)
}
if e, a := "content/type", aws.ToString(cmu.ContentType); e != a {
t.Errorf("expect %q, got %q", e, a)
}
}
func TestUploadOrderMultiDifferentPartSize(t *testing.T) {
c, ops, args := s3testing.NewUploadLoggingClient(nil)
mgr := New(c, func(options *Options) {
options.PartSizeBytes = 1024 * 1024 * 11
options.Concurrency = 1
})
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewReader(buf20MB),
})
if err != nil {
t.Errorf("expect no error, got %v", err)
}
vals := []string{"CreateMultipartUpload", "UploadPart", "UploadPart", "CompleteMultipartUpload"}
if !reflect.DeepEqual(vals, *ops) {
t.Errorf("expect %v, got %v", vals, *ops)
}
// Part lengths
if len := getReaderLength((*args)[1].(*s3.UploadPartInput).Body); 1024*1024*11 != len {
t.Errorf("expect %d, got %d", 1024*1024*7, len)
}
if len := getReaderLength((*args)[2].(*s3.UploadPartInput).Body); 1024*1024*9 != len {
t.Errorf("expect %d, got %d", 1024*1024*5, len)
}
}
func TestUploadWithPartSizeIncreased(t *testing.T) {
c, ops, args := s3testing.NewUploadLoggingClient([]string{"CreateMultipartUpload", "CompleteMultipartUpload"})
mgr := New(c, func(o *Options) {
o.PartSizeBytes = 5
o.Concurrency = 1
})
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewReader(buf20MB),
})
if err != nil {
t.Errorf("expect no error, got %v", err)
}
// in this case the content length is 20971520 bytes, the calculated parts count will be
// 20971520 / 5 = 4194304 exceeding 10000 limit, so the part size increase to
// 20971520 / 10000 + 1 = 2098 bytes, and total parts uploaded will be 20971520 / 2098 = 9996
// (upper bound for last part)
if e, a := 9996, len(*ops); e != a {
t.Errorf("expect %d parts uploaded, got %d", e, a)
}
if e, a := int64(2098), getReaderLength((*args)[0].(*s3.UploadPartInput).Body); e != a {
t.Errorf("expect updated part size to be %d, got %d", e, a)
}
}
// TestUploadMultipartTriggeredByThreshold tests that MultipartUploadThreshold is used
// to decide when to use multipart upload. With a low threshold (1KB), a 100KB file
// should trigger multipart upload even though it's smaller than PartSizeBytes (5MB).
func TestUploadMultipartTriggeredByThreshold(t *testing.T) {
c, invocations, _ := s3testing.NewUploadLoggingClient(nil)
mgr := New(c, func(o *Options) {
o.MultipartUploadThreshold = 1024 // 1 KB threshold
o.PartSizeBytes = 5 * 1024 * 1024 // 5 MB part size (S3 minimum)
})
// Upload 100KB - this is > 1KB threshold, so should use multipart
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewReader(make([]byte, 100*1024)), // 100 KB
})
if err != nil {
t.Errorf("expect no error but received %v", err)
}
// Should use multipart upload (CreateMultipartUpload -> UploadPart -> Complete)
// not single upload (PutObject)
if len(*invocations) == 0 {
t.Fatal("expected at least one invocation")
}
if (*invocations)[0] != "CreateMultipartUpload" {
t.Errorf("expected multipart upload due to threshold, but got %v", *invocations)
}
}
// TestUploadSingleWhenBelowThreshold tests that files smaller than
// MultipartUploadThreshold use single upload (PutObject).
func TestUploadSingleWhenBelowThreshold(t *testing.T) {
c, invocations, _ := s3testing.NewUploadLoggingClient(nil)
mgr := New(c, func(o *Options) {
o.MultipartUploadThreshold = 200 * 1024 // 200 KB threshold
o.PartSizeBytes = 5 * 1024 * 1024 // 5 MB part size
})
// Upload 100KB - this is < 200KB threshold, so should use single upload
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewReader(make([]byte, 100*1024)), // 100 KB
})
if err != nil {
t.Errorf("expect no error but received %v", err)
}
// Should use single upload (PutObject), not multipart
if diff := cmpDiff([]string{"PutObject"}, *invocations); len(diff) > 0 {
t.Errorf("expected single upload when below threshold: %s", diff)
}
}
// TestUploadThresholdCappedByPartSize tests that when MultipartUploadThreshold
// is larger than PartSizeBytes, the effective threshold is capped to PartSizeBytes.
// This ensures backward compatibility with existing behavior.
func TestUploadThresholdCappedByPartSize(t *testing.T) {
c, invocations, _ := s3testing.NewUploadLoggingClient(nil)
mgr := New(c, func(o *Options) {
o.MultipartUploadThreshold = 16 * 1024 * 1024 // 16 MB threshold
o.PartSizeBytes = 8 * 1024 * 1024 // 8 MB part size
})
// Upload exactly 8MB - should trigger multipart because it equals PartSizeBytes
// (which caps the effective threshold)
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewReader(make([]byte, 8*1024*1024)), // 8 MB
})
if err != nil {
t.Errorf("expect no error but received %v", err)
}
// Should use multipart upload because 8MB >= min(16MB, 8MB) = 8MB
if len(*invocations) == 0 {
t.Fatal("expected at least one invocation")
}
if (*invocations)[0] != "CreateMultipartUpload" {
t.Errorf("expected multipart upload when at part size boundary, but got %v", *invocations)
}
}
func TestUploadOrderSingle(t *testing.T) {
c, invocations, params := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
resp, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key - value"),
Body: bytes.NewReader(buf2MB),
ServerSideEncryption: "aws:kms",
SSEKMSKeyID: aws.String("KmsId"),
ContentType: aws.String("content/type"),
})
if err != nil {
t.Errorf("expect no error but received %v", err)
}
if diff := cmpDiff([]string{"PutObject"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
if e := "VERSION-ID"; e != aws.ToString(resp.VersionID) {
t.Errorf("expect %q, got %q", e, aws.ToString(resp.VersionID))
}
if len(aws.ToString(resp.UploadID)) > 0 {
t.Errorf("expect empty string, got %q", aws.ToString(resp.UploadID))
}
putObjectInput := (*params)[0].(*s3.PutObjectInput)
if e, a := types.ServerSideEncryption("aws:kms"), putObjectInput.ServerSideEncryption; e != a {
t.Errorf("expect %q, got %q", e, a)
}
if e, a := "KmsId", aws.ToString(putObjectInput.SSEKMSKeyId); e != a {
t.Errorf("expect %q, got %q", e, a)
}
if e, a := "content/type", aws.ToString(putObjectInput.ContentType); e != a {
t.Errorf("Expected %q, but received %q", e, a)
}
}
func TestUploadSingleFailure(t *testing.T) {
c, invocations, _ := s3testing.NewUploadLoggingClient(nil)
c.PutObjectFn = func(*s3testing.TransferManagerLoggingClient, *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
return nil, fmt.Errorf("put object failure")
}
mgr := New(c)
resp, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewReader(buf2MB),
})
if err == nil {
t.Error("expect error, got nil")
}
if diff := cmpDiff([]string{"PutObject"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
if resp != nil {
t.Errorf("expect response to be nil, got %v", resp)
}
}
func TestUploadOrderZero(t *testing.T) {
c, invocations, params := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
resp, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewReader(make([]byte, 0)),
})
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if diff := cmpDiff([]string{"PutObject"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
if len(aws.ToString(resp.UploadID)) > 0 {
t.Errorf("expect empty string, got %q", aws.ToString(resp.UploadID))
}
if e, a := int64(0), getReaderLength((*params)[0].(*s3.PutObjectInput).Body); e != a {
t.Errorf("Expected %d, but received %d", e, a)
}
}
func TestUploadOrderMultiFailure(t *testing.T) {
c, invocations, _ := s3testing.NewUploadLoggingClient(nil)
c.UploadPartFn = func(u *s3testing.TransferManagerLoggingClient, params *s3.UploadPartInput) (*s3.UploadPartOutput, error) {
if *params.PartNumber == 2 {
return nil, fmt.Errorf("an unexpected error")
}
return &s3.UploadPartOutput{ETag: aws.String(fmt.Sprintf("ETAG%d", u.PartNum))}, nil
}
mgr := New(c, func(o *Options) {
o.Concurrency = 1
})
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewReader(buf20MB),
})
if err == nil {
t.Error("expect error, got nil")
}
if diff := cmpDiff([]string{"CreateMultipartUpload", "UploadPart", "UploadPart", "AbortMultipartUpload"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
}
func TestUploadOrderMultiFailureOnComplete(t *testing.T) {
c, invocations, _ := s3testing.NewUploadLoggingClient(nil)
c.CompleteMultipartUploadFn = func(*s3testing.TransferManagerLoggingClient, *s3.CompleteMultipartUploadInput) (*s3.CompleteMultipartUploadOutput, error) {
return nil, fmt.Errorf("complete multipart error")
}
mgr := New(c, func(o *Options) {
o.Concurrency = 1
})
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewReader(buf20MB),
})
if err == nil {
t.Error("expect error, got nil")
}
if diff := cmpDiff([]string{"CreateMultipartUpload", "UploadPart", "UploadPart", "UploadPart",
"CompleteMultipartUpload", "AbortMultipartUpload"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
}
func TestUploadOrderMultiFailureOnCreate(t *testing.T) {
c, invocations, _ := s3testing.NewUploadLoggingClient(nil)
c.CreateMultipartUploadFn = func(*s3testing.TransferManagerLoggingClient, *s3.CreateMultipartUploadInput) (*s3.CreateMultipartUploadOutput, error) {
return nil, fmt.Errorf("create multipart upload failure")
}
mgr := New(c)
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewReader(make([]byte, 1024*1024*12)),
})
if err == nil {
t.Error("expect error, got nil")
}
if diff := cmpDiff([]string{"CreateMultipartUpload"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
}
type failreader struct {
failBytes int64
readBytes int64
}
func (f *failreader) Read(b []byte) (int, error) {
f.readBytes += int64(len(b))
if f.readBytes > f.failBytes {
return 0, fmt.Errorf("random failure")
}
return len(b), nil
}
func TestUploadOrderReadFail1(t *testing.T) {
c, invocations, _ := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: &failreader{failBytes: 1},
})
if err == nil {
t.Fatalf("expect error to not be nil")
}
if e, a := "random failure", err.Error(); !strings.Contains(a, e) {
t.Errorf("expect %v, got %v", e, a)
}
if diff := cmpDiff([]string(nil), *invocations); len(diff) > 0 {
t.Error(diff)
}
}
func TestUploadOrderReadFail2(t *testing.T) {
c, invocations, _ := s3testing.NewUploadLoggingClient([]string{"UploadPart"})
mgr := New(c, func(o *Options) {
o.Concurrency = 1
})
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: &failreader{failBytes: 8 * 1024 * 1024},
})
if err == nil {
t.Fatalf("expect error to not be nil")
}
if e, a := "random failure", err.Error(); !strings.Contains(a, e) {
t.Errorf("expect %v, got %q", e, a)
}
if diff := cmpDiff([]string{"CreateMultipartUpload", "AbortMultipartUpload"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
}
type sizedReader struct {
size int
cur int
err error
}
func (s *sizedReader) Read(p []byte) (n int, err error) {
if s.cur >= s.size {
if s.err == nil {
s.err = io.EOF
}
return 0, s.err
}
n = len(p)
s.cur += len(p)
if s.cur > s.size {
n -= s.cur - s.size
}
return n, err
}
func TestUploadOrderMultiBufferedReader(t *testing.T) {
c, invocations, params := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: &sizedReader{size: 1024 * 1024 * 21},
})
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if diff := cmpDiff([]string{"CreateMultipartUpload", "UploadPart", "UploadPart",
"UploadPart", "CompleteMultipartUpload"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
// Part lengths
var parts []int64
for i := 1; i <= 3; i++ {
parts = append(parts, getReaderLength((*params)[i].(*s3.UploadPartInput).Body))
}
slices.Sort(parts)
if diff := cmpDiff([]int64{1024 * 1024 * 5, 1024 * 1024 * 8, 1024 * 1024 * 8}, parts); len(diff) > 0 {
t.Error(diff)
}
}
func TestUploadOrderMultiBufferedReaderWithSinglePartSize(t *testing.T) {
c, invocations, params := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: &sizedReader{size: 1024 * 1024 * 8},
})
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if diff := cmpDiff([]string{"CreateMultipartUpload", "UploadPart", "CompleteMultipartUpload"},
*invocations); len(diff) > 0 {
t.Error(diff)
}
// Part lengths
parts := []int64{getReaderLength((*params)[1].(*s3.UploadPartInput).Body)}
if diff := cmpDiff([]int64{1024 * 1024 * 8}, parts); len(diff) > 0 {
t.Error(diff)
}
}
func TestUploadOrderMultiBufferedReaderJustExceedSinglePart(t *testing.T) {
c, invocations, params := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: &sizedReader{size: defaultPartSizeBytes + 1},
})
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if diff := cmpDiff([]string{"CreateMultipartUpload", "UploadPart", "UploadPart",
"CompleteMultipartUpload"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
// Part lengths
var parts []int64
for i := 1; i < 3; i++ {
parts = append(parts, getReaderLength((*params)[i].(*s3.UploadPartInput).Body))
}
slices.Sort(parts)
if diff := cmpDiff([]int64{1, 1024 * 1024 * 8}, parts); len(diff) > 0 {
t.Error(diff)
}
}
func TestUploadOrderMultiBufferedReaderPartial(t *testing.T) {
c, invocations, params := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: &sizedReader{size: 1024 * 1024 * 21, err: io.EOF},
})
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if diff := cmpDiff([]string{"CreateMultipartUpload", "UploadPart", "UploadPart",
"UploadPart", "CompleteMultipartUpload"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
// Part lengths
var parts []int64
for i := 1; i <= 3; i++ {
parts = append(parts, getReaderLength((*params)[i].(*s3.UploadPartInput).Body))
}
slices.Sort(parts)
if diff := cmpDiff([]int64{1024 * 1024 * 5, 1024 * 1024 * 8, 1024 * 1024 * 8}, parts); len(diff) > 0 {
t.Error(diff)
}
}
// TestUploadOrderMultiBufferedReaderEOF tests the edge case where the
// file size is the same as part size.
func TestUploadOrderMultiBufferedReaderEOF(t *testing.T) {
c, invocations, params := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: &sizedReader{size: 1024 * 1024 * 16, err: io.EOF},
})
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if diff := cmpDiff([]string{"CreateMultipartUpload", "UploadPart", "UploadPart", "CompleteMultipartUpload"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
// Part lengths
var parts []int64
for i := 1; i <= 2; i++ {
parts = append(parts, getReaderLength((*params)[i].(*s3.UploadPartInput).Body))
}
slices.Sort(parts)
if diff := cmpDiff([]int64{1024 * 1024 * 8, 1024 * 1024 * 8}, parts); len(diff) > 0 {
t.Error(diff)
}
}
func TestUploadOrderSingleBufferedReader(t *testing.T) {
c, invocations, _ := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
resp, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: &sizedReader{size: 1024 * 1024 * 2},
})
if err != nil {
t.Errorf("expect no error, got %v", err)
}
if diff := cmpDiff([]string{"PutObject"}, *invocations); len(diff) > 0 {
t.Error(diff)
}
if len(aws.ToString(resp.UploadID)) > 0 {
t.Errorf("expect no value, got %q", aws.ToString(resp.UploadID))
}
}
func TestUploadZeroLenObject(t *testing.T) {
c, invocations, _ := s3testing.NewUploadLoggingClient(nil)
mgr := New(c)
resp, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: strings.NewReader(""),
})
if err != nil {
t.Errorf("expect no error but received %v", err)
}
if diff := cmpDiff([]string{"PutObject"}, *invocations); len(diff) > 0 {
t.Errorf("expect request to have been made, but was not, %v", diff)
}
if len(aws.ToString(resp.UploadID)) > 0 {
t.Errorf("expect empty string, but received %q", aws.ToString(resp.UploadID))
}
}
func TestProgressListener_SingleUpload_SeekableBody(t *testing.T) {
c, _, _ := s3testing.NewUploadLoggingClient(nil)
listener := &mockListener{}
mgr := New(c, func(options *Options) {
options.ObjectProgressListeners.Register(listener)
})
body := "foobarbaz"
in := &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: strings.NewReader(body),
}
out, err := mgr.UploadObject(context.Background(), in)
if err != nil {
t.Fatal(err)
}
objectSize := int64(len(body))
listener.expectComplete(t, in, out)
listener.expectStartTotalBytes(t, objectSize)
listener.expectCompleteTotalBytes(t, objectSize)
listener.expectByteTransfers(t, objectSize)
}
func TestProgressListener_SingleUpload_UnseekableBody(t *testing.T) {
c, _, _ := s3testing.NewUploadLoggingClient(nil)
listener := &mockListener{}
mgr := New(c, func(options *Options) {
options.ObjectProgressListeners.Register(listener)
})
body := "foobarbaz"
in := &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewBuffer([]byte(body)),
}
out, err := mgr.UploadObject(context.Background(), in)
if err != nil {
t.Fatal(err)
}
objectSize := int64(len(body))
listener.expectComplete(t, in, out)
listener.expectStartTotalBytes(t, objectSize)
listener.expectCompleteTotalBytes(t, objectSize)
listener.expectByteTransfers(t, objectSize)
}
func TestProgressListener_MultiUpload(t *testing.T) {
c, _, _ := s3testing.NewUploadLoggingClient(nil)
listener := &mockListener{}
mgr := New(c, func(options *Options) {
options.ObjectProgressListeners.Register(listener)
options.Concurrency = 1
})
in := &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: bytes.NewReader(buf40MB),
}
out, err := mgr.UploadObject(context.Background(), in)
if err != nil {
t.Fatal(err)
}
objectSize := int64(len(buf40MB))
listener.expectComplete(t, in, out)
listener.expectStartTotalBytes(t, objectSize)
listener.expectCompleteTotalBytes(t, objectSize)
// 40MB / 8 (default part size) = 5 expected events
// we keep the input size a multiple of the part size so the intermediate
// byte counts are predictable
const eightMB = 1024 * 1024 * 8
listener.expectByteTransfers(t,
eightMB,
eightMB*2,
eightMB*3,
eightMB*4,
eightMB*5)
}
type testIncompleteReader struct {
Size int64
read int64
}
func (r *testIncompleteReader) Read(p []byte) (n int, err error) {
r.read += int64(len(p))
if r.read >= r.Size {
return int(r.read - r.Size), io.ErrUnexpectedEOF
}
return len(p), nil
}
func TestUploadUnexpectedEOF(t *testing.T) {
c, invocations, _ := s3testing.NewUploadLoggingClient(nil)
mgr := New(c, func(o *Options) {
o.Concurrency = 1
o.PartSizeBytes = defaultPartSizeBytes
})
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
Body: &testIncompleteReader{
Size: defaultPartSizeBytes + 1,
},
})
if err == nil {
t.Error("expect error, got nil")
}
// Ensure upload started.
if e, a := "CreateMultipartUpload", (*invocations)[0]; e != a {
t.Errorf("expect %q, got %q", e, a)
}
// Part may or may not be sent because of timing of sending parts and
// reading next part in upload manager. Just check for the last abort.
if e, a := "AbortMultipartUpload", (*invocations)[len(*invocations)-1]; e != a {
t.Errorf("expect %q, got %q", e, a)
}
}
func TestSSE(t *testing.T) {
c, _, _ := s3testing.NewUploadLoggingClient(nil)
c.UploadPartFn = func(u *s3testing.TransferManagerLoggingClient, params *s3.UploadPartInput) (*s3.UploadPartOutput, error) {
if params.SSECustomerAlgorithm == nil {
t.Fatal("SSECustomerAlgoritm should not be nil")
}
if params.SSECustomerKey == nil {
t.Fatal("SSECustomerKey should not be nil")
}
return &s3.UploadPartOutput{ETag: aws.String(fmt.Sprintf("ETAG%d", u.PartNum))}, nil
}
mgr := New(c, func(o *Options) {
o.Concurrency = 5
})
_, err := mgr.UploadObject(context.Background(), &UploadObjectInput{
Bucket: aws.String("Bucket"),
Key: aws.String("Key"),
SSECustomerAlgorithm: aws.String("AES256"),
SSECustomerKey: aws.String("foo"),
Body: bytes.NewBuffer(make([]byte, 1024*1024*10)),
})
if err != nil {
t.Fatal("Expected no error, but received" + err.Error())