github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/cmd/mountlib/rc_test.go (about) 1 package mountlib_test 2 3 import ( 4 "context" 5 "os" 6 "path/filepath" 7 "runtime" 8 "testing" 9 "time" 10 11 _ "github.com/rclone/rclone/backend/local" 12 _ "github.com/rclone/rclone/cmd/cmount" 13 _ "github.com/rclone/rclone/cmd/mount" 14 _ "github.com/rclone/rclone/cmd/mount2" 15 "github.com/rclone/rclone/cmd/mountlib" 16 "github.com/rclone/rclone/fs/config/configfile" 17 "github.com/rclone/rclone/fs/rc" 18 "github.com/rclone/rclone/fstest/testy" 19 "github.com/stretchr/testify/assert" 20 "github.com/stretchr/testify/require" 21 ) 22 23 func TestRc(t *testing.T) { 24 // Disable tests under macOS and the CI since they are locking up 25 if runtime.GOOS == "darwin" { 26 testy.SkipUnreliable(t) 27 } 28 ctx := context.Background() 29 configfile.Install() 30 mount := rc.Calls.Get("mount/mount") 31 assert.NotNil(t, mount) 32 unmount := rc.Calls.Get("mount/unmount") 33 assert.NotNil(t, unmount) 34 getMountTypes := rc.Calls.Get("mount/types") 35 assert.NotNil(t, getMountTypes) 36 37 localDir := t.TempDir() 38 err := os.WriteFile(filepath.Join(localDir, "file.txt"), []byte("hello"), 0666) 39 require.NoError(t, err) 40 41 mountPoint := t.TempDir() 42 if runtime.GOOS == "windows" { 43 // Windows requires the mount point not to exist 44 require.NoError(t, os.RemoveAll(mountPoint)) 45 } 46 47 out, err := getMountTypes.Fn(ctx, nil) 48 require.NoError(t, err) 49 var mountTypes []string 50 51 err = out.GetStruct("mountTypes", &mountTypes) 52 require.NoError(t, err) 53 t.Logf("Mount types %v", mountTypes) 54 55 t.Run("Errors", func(t *testing.T) { 56 _, err := mount.Fn(ctx, rc.Params{}) 57 assert.Error(t, err) 58 59 _, err = mount.Fn(ctx, rc.Params{"fs": "/tmp"}) 60 assert.Error(t, err) 61 62 _, err = mount.Fn(ctx, rc.Params{"mountPoint": "/tmp"}) 63 assert.Error(t, err) 64 }) 65 66 t.Run("Mount", func(t *testing.T) { 67 if len(mountTypes) == 0 { 68 t.Skip("Can't mount") 69 } 70 in := rc.Params{ 71 "fs": localDir, 72 "mountPoint": mountPoint, 73 "vfsOpt": rc.Params{ 74 "FilePerms": 0400, 75 }, 76 } 77 78 // check file.txt is not there 79 filePath := filepath.Join(mountPoint, "file.txt") 80 _, err := os.Stat(filePath) 81 require.Error(t, err) 82 require.True(t, os.IsNotExist(err)) 83 84 // mount 85 _, err = mount.Fn(ctx, in) 86 if err != nil { 87 t.Skipf("Mount failed - skipping test: %v", err) 88 } 89 90 // check file.txt is there now 91 fi, err := os.Stat(filePath) 92 require.NoError(t, err) 93 assert.Equal(t, int64(5), fi.Size()) 94 if runtime.GOOS == "linux" { 95 assert.Equal(t, os.FileMode(0400), fi.Mode()) 96 } 97 98 // check mount point list 99 checkMountList := func() []mountlib.MountInfo { 100 listCall := rc.Calls.Get("mount/listmounts") 101 require.NotNil(t, listCall) 102 listReply, err := listCall.Fn(ctx, rc.Params{}) 103 require.NoError(t, err) 104 mountPointsReply, err := listReply.Get("mountPoints") 105 require.NoError(t, err) 106 mountPoints, ok := mountPointsReply.([]mountlib.MountInfo) 107 require.True(t, ok) 108 return mountPoints 109 } 110 mountPoints := checkMountList() 111 require.Equal(t, 1, len(mountPoints)) 112 require.Equal(t, mountPoint, mountPoints[0].MountPoint) 113 114 // FIXME the OS sometimes appears to be using the mount 115 // immediately after it appears so wait a moment 116 time.Sleep(100 * time.Millisecond) 117 118 t.Run("Unmount", func(t *testing.T) { 119 _, err := unmount.Fn(ctx, in) 120 require.NoError(t, err) 121 assert.Equal(t, 0, len(checkMountList())) 122 }) 123 }) 124 }