2016-07-17 22:03:23 +00:00
|
|
|
// Test suite for rclonefs
|
|
|
|
|
2020-04-16 12:33:46 +00:00
|
|
|
package vfstest
|
2016-07-17 22:03:23 +00:00
|
|
|
|
|
|
|
import (
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2016-07-17 22:03:23 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2020-01-08 17:14:35 +00:00
|
|
|
"io"
|
2016-07-17 22:03:23 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
2017-11-18 11:59:56 +00:00
|
|
|
"path/filepath"
|
2017-11-17 10:30:35 +00:00
|
|
|
"reflect"
|
2017-05-08 13:55:05 +00:00
|
|
|
"runtime"
|
2016-07-17 22:03:23 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-06-13 09:52:10 +00:00
|
|
|
"time"
|
2016-07-17 22:03:23 +00:00
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
_ "github.com/rclone/rclone/backend/all" // import all the backends
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/walk"
|
|
|
|
"github.com/rclone/rclone/fstest"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
2020-02-28 14:44:15 +00:00
|
|
|
"github.com/rclone/rclone/vfs/vfscommon"
|
2016-07-17 22:03:23 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-05-02 21:35:07 +00:00
|
|
|
type (
|
2017-05-08 17:05:12 +00:00
|
|
|
// UnmountFn is called to unmount the file system
|
2017-05-02 21:35:07 +00:00
|
|
|
UnmountFn func() error
|
2017-05-08 17:05:12 +00:00
|
|
|
// MountFn is called to mount the file system
|
2020-04-16 12:33:46 +00:00
|
|
|
MountFn func(f fs.Fs, mountpoint string) (vfs *vfs.VFS, unmountResult <-chan error, unmount func() error, err error)
|
2017-05-02 21:35:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mountFn MountFn
|
|
|
|
)
|
|
|
|
|
2018-04-08 10:18:15 +00:00
|
|
|
// RunTests runs all the tests against all the VFS cache modes
|
2020-04-16 12:33:46 +00:00
|
|
|
//
|
|
|
|
// If useVFS is set then it runs the tests against a VFS rather than amount
|
|
|
|
func RunTests(t *testing.T, useVFS bool, fn MountFn) {
|
2017-05-02 21:35:07 +00:00
|
|
|
mountFn = fn
|
2016-07-17 22:03:23 +00:00
|
|
|
flag.Parse()
|
2020-02-28 14:44:15 +00:00
|
|
|
cacheModes := []vfscommon.CacheMode{
|
|
|
|
vfscommon.CacheModeOff,
|
|
|
|
vfscommon.CacheModeMinimal,
|
|
|
|
vfscommon.CacheModeWrites,
|
|
|
|
vfscommon.CacheModeFull,
|
2017-11-07 18:03:23 +00:00
|
|
|
}
|
2020-04-16 12:33:46 +00:00
|
|
|
run = newRun(useVFS)
|
2017-11-07 18:03:23 +00:00
|
|
|
for _, cacheMode := range cacheModes {
|
2017-11-18 11:59:56 +00:00
|
|
|
run.cacheMode(cacheMode)
|
2017-11-07 18:03:23 +00:00
|
|
|
log.Printf("Starting test run with cache mode %v", cacheMode)
|
2018-04-08 10:18:15 +00:00
|
|
|
ok := t.Run(fmt.Sprintf("CacheMode=%v", cacheMode), func(t *testing.T) {
|
2018-05-24 18:45:11 +00:00
|
|
|
t.Run("TestTouchAndDelete", TestTouchAndDelete)
|
|
|
|
t.Run("TestRenameOpenHandle", TestRenameOpenHandle)
|
2018-04-08 10:18:15 +00:00
|
|
|
t.Run("TestDirLs", TestDirLs)
|
|
|
|
t.Run("TestDirCreateAndRemoveDir", TestDirCreateAndRemoveDir)
|
|
|
|
t.Run("TestDirCreateAndRemoveFile", TestDirCreateAndRemoveFile)
|
|
|
|
t.Run("TestDirRenameFile", TestDirRenameFile)
|
|
|
|
t.Run("TestDirRenameEmptyDir", TestDirRenameEmptyDir)
|
|
|
|
t.Run("TestDirRenameFullDir", TestDirRenameFullDir)
|
|
|
|
t.Run("TestDirModTime", TestDirModTime)
|
|
|
|
t.Run("TestDirCacheFlush", TestDirCacheFlush)
|
|
|
|
t.Run("TestDirCacheFlushOnDirRename", TestDirCacheFlushOnDirRename)
|
|
|
|
t.Run("TestFileModTime", TestFileModTime)
|
|
|
|
t.Run("TestFileModTimeWithOpenWriters", TestFileModTimeWithOpenWriters)
|
|
|
|
t.Run("TestMount", TestMount)
|
|
|
|
t.Run("TestRoot", TestRoot)
|
|
|
|
t.Run("TestReadByByte", TestReadByByte)
|
|
|
|
t.Run("TestReadChecksum", TestReadChecksum)
|
|
|
|
t.Run("TestReadFileDoubleClose", TestReadFileDoubleClose)
|
|
|
|
t.Run("TestReadSeek", TestReadSeek)
|
|
|
|
t.Run("TestWriteFileNoWrite", TestWriteFileNoWrite)
|
|
|
|
t.Run("TestWriteFileWrite", TestWriteFileWrite)
|
|
|
|
t.Run("TestWriteFileOverwrite", TestWriteFileOverwrite)
|
|
|
|
t.Run("TestWriteFileDoubleClose", TestWriteFileDoubleClose)
|
|
|
|
t.Run("TestWriteFileFsync", TestWriteFileFsync)
|
2019-10-06 20:05:21 +00:00
|
|
|
t.Run("TestWriteFileDup", TestWriteFileDup)
|
2019-10-15 00:57:15 +00:00
|
|
|
t.Run("TestWriteFileAppend", TestWriteFileAppend)
|
2018-04-08 10:18:15 +00:00
|
|
|
})
|
|
|
|
log.Printf("Finished test run with cache mode %v (ok=%v)", cacheMode, ok)
|
|
|
|
if !ok {
|
2017-11-07 18:03:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-11-18 11:59:56 +00:00
|
|
|
run.Finalise()
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run holds the remotes for a test run
|
|
|
|
type Run struct {
|
2020-04-16 12:33:46 +00:00
|
|
|
os Oser
|
2017-10-28 19:01:34 +00:00
|
|
|
vfs *vfs.VFS
|
2020-04-16 12:33:46 +00:00
|
|
|
useVFS bool // set if we are testing a VFS not a mount
|
2017-06-19 12:44:49 +00:00
|
|
|
mountPath string
|
|
|
|
fremote fs.Fs
|
|
|
|
fremoteName string
|
|
|
|
cleanRemote func()
|
|
|
|
umountResult <-chan error
|
|
|
|
umountFn UnmountFn
|
|
|
|
skip bool
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run holds the master Run data
|
|
|
|
var run *Run
|
|
|
|
|
|
|
|
// newRun initialise the remote mount for testing and returns a run
|
|
|
|
// object.
|
|
|
|
//
|
|
|
|
// r.fremote is an empty remote Fs
|
|
|
|
//
|
|
|
|
// Finalise() will tidy them away when done.
|
2020-04-16 12:33:46 +00:00
|
|
|
func newRun(useVFS bool) *Run {
|
2016-07-17 22:03:23 +00:00
|
|
|
r := &Run{
|
2020-04-16 12:33:46 +00:00
|
|
|
useVFS: useVFS,
|
2016-07-17 22:03:23 +00:00
|
|
|
umountResult: make(chan error, 1),
|
|
|
|
}
|
2017-07-24 21:46:43 +00:00
|
|
|
fstest.Initialise()
|
|
|
|
|
2016-07-17 22:03:23 +00:00
|
|
|
var err error
|
2019-08-08 18:58:02 +00:00
|
|
|
r.fremote, r.fremoteName, r.cleanRemote, err = fstest.RandomRemote()
|
2016-07-17 22:03:23 +00:00
|
|
|
if err != nil {
|
2017-07-24 21:46:43 +00:00
|
|
|
log.Fatalf("Failed to open remote %q: %v", *fstest.RemoteName, err)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 08:34:30 +00:00
|
|
|
err = r.fremote.Mkdir(context.Background(), "")
|
2016-11-05 09:57:45 +00:00
|
|
|
if err != nil {
|
2017-07-24 21:46:43 +00:00
|
|
|
log.Fatalf("Failed to open mkdir %q: %v", *fstest.RemoteName, err)
|
2016-11-05 09:57:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 12:33:46 +00:00
|
|
|
if !r.useVFS {
|
|
|
|
r.mountPath = findMountPath()
|
|
|
|
}
|
2018-05-28 15:49:26 +00:00
|
|
|
// Mount it up
|
|
|
|
r.mount()
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func findMountPath() string {
|
2017-05-08 13:55:05 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
2018-05-28 15:49:26 +00:00
|
|
|
mountPath, err := ioutil.TempDir("", "rclonefs-mount")
|
2017-05-08 13:55:05 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create mount dir: %v", err)
|
|
|
|
}
|
2018-05-28 15:49:26 +00:00
|
|
|
return mountPath
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 15:49:26 +00:00
|
|
|
// Find a free drive letter
|
|
|
|
drive := ""
|
|
|
|
for letter := 'E'; letter <= 'Z'; letter++ {
|
|
|
|
drive = string(letter) + ":"
|
|
|
|
_, err := os.Stat(drive + "\\")
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
goto found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Fatalf("Couldn't find free drive letter for test")
|
|
|
|
found:
|
|
|
|
return drive
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Run) mount() {
|
|
|
|
log.Printf("mount %q %q", r.fremote, r.mountPath)
|
|
|
|
var err error
|
2017-10-28 19:01:34 +00:00
|
|
|
r.vfs, r.umountResult, r.umountFn, err = mountFn(r.fremote, r.mountPath)
|
2016-07-17 22:03:23 +00:00
|
|
|
if err != nil {
|
2017-11-18 17:49:09 +00:00
|
|
|
log.Printf("mount FAILED: %v", err)
|
2016-08-20 11:43:33 +00:00
|
|
|
r.skip = true
|
2017-11-18 17:49:09 +00:00
|
|
|
} else {
|
|
|
|
log.Printf("mount OK")
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
2020-04-16 12:33:46 +00:00
|
|
|
if r.useVFS {
|
|
|
|
r.os = vfsOs{r.vfs}
|
|
|
|
} else {
|
|
|
|
r.os = realOs{}
|
|
|
|
}
|
|
|
|
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Run) umount() {
|
2016-08-20 11:43:33 +00:00
|
|
|
if r.skip {
|
|
|
|
log.Printf("FUSE not found so skipping umount")
|
|
|
|
return
|
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
/*
|
|
|
|
log.Printf("Calling fusermount -u %q", r.mountPath)
|
|
|
|
err := exec.Command("fusermount", "-u", r.mountPath).Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("fusermount failed: %v", err)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
log.Printf("Unmounting %q", r.mountPath)
|
|
|
|
err := r.umountFn()
|
2017-06-13 09:52:10 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("signal to umount failed - retrying: %v", err)
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
err = r.umountFn()
|
|
|
|
}
|
2016-07-17 22:03:23 +00:00
|
|
|
if err != nil {
|
2017-05-02 21:35:07 +00:00
|
|
|
log.Fatalf("signal to umount failed: %v", err)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
log.Printf("Waiting for umount")
|
|
|
|
err = <-r.umountResult
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("umount failed: %v", err)
|
|
|
|
}
|
2017-11-07 18:03:23 +00:00
|
|
|
|
|
|
|
// Cleanup the VFS cache - umount has called Shutdown
|
|
|
|
err = r.vfs.CleanUp()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to cleanup the VFS cache: %v", err)
|
|
|
|
}
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 11:59:56 +00:00
|
|
|
// cacheMode flushes the VFS and changes the CacheMode
|
2020-02-28 14:44:15 +00:00
|
|
|
func (r *Run) cacheMode(cacheMode vfscommon.CacheMode) {
|
2017-11-18 17:49:09 +00:00
|
|
|
if r.skip {
|
|
|
|
log.Printf("FUSE not found so skipping cacheMode")
|
|
|
|
return
|
|
|
|
}
|
2017-11-18 11:59:56 +00:00
|
|
|
// Wait for writers to finish
|
|
|
|
r.vfs.WaitForWriters(30 * time.Second)
|
|
|
|
// Empty and remake the remote
|
|
|
|
r.cleanRemote()
|
2019-06-17 08:34:30 +00:00
|
|
|
err := r.fremote.Mkdir(context.Background(), "")
|
2017-11-18 11:59:56 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to open mkdir %q: %v", *fstest.RemoteName, err)
|
|
|
|
}
|
|
|
|
// Empty the cache
|
|
|
|
err = r.vfs.CleanUp()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to cleanup the VFS cache: %v", err)
|
|
|
|
}
|
|
|
|
// Reset the cache mode
|
2018-04-16 15:38:32 +00:00
|
|
|
r.vfs.SetCacheMode(cacheMode)
|
2017-11-18 11:59:56 +00:00
|
|
|
// Flush the directory cache
|
|
|
|
r.vfs.FlushDirCache()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-20 11:43:33 +00:00
|
|
|
func (r *Run) skipIfNoFUSE(t *testing.T) {
|
|
|
|
if r.skip {
|
|
|
|
t.Skip("FUSE not found so skipping test")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 12:33:46 +00:00
|
|
|
func (r *Run) skipIfVFS(t *testing.T) {
|
|
|
|
if r.useVFS {
|
|
|
|
t.Skip("Not running under VFS")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-17 22:03:23 +00:00
|
|
|
// Finalise cleans the remote and unmounts
|
|
|
|
func (r *Run) Finalise() {
|
|
|
|
r.umount()
|
|
|
|
r.cleanRemote()
|
2020-04-16 12:33:46 +00:00
|
|
|
if r.useVFS {
|
|
|
|
// FIXME
|
|
|
|
} else {
|
|
|
|
err := os.RemoveAll(r.mountPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to clean mountPath %q: %v", r.mountPath, err)
|
|
|
|
}
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-18 11:59:56 +00:00
|
|
|
// path returns an OS local path for filepath
|
|
|
|
func (r *Run) path(filePath string) string {
|
2020-04-16 12:33:46 +00:00
|
|
|
if r.useVFS {
|
|
|
|
return filePath
|
|
|
|
}
|
2017-11-18 11:59:56 +00:00
|
|
|
// return windows drive letter root as E:\
|
|
|
|
if filePath == "" && runtime.GOOS == "windows" {
|
|
|
|
return run.mountPath + `\`
|
2017-05-08 13:55:05 +00:00
|
|
|
}
|
2017-11-18 11:59:56 +00:00
|
|
|
return filepath.Join(run.mountPath, filepath.FromSlash(filePath))
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type dirMap map[string]struct{}
|
|
|
|
|
|
|
|
// Create a dirMap from a string
|
|
|
|
func newDirMap(dirString string) (dm dirMap) {
|
|
|
|
dm = make(dirMap)
|
|
|
|
for _, entry := range strings.Split(dirString, "|") {
|
|
|
|
if entry != "" {
|
|
|
|
dm[entry] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dm
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a dirmap with only the files in
|
|
|
|
func (dm dirMap) filesOnly() dirMap {
|
|
|
|
newDm := make(dirMap)
|
|
|
|
for name := range dm {
|
|
|
|
if !strings.HasSuffix(name, "/") {
|
|
|
|
newDm[name] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newDm
|
|
|
|
}
|
|
|
|
|
|
|
|
// reads the local tree into dir
|
2017-11-18 11:59:56 +00:00
|
|
|
func (r *Run) readLocal(t *testing.T, dir dirMap, filePath string) {
|
|
|
|
realPath := r.path(filePath)
|
2020-04-16 12:33:46 +00:00
|
|
|
files, err := r.os.ReadDir(realPath)
|
2016-07-17 22:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
for _, fi := range files {
|
2017-11-18 11:59:56 +00:00
|
|
|
name := path.Join(filePath, fi.Name())
|
2016-07-17 22:03:23 +00:00
|
|
|
if fi.IsDir() {
|
|
|
|
dir[name+"/"] = struct{}{}
|
|
|
|
r.readLocal(t, dir, name)
|
2017-10-29 21:14:05 +00:00
|
|
|
assert.Equal(t, run.vfs.Opt.DirPerms&os.ModePerm, fi.Mode().Perm())
|
2016-07-17 22:03:23 +00:00
|
|
|
} else {
|
|
|
|
dir[fmt.Sprintf("%s %d", name, fi.Size())] = struct{}{}
|
2017-10-29 21:14:05 +00:00
|
|
|
assert.Equal(t, run.vfs.Opt.FilePerms&os.ModePerm, fi.Mode().Perm())
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reads the remote tree into dir
|
|
|
|
func (r *Run) readRemote(t *testing.T, dir dirMap, filepath string) {
|
2019-06-17 08:34:30 +00:00
|
|
|
objs, dirs, err := walk.GetAll(context.Background(), r.fremote, filepath, true, 1)
|
2016-07-17 22:03:23 +00:00
|
|
|
if err == fs.ErrorDirNotFound {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, obj := range objs {
|
|
|
|
dir[fmt.Sprintf("%s %d", obj.Remote(), obj.Size())] = struct{}{}
|
|
|
|
}
|
|
|
|
for _, d := range dirs {
|
|
|
|
name := d.Remote()
|
|
|
|
dir[name+"/"] = struct{}{}
|
|
|
|
r.readRemote(t, dir, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkDir checks the local and remote against the string passed in
|
|
|
|
func (r *Run) checkDir(t *testing.T, dirString string) {
|
2017-11-17 10:30:35 +00:00
|
|
|
var retries = *fstest.ListRetries
|
2017-11-18 11:59:56 +00:00
|
|
|
sleep := time.Second / 5
|
2017-11-17 10:30:35 +00:00
|
|
|
var remoteOK, fuseOK bool
|
2017-11-18 11:59:56 +00:00
|
|
|
var dm, localDm, remoteDm dirMap
|
2017-11-17 10:30:35 +00:00
|
|
|
for i := 1; i <= retries; i++ {
|
2017-11-18 11:59:56 +00:00
|
|
|
dm = newDirMap(dirString)
|
|
|
|
localDm = make(dirMap)
|
2017-11-17 10:30:35 +00:00
|
|
|
r.readLocal(t, localDm, "")
|
2017-11-18 11:59:56 +00:00
|
|
|
remoteDm = make(dirMap)
|
2017-11-17 10:30:35 +00:00
|
|
|
r.readRemote(t, remoteDm, "")
|
|
|
|
// Ignore directories for remote compare
|
|
|
|
remoteOK = reflect.DeepEqual(dm.filesOnly(), remoteDm.filesOnly())
|
|
|
|
fuseOK = reflect.DeepEqual(dm, localDm)
|
|
|
|
if remoteOK && fuseOK {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sleep *= 2
|
|
|
|
t.Logf("Sleeping for %v for list eventual consistency: %d/%d", sleep, i, retries)
|
|
|
|
time.Sleep(sleep)
|
|
|
|
}
|
2017-11-18 11:59:56 +00:00
|
|
|
assert.Equal(t, dm.filesOnly(), remoteDm.filesOnly(), "expected vs remote")
|
|
|
|
assert.Equal(t, dm, localDm, "expected vs fuse mount")
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 15:50:38 +00:00
|
|
|
// wait for any files being written to be released by fuse
|
|
|
|
func (r *Run) waitForWriters() {
|
|
|
|
run.vfs.WaitForWriters(10 * time.Second)
|
|
|
|
}
|
|
|
|
|
2020-01-08 17:14:35 +00:00
|
|
|
// writeFile writes data to a file named by filename.
|
|
|
|
// If the file does not exist, WriteFile creates it with permissions perm;
|
|
|
|
// otherwise writeFile truncates it before writing.
|
|
|
|
// If there is an error writing then writeFile
|
|
|
|
// deletes it an existing file and tries again.
|
|
|
|
func writeFile(filename string, data []byte, perm os.FileMode) error {
|
2020-04-16 12:33:46 +00:00
|
|
|
f, err := run.os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
|
2020-01-08 17:14:35 +00:00
|
|
|
if err != nil {
|
2020-04-16 12:33:46 +00:00
|
|
|
err = run.os.Remove(filename)
|
2020-01-08 17:14:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-16 12:33:46 +00:00
|
|
|
f, err = run.os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, perm)
|
2020-01-08 17:14:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n, err := f.Write(data)
|
|
|
|
if err == nil && n < len(data) {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
}
|
|
|
|
if err1 := f.Close(); err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-17 22:03:23 +00:00
|
|
|
func (r *Run) createFile(t *testing.T, filepath string, contents string) {
|
|
|
|
filepath = r.path(filepath)
|
2020-01-08 17:14:35 +00:00
|
|
|
err := writeFile(filepath, []byte(contents), 0600)
|
2016-07-17 22:03:23 +00:00
|
|
|
require.NoError(t, err)
|
2017-11-18 15:50:38 +00:00
|
|
|
r.waitForWriters()
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Run) readFile(t *testing.T, filepath string) string {
|
|
|
|
filepath = r.path(filepath)
|
2020-04-16 12:33:46 +00:00
|
|
|
result, err := run.os.ReadFile(filepath)
|
2016-07-17 22:03:23 +00:00
|
|
|
require.NoError(t, err)
|
2017-11-19 19:58:09 +00:00
|
|
|
time.Sleep(100 * time.Millisecond) // FIXME wait for Release
|
2016-07-17 22:03:23 +00:00
|
|
|
return string(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Run) mkdir(t *testing.T, filepath string) {
|
|
|
|
filepath = r.path(filepath)
|
2020-04-16 12:33:46 +00:00
|
|
|
err := run.os.Mkdir(filepath, 0700)
|
2016-07-17 22:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Run) rm(t *testing.T, filepath string) {
|
|
|
|
filepath = r.path(filepath)
|
2020-04-16 12:33:46 +00:00
|
|
|
err := run.os.Remove(filepath)
|
2016-07-17 22:03:23 +00:00
|
|
|
require.NoError(t, err)
|
2017-11-18 18:40:28 +00:00
|
|
|
|
|
|
|
// Wait for file to disappear from listing
|
2018-03-09 21:16:16 +00:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-04-16 12:33:46 +00:00
|
|
|
_, err := run.os.Stat(filepath)
|
2017-11-18 18:40:28 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
assert.Fail(t, "failed to delete file", filepath)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Run) rmdir(t *testing.T, filepath string) {
|
|
|
|
filepath = r.path(filepath)
|
2020-04-16 12:33:46 +00:00
|
|
|
err := run.os.Remove(filepath)
|
2016-07-17 22:03:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2017-05-08 17:05:12 +00:00
|
|
|
// TestMount checks that the Fs is mounted by seeing if the mountpoint
|
|
|
|
// is in the mount output
|
2016-07-17 22:03:23 +00:00
|
|
|
func TestMount(t *testing.T) {
|
2020-04-16 12:33:46 +00:00
|
|
|
run.skipIfVFS(t)
|
2016-08-20 11:43:33 +00:00
|
|
|
run.skipIfNoFUSE(t)
|
2017-11-18 12:05:17 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("not running on windows")
|
|
|
|
}
|
2016-08-20 11:43:33 +00:00
|
|
|
|
2016-07-17 22:03:23 +00:00
|
|
|
out, err := exec.Command("mount").Output()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Contains(t, string(out), run.mountPath)
|
|
|
|
}
|
|
|
|
|
2017-05-08 17:05:12 +00:00
|
|
|
// TestRoot checks root directory is present and correct
|
2016-07-17 22:03:23 +00:00
|
|
|
func TestRoot(t *testing.T) {
|
2020-04-16 12:33:46 +00:00
|
|
|
run.skipIfVFS(t)
|
2016-08-20 11:43:33 +00:00
|
|
|
run.skipIfNoFUSE(t)
|
|
|
|
|
2016-07-17 22:03:23 +00:00
|
|
|
fi, err := os.Lstat(run.mountPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.True(t, fi.IsDir())
|
2017-10-29 21:14:05 +00:00
|
|
|
assert.Equal(t, run.vfs.Opt.DirPerms&os.ModePerm, fi.Mode().Perm())
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|