Files
cgofuse/fuse/host_test.go

113 lines
2.1 KiB
Go

//go:build cgo && (linux || darwin || freebsd || netbsd || openbsd)
package fuse
import (
"context"
"os"
"os/signal"
"path/filepath"
"syscall"
"testing"
)
type testfs struct {
FileSystemBase
t *testing.T
init, dstr int
}
func (selffs *testfs) Init() {
if selffs.t != nil {
selffs.t.Log("Init() called more than once")
}
selffs.init++
}
func (selffs *testfs) Destroy() {
if selffs.t != nil {
selffs.t.Log("Destroy() called more than once")
}
selffs.dstr++
}
func (selffs *testfs) Getattr(path string, stat *Stat_t, fh uint64) (errc int) {
if selffs.t != nil {
selffs.t.Logf("Getattr(%s)", path)
}
switch path {
case "/":
stat.Mode = S_IFDIR | 0555
return 0
default:
return -ENOENT
}
}
func (selffs *testfs) Readdir(path string, fill StatFill, ofst int64, fh uint64) (errc int) {
if selffs.t != nil {
selffs.t.Logf("Readdir(%s)", path)
}
fill(".", nil, 0)
fill("..", nil, 0)
return 0
}
func (selffs *testfs) Readlink(path string) (int, string) {
if selffs.t != nil {
selffs.t.Logf("Readlink(%s)", path)
}
return 0, "/"
}
func testHost(t *testing.T, unmount bool) {
mntp := filepath.Join(t.TempDir(), "m")
if err := os.Mkdir(mntp, os.FileMode(0755)); err != nil {
panic(err)
}
ctx, done := context.WithCancel(t.Context())
defer done()
if !unmount {
var stop context.CancelFunc
ctx, stop = signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stop()
}
tstf := &testfs{t: t}
host := NewFileSystemHost(tstf)
mres := host.Mount(ctx, mntp)
_, _ = os.ReadDir(mntp)
_, _ = os.Stat(mntp)
_, _ = os.Readlink(mntp)
if unmount {
done()
} else {
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}
ures := host.Done()
if mres != nil {
t.Errorf("Mount failed: %s", mres)
}
if ures != nil {
t.Errorf("Unmount failed: %s", ures)
}
if tstf.init != 1 {
t.Errorf("Init() called %v times; expected 1", tstf.init)
}
if tstf.dstr != 1 {
t.Errorf("Destroy() called %v times; expected 1", tstf.dstr)
}
}
func TestUnmount(t *testing.T) {
testHost(t, true)
}
func TestSignal(t *testing.T) {
testHost(t, false)
}