Debian Codegraph @

fs/fstestutil/mounted.go

raw symbols SCIP
1 package fstestutil
2
3 import (
4 "errors"
5 "io/ioutil"
6 "log"
7 "os"
8 "testing"
9 "time"
10
11 "github.com/jacobsa/bazilfuse"
12 "github.com/jacobsa/bazilfuse/fs"
13 )
14
15 // Mount contains information about the mount for the test to use.
16 type Mount struct {
17 // Dir is the temporary directory where the filesystem is mounted.
18 Dir string
19
20 Conn *fuse.Conn
21 Server *fs.Server
22
23 // Error will receive the return value of Serve.
24 Error <-chan error
25
26 done <-chan struct{}
27 closed bool
28 }
29
30 // Close unmounts the filesystem and waits for fs.Serve to return. Any
31 // returned error will be stored in Err. It is safe to call Close
32 // multiple times.
33 func (mnt *Mount) Close() {
34 if mnt.closed {
35 return
36 }
37 mnt.closed = true
38 for tries := 0; tries < 1000; tries++ {
39 err := fuse.Unmount(mnt.Dir)
40 if err != nil {
41 // TODO do more than log?
42 log.Printf("unmount error: %v", err)
43 time.Sleep(10 * time.Millisecond)
44 continue
45 }
46 break
47 }
48 <-mnt.done
49 mnt.Conn.Close()
50 os.Remove(mnt.Dir)
51 }
52
53 // Mounted mounts the fuse.Server at a temporary directory.
54 //
55 // It also waits until the filesystem is known to be visible (OS X
56 // workaround).
57 //
58 // After successful return, caller must clean up by calling Close.
59 func Mounted(filesys fs.FS, conf *fs.Config, options ...fuse.MountOption) (*Mount, error) {
60 dir, err := ioutil.TempDir("", "fusetest")
61 if err != nil {
62 return nil, err
63 }
64 c, err := fuse.Mount(dir, options...)
65 if err != nil {
66 return nil, err
67 }
68 server := fs.New(c, conf)
69 done := make(chan struct{})
70 serveErr := make(chan error, 1)
71 mnt := &Mount{
72 Dir: dir,
73 Conn: c,
74 Server: server,
75 Error: serveErr,
76 done: done,
77 }
78 go func() {
79 defer close(done)
80 serveErr <- server.Serve(filesys)
81 }()
82
83 select {
84 case <-mnt.Conn.Ready:
85 if err := mnt.Conn.MountError; err != nil {
86 return nil, err
87 }
88 return mnt, nil
89 case err = <-mnt.Error:
90 // Serve quit early
91 if err != nil {
92 return nil, err
93 }
94 return nil, errors.New("Serve exited early")
95 }
96 }
97
98 // MountedT mounts the filesystem at a temporary directory,
99 // directing it's debug log to the testing logger.
100 //
101 // See Mounted for usage.
102 //
103 // The debug log is not enabled by default. Use `-fuse.debug` or call
104 // DebugByDefault to enable.
105 func MountedT(t testing.TB, filesys fs.FS, options ...fuse.MountOption) (*Mount, error) {
106 conf := &fs.Config{}
107 if debug {
108 conf.Debug = func(msg interface{}) {
109 t.Logf("FUSE: %s", msg)
110 }
111 }
112 return Mounted(filesys, conf, options...)
113 }