-
Notifications
You must be signed in to change notification settings - Fork 9
/
storage_test.go
237 lines (205 loc) · 5.16 KB
/
storage_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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/johannesboyne/gofakes3"
"github.com/johannesboyne/gofakes3/backend/s3mem"
"github.com/stretchr/testify/assert"
)
func files(paths ...string) []*StorageFile {
var files []*StorageFile
for _, p := range paths {
files = append(files, NewStorageFile(p, 1))
}
return files
}
func dirs(paths ...string) []*StorageDirectory {
var dirs []*StorageDirectory
for _, p := range paths {
dirs = append(dirs, NewStorageDirectory(p))
}
return dirs
}
func TestStorageDirectory_Parents(t *testing.T) {
testCases := []struct {
p string
expected []*StorageDirectory
}{
{
p: "",
expected: dirs(),
},
{
p: "a",
expected: dirs(""),
},
{
p: "a/b",
expected: dirs("", "a"),
},
{
p: "a/b/c",
expected: dirs("", "a", "a/b"),
},
}
for _, tc := range testCases {
dir := NewStorageDirectory(tc.p)
assert.EqualValues(t, tc.expected, dir.Parents())
}
}
func newTestS3Config() (S3Config, func()) {
backend := s3mem.New()
faker := gofakes3.New(backend)
ts := httptest.NewServer(faker.Server())
region := "test"
return S3Config{
Region: ®ion,
Endpoint: &ts.URL,
Bucket: "test",
ForcePathStyle: true,
Credentials: &S3Credentials{
ID: "id1",
Secret: "secret1",
},
RequestPresignExpiry: Duration(time.Minute),
}, ts.Close
}
func TestS3Storage(t *testing.T) {
asrt := assert.New(t)
cfg, closeS3 := newTestS3Config()
defer closeS3()
s, err := NewS3Storage(cfg)
asrt.NoError(err)
put := func(path, content string) {
t.Helper()
_, err := s.s3.PutObject(&s3.PutObjectInput{
Body: strings.NewReader(content),
Bucket: aws.String("test"),
Key: aws.String(path),
})
asrt.NoError(err)
}
// Bucket doesn't exist.
_, _, err = s.List("")
asrt.Error(err)
// Bucket exists, but has no content.
_, err = s.s3.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String("test"),
})
asrt.NoError(err)
_, _, err = s.List("")
asrt.Error(err)
// Single file.
put("file1.jpg", "1")
put("empty", "") // Empty files should be ignored.
dirs, files, err := s.List("")
asrt.NoError(err)
asrt.Empty(dirs)
asrt.Len(files, 1)
asrt.Equal("file1.jpg", files[0].path)
// Single directory.
put("dir1/file2.jpg", "12")
put("dir1/empty", "")
dirs, files, err = s.List("")
asrt.NoError(err)
asrt.Len(dirs, 1)
asrt.Equal("dir1", dirs[0].path)
asrt.Len(dirs[0].Parents(), 1)
asrt.Equal("", dirs[0].Parents()[0].path)
asrt.Len(files, 1)
asrt.Equal("file1.jpg", files[0].path)
// Two directories.
put("dir2/file3.jpg", "123")
dirs, files, err = s.List("")
asrt.NoError(err)
asrt.Len(dirs, 2)
asrt.Equal("dir1", dirs[0].path)
asrt.Equal("dir2", dirs[1].path)
asrt.Len(files, 1)
asrt.Equal("file1.jpg", files[0].path)
// Nested directories.
put("dir2/dir22/file4.jpg", "1234")
dirs, files, err = s.List("")
asrt.NoError(err)
asrt.Len(dirs, 2)
asrt.Equal("dir1", dirs[0].path)
asrt.Equal("dir2", dirs[1].path)
asrt.Len(files, 1)
asrt.Equal("file1.jpg", files[0].path)
dirs, files, err = s.List("dir1")
asrt.NoError(err)
asrt.Empty(dirs)
asrt.Len(files, 1)
asrt.Equal("dir1/file2.jpg", files[0].path)
asrt.Equal("file2.jpg", files[0].Name())
asrt.Equal("file2", files[0].FriendlyName())
dirs, files, err = s.List("dir2")
asrt.NoError(err)
asrt.Len(dirs, 1)
asrt.Equal("dir2/dir22", dirs[0].path)
asrt.Equal("dir22", dirs[0].Name())
asrt.Len(dirs[0].Parents(), 2)
asrt.Equal("", dirs[0].Parents()[0].path)
asrt.Equal("dir2", dirs[0].Parents()[1].path)
asrt.Len(files, 1)
asrt.Equal("dir2/file3.jpg", files[0].path)
dirs, files, err = s.List("dir2/dir22")
asrt.NoError(err)
asrt.Empty(dirs)
asrt.Len(files, 1)
asrt.Equal("dir2/dir22/file4.jpg", files[0].path)
// Prefix doexn't exist.
_, _, err = s.List("dir3")
asrt.Error(err)
_, _, err = s.List("dir2/dir23")
asrt.Error(err)
// Content URL.
url, err := s.FileContentURL("file1.jpg")
asrt.NoError(err)
asrt.NotEmpty(url)
url, err = s.FileContentURL("dir2/dir22/file4.jpg")
asrt.NoError(err)
asrt.NotEmpty(url)
url, err = s.FileContentURL("dir2/dir22/file5.jpg")
asrt.Error(err)
asrt.Empty(url)
// File size.
size, err := s.FileSize("file1.jpg")
asrt.NoError(err)
asrt.EqualValues(1, size)
size, err = s.FileSize("dir2/dir22/file4.jpg")
asrt.NoError(err)
asrt.EqualValues(4, size)
_, err = s.FileSize("dir2/dir22/file5.jpg")
asrt.Error(err)
// Base prefix dir1.
s.cfg.BasePrefix = "dir1/"
dirs, files, err = s.List("")
asrt.NoError(err)
asrt.Empty(dirs)
asrt.Len(files, 1)
asrt.Equal("file2.jpg", files[0].path)
// Base prefix dir2.
s.cfg.BasePrefix = "dir2/"
dirs, files, err = s.List("")
asrt.NoError(err)
asrt.Len(dirs, 1)
asrt.Equal("dir22", dirs[0].path)
asrt.Len(dirs[0].Parents(), 1)
asrt.Equal("", dirs[0].Parents()[0].path)
asrt.Len(files, 1)
asrt.Equal("file3.jpg", files[0].path)
dirs, files, err = s.List("dir22")
asrt.NoError(err)
asrt.Empty(dirs)
asrt.Len(files, 1)
asrt.Equal("dir22/file4.jpg", files[0].path)
// Base prefix doesn't exist.
s.cfg.BasePrefix = "dir3/"
_, _, err = s.List("")
asrt.Error(err)
}