-
Notifications
You must be signed in to change notification settings - Fork 90
/
mock.go
87 lines (71 loc) · 1.73 KB
/
mock.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
package s3_test
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
"sync"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
)
func NewMockS3() *MockS3 {
return &MockS3{
buckets: map[string]map[string][]byte{},
}
}
// MockS3 mimics an S3 blob store for testing.
type MockS3 struct {
sync.RWMutex
buckets map[string]map[string][]byte
s3iface.S3API
}
func (m *MockS3) NewBucket(name string) {
m.buckets[name] = map[string][]byte{}
}
func (m *MockS3) PutObject(in *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
data, err := ioutil.ReadAll(in.Body)
if err != nil {
return nil, err
}
m.Lock()
defer m.Unlock()
bucket, ok := m.buckets[*in.Bucket]
if !ok {
bucket = map[string][]byte{}
m.buckets[*in.Bucket] = bucket
}
bucket[*in.Key] = data
return &s3.PutObjectOutput{}, nil
}
func (m *MockS3) GetObject(in *s3.GetObjectInput) (*s3.GetObjectOutput, error) {
m.RLock()
defer m.RUnlock()
bucket, ok := m.buckets[*in.Bucket]
if !ok {
return nil, fmt.Errorf("bucket '%s' does not exist", *in.Bucket)
}
data, ok := bucket[*in.Key]
if !ok {
return nil, fmt.Errorf("key '%s' does not exist in bucket '%s'", *in.Key, *in.Bucket)
}
return &s3.GetObjectOutput{
Body: ioutil.NopCloser(bytes.NewBuffer(data)),
}, nil
}
func (m *MockS3) ListObjectsV2(in *s3.ListObjectsV2Input) (*s3.ListObjectsV2Output, error) {
bucket, ok := m.buckets[*in.Bucket]
if !ok {
return nil, fmt.Errorf("bucket '%s' does not exist", *in.Bucket)
}
var objects []*s3.Object
for key := range bucket {
if strings.HasPrefix(key, *in.Prefix) {
objKey := key
obj := &s3.Object{Key: &objKey}
objects = append(objects, obj)
}
}
out := new(s3.ListObjectsV2Output)
out.SetContents(objects)
return out, nil
}