forked from coinbase/step
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_s3.go
174 lines (138 loc) · 3.92 KB
/
mock_s3.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
package mocks
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/cleardataeng/step/utils/to"
)
// S3Client
type GetObjectResponse struct {
Resp *s3.GetObjectOutput
Body string
Error error
}
type PutObjectResponse struct {
Resp *s3.PutObjectOutput
Error error
}
type DeleteObjectResponse struct {
Resp *s3.DeleteObjectOutput
Error error
}
type GetBucketTaggingResponse struct {
Resp *s3.GetBucketTaggingOutput
Error error
}
type MockS3Client struct {
s3iface.S3API
GetObjectResp map[string]*GetObjectResponse
PutObjectResp map[string]*PutObjectResponse
DeleteObjectResp map[string]*DeleteObjectResponse
GetBucketTaggingResp map[string]*GetBucketTaggingResponse
}
func (m *MockS3Client) init() {
if m.GetObjectResp == nil {
m.GetObjectResp = map[string]*GetObjectResponse{}
}
if m.PutObjectResp == nil {
m.PutObjectResp = map[string]*PutObjectResponse{}
}
if m.DeleteObjectResp == nil {
m.DeleteObjectResp = map[string]*DeleteObjectResponse{}
}
if m.GetBucketTaggingResp == nil {
m.GetBucketTaggingResp = map[string]*GetBucketTaggingResponse{}
}
}
func MakeS3Body(ret string) io.ReadCloser {
return ioutil.NopCloser(strings.NewReader(ret))
}
func makeS3Resp(ret string, contentType *string, cacheControl *string) *s3.GetObjectOutput {
return &s3.GetObjectOutput{
Body: MakeS3Body(ret),
ContentType: contentType,
CacheControl: cacheControl,
LastModified: to.Timep(time.Now()),
}
}
func AWSS3NotFoundError() error {
return awserr.New(s3.ErrCodeNoSuchKey, "not found", nil)
}
func (m *MockS3Client) addGetObjectWithContentTypeAndCacheControl(key string, body string, contentType *string, cacheControl *string, err error) {
m.init()
m.GetObjectResp[key] = &GetObjectResponse{
Resp: makeS3Resp(body, contentType, cacheControl),
Body: body,
Error: err,
}
}
func (m *MockS3Client) AddGetObject(key string, body string, err error) {
m.addGetObjectWithContentTypeAndCacheControl(key, body, nil, nil, err)
}
func (m *MockS3Client) AddPutObject(key string, err error) {
m.init()
m.PutObjectResp[key] = &PutObjectResponse{
Resp: &s3.PutObjectOutput{},
Error: err,
}
}
func (m *MockS3Client) SetBucketTags(bucket string, tags map[string]string, err error) {
m.init()
tagSet := []*s3.Tag{}
for tk, tv := range tags {
tagSet = append(tagSet, &s3.Tag{Key: to.Strp(tk), Value: to.Strp(tv)})
}
m.GetBucketTaggingResp[bucket] = &GetBucketTaggingResponse{
Resp: &s3.GetBucketTaggingOutput{
TagSet: tagSet,
},
Error: err,
}
}
func (m *MockS3Client) GetObject(in *s3.GetObjectInput) (*s3.GetObjectOutput, error) {
m.init()
resp := m.GetObjectResp[*in.Key]
if resp == nil {
return nil, AWSS3NotFoundError()
}
resp.Resp.Body = MakeS3Body(resp.Body)
return resp.Resp, resp.Error
}
func (m *MockS3Client) ListObjects(in *s3.ListObjectsInput) (*s3.ListObjectsOutput, error) {
return nil, nil
}
func (m *MockS3Client) PutObject(in *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
m.init()
resp := m.PutObjectResp[*in.Key]
// Simulates adding the object
buf := new(bytes.Buffer)
buf.ReadFrom(in.Body)
m.addGetObjectWithContentTypeAndCacheControl(*in.Key, buf.String(), in.ContentType, in.CacheControl, nil)
if resp == nil {
return &s3.PutObjectOutput{}, nil
}
return resp.Resp, resp.Error
}
func (m *MockS3Client) GetBucketTagging(in *s3.GetBucketTaggingInput) (*s3.GetBucketTaggingOutput, error) {
m.init()
resp := m.GetBucketTaggingResp[*in.Bucket]
if resp == nil {
return nil, fmt.Errorf("Unkown Bucket, should mock the tags")
}
return resp.Resp, resp.Error
}
func (m *MockS3Client) DeleteObject(in *s3.DeleteObjectInput) (*s3.DeleteObjectOutput, error) {
m.init()
resp := m.DeleteObjectResp[*in.Key]
delete(m.GetObjectResp, *in.Key)
if resp == nil {
return &s3.DeleteObjectOutput{}, nil
}
return resp.Resp, resp.Error
}