download_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package s3manager_test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "regexp"
  8. "strconv"
  9. "sync"
  10. "testing"
  11. "github.com/aws/aws-sdk-go/aws"
  12. "github.com/aws/aws-sdk-go/aws/request"
  13. "github.com/aws/aws-sdk-go/internal/test/unit"
  14. "github.com/aws/aws-sdk-go/service/s3"
  15. "github.com/aws/aws-sdk-go/service/s3/s3manager"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. var _ = unit.Imported
  19. func dlLoggingSvc(data []byte) (*s3.S3, *[]string, *[]string) {
  20. var m sync.Mutex
  21. names := []string{}
  22. ranges := []string{}
  23. svc := s3.New(nil)
  24. svc.Handlers.Send.Clear()
  25. svc.Handlers.Send.PushBack(func(r *request.Request) {
  26. m.Lock()
  27. defer m.Unlock()
  28. names = append(names, r.Operation.Name)
  29. ranges = append(ranges, *r.Params.(*s3.GetObjectInput).Range)
  30. rerng := regexp.MustCompile(`bytes=(\d+)-(\d+)`)
  31. rng := rerng.FindStringSubmatch(r.HTTPRequest.Header.Get("Range"))
  32. start, _ := strconv.ParseInt(rng[1], 10, 64)
  33. fin, _ := strconv.ParseInt(rng[2], 10, 64)
  34. fin++
  35. if fin > int64(len(data)) {
  36. fin = int64(len(data))
  37. }
  38. r.HTTPResponse = &http.Response{
  39. StatusCode: 200,
  40. Body: ioutil.NopCloser(bytes.NewReader(data[start:fin])),
  41. Header: http.Header{},
  42. }
  43. r.HTTPResponse.Header.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d",
  44. start, fin, len(data)))
  45. })
  46. return svc, &names, &ranges
  47. }
  48. func TestDownloadOrder(t *testing.T) {
  49. s, names, ranges := dlLoggingSvc(buf12MB)
  50. opts := &s3manager.DownloadOptions{S3: s, Concurrency: 1}
  51. d := s3manager.NewDownloader(opts)
  52. w := &aws.WriteAtBuffer{}
  53. n, err := d.Download(w, &s3.GetObjectInput{
  54. Bucket: aws.String("bucket"),
  55. Key: aws.String("key"),
  56. })
  57. assert.Nil(t, err)
  58. assert.Equal(t, int64(len(buf12MB)), n)
  59. assert.Equal(t, []string{"GetObject", "GetObject", "GetObject"}, *names)
  60. assert.Equal(t, []string{"bytes=0-5242879", "bytes=5242880-10485759", "bytes=10485760-15728639"}, *ranges)
  61. count := 0
  62. for _, b := range w.Bytes() {
  63. count += int(b)
  64. }
  65. assert.Equal(t, 0, count)
  66. }
  67. func TestDownloadZero(t *testing.T) {
  68. s, names, ranges := dlLoggingSvc([]byte{})
  69. opts := &s3manager.DownloadOptions{S3: s}
  70. d := s3manager.NewDownloader(opts)
  71. w := &aws.WriteAtBuffer{}
  72. n, err := d.Download(w, &s3.GetObjectInput{
  73. Bucket: aws.String("bucket"),
  74. Key: aws.String("key"),
  75. })
  76. assert.Nil(t, err)
  77. assert.Equal(t, int64(0), n)
  78. assert.Equal(t, []string{"GetObject"}, *names)
  79. assert.Equal(t, []string{"bytes=0-5242879"}, *ranges)
  80. }
  81. func TestDownloadSetPartSize(t *testing.T) {
  82. s, names, ranges := dlLoggingSvc([]byte{1, 2, 3})
  83. opts := &s3manager.DownloadOptions{S3: s, PartSize: 1, Concurrency: 1}
  84. d := s3manager.NewDownloader(opts)
  85. w := &aws.WriteAtBuffer{}
  86. n, err := d.Download(w, &s3.GetObjectInput{
  87. Bucket: aws.String("bucket"),
  88. Key: aws.String("key"),
  89. })
  90. assert.Nil(t, err)
  91. assert.Equal(t, int64(3), n)
  92. assert.Equal(t, []string{"GetObject", "GetObject", "GetObject"}, *names)
  93. assert.Equal(t, []string{"bytes=0-0", "bytes=1-1", "bytes=2-2"}, *ranges)
  94. assert.Equal(t, []byte{1, 2, 3}, w.Bytes())
  95. }
  96. func TestDownloadError(t *testing.T) {
  97. s, names, _ := dlLoggingSvc([]byte{1, 2, 3})
  98. opts := &s3manager.DownloadOptions{S3: s, PartSize: 1, Concurrency: 1}
  99. num := 0
  100. s.Handlers.Send.PushBack(func(r *request.Request) {
  101. num++
  102. if num > 1 {
  103. r.HTTPResponse.StatusCode = 400
  104. r.HTTPResponse.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
  105. }
  106. })
  107. d := s3manager.NewDownloader(opts)
  108. w := &aws.WriteAtBuffer{}
  109. n, err := d.Download(w, &s3.GetObjectInput{
  110. Bucket: aws.String("bucket"),
  111. Key: aws.String("key"),
  112. })
  113. assert.NotNil(t, err)
  114. assert.Equal(t, int64(1), n)
  115. assert.Equal(t, []string{"GetObject", "GetObject"}, *names)
  116. assert.Equal(t, []byte{1}, w.Bytes())
  117. }