-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
197 lines (161 loc) · 3.9 KB
/
server_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
package fs
import (
"bytes"
"context"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/hown3d/s3-csi/internal/aws"
"github.com/hown3d/s3-csi/internal/aws/s3"
"github.com/hown3d/s3-csi/test/aws/localstack"
"github.com/stretchr/testify/assert"
"io"
"log"
"os"
"path/filepath"
"testing"
)
func setupServer(t *testing.T, cfg *Config) {
server, err := NewServer(cfg)
failIfErr(t, err)
go server.start()
err = server.waitMount()
failIfErr(t, err)
t.Cleanup(func() {
umountErr := server.fuseServer.Unmount()
if umountErr != nil {
t.Logf("error unmounting fuse server: %s", umountErr)
}
})
}
func TestMain(m *testing.M) {
cleanup, err := startLocalstack()
if err != nil {
cleanup()
log.Print(err)
return
}
exitCode := m.Run()
cleanup()
os.Exit(exitCode)
}
func startLocalstack() (cleanup func(), err error) {
con, err := localstack.New()
if err != nil {
return func() {}, err
}
cleanup = func() {
con.Delete()
}
if err := con.SetAWSEnvVariables(); err != nil {
return cleanup, err
}
return cleanup, err
}
// setupEnvironment setups the environment and fills the created config in cfg
func setupEnvironment(t *testing.T, cfg *Config) {
awsCfg, err := aws.NewConfig(context.Background())
failIfErr(t, err)
dir, err := os.MkdirTemp("", t.Name())
failIfErr(t, err)
t.Cleanup(func() {
os.RemoveAll(dir)
})
bucketName := "testbucket"
cfg.MountDir = dir
cfg.S3Client = s3.NewClient(awsCfg)
cfg.BucketName = bucketName
bucket := cfg.S3Client.NewBucket(bucketName)
err = bucket.Create(context.Background(), "")
if err != nil {
var alreadyExists *types.BucketAlreadyExists
if !assert.ErrorAs(t, err, &alreadyExists) {
t.Fatal(err)
}
}
t.Cleanup(func() {
bucket.Empty(context.Background())
})
setupServer(t, cfg)
}
func TestCreate(t *testing.T) {
cfg := new(Config)
setupEnvironment(t, cfg)
filename := "testfile"
name := filepath.Join(cfg.MountDir, filename)
f, err := os.Create(name)
failIfErr(t, err)
t.Cleanup(func() {
f.Close()
})
ctx := context.Background()
obj := cfg.S3Client.NewObject(cfg.BucketName, objKey(filename))
assert.True(t, obj.Exists(ctx))
r, err := obj.Read(ctx, nil)
failIfErr(t, err)
data, err := io.ReadAll(r)
failIfErr(t, err)
assert.Equal(t, 0, len(data))
}
func TestWrite(t *testing.T) {
cfg := new(Config)
setupEnvironment(t, cfg)
filename := "testfile"
name := filepath.Join(cfg.MountDir, filename)
data := []byte("hello-world")
err := os.WriteFile(name, data, 0755)
failIfErr(t, err)
ctx := context.Background()
obj := cfg.S3Client.NewObject(cfg.BucketName, objKey(filename))
assert.True(t, obj.Exists(ctx))
r, err := obj.Read(ctx, nil)
failIfErr(t, err)
actualData, err := io.ReadAll(r)
failIfErr(t, err)
assert.Equal(t, len(data), len(actualData))
assert.Equal(t, data, actualData)
}
func TestRead(t *testing.T) {
cfg := new(Config)
setupEnvironment(t, cfg)
ctx := context.Background()
name := "testfile"
key := objKey(name)
data := []byte("hello-world")
obj := cfg.S3Client.NewObject(cfg.BucketName, key)
err := obj.Create(ctx, nil)
failIfErr(t, err)
err = obj.Write(ctx, bytes.NewReader(data), nil)
failIfErr(t, err)
filename := filepath.Join(cfg.MountDir, name)
actualData, err := os.ReadFile(filename)
failIfErr(t, err)
assert.Equal(t, data, actualData)
}
func TestReadDir(t *testing.T) {
cfg := new(Config)
setupEnvironment(t, cfg)
folderName := "testfolder"
dirName := filepath.Join(cfg.MountDir, folderName)
files := []string{
filepath.Join(dirName, "test1"),
filepath.Join(dirName, "test2"),
}
for _, f := range files {
fd, err := os.Create(f)
if assert.NoError(t, err) {
fd.Close()
}
}
entries, err := os.ReadDir(dirName)
assert.NoError(t, err)
for _, entry := range entries {
assert.Contains(t, files, entry.Name())
}
}
func objKey(filename string) string {
return ROOT_KEY + filename
}
func failIfErr(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}