forked from golang-migrate/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3_test.go
127 lines (120 loc) · 3.12 KB
/
s3_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package awss3
import (
"errors"
"io/ioutil"
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
st "github.com/golang-migrate/migrate/v4/source/testing"
"github.com/stretchr/testify/assert"
)
func Test(t *testing.T) {
s3Client := fakeS3{
bucket: "some-bucket",
objects: map[string]string{
"staging/migrations/1_foobar.up.sql": "1 up",
"staging/migrations/1_foobar.down.sql": "1 down",
"prod/migrations/1_foobar.up.sql": "1 up",
"prod/migrations/1_foobar.down.sql": "1 down",
"prod/migrations/3_foobar.up.sql": "3 up",
"prod/migrations/4_foobar.up.sql": "4 up",
"prod/migrations/4_foobar.down.sql": "4 down",
"prod/migrations/5_foobar.down.sql": "5 down",
"prod/migrations/7_foobar.up.sql": "7 up",
"prod/migrations/7_foobar.down.sql": "7 down",
"prod/migrations/not-a-migration.txt": "",
"prod/migrations/0-random-stuff/whatever.txt": "",
},
}
driver, err := WithInstance(&s3Client, &Config{
Bucket: "some-bucket",
Prefix: "prod/migrations/",
})
if err != nil {
t.Fatal(err)
}
st.Test(t, driver)
}
func TestParseURI(t *testing.T) {
tests := []struct {
name string
uri string
config *Config
}{
{
"with prefix, no trailing slash",
"s3://migration-bucket/production",
&Config{
Bucket: "migration-bucket",
Prefix: "production/",
},
},
{
"without prefix, no trailing slash",
"s3://migration-bucket",
&Config{
Bucket: "migration-bucket",
},
},
{
"with prefix, trailing slash",
"s3://migration-bucket/production/",
&Config{
Bucket: "migration-bucket",
Prefix: "production/",
},
},
{
"without prefix, trailing slash",
"s3://migration-bucket/",
&Config{
Bucket: "migration-bucket",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual, err := parseURI(test.uri)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, test.config, actual)
})
}
}
type fakeS3 struct {
s3.S3
bucket string
objects map[string]string
}
func (s *fakeS3) ListObjects(input *s3.ListObjectsInput) (*s3.ListObjectsOutput, error) {
bucket := aws.StringValue(input.Bucket)
if bucket != s.bucket {
return nil, errors.New("bucket not found")
}
prefix := aws.StringValue(input.Prefix)
delimiter := aws.StringValue(input.Delimiter)
var output s3.ListObjectsOutput
for name := range s.objects {
if strings.HasPrefix(name, prefix) {
if delimiter == "" || !strings.Contains(strings.Replace(name, prefix, "", 1), delimiter) {
output.Contents = append(output.Contents, &s3.Object{
Key: aws.String(name),
})
}
}
}
return &output, nil
}
func (s *fakeS3) GetObject(input *s3.GetObjectInput) (*s3.GetObjectOutput, error) {
bucket := aws.StringValue(input.Bucket)
if bucket != s.bucket {
return nil, errors.New("bucket not found")
}
if data, ok := s.objects[aws.StringValue(input.Key)]; ok {
body := ioutil.NopCloser(strings.NewReader(data))
return &s3.GetObjectOutput{Body: body}, nil
}
return nil, errors.New("object not found")
}