You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
2.9 KiB

4 years ago
4 years ago
4 years ago
  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/chrislusf/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. CompletedPart{
  50. ETag: "xxx",
  51. PartNumber: 1,
  52. },
  53. CompletedPart{
  54. ETag: "yyy",
  55. PartNumber: 3,
  56. },
  57. CompletedPart{
  58. ETag: "zzz",
  59. PartNumber: 5,
  60. },
  61. }
  62. tests := []struct {
  63. name string
  64. args args
  65. wantEtag string
  66. wantFound bool
  67. }{
  68. {
  69. "first",
  70. args{
  71. "0001.part",
  72. parts,
  73. },
  74. "xxx",
  75. true,
  76. },
  77. {
  78. "second",
  79. args{
  80. "0002.part",
  81. parts,
  82. },
  83. "",
  84. false,
  85. },
  86. {
  87. "third",
  88. args{
  89. "0003.part",
  90. parts,
  91. },
  92. "yyy",
  93. true,
  94. },
  95. {
  96. "fourth",
  97. args{
  98. "0004.part",
  99. parts,
  100. },
  101. "",
  102. false,
  103. },
  104. {
  105. "fifth",
  106. args{
  107. "0005.part",
  108. parts,
  109. },
  110. "zzz",
  111. true,
  112. },
  113. }
  114. for _, tt := range tests {
  115. t.Run(tt.name, func(t *testing.T) {
  116. gotEtag, gotFound := findByPartNumber(tt.args.fileName, tt.args.parts)
  117. assert.Equalf(t, tt.wantEtag, gotEtag, "findByPartNumber(%v, %v)", tt.args.fileName, tt.args.parts)
  118. assert.Equalf(t, tt.wantFound, gotFound, "findByPartNumber(%v, %v)", tt.args.fileName, tt.args.parts)
  119. })
  120. }
  121. }