-
Notifications
You must be signed in to change notification settings - Fork 0
/
upspinfsys_test.go
311 lines (271 loc) · 8.02 KB
/
upspinfsys_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
package upspinfsys_test
import (
"bytes"
"io"
"io/fs"
"log"
"path/filepath"
"testing"
"testing/fstest"
"github.com/rschio/upspinfsys"
"upspin.io/client"
"upspin.io/config"
"upspin.io/path"
"upspin.io/transports"
"upspin.io/upbox"
"upspin.io/upspin"
)
var (
c upspin.Client
cfg upspin.Config
)
func TestMain(m *testing.M) {
var cleanup func()
c, cfg, cleanup = newClient()
defer cleanup()
createDirTree(c, cfg)
m.Run()
}
func TestStd(t *testing.T) {
fsys := upspinfsys.UpspinFS(c)
var (
documents = "documents"
photos = filepath.Join(documents, "photos")
code = "code"
)
expected := []string{
documents, photos, code,
"rootfile.txt",
filepath.Join(documents, "doc1.txt"),
filepath.Join(documents, "doc2.txt"),
filepath.Join(documents, "link_text"),
filepath.Join(documents, "link_link_text"),
filepath.Join(code, "main.go"),
filepath.Join(code, "go.mod"),
filepath.Join(code, "text.txt"),
}
// To use fstest.TestFS is necessary that "." dir works.
// Upspin does not have a root dir, each user has its own home dir
// and it's impossible or at least not practical to list all Upspin users
// so a ReadDir(".") would not be possible.
// One option to make "." is to root it at client's home dir
// e.g. "user@example.com", but this solution does not make possible to
// list other users content.
// The solution found is: root at "" and use
// fs.Sub(fsys, "user@example.com") to test, so we can test against a good
// test lib and use full Upspin power.
fsys, _ = fs.Sub(fsys, "user@example.com")
if err := fstest.TestFS(fsys, expected...); err != nil {
t.Fatalf("fstest: %v", err)
}
}
func TestDir(t *testing.T) {
fsys := upspinfsys.UpspinFS(c)
root := string(cfg.UserName())
visited := map[string]bool{
filepath.Join(root): false,
filepath.Join(root, "rootfile.txt"): false,
filepath.Join(root, "documents"): false,
filepath.Join(root, "documents", "photos"): false,
filepath.Join(root, "documents", "doc1.txt"): false,
filepath.Join(root, "documents", "doc2.txt"): false,
filepath.Join(root, "documents", "link_text"): false,
filepath.Join(root, "documents", "link_link_text"): false,
filepath.Join(root, "code"): false,
filepath.Join(root, "code", "main.go"): false,
filepath.Join(root, "code", "go.mod"): false,
filepath.Join(root, "code", "text.txt"): false,
}
wantLen := len(visited)
err := fs.WalkDir(fsys, root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
visited[path] = true
return nil
})
if err != nil {
t.Fatalf("walkdir: %v", err)
}
if len(visited) != wantLen {
t.Errorf("got wrong number of files, got %d want %d", len(visited), wantLen)
}
for path, ok := range visited {
if !ok {
t.Errorf("path %s was not visited", path)
}
}
}
func TestUpspinFSDirRead(t *testing.T) {
fsys := upspinfsys.UpspinFS(c)
root := string(cfg.UserName())
f, err := fsys.Open(root)
if err != nil {
t.Fatalf("failed to open root: %v", err)
}
if _, err := io.ReadAll(f); err == nil {
t.Fatal("should return error when reading a dir")
}
}
func TestReadDirFile(t *testing.T) {
fsys := upspinfsys.UpspinFS(c)
root := string(cfg.UserName())
f, err := fsys.Open(filepath.Join(root, "documents"))
if err != nil {
t.Fatalf("failed to open file: %v", err)
}
dir, ok := f.(fs.ReadDirFile)
if !ok {
t.Fatal("should implement fs.ReadDirFile")
}
des, err := dir.ReadDir(1)
if err != nil {
t.Fatalf("read dir: %v", err)
}
if len(des) != 1 {
t.Fatalf("should return 1 entry, got %d", len(des))
}
des2, err := dir.ReadDir(-1)
if err != nil {
t.Fatalf("read dir(-1): %v", err)
}
if len(des2) != 4 {
t.Fatalf("should return 4 entry, got %d", len(des2))
}
}
func TestLink(t *testing.T) {
fsys := upspinfsys.UpspinFS(c)
root := string(cfg.UserName())
target, err := fsys.Open(filepath.Join(root, "code", "text.txt"))
if err != nil {
t.Fatalf("Open link: %v", err)
}
tContent, err := io.ReadAll(target)
target.Close()
if err != nil {
t.Fatalf("ReadAll target: %v", err)
}
l, err := fsys.Open(filepath.Join(root, "documents", "link_text"))
if err != nil {
t.Fatalf("Open link: %v", err)
}
got, err := io.ReadAll(l)
if err != nil {
t.Fatalf("link ReadAll: %v", err)
}
if !bytes.Equal(got, tContent) {
t.Fatalf("got %s want %s", got, tContent)
}
linkOfLink, err := fsys.Open(filepath.Join(root, "documents", "link_link_text"))
if err != nil {
t.Fatalf("Open linkOfLink: %v", err)
}
got, err = io.ReadAll(linkOfLink)
if err != nil {
t.Fatalf("linkOfLink ReadAll: %v", err)
}
if !bytes.Equal(got, tContent) {
t.Fatalf("got %s want %s", got, tContent)
}
}
func TestLinkDir(t *testing.T) {
fsys := upspinfsys.UpspinFS(c)
root := string(cfg.UserName())
// Link to dir
linkDir := filepath.Join(root, "documents", "link_dir")
codeDir := filepath.Join(root, "code")
// Create here to avoid fstest.TestFS:
// Ignore link_dir: https://github.com/golang/go/issues/50401
if _, err := c.PutLink(upspin.PathName(codeDir), upspin.PathName(linkDir)); err != nil {
log.Fatalf("failed to create link_dir: %v", err)
}
want, err := fs.ReadDir(fsys, codeDir)
if err != nil {
t.Fatalf("ReadDir: %v", err)
}
got, err := fs.ReadDir(fsys, linkDir)
if err != nil {
t.Fatalf("ReadDir: %v", err)
}
if len(got) != len(want) {
t.Fatalf("got %d entries, want %d", len(got), len(want))
}
for i := 0; i < len(got); i++ {
if got[i].Name() != want[i].Name() {
t.Errorf("got %q want %q", got[i].Name(), want[i].Name())
}
}
}
func createDirTree(c upspin.Client, cfg upspin.Config) {
var (
root = upspin.PathName(cfg.UserName())
documents = path.Join(root, "documents")
photos = path.Join(documents, "photos")
code = path.Join(root, "code")
)
if _, err := c.MakeDirectory(root); err != nil {
log.Fatalf("failed to create root dir: %v", err)
}
if _, err := c.MakeDirectory(documents); err != nil {
log.Fatalf("failed to create documents dir: %v", err)
}
if _, err := c.MakeDirectory(photos); err != nil {
log.Fatalf("failed to create photos dir: %v", err)
}
if _, err := c.MakeDirectory(code); err != nil {
log.Fatalf("failed to create code dir: %v", err)
}
if _, err := c.Put(path.Join(root, "rootfile.txt"), []byte("rootfile")); err != nil {
log.Fatalf("failed to create rootfile.txt: %v", err)
}
if _, err := c.Put(path.Join(documents, "doc1.txt"), []byte("doc1")); err != nil {
log.Fatalf("failed to create doc1.txt: %v", err)
}
if _, err := c.Put(path.Join(documents, "doc2.txt"), []byte("doc2")); err != nil {
log.Fatalf("failed to create doc2.txt: %v", err)
}
if _, err := c.Put(path.Join(code, "main.go"), []byte("package main")); err != nil {
log.Fatalf("failed to create main.go: %v", err)
}
if _, err := c.Put(path.Join(code, "go.mod"), []byte("module fake")); err != nil {
log.Fatalf("failed to create go.mod: %v", err)
}
if _, err := c.Put(path.Join(code, "text.txt"), []byte("text")); err != nil {
log.Fatalf("failed to create text.txt: %v", err)
}
// Link
if _, err := c.PutLink(path.Join(code, "text.txt"), path.Join(documents, "link_text")); err != nil {
log.Fatalf("failed to create link_text: %v", err)
}
// Link to link
if _, err := c.PutLink(path.Join(documents, "link_text"), path.Join(documents, "link_link_text")); err != nil {
log.Fatalf("failed to create link_link_text: %v", err)
}
}
const schema = `
users:
- name: user
servers:
- name: keyserver
- name: storeserver
- name: dirserver
domain: example.com
`
func newClient() (upspin.Client, upspin.Config, func()) {
sc, err := upbox.SchemaFromYAML(schema)
if err != nil {
log.Fatalf("failed to parse schema: %v", err)
}
if err := sc.Start(); err != nil {
log.Fatalf("failed to start schema: %v", err)
}
cleanup := func() { sc.Stop() }
cfg, err := config.FromFile(sc.Config(sc.Users[0].Name))
if err != nil {
cleanup()
log.Fatalf("failed to parse config: %v", err)
}
transports.Init(cfg)
c := client.New(cfg)
return c, cfg, cleanup
}