-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmount_test.go
89 lines (77 loc) · 1.95 KB
/
mount_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
package main
import (
"archive/zip"
"bytes"
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"syscall"
"testing"
"bazil.org/fuse/fs/fstestutil"
"bazil.org/fuse/fs/fstestutil/spawntest"
"bazil.org/fuse/fs/fstestutil/spawntest/httpjson"
"golang.org/x/net/context"
)
var helpers spawntest.Registry
func TestMain(m *testing.M) {
helpers.AddFlag(flag.CommandLine)
flag.Parse()
helpers.RunIfNeeded()
os.Exit(m.Run())
}
func doSeek(ctx context.Context, path string) (*struct{}, error) {
f, err := os.Open(filepath.Join(path, "greeting"))
if err != nil {
return nil, fmt.Errorf("cannot open greeting: %v", err)
}
defer f.Close()
buf := make([]byte, 1)
if _, err := f.Read(buf); err != nil {
return nil, fmt.Errorf("cannot read from greeting: %v", err)
}
if _, err := f.Seek(0, io.SeekStart); !errors.Is(err, syscall.ESPIPE) {
return nil, fmt.Errorf("wrong error: %v", err)
}
return &struct{}{}, nil
}
var seekHelper = helpers.Register("seek", httpjson.ServePOST(doSeek))
func TestSeek(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var zipData bytes.Buffer
{
w := zip.NewWriter(&zipData)
f, err := w.Create("greeting")
if err != nil {
t.Fatalf("cannot create zip: %v", err)
}
if _, err := f.Write([]byte("hello, world\n")); err != nil {
t.Fatalf("cannot write to zip: %v", err)
}
if err := w.Close(); err != nil {
t.Fatalf("cannot finalize zip: %v", err)
}
}
// TODO refactor to share setup
r, err := zip.NewReader(bytes.NewReader(zipData.Bytes()), int64(zipData.Len()))
if err != nil {
t.Fatalf("cannot read zip: %v", err)
}
filesys := &FS{
archive: r,
}
mnt, err := fstestutil.MountedT(t, filesys, nil)
if err != nil {
t.Fatalf("cannot mount zipfs: %v", err)
}
defer mnt.Close()
control := seekHelper.Spawn(ctx, t)
defer control.Close()
var nothing struct{}
if err := control.JSON("/").Call(ctx, mnt.Dir, ¬hing); err != nil {
t.Fatalf("calling helper: %v", err)
}
}