-
Notifications
You must be signed in to change notification settings - Fork 84
/
init_test.go
1004 lines (856 loc) · 26.4 KB
/
init_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
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 gofakes3_test
// Initialisation file for tests in the 'gofakes3_test' package. Integration tests
// and the like go in this package as we are unable to use backends without the
// '_test' suffix without causing an import cycle.
import (
"bytes"
"crypto/md5"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"flag"
"fmt"
"github.com/cevatbarisyilmaz/ara"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"os"
"path"
"reflect"
"sort"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/johannesboyne/gofakes3"
"github.com/johannesboyne/gofakes3/backend/s3mem"
)
const (
defaultBucket = "mybucket"
// docs say MB, client SDK uses MiB, gofakes3 assumes MB as the lowest common denominator
// to accept, but we need to satisfy the client SDK in the test suite so this is MiB:
defaultUploadPartSize = 5 * 1024 * 1024
)
var (
logFile string
defaultDate = time.Date(2018, 1, 1, 12, 0, 0, 0, time.UTC)
)
func TestMain(m *testing.M) {
if err := runTestMain(m); err != nil {
fmt.Fprintln(os.Stderr, err)
code, ok := err.(errCode)
if !ok {
code = 1
}
os.Exit(int(code))
}
os.Exit(0)
}
func runTestMain(m *testing.M) error {
flag.StringVar(&logFile, "fakes3.log", "", "Log file (temp file by default)")
flag.Parse()
var logOutput *os.File
var err error
if logFile == "" {
logOutput, err = ioutil.TempFile("", "gofakes3-*.log")
} else {
logOutput, err = os.Create(logFile)
}
if err != nil {
return err
}
defer logOutput.Close()
fmt.Fprintf(os.Stderr, "log output redirected to %q\n", logOutput.Name())
log.SetOutput(logOutput)
if code := m.Run(); code > 0 {
return errCode(code)
}
return nil
}
type errCode int
func (e errCode) Error() string { return fmt.Sprintf("exit code %d", e) }
// lsItems is used by testServer and testCLI to represent the result of a
// bucket list operation.
type lsItems []lsItem
func (cl lsItems) assertContents(tt gofakes3.TT, dirs []string, files []string) {
tt.Helper()
cl.assertFiles(tt, files...)
cl.assertDirs(tt, dirs...)
}
func (cl lsItems) assertDirs(tt gofakes3.TT, names ...string) {
tt.Helper()
cl.assertItems(tt, true, names...)
}
func (cl lsItems) assertFiles(tt gofakes3.TT, names ...string) {
tt.Helper()
cl.assertItems(tt, false, names...)
}
func (cl lsItems) assertItems(tt gofakes3.TT, isDir bool, names ...string) {
tt.Helper()
var found []string
for _, item := range cl {
if item.isDir == isDir {
found = append(found, item.name)
}
}
sort.Strings(found)
sort.Strings(names)
if !reflect.DeepEqual(found, names) {
tt.Fatalf("items:\nexp: %v\ngot: %v", names, found)
}
}
type lsItem struct {
name string
date time.Time
size int64
isDir bool
}
type testServer struct {
gofakes3.TT
gofakes3.TimeSourceAdvancer
*gofakes3.GoFakeS3
backend gofakes3.Backend
versioned gofakes3.VersionedBackend
server *httptest.Server
options []gofakes3.Option
useHostBucket bool
// if this is nil, no buckets are created. by default, a starting bucket is
// created using the value of the 'defaultBucket' constant.
initialBuckets []string
versioning bool
}
type testServerOption func(ts *testServer)
func withoutInitialBuckets() testServerOption {
return func(ts *testServer) { ts.initialBuckets = nil }
}
func withInitialBuckets(buckets ...string) testServerOption {
return func(ts *testServer) { ts.initialBuckets = buckets }
}
func withHostBucket() testServerOption {
return func(ts *testServer) {
ts.options = append(ts.options, gofakes3.WithHostBucket(true))
ts.useHostBucket = true
}
}
func withVersioning() testServerOption {
return func(ts *testServer) { ts.versioning = true }
}
func withFakerOptions(opts ...gofakes3.Option) testServerOption {
return func(ts *testServer) { ts.options = opts }
}
func withBackend(backend gofakes3.Backend) testServerOption {
return func(ts *testServer) { ts.backend = backend }
}
func newTestServer(t *testing.T, opts ...testServerOption) *testServer {
t.Helper()
var ts = testServer{
TT: gofakes3.TT{t},
initialBuckets: []string{defaultBucket},
}
for _, o := range opts {
o(&ts)
}
if ts.backend == nil {
if ts.TimeSourceAdvancer == nil {
ts.TimeSourceAdvancer = gofakes3.FixedTimeSource(defaultDate)
}
ts.backend = s3mem.New(
s3mem.WithTimeSource(ts.TimeSourceAdvancer),
s3mem.WithVersionSeed(0))
}
fakerOpts := []gofakes3.Option{
gofakes3.WithTimeSource(ts.TimeSourceAdvancer),
gofakes3.WithTimeSkewLimit(0),
// TestMain wires the stdlib's global logger up to a file already,
// which this takes advantage of:
gofakes3.WithGlobalLog(),
}
fakerOpts = append(fakerOpts, ts.options...)
ts.GoFakeS3 = gofakes3.New(ts.backend, fakerOpts...)
ts.server = httptest.NewServer(ts.GoFakeS3.Server())
for _, bucket := range ts.initialBuckets {
ts.TT.OK(ts.backend.CreateBucket(bucket))
}
if ts.versioning {
mem, ok := ts.backend.(*s3mem.Backend)
if !ok {
panic("backend is not a versioned backend")
}
ts.versioned = mem
for _, bucket := range ts.initialBuckets {
ts.TT.OK(ts.versioned.SetVersioningConfiguration(bucket, gofakes3.VersioningConfiguration{
Status: gofakes3.VersioningEnabled,
}))
}
}
return &ts
}
func (ts *testServer) url(url string) string {
return fmt.Sprintf("%s/%s", ts.server.URL, strings.TrimLeft(url, "/"))
}
func (ts *testServer) backendCreateBucket(bucket string) {
ts.Helper()
if err := ts.backend.CreateBucket(bucket); err != nil {
ts.Fatal("create bucket failed", err)
}
}
func (ts *testServer) backendObjectExists(bucket, key string) bool {
ts.Helper()
obj, err := ts.backend.HeadObject(bucket, key)
if err != nil {
if hasErrorCode(err, gofakes3.ErrNoSuchKey) {
return false
} else {
ts.Fatal(err)
}
}
return obj != nil
}
func (ts *testServer) backendPutString(bucket, key string, meta map[string]string, in string) {
ts.Helper()
ts.OKAll(ts.backend.PutObject(bucket, key, meta, strings.NewReader(in), int64(len(in))))
}
func (ts *testServer) backendPutBytes(bucket, key string, meta map[string]string, in []byte) {
ts.Helper()
ts.OKAll(ts.backend.PutObject(bucket, key, meta, bytes.NewReader(in), int64(len(in))))
}
func (ts *testServer) backendGetString(bucket, key string, rnge *gofakes3.ObjectRangeRequest) string {
ts.Helper()
obj, err := ts.backend.GetObject(bucket, key, rnge)
ts.OK(err)
defer obj.Contents.Close()
data, err := ioutil.ReadAll(obj.Contents)
ts.OK(err)
return string(data)
}
func (ts *testServer) s3Client() *s3.S3 {
ts.Helper()
config := aws.NewConfig()
config.WithEndpoint(ts.server.URL)
config.WithRegion("region")
config.WithHTTPClient(ara.NewClient(ara.NewCustomResolver(map[string][]string{fmt.Sprintf("%s.127.0.0.1", defaultBucket): {"127.0.0.1"}, "localhost": {"127.0.0.1"}})))
config.WithCredentials(credentials.NewStaticCredentials("dummy-access", "dummy-secret", ""))
config.WithS3ForcePathStyle(!ts.useHostBucket)
svc := s3.New(session.New(), config)
return svc
}
func (ts *testServer) assertLs(bucket string, prefix string, expectedPrefixes []string, expectedObjects []string) {
ts.Helper()
client := ts.s3Client()
rs, err := client.ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(bucket),
Delimiter: aws.String("/"),
Prefix: aws.String(prefix),
})
ts.OK(err)
var ls lsItems
for _, item := range rs.CommonPrefixes {
ls = append(ls, lsItem{name: *item.Prefix, isDir: true})
}
for _, item := range rs.Contents {
ls = append(ls, lsItem{name: *item.Key, date: *item.LastModified, size: *item.Size})
}
ls.assertContents(ts.TT, expectedPrefixes, expectedObjects)
}
func (ts *testServer) rawClient() *rawClient {
return newRawClient(httpClient(), ts.server.URL)
}
type multipartUploadOptions struct {
partSize int64
}
func (ts *testServer) assertMultipartUpload(bucket, object string, body interface{}, options *multipartUploadOptions) {
ts.Helper()
if options == nil {
options = &multipartUploadOptions{}
}
if options.partSize <= 0 {
options.partSize = defaultUploadPartSize
}
s3 := ts.s3Client()
uploader := s3manager.NewUploaderWithClient(s3)
contents := readBody(ts.TT, body)
upParams := &s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
Body: bytes.NewReader(contents),
ContentMD5: aws.String(hashMD5Bytes(contents).Base64()),
}
out, err := uploader.Upload(upParams, func(u *s3manager.Uploader) {
u.LeavePartsOnError = true
u.PartSize = options.partSize
})
ts.OK(err)
// s3manager will use multipart upload if the body consists of more than 1 part of the specified part size
if options.partSize < int64(len(contents)) {
if expectedETag := calculateETagByBody(contents, options.partSize); *out.ETag != expectedETag {
ts.Fatal("multipart upload etag mismatch:", *out.ETag, "!=", expectedETag)
}
}
ts.assertObject(bucket, object, nil, body)
}
func calculateETagByBody(contents []byte, partSize int64) string {
partCount := len(contents) / int(partSize)
if len(contents)%int(partSize) != 0 {
partCount++
}
hash := md5.New()
toHash := contents[:]
for len(toHash) > 0 {
toHashPartSize := partSize
if reminderSize := int64(len(toHash)); reminderSize < partSize {
toHashPartSize = reminderSize
}
hash.Write(hashMD5Bytes(toHash[:toHashPartSize]))
toHash = toHash[toHashPartSize:]
}
return fmt.Sprintf(`"%s-%d"`, hex.EncodeToString(hash.Sum(nil)), partCount)
}
func (ts *testServer) createMultipartUpload(bucket, object string, meta map[string]string) (uploadID string) {
ts.Helper()
svc := ts.s3Client()
mpu, err := svc.CreateMultipartUpload(&s3.CreateMultipartUploadInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
})
ts.OK(err)
return *mpu.UploadId
}
func (ts *testServer) uploadPart(bucket, object string, uploadID string, num int64, body []byte) *s3.CompletedPart {
ts.Helper()
hash := hashMD5Bytes(body)
svc := ts.s3Client()
mpu, err := svc.UploadPart(&s3.UploadPartInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
Body: bytes.NewReader(body),
UploadId: aws.String(uploadID),
PartNumber: aws.Int64(num),
ContentMD5: aws.String(hash.Base64()),
})
ts.OK(err)
return &s3.CompletedPart{ETag: aws.String(*mpu.ETag), PartNumber: aws.Int64(num)}
}
func (ts *testServer) assertCompleteUpload(bucket, object, uploadID string, parts []*s3.CompletedPart, body interface{}) {
ts.Helper()
svc := ts.s3Client()
mpu, err := svc.CompleteMultipartUpload(&s3.CompleteMultipartUploadInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
UploadId: aws.String(uploadID),
MultipartUpload: &s3.CompletedMultipartUpload{
Parts: parts,
},
})
ts.OK(err)
if mpu.Location == nil {
ts.Fatal("missing location")
}
// split into protocol, host, port
u, err := url.Parse(ts.server.URL)
ts.OK(err)
if ts.useHostBucket {
if *mpu.Location != fmt.Sprintf("%s://%s.%s/%s", u.Scheme, bucket, u.Host, object) {
ts.Fatal("unexpected location:", *mpu.Location)
}
} else {
if *mpu.Location != fmt.Sprintf("%s/%s/%s", ts.server.URL, bucket, object) {
ts.Fatal("unexpected location:", *mpu.Location)
}
}
if expectedETag := ts.calculateETagBodyByParts(parts); *mpu.ETag != expectedETag {
ts.Fatal("multipart upload etag mismatch:", *mpu.ETag, "!=", expectedETag)
}
ts.assertObject(bucket, object, nil, body)
}
func (ts *testServer) calculateETagBodyByParts(parts []*s3.CompletedPart) string {
hash := md5.New()
for _, part := range parts {
etagBytes, err := hex.DecodeString(strings.Trim(*part.ETag, `"`))
if err != nil {
ts.Fatal("failed to decode etag:", err)
}
hash.Write(etagBytes)
}
return fmt.Sprintf(`"%s-%d"`, hex.EncodeToString(hash.Sum(nil)), len(parts))
}
func (ts *testServer) assertAbortMultipartUpload(bucket, object string, uploadID gofakes3.UploadID) {
ts.Helper()
svc := ts.s3Client()
rs, err := svc.AbortMultipartUpload(&s3.AbortMultipartUploadInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
UploadId: aws.String(string(uploadID)),
})
ts.OK(err)
_ = rs
{ // FIXME: Currently, the only way to sanity check this using the HTTP API
// is to try to list the parts, which should indicate if the upload was
// successfully removed. Once the upload API becomes a first class
// citizen, we should be able to call it directly.
rs, err := svc.ListParts(&s3.ListPartsInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
UploadId: aws.String(string(uploadID)),
})
_ = rs
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == string(gofakes3.ErrNoSuchUpload) {
return
}
ts.Fatal("expected NotFound error, found", err)
}
}
type listUploadPartsOpts struct {
Prefix *gofakes3.Prefix
Marker int64
Limit int64
// Supports s3.CompletedPart or s3.Part:
Parts []interface{}
}
func (opts listUploadPartsOpts) withCompletedParts(parts ...*s3.CompletedPart) listUploadPartsOpts {
for _, p := range parts {
opts.Parts = append(opts.Parts, p)
}
return opts
}
func (opts listUploadPartsOpts) input(bucket, object string, uploadID string) *s3.ListPartsInput {
rq := &s3.ListPartsInput{
Bucket: aws.String(bucket),
Key: aws.String(object),
UploadId: aws.String(uploadID),
}
if opts.Limit > 0 {
rq.MaxParts = aws.Int64(opts.Limit)
}
if opts.Marker > 0 {
rq.PartNumberMarker = aws.Int64(opts.Marker)
}
return rq
}
func (ts *testServer) assertListUploadPartsFails(code gofakes3.ErrorCode, bucket, object string, uploadID string, opts listUploadPartsOpts) {
ts.Helper()
svc := ts.s3Client()
_, err := svc.ListParts(opts.input(bucket, object, uploadID))
if !hasErrorCode(err, code) {
ts.Fatal("expected", code, "found", err)
}
}
func (ts *testServer) assertListUploadParts(bucket, object string, uploadID string, opts listUploadPartsOpts) {
ts.Helper()
rq := opts.input(bucket, object, uploadID)
svc := ts.s3Client()
rs, err := svc.ListParts(rq)
ts.OK(err)
if *rs.Bucket != bucket {
ts.Fatal("bucket mismatch:", *rs.Bucket, "!=", bucket)
}
if *rs.Key != object {
ts.Fatal("object mismatch:", *rs.Key, "!=", object)
}
if *rs.UploadId != uploadID {
ts.Fatal("upload mismatch:", *rs.UploadId, "!=", uploadID)
}
if len(rs.Parts) != len(opts.Parts) {
ts.Fatal("parts mismatch:", rs.Parts, "!=", opts.Parts)
}
for idx, found := range rs.Parts {
switch expected := opts.Parts[idx].(type) {
case *s3.CompletedPart:
if *expected.ETag != *found.ETag {
ts.Fatal("part", idx, "ETag mismatch:", expected.ETag, "!=", found.ETag)
}
if *expected.PartNumber != *found.PartNumber {
ts.Fatal("part", idx, "PartNumber mismatch:", expected.PartNumber, "!=", found.PartNumber)
}
case *s3.Part:
if !reflect.DeepEqual(expected, found) {
ts.Fatal("part", idx, "mismatch:", expected, "!=", found)
}
default:
ts.Fatalf("unexpected type %T", expected)
}
}
}
type listUploadsOpts struct {
Prefix *gofakes3.Prefix
Marker string
Limit int64
Prefixes []string
Uploads []string
}
// assertListMultipartUploads
//
// If marker is not an empty string, it should be in the format "[<object>][/<uploadID>]".
// Each item in expectedUploads must be in the format "<object>/<uploadID>".
func (ts *testServer) assertListMultipartUploads(
bucket string,
opts listUploadsOpts,
) {
ts.Helper()
svc := ts.s3Client()
rq := &s3.ListMultipartUploadsInput{
Bucket: aws.String(bucket),
MaxUploads: aws.Int64(opts.Limit),
}
var expectedKeyMarker, expectedUploadIDMarker string
if opts.Marker != "" {
parts := strings.SplitN(opts.Marker, "/", 2)
expectedKeyMarker = parts[0]
if len(parts) == 2 {
expectedUploadIDMarker = parts[1]
}
rq.KeyMarker = aws.String(expectedKeyMarker)
rq.UploadIdMarker = aws.String(expectedUploadIDMarker)
}
var expectedPrefix, expectedDelimiter string
if opts.Prefix != nil {
rq.Prefix = aws.String(opts.Prefix.Prefix)
rq.Delimiter = aws.String(opts.Prefix.Delimiter)
expectedPrefix, expectedDelimiter = opts.Prefix.Prefix, opts.Prefix.Delimiter
}
rs, err := svc.ListMultipartUploads(rq)
ts.OK(err)
{ // assert response fields match input
var foundPrefix, foundDelimiter, foundKeyMarker, foundUploadIDMarker string
if rs.Delimiter != nil {
foundDelimiter = *rs.Delimiter
}
if rs.Prefix != nil {
foundPrefix = *rs.Prefix
}
if rs.KeyMarker != nil {
foundKeyMarker = *rs.KeyMarker
}
if rs.UploadIdMarker != nil {
foundUploadIDMarker = *rs.UploadIdMarker
}
if foundPrefix != expectedPrefix {
ts.Fatal("unexpected prefix", foundPrefix, "!=", expectedPrefix)
}
if foundDelimiter != expectedDelimiter {
ts.Fatal("unexpected delimiter", foundDelimiter, "!=", expectedDelimiter)
}
if foundKeyMarker != expectedKeyMarker {
ts.Fatal("unexpected key marker", foundKeyMarker, "!=", expectedKeyMarker)
}
if foundUploadIDMarker != expectedUploadIDMarker {
ts.Fatal("unexpected upload ID marker", foundUploadIDMarker, "!=", expectedUploadIDMarker)
}
var foundUploads int64
if rs.MaxUploads != nil {
foundUploads = *rs.MaxUploads
}
if opts.Limit > 0 && foundUploads != opts.Limit {
ts.Fatal("unexpected max uploads", foundUploads, "!=", opts.Limit)
}
}
var foundUploads []string
for _, up := range rs.Uploads {
foundUploads = append(foundUploads, fmt.Sprintf("%s/%s", *up.Key, *up.UploadId))
}
var foundPrefixes []string
for _, cp := range rs.CommonPrefixes {
foundPrefixes = append(foundPrefixes, *cp.Prefix)
}
if !reflect.DeepEqual(foundPrefixes, opts.Prefixes) {
ts.Fatal("common prefix list mismatch:", foundPrefixes, "!=", opts.Prefixes)
}
if !reflect.DeepEqual(foundUploads, opts.Uploads) {
ts.Fatal("upload list mismatch:", foundUploads, "!=", opts.Uploads)
}
}
// If meta is nil, the metadata is not checked.
// If meta is map[string]string{}, it is checked against the empty map.
//
// If contents is a string, a []byte or an io.Reader, it is compared against
// the object's contents, otherwise a panic occurs.
func (ts *testServer) assertObject(bucket string, object string, meta map[string]string, contents interface{}) {
ts.Helper()
obj, err := ts.backend.GetObject(bucket, object, nil)
ts.OK(err)
defer obj.Contents.Close()
data, err := gofakes3.ReadAll(obj.Contents, obj.Size)
ts.OK(err)
if meta != nil {
if !reflect.DeepEqual(meta, obj.Metadata) {
ts.Fatal("metadata mismatch:", meta, "!=", obj.Metadata)
}
}
checkContents := readBody(ts.TT, contents)
if !bytes.Equal(checkContents, data) {
ts.Fatal("data mismatch") // FIXME: more detail
}
if int64(len(checkContents)) != obj.Size {
ts.Fatal("size mismatch:", len(checkContents), "!=", obj.Size)
}
}
type listBucketResult struct {
CommonPrefixes []*s3.CommonPrefix
Contents []*s3.Object
}
func (ts *testServer) mustListBucketV1Pages(prefix *gofakes3.Prefix, maxKeys int64, marker string) *listBucketResult {
r, err := ts.listBucketV1Pages(prefix, maxKeys, marker)
ts.OK(err)
return r
}
func (ts *testServer) listBucketV1Pages(prefix *gofakes3.Prefix, maxKeys int64, marker string) (*listBucketResult, error) {
const pageLimit = 20
svc := ts.s3Client()
pages := 0
in := &s3.ListObjectsInput{
Bucket: aws.String(defaultBucket),
MaxKeys: aws.Int64(maxKeys),
}
if prefix != nil && prefix.HasDelimiter {
in.Delimiter = aws.String(prefix.Delimiter)
}
if prefix != nil && prefix.HasPrefix {
in.Prefix = aws.String(prefix.Prefix)
}
if marker != "" {
in.Marker = aws.String(marker)
}
var rs listBucketResult
if err := (svc.ListObjectsPages(in, func(out *s3.ListObjectsOutput, lastPage bool) bool {
pages++
if pages > pageLimit {
panic("stuck in a page loop")
}
rs.CommonPrefixes = append(rs.CommonPrefixes, out.CommonPrefixes...)
rs.Contents = append(rs.Contents, out.Contents...)
return !lastPage
})); err != nil {
return nil, err
}
return &rs, nil
}
func (ts *testServer) mustListBucketV2Pages(prefix *gofakes3.Prefix, maxKeys int64, marker string) *listBucketResult {
r, err := ts.listBucketV2Pages(prefix, maxKeys, marker)
ts.OK(err)
return r
}
func (ts *testServer) listBucketV2Pages(prefix *gofakes3.Prefix, maxKeys int64, startAfter string) (*listBucketResult, error) {
const pageLimit = 20
svc := ts.s3Client()
pages := 0
in := &s3.ListObjectsV2Input{
Bucket: aws.String(defaultBucket),
MaxKeys: aws.Int64(maxKeys),
}
if prefix != nil && prefix.HasDelimiter {
in.Delimiter = aws.String(prefix.Delimiter)
}
if prefix != nil && prefix.HasPrefix {
in.Prefix = aws.String(prefix.Prefix)
}
if startAfter != "" {
in.StartAfter = aws.String(startAfter)
}
var rs listBucketResult
if err := (svc.ListObjectsV2Pages(in, func(out *s3.ListObjectsV2Output, lastPage bool) bool {
pages++
if pages > pageLimit {
panic("stuck in a page loop")
}
rs.CommonPrefixes = append(rs.CommonPrefixes, out.CommonPrefixes...)
rs.Contents = append(rs.Contents, out.Contents...)
return !lastPage
})); err != nil {
return nil, err
}
return &rs, nil
}
func (ts *testServer) Close() {
ts.server.Close()
}
func hashMD5Bytes(body []byte) hashValue {
h := md5.New()
h.Write(body)
return hashValue(h.Sum(nil))
}
func hashSHA256Bytes(body []byte) hashValue {
h := sha256.New()
h.Write(body)
return hashValue(h.Sum(nil))
}
type hashValue []byte
func (h hashValue) Base64() string { return base64.StdEncoding.EncodeToString(h) }
func (h hashValue) Hex() string { return hex.EncodeToString(h) }
var (
randState = uint64(time.Now().UnixNano()) // FIXME: global seedable testing rand
randMu sync.Mutex
)
func randomFileBody(size int64) []byte {
randMu.Lock()
defer randMu.Unlock()
neat := size/8*8 + 8 // cheap and nasty way to ensure a multiple of 8 definitely greater than size
b := make([]byte, neat)
// Using the default source of randomness was extremely slow; this is a
// simple inline implementation of http://xoshiro.di.unimi.it/splitmix64.c
// as we *really* don't care about the quality of the randomness, just that
// it appears random enough to distribute byte values a bit.
for i := int64(0); i < neat; i += 8 {
randState += 0x9E3779B97F4A7C15
z := randState
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9
z = (z ^ (z >> 27)) * 0x94D049BB133111EB
b[i], b[i+1], b[i+2], b[i+3], b[i+4], b[i+5], b[i+6], b[i+7] =
byte(z), byte(z>>8), byte(z>>16), byte(z>>24), byte(z>>32), byte(z>>40), byte(z>>48), byte(z>>56)
}
b = b[:size]
return b
}
func readBody(tt gofakes3.TT, body interface{}) []byte {
switch body := body.(type) {
case nil:
return []byte{}
case string:
return []byte(body)
case []byte:
return body
case io.Reader:
out, err := ioutil.ReadAll(body)
tt.OK(err)
return out
default:
panic("unexpected contents")
}
}
func prefixFile(prefix string) *gofakes3.Prefix {
return &gofakes3.Prefix{Delimiter: "/", Prefix: prefix}
}
func prefix(prefix string) *gofakes3.Prefix {
return &gofakes3.Prefix{Prefix: prefix}
}
func prefixDelim(prefix string, delim string) *gofakes3.Prefix {
return &gofakes3.Prefix{Prefix: prefix, Delimiter: delim}
}
func strs(s ...string) []string {
return s
}
// hasErrorCode is like gofakes3.HasErrorCode, but supports awserr.Error as
// well, which we can't do directly in gofakes3 to avoid the dependency.
func hasErrorCode(err error, code gofakes3.ErrorCode) bool {
if awsErr, ok := err.(awserr.Error); ok {
return awsErr.Code() == string(code)
} else {
return gofakes3.HasErrorCode(err, code)
}
}
func s3HasErrorCode(err error, code gofakes3.ErrorCode) bool {
if err, ok := err.(awserr.Error); ok {
return code == gofakes3.ErrorCode(err.Code())
}
return false
}
func httpClient() *http.Client {
return &http.Client{
Timeout: 2 * time.Second,
}
}
type backendWithUnimplementedPaging struct {
gofakes3.Backend
}
func (b *backendWithUnimplementedPaging) ListBucket(name string, prefix *gofakes3.Prefix, page gofakes3.ListBucketPage) (*gofakes3.ObjectList, error) {
if !page.IsEmpty() {
return nil, gofakes3.ErrInternalPageNotImplemented
}
return b.Backend.ListBucket(name, prefix, page)
}
type rawClient struct {
client *http.Client
base *url.URL
}
func newRawClient(client *http.Client, base string) *rawClient {
u, err := url.Parse(base)
if err != nil {
panic(err)
}
return &rawClient{client: client, base: u}
}
func (c *rawClient) URL(rqpath string) *url.URL {
u, err := url.Parse(c.base.String())
if err != nil {
panic(err)
}
u.Path = path.Join(u.Path, rqpath)
return u
}
func (c *rawClient) Request(method, rqpath string, body []byte) *http.Request {
u := c.URL(rqpath)
rq, err := http.NewRequest(method, u.String(), bytes.NewReader(body))
if err != nil {
panic(err)
}
c.SetHeaders(rq, body)
return rq
}
func (c *rawClient) SetHeaders(rq *http.Request, body []byte) {
// NOTE: This was put together by using httputil.DumpRequest inside routeBase(). We
// don't currently implement the Authorization header, so that has been skimmed for
// now.
rq.Header.Set("Accept-Encoding", "gzip")
rq.Header.Set("Authorization", "...") // TODO
rq.Header.Set("Content-Length", strconv.FormatInt(int64(len(body)), 10))
rq.Header.Set("Content-Md5", hashMD5Bytes(body).Base64())
rq.Header.Set("User-Agent", "aws-sdk-go/1.17.4 (go1.14rc1; linux; amd64)")
rq.Header.Set("X-Amz-Date", time.Now().In(time.UTC).Format("20060102T030405-0700"))
rq.Header.Set("X-Amz-Content-Sha256", hashSHA256Bytes(body).Hex())
}
// SendRaw can be used to bypass Go's http client, which helps us out a lot by taking
// care of some things for us, but which we actually want to test messing up from
// time to time.
func (c *rawClient) SendRaw(rq *http.Request) ([]byte, error) {
b, err := httputil.DumpRequest(rq, true)
if err != nil {
return nil, err
}
conn, err := net.DialTimeout("tcp", c.base.Host, 2*time.Second)
if err != nil {
return nil, err
}
defer conn.Close()
deadline := time.Now().Add(2 * time.Second)
conn.SetDeadline(deadline)
if _, err := conn.Write(b); err != nil {
return nil, err
}
var rs []byte
var scratch = make([]byte, 1024)
for {
n, err := conn.Read(scratch)
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
rs = append(rs, scratch[:n]...)
}
return rs, nil
}
func (c *rawClient) Do(rq *http.Request) (*http.Response, error) {
return c.client.Do(rq)
}
func maskReader(r io.Reader) io.Reader {
// http.NewRequest() forces a ContentLength if it recognises
// the type of reader you pass as the body. This is a cheeky
// way to bypass that:
return &maskedReader{r}
}
type maskedReader struct {
inner io.Reader
}