Commit ba08cb01 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys Committed by Han-Wen Nienhuys

fs: use testing.Cleanup throughout

Change-Id: I115ebce34aaf22767bb8b90b842927c133e30b65
parent 95d4266e
......@@ -22,7 +22,6 @@ import (
func TestBridgeReaddirPlusVirtualEntries(t *testing.T) {
// Set suppressDebug as we do our own logging
tc := newTestCase(t, &testOptions{suppressDebug: true})
defer tc.Clean()
rb := tc.rawFS.(*rawBridge)
......@@ -92,8 +91,7 @@ func TestBridgeReaddirPlusVirtualEntries(t *testing.T) {
// we just have not received the FORGET yet.
func TestTypeChange(t *testing.T) {
rootNode := testTypeChangeIno{}
mnt, _, clean := testMount(t, &rootNode, nil)
defer clean()
mnt, _ := testMount(t, &rootNode, nil)
for i := 0; i < 100; i++ {
fi, _ := os.Stat(mnt + "/file")
......@@ -141,8 +139,7 @@ func (fn *testTypeChangeIno) Lookup(ctx context.Context, name string, out *fuse.
// disconnected from the hierarchy (=orphaned)
func TestDeletedInodePath(t *testing.T) {
rootNode := testDeletedIno{}
mnt, _, clean := testMount(t, &rootNode, &Options{Logger: log.New(os.Stderr, "", 0)})
defer clean()
mnt, _ := testMount(t, &rootNode, &Options{Logger: log.New(os.Stderr, "", 0)})
// Open a file handle so the kernel cannot FORGET the inode
fd, err := os.Open(mnt + "/dir")
......@@ -209,8 +206,7 @@ func (n *testDeletedIno) Getattr(ctx context.Context, f FileHandle, out *fuse.At
// panic: using reserved ID 1 for inode number
func TestIno1(t *testing.T) {
rootNode := testIno1{}
mnt, _, clean := testMount(t, &rootNode, nil)
defer clean()
mnt, _ := testMount(t, &rootNode, nil)
var st syscall.Stat_t
err := syscall.Stat(mnt+"/ino1", &st)
......
......@@ -92,8 +92,8 @@ func (r *keepCacheRoot) OnAdd(ctx context.Context) {
// change content but no metadata.
func TestKeepCache(t *testing.T) {
root := &keepCacheRoot{}
mntDir, _, clean := testMount(t, root, nil)
defer clean()
mntDir, _ := testMount(t, root, nil)
c1, err := ioutil.ReadFile(mntDir + "/keep")
if err != nil {
t.Fatalf("read keep 1: %v", err)
......
......@@ -55,8 +55,7 @@ func (f *dioFile) Open(ctx context.Context, flags uint32) (fh FileHandle, fuseFl
// this tests FOPEN_DIRECT_IO (as opposed to O_DIRECTIO)
func TestFUSEDirectIO(t *testing.T) {
root := &dioRoot{}
mntDir, server, clean := testMount(t, root, nil)
defer clean()
mntDir, server := testMount(t, root, nil)
f, err := os.Open(mntDir + "/file")
if err != nil {
......
......@@ -55,15 +55,10 @@ func TestInterrupt(t *testing.T) {
root := &interruptRoot{}
oneSec := time.Second
mntDir, _, clean := testMount(t, root, &Options{
mntDir, server := testMount(t, root, &Options{
EntryTimeout: &oneSec,
AttrTimeout: &oneSec,
})
defer func() {
if clean != nil {
clean()
}
}()
cmd := exec.Command("cat", mntDir+"/file")
if err := cmd.Start(); err != nil {
......@@ -74,9 +69,7 @@ func TestInterrupt(t *testing.T) {
if err := cmd.Process.Kill(); err != nil {
t.Errorf("Kill: %v", err)
}
clean()
clean = nil
server.Unmount()
if !root.child.interrupted {
t.Errorf("open request was not interrupted")
......
......@@ -22,7 +22,6 @@ import (
func TestRenameExchange(t *testing.T) {
tc := newTestCase(t, &testOptions{attrCache: true, entryCache: true})
defer tc.Clean()
if err := os.Mkdir(tc.origDir+"/dir", 0755); err != nil {
t.Fatalf("Mkdir: %v", err)
......@@ -95,7 +94,6 @@ func TestRenameExchange(t *testing.T) {
func TestRenameNoOverwrite(t *testing.T) {
tc := newTestCase(t, &testOptions{attrCache: true, entryCache: true})
defer tc.Clean()
if err := os.Mkdir(tc.origDir+"/dir", 0755); err != nil {
t.Fatalf("Mkdir: %v", err)
......@@ -123,7 +121,6 @@ func TestRenameNoOverwrite(t *testing.T) {
func TestXAttr(t *testing.T) {
tc := newTestCase(t, &testOptions{attrCache: true, entryCache: true})
defer tc.Clean()
tc.writeOrig("file", "", 0644)
......@@ -188,7 +185,6 @@ func TestXAttr(t *testing.T) {
// so don't even bother. See `man 7 xattr` for more info.
func TestXAttrSymlink(t *testing.T) {
tc := newTestCase(t, nil)
defer tc.Clean()
path := tc.mntDir + "/symlink"
if err := syscall.Symlink("target/does/not/exist", path); err != nil {
......@@ -203,7 +199,6 @@ func TestXAttrSymlink(t *testing.T) {
func TestCopyFileRange(t *testing.T) {
tc := newTestCase(t, &testOptions{attrCache: true, entryCache: true})
defer tc.Clean()
if !tc.server.KernelSettings().SupportsVersion(7, 28) {
t.Skip("need v7.28 for CopyFileRange")
......@@ -398,8 +393,7 @@ func TestParallelDiropsHang(t *testing.T) {
}
func TestRoMount(t *testing.T) {
tc := newTestCase(t, &testOptions{ro: true})
defer tc.Clean()
newTestCase(t, &testOptions{ro: true})
}
func TestDirectMount(t *testing.T) {
......@@ -410,6 +404,5 @@ func TestDirectMount(t *testing.T) {
t.Log("running as root, setting DirectMountStrict")
opts.directMountStrict = true
}
tc := newTestCase(t, opts)
defer tc.Clean()
newTestCase(t, opts)
}
......@@ -152,8 +152,7 @@ func TestMaxWrite(t *testing.T) {
root := &maxWriteTestRoot{}
root.resetStats()
mntDir, srv, clean := testMount(t, root, &Options{MountOptions: tc})
defer clean()
mntDir, srv := testMount(t, root, &Options{MountOptions: tc})
readAheadWant := tc.MaxReadAhead
if readAheadWant == 0 {
......
......@@ -21,7 +21,7 @@ import (
"github.com/hanwen/go-fuse/v2/internal/testutil"
)
func testMount(t *testing.T, root InodeEmbedder, opts *Options) (string, *fuse.Server, func()) {
func testMount(t *testing.T, root InodeEmbedder, opts *Options) (string, *fuse.Server) {
t.Helper()
mntDir := testutil.TempDir()
......@@ -36,20 +36,21 @@ func testMount(t *testing.T, root InodeEmbedder, opts *Options) (string, *fuse.S
if err != nil {
t.Fatal(err)
}
return mntDir, server, func() {
t.Cleanup(func() {
if err := server.Unmount(); err != nil {
t.Fatalf("testMount: Unmount failed: %v", err)
}
if err := syscall.Rmdir(mntDir); err != nil {
t.Errorf("testMount: Remove failed: %v", err)
}
}
})
return mntDir, server
}
func TestDefaultOwner(t *testing.T) {
want := "hello"
root := &Inode{}
mntDir, _, clean := testMount(t, root, &Options{
mntDir, _ := testMount(t, root, &Options{
FirstAutomaticIno: 1,
OnAdd: func(ctx context.Context) {
n := root.EmbeddedInode()
......@@ -64,7 +65,6 @@ func TestDefaultOwner(t *testing.T) {
UID: 42,
GID: 43,
})
defer clean()
var st syscall.Stat_t
if err := syscall.Lstat(mntDir+"/file", &st); err != nil {
......@@ -78,13 +78,12 @@ func TestRootInode(t *testing.T) {
var rootIno uint64 = 42
root := &Inode{}
mntDir, _, clean := testMount(t, root, &Options{
mntDir, _ := testMount(t, root, &Options{
RootStableAttr: &StableAttr{
Ino: rootIno,
Gen: 1,
},
})
defer clean()
var st syscall.Stat_t
if err := syscall.Lstat(mntDir, &st); err != nil {
......@@ -97,7 +96,7 @@ func TestRootInode(t *testing.T) {
func TestDataFile(t *testing.T) {
want := "hello"
root := &Inode{}
mntDir, _, clean := testMount(t, root, &Options{
mntDir, _ := testMount(t, root, &Options{
FirstAutomaticIno: 1,
OnAdd: func(ctx context.Context) {
n := root.EmbeddedInode()
......@@ -113,7 +112,6 @@ func TestDataFile(t *testing.T) {
n.AddChild("file", ch, false)
},
})
defer clean()
var st syscall.Stat_t
if err := syscall.Lstat(mntDir+"/file", &st); err != nil {
......@@ -165,7 +163,7 @@ func TestDataFileLargeRead(t *testing.T) {
data := make([]byte, 256*1024)
rand.Read(data[:])
mntDir, _, clean := testMount(t, root, &Options{
mntDir, _ := testMount(t, root, &Options{
FirstAutomaticIno: 1,
OnAdd: func(ctx context.Context) {
n := root.EmbeddedInode()
......@@ -181,7 +179,6 @@ func TestDataFileLargeRead(t *testing.T) {
n.AddChild("file", ch, false)
},
})
defer clean()
got, err := ioutil.ReadFile(mntDir + "/file")
if err != nil {
t.Fatalf("ReadFile: %v", err)
......@@ -208,8 +205,7 @@ func (s *SymlinkerRoot) Symlink(ctx context.Context, target, name string, out *f
func TestDataSymlink(t *testing.T) {
root := &SymlinkerRoot{}
mntDir, _, clean := testMount(t, root, nil)
defer clean()
mntDir, _ := testMount(t, root, nil)
if err := syscall.Symlink("target", mntDir+"/link"); err != nil {
t.Fatalf("Symlink: %v", err)
......@@ -227,7 +223,7 @@ func TestReaddirplusParallel(t *testing.T) {
N := 100
oneSec := time.Second
names := map[string]int64{}
mntDir, _, clean := testMount(t, root, &Options{
mntDir, _ := testMount(t, root, &Options{
FirstAutomaticIno: 1,
EntryTimeout: &oneSec,
AttrTimeout: &oneSec,
......@@ -248,7 +244,6 @@ func TestReaddirplusParallel(t *testing.T) {
}
},
})
defer clean()
read := func() (map[string]int64, error) {
es, err := os.ReadDir(mntDir)
......
......@@ -55,8 +55,7 @@ func (fn *randomTypeTest) Readdir(ctx context.Context) (DirStream, syscall.Errno
func TestReaddirTypeFixup(t *testing.T) {
root := &randomTypeTest{}
mntDir, _, clean := testMount(t, root, nil)
defer clean()
mntDir, _ := testMount(t, root, nil)
f, err := os.Open(mntDir)
if err != nil {
......
......@@ -16,8 +16,7 @@ import (
func TestReadonlyCreate(t *testing.T) {
root := &Inode{}
mntDir, _, clean := testMount(t, root, nil)
defer clean()
mntDir, _ := testMount(t, root, nil)
_, err := syscall.Creat(mntDir+"/test", 0644)
if want := syscall.EROFS; want != err {
......@@ -28,7 +27,7 @@ func TestReadonlyCreate(t *testing.T) {
func TestDefaultPermissions(t *testing.T) {
root := &Inode{}
mntDir, _, clean := testMount(t, root, &Options{
mntDir, _ := testMount(t, root, &Options{
OnAdd: func(ctx context.Context) {
dir := root.NewPersistentInode(ctx, &Inode{}, StableAttr{Mode: syscall.S_IFDIR})
file := root.NewPersistentInode(ctx, &Inode{}, StableAttr{Mode: syscall.S_IFREG})
......@@ -37,7 +36,6 @@ func TestDefaultPermissions(t *testing.T) {
root.AddChild("file", file, false)
},
})
defer clean()
for k, v := range map[string]uint32{
"dir": fuse.S_IFDIR | 0755,
......
......@@ -16,7 +16,7 @@ import (
func TestRmChildParallel(t *testing.T) {
want := "hello"
root := &Inode{}
_, _, clean := testMount(t, root, &Options{
testMount(t, root, &Options{
FirstAutomaticIno: 1,
OnAdd: func(ctx context.Context) {
n := root.EmbeddedInode()
......@@ -53,5 +53,4 @@ func TestRmChildParallel(t *testing.T) {
wg.Wait()
},
})
defer clean()
}
......@@ -46,7 +46,7 @@ func (tc *testCase) writeOrig(path, content string, mode os.FileMode) {
}
}
func (tc *testCase) Clean() {
func (tc *testCase) clean() {
if err := tc.server.Unmount(); err != nil {
tc.Fatal(err)
}
......@@ -130,12 +130,13 @@ func newTestCase(t *testing.T, opts *testOptions) *testCase {
if err := tc.server.WaitMount(); err != nil {
t.Fatal(err)
}
t.Cleanup(tc.clean)
return tc
}
func TestBasic(t *testing.T) {
tc := newTestCase(t, &testOptions{attrCache: true, entryCache: true})
defer tc.Clean()
tc.writeOrig("file", "hello", 0644)
......@@ -169,15 +170,10 @@ func TestFileFdLeak(t *testing.T) {
attrCache: true,
entryCache: true,
})
defer func() {
if tc != nil {
tc.Clean()
}
}()
posixtest.FdLeak(t, tc.mntDir)
tc.Clean()
tc.clean()
bridge := tc.rawFS.(*rawBridge)
tc = nil
......@@ -188,7 +184,6 @@ func TestFileFdLeak(t *testing.T) {
func TestNotifyEntry(t *testing.T) {
tc := newTestCase(t, &testOptions{attrCache: true, entryCache: true})
defer tc.Clean()
orig := tc.origDir + "/file"
fn := tc.mntDir + "/file"
......@@ -221,7 +216,6 @@ func TestNotifyEntry(t *testing.T) {
func TestReadDirStress(t *testing.T) {
tc := newTestCase(t, &testOptions{suppressDebug: true, attrCache: true, entryCache: true})
defer tc.Clean()
// Create 110 entries
for i := 0; i < 110; i++ {
......@@ -262,7 +256,6 @@ func TestReadDirStress(t *testing.T) {
// runs, we may see spurious differences between the two statfs() calls.
func TestStatFs(t *testing.T) {
tc := newTestCase(t, &testOptions{attrCache: true, entryCache: true})
defer tc.Clean()
empty := syscall.Statfs_t{}
orig := empty
......@@ -290,7 +283,6 @@ func TestGetAttrParallel(t *testing.T) {
// (f)stat in parallel don't lead to fstat on closed files.
// We can only test that if we switch off caching
tc := newTestCase(t, &testOptions{suppressDebug: true})
defer tc.Clean()
N := 100
......@@ -331,7 +323,6 @@ func TestGetAttrParallel(t *testing.T) {
func TestMknod(t *testing.T) {
tc := newTestCase(t, &testOptions{})
defer tc.Clean()
modes := map[string]uint32{
"regular": syscall.S_IFREG,
......@@ -396,7 +387,6 @@ func TestPosix(t *testing.T) {
entryCache: true,
enableLocks: true,
})
defer tc.Clean()
fn(t, tc.mntDir)
})
......@@ -424,7 +414,6 @@ func TestOpenDirectIO(t *testing.T) {
}
tc := newTestCase(t, &opts)
defer tc.Clean()
posixtest.DirectIO(t, tc.mntDir)
}
......@@ -440,7 +429,6 @@ func TestOpenDirectIO(t *testing.T) {
// distributions, and tmpfs does not reuse inode numbers, hiding the problem.
func TestFsstress(t *testing.T) {
tc := newTestCase(t, &testOptions{suppressDebug: true, attrCache: true, entryCache: true})
defer tc.Clean()
{
old := runtime.GOMAXPROCS(100)
......@@ -653,7 +641,6 @@ func TestFsstress(t *testing.T) {
func TestStaleHardlinks(t *testing.T) {
// Disable all caches we can disable
tc := newTestCase(t, &testOptions{attrCache: false, entryCache: false})
defer tc.Clean()
// "link0" is original file
link0 := tc.mntDir + "/link0"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment