2024-06-24 20:08:33 +08:00
|
|
|
//go:build unix
|
2023-10-05 01:33:12 +08:00
|
|
|
|
|
|
|
package nfsmount
|
|
|
|
|
|
|
|
import (
|
2024-10-12 00:26:29 +08:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"os"
|
2024-06-24 20:08:33 +08:00
|
|
|
"os/exec"
|
|
|
|
"runtime"
|
2023-10-05 01:33:12 +08:00
|
|
|
"testing"
|
|
|
|
|
2024-08-27 23:49:42 +08:00
|
|
|
"github.com/rclone/rclone/cmd/serve/nfs"
|
2023-10-05 01:33:12 +08:00
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
|
|
|
"github.com/rclone/rclone/vfs/vfstest"
|
2024-06-25 23:15:21 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-10-05 01:33:12 +08:00
|
|
|
)
|
|
|
|
|
2024-06-24 20:08:33 +08:00
|
|
|
// Return true if the command ran without error
|
|
|
|
func commandOK(name string, arg ...string) bool {
|
|
|
|
cmd := exec.Command(name, arg...)
|
|
|
|
_, err := cmd.CombinedOutput()
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 01:33:12 +08:00
|
|
|
func TestMount(t *testing.T) {
|
2024-06-24 20:08:33 +08:00
|
|
|
if runtime.GOOS != "darwin" {
|
|
|
|
if !commandOK("sudo", "-n", "mount", "--help") {
|
|
|
|
t.Skip("Can't run sudo mount without a password")
|
|
|
|
}
|
|
|
|
if !commandOK("sudo", "-n", "umount", "--help") {
|
|
|
|
t.Skip("Can't run sudo umount without a password")
|
|
|
|
}
|
|
|
|
sudo = true
|
|
|
|
}
|
2024-10-12 00:26:29 +08:00
|
|
|
for _, cacheType := range []string{"memory", "disk", "symlink"} {
|
|
|
|
t.Run(cacheType, func(t *testing.T) {
|
|
|
|
nfs.Opt.HandleCacheDir = t.TempDir()
|
|
|
|
require.NoError(t, nfs.Opt.HandleCache.Set(cacheType))
|
|
|
|
// Check we can create a handler
|
|
|
|
_, err := nfs.NewHandler(context.Background(), nil, &nfs.Opt)
|
|
|
|
if errors.Is(err, nfs.ErrorSymlinkCacheNotSupported) || errors.Is(err, nfs.ErrorSymlinkCacheNoPermission) {
|
|
|
|
t.Skip(err.Error() + ": run with: go test -c && sudo setcap cap_dac_read_search+ep ./nfsmount.test && ./nfsmount.test -test.v")
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
// Configure rclone via environment var since the mount gets run in a subprocess
|
|
|
|
_ = os.Setenv("RCLONE_NFS_CACHE_DIR", nfs.Opt.HandleCacheDir)
|
|
|
|
_ = os.Setenv("RCLONE_NFS_CACHE_TYPE", cacheType)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
_ = os.Unsetenv("RCLONE_NFS_CACHE_DIR")
|
|
|
|
_ = os.Unsetenv("RCLONE_NFS_CACHE_TYPE")
|
|
|
|
})
|
|
|
|
vfstest.RunTests(t, false, vfscommon.CacheModeWrites, false, mount)
|
|
|
|
})
|
|
|
|
}
|
2023-10-05 01:33:12 +08:00
|
|
|
}
|