2020-05-14 23:28:08 +08:00
|
|
|
package mountlib_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/rclone/rclone/backend/local"
|
|
|
|
_ "github.com/rclone/rclone/cmd/cmount"
|
|
|
|
_ "github.com/rclone/rclone/cmd/mount"
|
|
|
|
_ "github.com/rclone/rclone/cmd/mount2"
|
2021-01-03 08:05:52 +08:00
|
|
|
"github.com/rclone/rclone/cmd/mountlib"
|
2021-03-10 23:40:34 +08:00
|
|
|
"github.com/rclone/rclone/fs/config/configfile"
|
2020-05-14 23:28:08 +08:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2022-01-27 17:38:24 +08:00
|
|
|
"github.com/rclone/rclone/fstest/testy"
|
2020-05-14 23:28:08 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRc(t *testing.T) {
|
2022-01-27 17:38:24 +08:00
|
|
|
// Disable tests under macOS and the CI since they are locking up
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
testy.SkipUnreliable(t)
|
|
|
|
}
|
2020-05-14 23:28:08 +08:00
|
|
|
ctx := context.Background()
|
2021-04-27 05:37:49 +08:00
|
|
|
configfile.Install()
|
2020-05-14 23:28:08 +08:00
|
|
|
mount := rc.Calls.Get("mount/mount")
|
|
|
|
assert.NotNil(t, mount)
|
|
|
|
unmount := rc.Calls.Get("mount/unmount")
|
|
|
|
assert.NotNil(t, unmount)
|
|
|
|
getMountTypes := rc.Calls.Get("mount/types")
|
|
|
|
assert.NotNil(t, getMountTypes)
|
|
|
|
|
2022-01-30 00:24:56 +08:00
|
|
|
localDir := t.TempDir()
|
2022-08-20 22:38:02 +08:00
|
|
|
err := os.WriteFile(filepath.Join(localDir, "file.txt"), []byte("hello"), 0666)
|
2020-05-14 23:28:08 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-30 00:24:56 +08:00
|
|
|
mountPoint := t.TempDir()
|
2020-05-14 23:28:08 +08:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Windows requires the mount point not to exist
|
|
|
|
require.NoError(t, os.RemoveAll(mountPoint))
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := getMountTypes.Fn(ctx, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
var mountTypes []string
|
|
|
|
|
|
|
|
err = out.GetStruct("mountTypes", &mountTypes)
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Logf("Mount types %v", mountTypes)
|
|
|
|
|
|
|
|
t.Run("Errors", func(t *testing.T) {
|
|
|
|
_, err := mount.Fn(ctx, rc.Params{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
_, err = mount.Fn(ctx, rc.Params{"fs": "/tmp"})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
_, err = mount.Fn(ctx, rc.Params{"mountPoint": "/tmp"})
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Mount", func(t *testing.T) {
|
|
|
|
if len(mountTypes) == 0 {
|
|
|
|
t.Skip("Can't mount")
|
|
|
|
}
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": localDir,
|
|
|
|
"mountPoint": mountPoint,
|
2020-07-23 01:31:54 +08:00
|
|
|
"vfsOpt": rc.Params{
|
|
|
|
"FilePerms": 0400,
|
|
|
|
},
|
2020-05-14 23:28:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// check file.txt is not there
|
|
|
|
filePath := filepath.Join(mountPoint, "file.txt")
|
|
|
|
_, err := os.Stat(filePath)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, os.IsNotExist(err))
|
|
|
|
|
|
|
|
// mount
|
|
|
|
_, err = mount.Fn(ctx, in)
|
2020-05-28 20:11:42 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Skipf("Mount failed - skipping test: %v", err)
|
|
|
|
}
|
2020-05-14 23:28:08 +08:00
|
|
|
|
|
|
|
// check file.txt is there now
|
|
|
|
fi, err := os.Stat(filePath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(5), fi.Size())
|
2020-07-23 01:31:54 +08:00
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
assert.Equal(t, os.FileMode(0400), fi.Mode())
|
|
|
|
}
|
2020-05-14 23:28:08 +08:00
|
|
|
|
2021-01-03 08:05:52 +08:00
|
|
|
// check mount point list
|
|
|
|
checkMountList := func() []mountlib.MountInfo {
|
|
|
|
listCall := rc.Calls.Get("mount/listmounts")
|
|
|
|
require.NotNil(t, listCall)
|
|
|
|
listReply, err := listCall.Fn(ctx, rc.Params{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
mountPointsReply, err := listReply.Get("mountPoints")
|
|
|
|
require.NoError(t, err)
|
|
|
|
mountPoints, ok := mountPointsReply.([]mountlib.MountInfo)
|
|
|
|
require.True(t, ok)
|
|
|
|
return mountPoints
|
|
|
|
}
|
|
|
|
mountPoints := checkMountList()
|
|
|
|
require.Equal(t, 1, len(mountPoints))
|
|
|
|
require.Equal(t, mountPoint, mountPoints[0].MountPoint)
|
|
|
|
|
2020-05-20 18:39:20 +08:00
|
|
|
// FIXME the OS sometimes appears to be using the mount
|
2020-05-14 23:28:08 +08:00
|
|
|
// immediately after it appears so wait a moment
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
t.Run("Unmount", func(t *testing.T) {
|
|
|
|
_, err := unmount.Fn(ctx, in)
|
|
|
|
require.NoError(t, err)
|
2021-01-03 08:05:52 +08:00
|
|
|
assert.Equal(t, 0, len(checkMountList()))
|
2020-05-14 23:28:08 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|