-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3fs_test.go
382 lines (355 loc) · 8.73 KB
/
s3fs_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package s3fs
import (
"bytes"
"io/ioutil"
"os"
"testing"
)
var fs *S3FS
func setup() {
endpoint := "http://127.0.0.1:9000"
if os.Getenv("DRONE") == "true" {
endpoint = "http://minio:9000"
}
fs = New(&Config{
EnableMinioCompat: true,
Endpoint: endpoint,
EnableIAMAuth: true,
AccessKeyID: "accesskey",
AccessSecretKey: "secretkey",
Bucket: "test",
})
}
func teardown() {
// if fs != nil {
// if err := fs.BulkDelete("/"); err != nil {
// println("teardown error:", err.Error())
// }
// }
}
func TestMain(m *testing.M) {
setup()
exitCode := m.Run()
teardown()
os.Exit(exitCode)
}
func TestS3FS_CreateBucket(t *testing.T) {
if err := fs.CreateBucket("test"); err != nil {
t.Fatal("bucket create error:", err)
}
}
func TestS3FS_Put(t *testing.T) {
body := []byte("this is test string")
readCloser := ioutil.NopCloser(bytes.NewReader(body))
if err := fs.Put("testfile", readCloser, "text/plain"); err != nil {
t.Fatal(err)
}
}
func TestS3FS_Info(t *testing.T) {
t.Run("file", func(st *testing.T) {
info := fs.Info("/testfile")
if info == nil {
st.Fatal("s3 error")
}
length := *info.ContentLength
if length != int64(len([]byte("this is test string"))) {
st.Fatal("io error")
}
})
}
func TestS3FS_Get(t *testing.T) {
t.Run("exists", func(st *testing.T) {
readCloser, err := fs.Get("/testfile")
if err != nil {
st.Fatal("get file error:", err)
}
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(*readCloser); err != nil {
st.Fatal("io error:", err)
}
body := buf.String()
if body != "this is test string" {
st.Fatal("invalid data")
}
})
t.Run("non exists", func(st *testing.T) {
_, err := fs.Get("/foobar")
if err == nil {
st.Fatal("io error:", err)
}
})
}
func TestS3FS_List(t *testing.T) {
t.Run("exists", func(st *testing.T) {
list := fs.List("/")
if list == nil {
t.Fatal("cannot connect s3")
}
if len(*list) != 1 {
t.Fatal("invalid state")
}
file := (*list)[0]
if file.Name != "testfile" {
t.Fatal("invalid file name:", file.Name)
}
if file.Path != "/testfile" {
t.Fatal("invalid file path:", file.Path)
}
if file.Size != int64(len([]byte("this is test string"))) {
t.Fatal("invalid file size:", file.Size)
}
if file.Type != File {
t.Fatal("invalid file type:", file.Type)
}
})
t.Run("non exists", func(st *testing.T) {
list := fs.List("/dummydir/")
if list == nil {
t.Fatal("cannot connect s3")
}
if len(*list) != 0 {
t.Fatal("invalid state")
}
})
}
func TestS3FS_MkDir(t *testing.T) {
t.Run("mkdir", func(st *testing.T) {
if err := fs.MkDir("/testdir1"); err != nil {
t.Fatal(err)
}
list := fs.List("/testdir1")
if list == nil {
t.Fatal("cannot connect s3")
}
if len(*list) != 1 {
t.Fatal("invalid state")
}
file := (*list)[0]
if file.Name != "testdir1" {
t.Fatal("invalid file name:", file.Name)
}
if file.Path != "/testdir1/" {
t.Fatal("invalid file path:", file.Path)
}
if file.Type != Directory {
t.Fatal("invalid file type:", file.Type)
}
})
t.Run("mkdir -p", func(st *testing.T) {
if err := fs.MkDir("/testdir2/child"); err != nil {
t.Fatal(err)
}
list := fs.List("/testdir2/")
if list == nil {
t.Fatal("cannot connect s3")
}
if len(*list) != 1 {
t.Fatal("invalid state")
}
file := (*list)[0]
if file.Name != "child" {
t.Fatal("invalid file name:", file.Name)
}
if file.Path != "/testdir2/child/" {
t.Fatal("invalid file path:", file.Path)
}
if file.Type != Directory {
t.Fatal("invalid file type:", file.Type)
}
})
}
func TestS3FS_Copy(t *testing.T) {
t.Run("single copy", func(st *testing.T) {
if err := fs.Copy("/testfile", "/testdir1/testfile", nil); err != nil {
st.Fatal("copy error:", err)
}
readCloser, err := fs.Get("/testdir1/testfile")
if err != nil {
st.Fatal("get file error:", err)
}
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(*readCloser); err != nil {
st.Fatal("io error:", err)
}
body := buf.String()
if body != "this is test string" {
st.Fatal("invalid data")
}
})
t.Run("single copy with metadata", func(st *testing.T) {
metadataKey := "Test-Metadata"
metadataValue := "test"
if err := fs.Copy("/testfile", "/testmetadata", &map[string]*string{
metadataKey: &metadataValue,
}); err != nil {
st.Fatal("copy error:", err)
}
readCloser, err := fs.Get("/testdir1/testfile")
if err != nil {
st.Fatal("get file error:", err)
}
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(*readCloser); err != nil {
st.Fatal("io error:", err)
}
body := buf.String()
if body != "this is test string" {
st.Fatal("invalid data")
}
info := fs.Info("/testmetadata")
if info == nil {
st.Fatal("s3 info error")
}
s3Metadata := *info.Metadata[metadataKey]
if s3Metadata != metadataValue {
st.Fatal("s3 metadata error:", s3Metadata)
}
})
t.Run("bulk copy", func(st *testing.T) {
if err := fs.MkDir("/bulkcopy_a"); err != nil {
st.Fatal(err)
}
if err := fs.Copy("/testfile", "/bulkcopy_a/testfile", nil); err != nil {
st.Fatal("copy error:", err)
}
if err := fs.MkDir("/bulkcopy_b"); err != nil {
st.Fatal("mkdir error:", err)
}
if err := fs.Copy("/bulkcopy_a/", "/bulkcopy_b/", nil); err != nil {
st.Fatal("copy error:", err)
}
readCloser, err := fs.Get("/bulkcopy_b/bulkcopy_a/testfile")
if err != nil {
st.Fatal("get file error:", err)
}
buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(*readCloser); err != nil {
st.Fatal("io error:", err)
}
body := buf.String()
if body != "this is test string" {
st.Fatal("invalid data")
}
})
}
func TestS3FS_PathExists(t *testing.T) {
t.Run("root exists", func(st *testing.T) {
exists := fs.PathExists("/")
if exists != true {
t.Fatal("root path doesnt exist")
}
})
t.Run("file exists", func(st *testing.T) {
exists := fs.PathExists("/testfile")
if exists != true {
t.Fatal("root path doesnt exist")
}
})
t.Run("folder exists", func(st *testing.T) {
exists := fs.PathExists("/testdir1")
if exists != true {
t.Fatal("root path doesnt exist")
}
})
t.Run("non exists file", func(st *testing.T) {
exists := fs.PathExists("/dummyfile")
if exists == true {
t.Fatal("dummyfile shouldnt exist")
}
})
t.Run("non exists folder", func(st *testing.T) {
exists := fs.PathExists("/dummydir/")
if exists == true {
t.Fatal("dummydir shouldnt exist")
}
})
}
func TestS3FS_ExactPathExists(t *testing.T) {
t.Run("exact file exists", func(st *testing.T) {
exists := fs.ExactPathExists("/testfile")
if exists != true {
t.Fatal("file doesn't exist")
}
})
t.Run("exact file doesn't exists", func(st *testing.T) {
exists := fs.ExactPathExists("/testfile2")
if exists == true {
t.Fatal("file exists")
}
})
}
func TestS3FS_Move(t *testing.T) {
t.Run("single", func(st *testing.T) {
if err := fs.MkDir("/singlemove"); err != nil {
st.Fatal("mkdir error:", err)
}
if err := fs.Copy("/testfile", "/movetest", nil); err != nil {
st.Fatal("cp error:", err)
}
if err := fs.Move("/movetest", "/singlemove/movetest"); err != nil {
st.Fatal("mv error:", err)
}
})
t.Run("bulk", func(st *testing.T) {
beforeList := fs.List("/testdir1")
if beforeList == nil {
st.Fatal("cannot connect s3")
}
if err := fs.Move("/testdir1/", "/testdir2/"); err != nil {
st.Fatal("move error:", err)
}
afterList := fs.List("/testdir2/testdir1")
if afterList == nil {
st.Fatal("cannot connect s3")
}
if len(*beforeList) != len(*afterList) {
st.Fatal("invalid files:", *beforeList, *afterList)
}
for i := range *beforeList {
if (*beforeList)[i].Name != (*afterList)[i].Name {
st.Fatal("name error:", (*beforeList)[i].Name, (*afterList)[i].Name)
}
if (*beforeList)[i].Size != (*afterList)[i].Size {
st.Fatal("size error:", (*beforeList)[i].Size, (*afterList)[i].Size)
}
if (*beforeList)[i].Type != (*afterList)[i].Type {
st.Fatal("type error:", (*beforeList)[i].Type, (*afterList)[i].Type)
}
}
})
}
func TestS3FS_Delete(t *testing.T) {
t.Run("rm", func(st *testing.T) {
if err := fs.Delete("/testfile"); err != nil {
t.Fatal("copy error:", err)
}
_, err := fs.Get("/testfile")
if err == nil {
t.Fatal("io error:", err)
}
})
t.Run("rm -r", func(st *testing.T) {
if err := fs.Delete("/"); err != nil {
t.Fatal("copy error:", err)
}
list := fs.List("/")
if list == nil {
t.Fatal("s3 error")
}
if len(*list) != 0 {
t.Fatal("io error")
}
})
}
func TestS3FS_DeleteBucket(t *testing.T) {
t.Run("exists", func(st *testing.T) {
if err := fs.DeleteBucket("test"); err != nil {
t.Fatal("bucket delete error:", err)
}
})
t.Run("non exists", func(st *testing.T) {
if err := fs.DeleteBucket("dummybucket"); err == nil {
t.Fatal("io error:", err)
}
})
}