Commit 8363cf88 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

internal/testutil: drop TempDir

Change-Id: I9357f137487d1c958a95ed46db081b7775604b6b
parent bd47fe9b
......@@ -25,7 +25,10 @@ import (
func setupFs(node fs.InodeEmbedder, N int) (string, func()) {
opts := &fs.Options{}
opts.Debug = testutil.VerboseTest()
mountPoint := testutil.TempDir()
mountPoint, err := os.MkdirTemp("", "")
if err != nil {
log.Panicf("TempDir: %v", err)
}
server, err := fs.Mount(mountPoint, node, opts)
if err != nil {
log.Panicf("cannot mount %v", err)
......@@ -53,8 +56,6 @@ func setupFs(node fs.InodeEmbedder, N int) (string, func()) {
err := server.Unmount()
if err != nil {
log.Println("error during unmount", err)
} else {
os.RemoveAll(mountPoint)
}
}
}
......@@ -251,8 +252,10 @@ func BenchmarkCFuseThreadedStat(b *testing.B) {
}
f.Close()
mountPoint := testutil.TempDir()
defer os.RemoveAll(mountPoint)
mountPoint, err := os.MkdirTemp("", "")
if err != nil {
b.Fatalf("MkdirTemp: %v", err)
}
cmd := exec.Command(wd+"/cstatfs",
"-o",
......
......@@ -301,7 +301,7 @@ func waitMount(mnt string) error {
func TestParallelDiropsHang(t *testing.T) {
// We do NOT want to use newTestCase() here because we need to know the
// mnt path before the filesystem is mounted
dir := testutil.TempDir()
dir := t.TempDir()
orig := dir + "/orig"
mnt := dir + "/mnt"
if err := os.Mkdir(orig, 0755); err != nil {
......@@ -310,7 +310,6 @@ func TestParallelDiropsHang(t *testing.T) {
if err := os.Mkdir(mnt, 0755); err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
// Unblock the goroutines onces the mount shows up in /proc/self/mounts
wait := make(chan struct{})
......
......@@ -24,7 +24,7 @@ import (
func testMount(t *testing.T, root InodeEmbedder, opts *Options) (string, *fuse.Server) {
t.Helper()
mntDir := testutil.TempDir()
mntDir := t.TempDir()
if opts == nil {
opts = &Options{
FirstAutomaticIno: 1,
......@@ -40,9 +40,6 @@ func testMount(t *testing.T, root InodeEmbedder, opts *Options) (string, *fuse.S
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
}
......
......@@ -50,9 +50,6 @@ func (tc *testCase) clean() {
if err := tc.server.Unmount(); err != nil {
tc.Fatal(err)
}
if err := os.RemoveAll(tc.dir); err != nil {
tc.Fatal(err)
}
}
type testOptions struct {
......@@ -73,7 +70,7 @@ func newTestCase(t *testing.T, opts *testOptions) *testCase {
opts = &testOptions{}
}
if opts.testDir == "" {
opts.testDir = testutil.TempDir()
opts.testDir = t.TempDir()
}
tc := &testCase{
dir: opts.testDir,
......@@ -356,8 +353,7 @@ func TestMknod(t *testing.T) {
}
func TestMknodNotSupported(t *testing.T) {
mountPoint := testutil.TempDir()
defer os.Remove(mountPoint)
mountPoint := t.TempDir()
server, err := Mount(mountPoint, &Inode{}, nil)
if err != nil {
......
......@@ -6,17 +6,12 @@ package pathfs
import (
"io/ioutil"
"os"
"testing"
"github.com/hanwen/go-fuse/v2/internal/testutil"
)
func TestCopyFile(t *testing.T) {
d1 := testutil.TempDir()
defer os.RemoveAll(d1)
d2 := testutil.TempDir()
defer os.RemoveAll(d2)
d1 := t.TempDir()
d2 := t.TempDir()
fs1 := NewLoopbackFileSystem(d1)
fs2 := NewLoopbackFileSystem(d2)
......
......@@ -35,7 +35,7 @@ func (fs *ownerFs) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse
}
func setupOwnerTest(t *testing.T, opts *nodefs.Options) (workdir string, cleanup func()) {
wd := testutil.TempDir()
wd := t.TempDir()
opts.Debug = testutil.VerboseTest()
fs := &ownerFs{NewDefaultFileSystem()}
......@@ -50,7 +50,6 @@ func setupOwnerTest(t *testing.T, opts *nodefs.Options) (workdir string, cleanup
}
return wd, func() {
state.Unmount()
os.RemoveAll(wd)
}
}
......
......@@ -16,7 +16,6 @@ import (
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/hanwen/go-fuse/v2/fuse/nodefs"
"github.com/hanwen/go-fuse/v2/internal/testutil"
)
var xattrGolden = map[string][]byte{
......@@ -103,7 +102,7 @@ func (fs *XAttrTestFs) RemoveXAttr(name string, attr string, context *fuse.Conte
func xattrTestCase(t *testing.T, nm string, m map[string][]byte) (mountPoint string, cleanup func()) {
xfs := NewXAttrFs(nm, m)
mountPoint = testutil.TempDir()
mountPoint = t.TempDir()
nfs := NewPathNodeFs(xfs, nil)
state, _, err := nodefs.MountRoot(mountPoint, nfs.Root(),
......@@ -115,7 +114,6 @@ func xattrTestCase(t *testing.T, nm string, m map[string][]byte) (mountPoint str
go state.Serve()
return mountPoint, func() {
state.Unmount()
os.RemoveAll(mountPoint)
}
}
......
......@@ -37,7 +37,7 @@ func (fs *cacheFs) Open(name string, flags uint32, context *fuse.Context) (fuseF
}
func setupCacheTest(t *testing.T) (string, *pathfs.PathNodeFs, func()) {
dir := testutil.TempDir()
dir := t.TempDir()
os.Mkdir(dir+"/mnt", 0755)
os.Mkdir(dir+"/orig", 0755)
......@@ -65,10 +65,7 @@ func setupCacheTest(t *testing.T) (string, *pathfs.PathNodeFs, func()) {
t.Fatal("WaitMount", err)
}
return dir, pfs, func() {
err := state.Unmount()
if err == nil {
os.RemoveAll(dir)
}
state.Unmount()
}
}
......@@ -186,8 +183,7 @@ func TestNonseekable(t *testing.T) {
fs := &nonseekFs{FileSystem: pathfs.NewDefaultFileSystem()}
fs.Length = 200 * 1024
dir := testutil.TempDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
nfs := pathfs.NewPathNodeFs(fs, nil)
opts := nodefs.NewOptions()
opts.Debug = testutil.VerboseTest()
......@@ -216,8 +212,7 @@ func TestNonseekable(t *testing.T) {
}
func TestGetAttrRace(t *testing.T) {
dir := testutil.TempDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
os.Mkdir(dir+"/mnt", 0755)
os.Mkdir(dir+"/orig", 0755)
......
......@@ -52,13 +52,7 @@ func (d *DataNode) Read(_ nodefs.File, dest []byte, off int64, _ *fuse.Context)
// TestCacheControl verifies that FUSE server process can store/retrieve kernel data cache.
func TestCacheControl(t *testing.T) {
dir := testutil.TempDir()
defer func() {
err := os.Remove(dir)
if err != nil {
t.Fatal(err)
}
}()
dir := t.TempDir()
// setup a filesystem with 1 file
root := nodefs.NewDefaultNode()
......
......@@ -16,8 +16,7 @@ import (
)
func TestDefaultNodeGetAttr(t *testing.T) {
dir := testutil.TempDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
opts := &nodefs.Options{
// Note: defaultNode.GetAttr() calling file.GetAttr() is only useful if
......
......@@ -6,7 +6,6 @@ package test
import (
"io/ioutil"
"os"
"testing"
"github.com/hanwen/go-fuse/v2/fuse"
......@@ -42,7 +41,7 @@ func defaultReadTest(t *testing.T) (root string, cleanup func()) {
}
var err error
dir := testutil.TempDir()
dir := t.TempDir()
pathfs := pathfs.NewPathNodeFs(fs, nil)
opts := nodefs.NewOptions()
opts.Debug = testutil.VerboseTest()
......@@ -57,7 +56,6 @@ func defaultReadTest(t *testing.T) (root string, cleanup func()) {
}
return dir, func() {
state.Unmount()
os.Remove(dir)
}
}
......
......@@ -34,8 +34,7 @@ func (f *flipNode) GetAttr(out *fuse.Attr, file nodefs.File, c *fuse.Context) fu
}
func TestDeleteNotify(t *testing.T) {
dir := testutil.TempDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
root := nodefs.NewMemNodeFSRoot(dir + "/backing")
conn := nodefs.NewFileSystemConnector(root,
&nodefs.Options{PortableInodes: true})
......
......@@ -123,8 +123,7 @@ func TestFlockInvoked(t *testing.T) {
t.Skip("flock command not found.")
}
dir := testutil.TempDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
opts := &nodefs.Options{
Owner: fuse.CurrentOwner(),
......@@ -188,17 +187,8 @@ func TestNoLockSupport(t *testing.T) {
t.Skip("flock command not found.")
}
tmp, err := ioutil.TempDir("", "TestNoLockSupport")
if err != nil {
t.Fatal(err)
}
mnt, err := ioutil.TempDir("", "TestNoLockSupport")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
defer os.RemoveAll(mnt)
tmp := t.TempDir()
mnt := t.TempDir()
opts := &nodefs.Options{
Owner: fuse.CurrentOwner(),
......
......@@ -143,7 +143,7 @@ func NewFile() *MutableDataFile {
}
func setupFAttrTest(t *testing.T, fs pathfs.FileSystem) (dir string, clean func()) {
dir = testutil.TempDir()
dir = t.TempDir()
nfs := pathfs.NewPathNodeFs(fs, nil)
opts := nodefs.NewOptions()
opts.Debug = testutil.VerboseTest()
......@@ -161,8 +161,6 @@ func setupFAttrTest(t *testing.T, fs pathfs.FileSystem) (dir string, clean func(
clean = func() {
if err := state.Unmount(); err != nil {
t.Errorf("cleanup: Unmount: %v", err)
} else {
os.RemoveAll(dir)
}
}
......
......@@ -17,7 +17,6 @@ import (
"golang.org/x/sys/unix"
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/hanwen/go-fuse/v2/internal/testutil"
)
var enableOverlayfsTest bool
......@@ -147,11 +146,9 @@ func TestOverlayfs(t *testing.T) {
tc.Mkdir(tc.origSubdir, 0777)
tc.WriteFile(filepath.Join(tc.origSubdir, testfile), content, 0700)
tmpMergedDir := testutil.TempDir()
defer os.RemoveAll(tmpMergedDir)
tmpWorkDir := testutil.TempDir()
defer os.RemoveAll(tmpWorkDir)
tmpUpperDir := testutil.TempDir()
tmpMergedDir := t.TempDir()
tmpWorkDir := t.TempDir()
tmpUpperDir := t.TempDir()
defer os.RemoveAll(tmpUpperDir)
if err := unix.Mount("overlay", tmpMergedDir, "overlay", 0,
fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", tc.mnt, tmpUpperDir, tmpWorkDir)); err != nil {
......
......@@ -77,7 +77,7 @@ func NewTestCase(t *testing.T) *testCase {
const subdir string = "subdir"
var err error
tc.tmpDir = testutil.TempDir()
tc.tmpDir = t.TempDir()
tc.orig = tc.tmpDir + "/orig"
tc.mnt = tc.tmpDir + "/mnt"
......@@ -125,7 +125,6 @@ func (tc *testCase) Cleanup() {
if err != nil {
tc.tester.Fatalf("Unmount failed: %v", err)
}
os.RemoveAll(tc.tmpDir)
}
func (tc *testCase) rootNode() *nodefs.Inode {
......@@ -774,8 +773,7 @@ func TestNonVerboseFStatFs(t *testing.T) {
}
func TestOriginalIsSymlink(t *testing.T) {
tmpDir := testutil.TempDir()
defer os.RemoveAll(tmpDir)
tmpDir := t.TempDir()
orig := tmpDir + "/orig"
err := os.Mkdir(orig, 0755)
if err != nil {
......
......@@ -15,7 +15,6 @@ import (
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/hanwen/go-fuse/v2/fuse/nodefs"
"github.com/hanwen/go-fuse/v2/fuse/pathfs"
"github.com/hanwen/go-fuse/v2/internal/testutil"
)
func TestMountOnExisting(t *testing.T) {
......@@ -171,8 +170,7 @@ func TestDeletedUnmount(t *testing.T) {
}
func TestDefaultNodeMount(t *testing.T) {
dir := testutil.TempDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
root := nodefs.NewDefaultNode()
s, conn, err := nodefs.MountRoot(dir, root, nil)
if err != nil {
......@@ -198,8 +196,7 @@ func TestDefaultNodeMount(t *testing.T) {
}
func TestLiveness(t *testing.T) {
dir := testutil.TempDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
root := nodefs.NewDefaultNode()
s, _, err := nodefs.MountRoot(dir, root, nil)
if err != nil {
......
......@@ -28,13 +28,7 @@ func (d *truncatableFile) Truncate(file nodefs.File, size uint64, context *fuse.
// TestNilFileTruncation verifies that the FUSE server process does not
// crash when file truncation is performed on nil file handles.
func TestNilFileTruncation(t *testing.T) {
dir := testutil.TempDir()
defer func() {
err := os.Remove(dir)
if err != nil {
t.Fatal(err)
}
}()
dir := t.TempDir()
root := nodefs.NewDefaultNode()
opts := nodefs.NewOptions()
......
......@@ -11,7 +11,6 @@ import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"
"golang.org/x/sync/errgroup"
......@@ -58,13 +57,7 @@ func verifyFileRead(path string, dataOK string) error {
}
func TestNodeParallelLookup(t *testing.T) {
dir := testutil.TempDir()
defer func() {
err := os.Remove(dir)
if err != nil {
t.Fatal(err)
}
}()
dir := t.TempDir()
root := &tRoot{
Node: nodefs.NewDefaultNode(),
......
......@@ -61,7 +61,7 @@ func (n *rootNode) Lookup(out *fuse.Attr, name string, context *fuse.Context) (*
}
func TestUpdateNode(t *testing.T) {
dir := testutil.TempDir()
dir := t.TempDir()
root := &rootNode{
Node: nodefs.NewDefaultNode(),
backing: map[string]string{"a": "aaa"},
......
......@@ -6,7 +6,6 @@ package test
import (
"io/ioutil"
"os"
"sync/atomic"
"testing"
......@@ -51,13 +50,7 @@ func (d *NoFileNode) Open(flags uint32, context *fuse.Context) (nodefs.File, fus
}
func TestNoFile(t *testing.T) {
dir := testutil.TempDir()
defer func() {
err := os.Remove(dir)
if err != nil {
t.Fatal(err)
}
}()
dir := t.TempDir()
// setup a filesystem with 2 files:
//
......
......@@ -78,7 +78,7 @@ type NotifyTest struct {
func NewNotifyTest(t *testing.T) *NotifyTest {
me := &NotifyTest{}
me.fs = newNotifyFs()
me.dir = testutil.TempDir()
me.dir = t.TempDir()
entryTTL := 100 * time.Millisecond
opts := &nodefs.Options{
EntryTimeout: entryTTL,
......
......@@ -34,7 +34,7 @@ func (fs *umaskFS) Mkdir(name string, mode uint32, context *fuse.Context) (code
}
func TestUmask(t *testing.T) {
tmpDir := testutil.TempDir()
tmpDir := t.TempDir()
orig := tmpDir + "/orig"
mnt := tmpDir + "/mnt"
......
......@@ -8,7 +8,6 @@
package test
import (
"os"
"path/filepath"
"syscall"
"testing"
......@@ -40,8 +39,7 @@ func (n *xattrChildNode) GetXAttr(attr string, context *fuse.Context) ([]byte, f
}
func TestDefaultXAttr(t *testing.T) {
dir := testutil.TempDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
root := &xattrNode{
Node: nodefs.NewDefaultNode(),
......@@ -70,8 +68,7 @@ func TestDefaultXAttr(t *testing.T) {
}
func TestEmptyXAttr(t *testing.T) {
dir := testutil.TempDir()
defer os.RemoveAll(dir)
dir := t.TempDir()
root := &xattrNode{
Node: nodefs.NewDefaultNode(),
......
// Copyright 2016 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package testutil
import (
"io/ioutil"
"log"
"runtime"
"strings"
)
// TempDir creates a temporary directory that includes the name of the
// testcase. Panics if there was an I/O problem creating the directory.
func TempDir() string {
frames := make([]uintptr, 10) // at least 1 entry needed
n := runtime.Callers(1, frames)
lastName := ""
for _, pc := range frames[:n] {
f := runtime.FuncForPC(pc)
name := f.Name()
i := strings.LastIndex(name, ".")
if i >= 0 {
name = name[i+1:]
}
if strings.HasPrefix(name, "Test") {
lastName = name
}
}
dir, err := ioutil.TempDir("", lastName)
if err != nil {
log.Panicf("TempDir(%s): %v", lastName, err)
}
return dir
}
......@@ -33,12 +33,11 @@ func (tc *testCase) Clean() {
tc.server.Unmount()
tc.server = nil
}
os.RemoveAll(tc.dir)
}
func newTestCase(t *testing.T, populate bool) *testCase {
t.Helper()
dir := testutil.TempDir()
dir := t.TempDir()
dirs := []string{"ro", "rw", "mnt"}
if populate {
dirs = append(dirs, "ro/dir")
......
......@@ -19,7 +19,7 @@ const testTtl = 100 * time.Millisecond
func setupMzfs(t *testing.T) (mountPoint string, state *fuse.Server, cleanup func()) {
root := &MultiZipFs{}
mountPoint = testutil.TempDir()
mountPoint = t.TempDir()
dt := testTtl
opts := &fs.Options{
......@@ -34,7 +34,6 @@ func setupMzfs(t *testing.T) (mountPoint string, state *fuse.Server, cleanup fun
}
return mountPoint, server, func() {
server.Unmount()
os.RemoveAll(mountPoint)
}
}
......
......@@ -67,8 +67,7 @@ func TestTar(t *testing.T) {
root := &tarRoot{rc: &addClose{buf}}
mnt := testutil.TempDir()
defer os.Remove(mnt)
mnt := t.TempDir()
opts := &fs.Options{}
opts.Debug = testutil.VerboseTest()
s, err := fs.Mount(mnt, root, opts)
......
......@@ -33,14 +33,13 @@ func setupZipfs(t *testing.T) (mountPoint string, cleanup func()) {
t.Fatalf("NewArchiveFileSystem failed: %v", err)
}
mountPoint = testutil.TempDir()
mountPoint = t.TempDir()
opts := &fs.Options{}
opts.Debug = testutil.VerboseTest()
server, err := fs.Mount(mountPoint, root, opts)
return mountPoint, func() {
server.Unmount()
os.RemoveAll(mountPoint)
}
}
......
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