This repository was archived by the owner on Mar 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3downloader_test.go
103 lines (94 loc) · 2.2 KB
/
s3downloader_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
package raws
import (
"context"
"errors"
"fmt"
"io"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/service/s3/s3manager/s3manageriface"
)
type mockS3Downloader struct {
s3manageriface.DownloaderAPI
// Mock of DownloadObject
dob int64
doerr error
}
func (m mockS3Downloader) DownloadWithContext(
_ aws.Context, _ io.WriterAt, _ *s3.GetObjectInput, _ ...func(*s3manager.Downloader),
) (int64, error) {
return m.dob, m.doerr
}
func TestDownloadObjet(t *testing.T) {
tests := []struct {
name string
mocked []*serviceConnector
regions []string
input *s3.GetObjectInput
expectedBytes int64
expectedError error
}{
{
name: "one region with error",
mocked: []*serviceConnector{
{
s3downloader: mockS3Downloader{
dob: 0,
doerr: errors.New("error with test"),
},
},
},
regions: []string{"test"},
input: &s3.GetObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("key"),
},
expectedError: fmt.Errorf("couldn't download 'bucket/key' in any of '[test]' regions"),
expectedBytes: 0,
},
{
name: "invalid file requested",
mocked: nil,
regions: []string{"test"},
input: &s3.GetObjectInput{
Bucket: nil,
Key: nil,
},
expectedError: fmt.Errorf("couldn't download undefined object (keys or bucket not set)"),
expectedBytes: 0,
},
{
name: "one region no error",
mocked: []*serviceConnector{
{
s3downloader: mockS3Downloader{
dob: 42,
doerr: nil,
},
},
},
regions: []string{"test"},
input: &s3.GetObjectInput{
Bucket: aws.String("bucket"),
Key: aws.String("key"),
},
expectedError: nil,
expectedBytes: 42,
},
}
var ctx = context.Background()
for i, tt := range tests {
c := &connector{
regions: tt.regions,
svcs: tt.mocked,
}
bytes, err := c.DownloadObject(ctx, nil, tt.input, nil)
checkErrors(t, tt.name, i, err, tt.expectedError)
if tt.expectedBytes != bytes {
t.Errorf("%s [%d] - S3 download object: received=%+v | expected=%+v",
tt.name, i, bytes, tt.expectedBytes)
}
}
}