| 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 |
|
| 16 |
type Mount struct { |
| 17 |
|
| 18 |
Dir string |
| 19 |
|
| 20 |
Conn *fuse.Conn |
| 21 |
Server *fs.Server |
| 22 |
|
| 23 |
|
| 24 |
Error <-chan error |
| 25 |
|
| 26 |
done <-chan struct{} |
| 27 |
closed bool |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 91 |
if err != nil { |
| 92 |
return nil, err |
| 93 |
} |
| 94 |
return nil, errors.New("Serve exited early") |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 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 |
} |