-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3fs_test.go
192 lines (150 loc) · 4.48 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
package s3fs
import (
"encoding/json"
"fmt"
"io/fs"
"os"
"strings"
"testing"
"testing/fstest"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/stretchr/testify/require"
)
func TestS3FS(t *testing.T) {
bucket := os.Getenv("S3FS_TESTING_BUCKET")
require.NotEqual(t, "", bucket, "S3FS_TESTING_BUCKET must be set")
sess, err := session.NewSession()
if err != nil {
panic(err)
}
client := s3.New(sess)
defer emptyBucket(client, bucket)
writeFile(client, bucket, "top.json", `{"data":"top"}`)
writeFile(client, bucket, "deep/down/top.json", `{"data":"liar"}`)
writeFile(client, bucket, "dir-a/one.json", `{"data":"one"}`)
writeFile(client, bucket, "dir-a/two.json", `{"data":"two"}`)
writeFile(client, bucket, "dir-a/three.json", `{"data":"three"}`)
writeFile(client, bucket, "dir-b/foo.json", `{"data":"foo"}`)
writeFile(client, bucket, "dir-b/foo.json", `{"data":"bar"}`)
myFS := NewS3FS(client, bucket)
if err := fstest.TestFS(myFS, "dir-b/foo.json"); err != nil {
t.Fatal(err)
}
}
func TestS3FS_ReadFile(t *testing.T) {
bucket := os.Getenv("S3FS_TESTING_BUCKET")
require.NotEqual(t, "", bucket, "S3FS_TESTING_BUCKET must be set")
sess, err := session.NewSession()
if err != nil {
panic(err)
}
client := s3.New(sess)
defer emptyBucket(client, bucket)
writeFile(client, bucket, "foo.json", `{"data":"foo"}`)
myFS := NewS3FS(client, bucket)
f, err := myFS.Open("foo.json")
require.Nil(t, err)
out := map[string]string{}
err = json.NewDecoder(f).Decode(&out)
require.Nil(t, err)
require.Equal(t, "foo", out["data"])
}
func TestS3FS_ReadDir(t *testing.T) {
bucket := os.Getenv("S3FS_TESTING_BUCKET")
require.NotEqual(t, "", bucket, "S3FS_TESTING_BUCKET must be set")
sess, err := session.NewSession()
if err != nil {
panic(err)
}
client := s3.New(sess)
defer emptyBucket(client, bucket)
writeFile(client, bucket, "mydir/foo.json", `{"data":"foo"}`)
writeFile(client, bucket, "mydir/bar.json", `{"data":"bar"}`)
writeFile(client, bucket, "mydir/baz.json", `{"data":"baz"}`)
myFS := NewS3FS(client, bucket)
entries, err := fs.ReadDir(myFS, "mydir")
require.Nil(t, err)
require.Equal(t, 3, len(entries))
require.True(t, dirEntriesContains(entries, "foo.json"))
require.True(t, dirEntriesContains(entries, "bar.json"))
require.True(t, dirEntriesContains(entries, "baz.json"))
}
func TestS3FS_FileAndDir(t *testing.T) {
bucket := os.Getenv("S3FS_TESTING_BUCKET")
require.NotEqual(t, "", bucket, "S3FS_TESTING_BUCKET must be set")
sess, err := session.NewSession()
if err != nil {
panic(err)
}
client := s3.New(sess)
defer emptyBucket(client, bucket)
writeFile(client, bucket, "foo", `{"data":"foo"}`)
writeFile(client, bucket, "foo/bar", `{"data":"bar"}`)
myFS := NewS3FS(client, bucket)
_, err = myFS.Open("foo")
require.NotNil(t, err)
require.Contains(t, err.Error(), "directory name matches file name")
}
func TestS3FS_FileEndingWithSlash(t *testing.T) {
bucket := os.Getenv("S3FS_TESTING_BUCKET")
require.NotEqual(t, "", bucket, "S3FS_TESTING_BUCKET must be set")
sess, err := session.NewSession()
if err != nil {
panic(err)
}
client := s3.New(sess)
defer emptyBucket(client, bucket)
writeFile(client, bucket, "weird/", `{"data":"weird"}`)
myFS := NewS3FS(client, bucket)
_, err = myFS.Open("weird/")
require.NotNil(t, err)
require.Contains(t, err.Error(), "invalid name")
}
func dirEntriesContains(entries []fs.DirEntry, name string) bool {
for _, e := range entries {
if e.Name() == name {
return true
}
}
return false
}
func writeFile(client *s3.S3, bucket, key, body string) {
_, err := client.PutObject(&s3.PutObjectInput{
Body: aws.ReadSeekCloser(strings.NewReader(body)),
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
panic(err)
}
}
func emptyBucket(client *s3.S3, bucket string) {
keys := []string{}
err := client.ListObjectsV2Pages(
&s3.ListObjectsV2Input{
Bucket: &bucket,
},
func(page *s3.ListObjectsV2Output, lastPage bool) bool {
for _, obj := range page.Contents {
keys = append(keys, *obj.Key)
}
return true
},
)
if err != nil {
fmt.Println("ERROR: could not delete objects after testing. Manual fix may be required")
panic(err)
}
for _, key := range keys {
_, err := client.DeleteObject(&s3.DeleteObjectInput{
Bucket: &bucket,
Key: &key,
})
if err != nil {
fmt.Println("ERROR: could not delete objects after testing. Manual fix may be required")
panic(err)
}
}
}