filer_multipart_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package s3api
  2. import (
  3. "github.com/aws/aws-sdk-go/aws"
  4. "github.com/aws/aws-sdk-go/service/s3"
  5. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  6. "github.com/stretchr/testify/assert"
  7. "testing"
  8. "time"
  9. )
  10. func TestInitiateMultipartUploadResult(t *testing.T) {
  11. expected := `<?xml version="1.0" encoding="UTF-8"?>
  12. <InitiateMultipartUploadResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Bucket>example-bucket</Bucket><Key>example-object</Key><UploadId>VXBsb2FkIElEIGZvciA2aWWpbmcncyBteS1tb3ZpZS5tMnRzIHVwbG9hZA</UploadId></InitiateMultipartUploadResult>`
  13. response := &InitiateMultipartUploadResult{
  14. CreateMultipartUploadOutput: s3.CreateMultipartUploadOutput{
  15. Bucket: aws.String("example-bucket"),
  16. Key: aws.String("example-object"),
  17. UploadId: aws.String("VXBsb2FkIElEIGZvciA2aWWpbmcncyBteS1tb3ZpZS5tMnRzIHVwbG9hZA"),
  18. },
  19. }
  20. encoded := string(s3err.EncodeXMLResponse(response))
  21. if encoded != expected {
  22. t.Errorf("unexpected output: %s\nexpecting:%s", encoded, expected)
  23. }
  24. }
  25. func TestListPartsResult(t *testing.T) {
  26. expected := `<?xml version="1.0" encoding="UTF-8"?>
  27. <ListPartsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Part><ETag>&#34;12345678&#34;</ETag><LastModified>1970-01-01T00:00:00Z</LastModified><PartNumber>1</PartNumber><Size>123</Size></Part></ListPartsResult>`
  28. response := &ListPartsResult{
  29. Part: []*s3.Part{
  30. {
  31. PartNumber: aws.Int64(int64(1)),
  32. LastModified: aws.Time(time.Unix(0, 0).UTC()),
  33. Size: aws.Int64(int64(123)),
  34. ETag: aws.String("\"12345678\""),
  35. },
  36. },
  37. }
  38. encoded := string(s3err.EncodeXMLResponse(response))
  39. if encoded != expected {
  40. t.Errorf("unexpected output: %s\nexpecting:%s", encoded, expected)
  41. }
  42. }
  43. func Test_findByPartNumber(t *testing.T) {
  44. type args struct {
  45. fileName string
  46. parts []CompletedPart
  47. }
  48. parts := []CompletedPart{
  49. {
  50. ETag: "xxx",
  51. PartNumber: 1,
  52. },
  53. {
  54. ETag: "lll",
  55. PartNumber: 1,
  56. },
  57. {
  58. ETag: "yyy",
  59. PartNumber: 3,
  60. },
  61. {
  62. ETag: "zzz",
  63. PartNumber: 5,
  64. },
  65. }
  66. tests := []struct {
  67. name string
  68. args args
  69. wantEtag string
  70. wantFound bool
  71. }{
  72. {
  73. "first",
  74. args{
  75. "0001.part",
  76. parts,
  77. },
  78. "lll",
  79. true,
  80. },
  81. {
  82. "second",
  83. args{
  84. "0002.part",
  85. parts,
  86. },
  87. "",
  88. false,
  89. },
  90. {
  91. "third",
  92. args{
  93. "0003.part",
  94. parts,
  95. },
  96. "yyy",
  97. true,
  98. },
  99. {
  100. "fourth",
  101. args{
  102. "0004.part",
  103. parts,
  104. },
  105. "",
  106. false,
  107. },
  108. {
  109. "fifth",
  110. args{
  111. "0005.part",
  112. parts,
  113. },
  114. "zzz",
  115. true,
  116. },
  117. }
  118. for _, tt := range tests {
  119. t.Run(tt.name, func(t *testing.T) {
  120. gotEtag, gotFound := findByPartNumber(tt.args.fileName, tt.args.parts)
  121. assert.Equalf(t, tt.wantEtag, gotEtag, "findByPartNumber(%v, %v)", tt.args.fileName, tt.args.parts)
  122. assert.Equalf(t, tt.wantFound, gotFound, "findByPartNumber(%v, %v)", tt.args.fileName, tt.args.parts)
  123. })
  124. }
  125. }