2018-10-26 21:51:28 +08:00
|
|
|
package operations_test
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2018-10-26 21:51:28 +08:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-06-18 18:49:35 +08:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path"
|
2018-10-26 21:51:28 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/cache"
|
|
|
|
"github.com/rclone/rclone/fs/operations"
|
|
|
|
"github.com/rclone/rclone/fs/rc"
|
|
|
|
"github.com/rclone/rclone/fstest"
|
2020-06-18 18:49:35 +08:00
|
|
|
"github.com/rclone/rclone/lib/rest"
|
2018-10-26 21:51:28 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func rcNewRun(t *testing.T, method string) (*fstest.Run, *rc.Call) {
|
|
|
|
if *fstest.RemoteName != "" {
|
|
|
|
t.Skip("Skipping test on non local remote")
|
|
|
|
}
|
|
|
|
r := fstest.NewRun(t)
|
|
|
|
call := rc.Calls.Get(method)
|
|
|
|
assert.NotNil(t, call)
|
2019-05-23 19:26:16 +08:00
|
|
|
cache.Put(r.LocalName, r.Flocal)
|
|
|
|
cache.Put(r.FremoteName, r.Fremote)
|
2018-10-26 21:51:28 +08:00
|
|
|
return r, call
|
|
|
|
}
|
|
|
|
|
|
|
|
// operations/about: Return the space used on the remote
|
|
|
|
func TestRcAbout(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "operations/about")
|
|
|
|
defer r.Finalise()
|
2019-06-17 16:34:30 +08:00
|
|
|
r.Mkdir(context.Background(), r.Fremote)
|
2018-10-26 21:51:28 +08:00
|
|
|
|
|
|
|
// Will get an error if remote doesn't support About
|
|
|
|
expectedErr := r.Fremote.Features().About == nil
|
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
if expectedErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Can't really check the output much!
|
|
|
|
assert.NotEqual(t, int64(0), out["Total"])
|
|
|
|
}
|
|
|
|
|
|
|
|
// operations/cleanup: Remove trashed files in the remote or path
|
|
|
|
func TestRcCleanup(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "operations/cleanup")
|
|
|
|
defer r.Finalise()
|
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.LocalName,
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
assert.Contains(t, err.Error(), "doesn't support cleanup")
|
|
|
|
}
|
|
|
|
|
|
|
|
// operations/copyfile: Copy a file from source remote to destination remote
|
|
|
|
func TestRcCopyfile(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "operations/copyfile")
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteFile("file1", "file1 contents", t1)
|
2019-06-17 16:34:30 +08:00
|
|
|
r.Mkdir(context.Background(), r.Fremote)
|
2018-10-26 21:51:28 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal, file1)
|
|
|
|
fstest.CheckItems(t, r.Fremote)
|
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"srcFs": r.LocalName,
|
|
|
|
"srcRemote": "file1",
|
|
|
|
"dstFs": r.FremoteName,
|
|
|
|
"dstRemote": "file1-renamed",
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
|
|
|
fstest.CheckItems(t, r.Flocal, file1)
|
|
|
|
file1.Path = "file1-renamed"
|
|
|
|
fstest.CheckItems(t, r.Fremote, file1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// operations/copyurl: Copy the URL to the object
|
|
|
|
func TestRcCopyurl(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "operations/copyurl")
|
|
|
|
defer r.Finalise()
|
|
|
|
contents := "file1 contents\n"
|
|
|
|
file1 := r.WriteFile("file1", contents, t1)
|
2019-06-17 16:34:30 +08:00
|
|
|
r.Mkdir(context.Background(), r.Fremote)
|
2018-10-26 21:51:28 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote)
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, err := w.Write([]byte(contents))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}))
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
in := rc.Params{
|
2019-09-04 00:25:19 +08:00
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "file1",
|
|
|
|
"url": ts.URL,
|
|
|
|
"autoFilename": false,
|
2020-04-19 19:40:17 +08:00
|
|
|
"noClobber": false,
|
2018-10-26 21:51:28 +08:00
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
2020-04-19 19:40:17 +08:00
|
|
|
in = rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "file1",
|
|
|
|
"url": ts.URL,
|
|
|
|
"autoFilename": false,
|
|
|
|
"noClobber": true,
|
|
|
|
}
|
|
|
|
out, err = call.Fn(context.Background(), in)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
2019-09-04 00:25:19 +08:00
|
|
|
urlFileName := "filename.txt"
|
|
|
|
in = rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "",
|
|
|
|
"url": ts.URL + "/" + urlFileName,
|
|
|
|
"autoFilename": true,
|
2020-04-19 19:40:17 +08:00
|
|
|
"noClobber": false,
|
2019-09-04 00:25:19 +08:00
|
|
|
}
|
|
|
|
out, err = call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
|
|
|
in = rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "",
|
|
|
|
"url": ts.URL,
|
|
|
|
"autoFilename": true,
|
2020-04-19 19:40:17 +08:00
|
|
|
"noClobber": false,
|
2019-09-04 00:25:19 +08:00
|
|
|
}
|
|
|
|
out, err = call.Fn(context.Background(), in)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1, fstest.NewItem(urlFileName, contents, t1)}, nil, fs.ModTimeNotSupported)
|
2018-10-26 21:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// operations/delete: Remove files in the path
|
|
|
|
func TestRcDelete(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "operations/delete")
|
|
|
|
defer r.Finalise()
|
|
|
|
|
2019-06-17 16:34:30 +08:00
|
|
|
file1 := r.WriteObject(context.Background(), "small", "1234567890", t2) // 10 bytes
|
|
|
|
file2 := r.WriteObject(context.Background(), "medium", "------------------------------------------------------------", t1) // 60 bytes
|
|
|
|
file3 := r.WriteObject(context.Background(), "large", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", t1) // 100 bytes
|
2018-10-26 21:51:28 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2, file3)
|
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
|
|
|
fstest.CheckItems(t, r.Fremote)
|
|
|
|
}
|
|
|
|
|
|
|
|
// operations/deletefile: Remove the single file pointed to
|
|
|
|
func TestRcDeletefile(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "operations/deletefile")
|
|
|
|
defer r.Finalise()
|
|
|
|
|
2019-06-17 16:34:30 +08:00
|
|
|
file1 := r.WriteObject(context.Background(), "small", "1234567890", t2) // 10 bytes
|
|
|
|
file2 := r.WriteObject(context.Background(), "medium", "------------------------------------------------------------", t1) // 60 bytes
|
2018-10-26 21:51:28 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2)
|
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "small",
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
|
|
|
fstest.CheckItems(t, r.Fremote, file2)
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:18:01 +08:00
|
|
|
// operations/list: List the given remote and path in JSON format.
|
2018-10-26 21:51:28 +08:00
|
|
|
func TestRcList(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "operations/list")
|
|
|
|
defer r.Finalise()
|
|
|
|
|
2019-06-17 16:34:30 +08:00
|
|
|
file1 := r.WriteObject(context.Background(), "a", "a", t1)
|
|
|
|
file2 := r.WriteObject(context.Background(), "subdir/b", "bb", t2)
|
2018-10-26 21:51:28 +08:00
|
|
|
|
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2)
|
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "",
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
list := out["list"].([]*operations.ListJSONItem)
|
|
|
|
assert.Equal(t, 2, len(list))
|
|
|
|
|
|
|
|
checkFile1 := func(got *operations.ListJSONItem) {
|
2019-02-03 19:01:52 +08:00
|
|
|
assert.WithinDuration(t, t1, got.ModTime.When, time.Second)
|
2018-10-26 21:51:28 +08:00
|
|
|
assert.Equal(t, "a", got.Path)
|
|
|
|
assert.Equal(t, "a", got.Name)
|
|
|
|
assert.Equal(t, int64(1), got.Size)
|
|
|
|
assert.Equal(t, "application/octet-stream", got.MimeType)
|
|
|
|
assert.Equal(t, false, got.IsDir)
|
|
|
|
}
|
|
|
|
checkFile1(list[0])
|
|
|
|
|
|
|
|
checkSubdir := func(got *operations.ListJSONItem) {
|
|
|
|
assert.Equal(t, "subdir", got.Path)
|
|
|
|
assert.Equal(t, "subdir", got.Name)
|
|
|
|
assert.Equal(t, int64(-1), got.Size)
|
|
|
|
assert.Equal(t, "inode/directory", got.MimeType)
|
|
|
|
assert.Equal(t, true, got.IsDir)
|
|
|
|
}
|
|
|
|
checkSubdir(list[1])
|
|
|
|
|
|
|
|
in = rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "",
|
|
|
|
"opt": rc.Params{
|
|
|
|
"recurse": true,
|
|
|
|
},
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err = call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
list = out["list"].([]*operations.ListJSONItem)
|
|
|
|
assert.Equal(t, 3, len(list))
|
|
|
|
checkFile1(list[0])
|
|
|
|
checkSubdir(list[1])
|
|
|
|
|
|
|
|
checkFile2 := func(got *operations.ListJSONItem) {
|
2019-02-03 19:01:52 +08:00
|
|
|
assert.WithinDuration(t, t2, got.ModTime.When, time.Second)
|
2018-10-26 21:51:28 +08:00
|
|
|
assert.Equal(t, "subdir/b", got.Path)
|
|
|
|
assert.Equal(t, "b", got.Name)
|
|
|
|
assert.Equal(t, int64(2), got.Size)
|
|
|
|
assert.Equal(t, "application/octet-stream", got.MimeType)
|
|
|
|
assert.Equal(t, false, got.IsDir)
|
|
|
|
}
|
|
|
|
checkFile2(list[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
// operations/mkdir: Make a destination directory or container
|
|
|
|
func TestRcMkdir(t *testing.T) {
|
2020-11-06 00:27:01 +08:00
|
|
|
ctx := context.Background()
|
2018-10-26 21:51:28 +08:00
|
|
|
r, call := rcNewRun(t, "operations/mkdir")
|
|
|
|
defer r.Finalise()
|
2019-06-17 16:34:30 +08:00
|
|
|
r.Mkdir(context.Background(), r.Fremote)
|
2018-10-26 21:51:28 +08:00
|
|
|
|
2020-11-06 00:27:01 +08:00
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{}, []string{}, fs.GetModifyWindow(ctx, r.Fremote))
|
2018-10-26 21:51:28 +08:00
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "subdir",
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
2020-11-06 00:27:01 +08:00
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{}, []string{"subdir"}, fs.GetModifyWindow(ctx, r.Fremote))
|
2018-10-26 21:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// operations/movefile: Move a file from source remote to destination remote
|
|
|
|
func TestRcMovefile(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "operations/movefile")
|
|
|
|
defer r.Finalise()
|
|
|
|
file1 := r.WriteFile("file1", "file1 contents", t1)
|
2019-06-17 16:34:30 +08:00
|
|
|
r.Mkdir(context.Background(), r.Fremote)
|
2018-10-26 21:51:28 +08:00
|
|
|
fstest.CheckItems(t, r.Flocal, file1)
|
|
|
|
fstest.CheckItems(t, r.Fremote)
|
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"srcFs": r.LocalName,
|
|
|
|
"srcRemote": "file1",
|
|
|
|
"dstFs": r.FremoteName,
|
|
|
|
"dstRemote": "file1-renamed",
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
|
|
|
fstest.CheckItems(t, r.Flocal)
|
|
|
|
file1.Path = "file1-renamed"
|
|
|
|
fstest.CheckItems(t, r.Fremote, file1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// operations/purge: Remove a directory or container and all of its contents
|
|
|
|
func TestRcPurge(t *testing.T) {
|
2020-11-06 00:27:01 +08:00
|
|
|
ctx := context.Background()
|
2018-10-26 21:51:28 +08:00
|
|
|
r, call := rcNewRun(t, "operations/purge")
|
|
|
|
defer r.Finalise()
|
2019-06-17 16:34:30 +08:00
|
|
|
file1 := r.WriteObject(context.Background(), "subdir/file1", "subdir/file1 contents", t1)
|
2018-10-26 21:51:28 +08:00
|
|
|
|
2020-11-06 00:27:01 +08:00
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{file1}, []string{"subdir"}, fs.GetModifyWindow(ctx, r.Fremote))
|
2018-10-26 21:51:28 +08:00
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "subdir",
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
2020-11-06 00:27:01 +08:00
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{}, []string{}, fs.GetModifyWindow(ctx, r.Fremote))
|
2018-10-26 21:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// operations/rmdir: Remove an empty directory or container
|
|
|
|
func TestRcRmdir(t *testing.T) {
|
2020-11-06 00:27:01 +08:00
|
|
|
ctx := context.Background()
|
2018-10-26 21:51:28 +08:00
|
|
|
r, call := rcNewRun(t, "operations/rmdir")
|
|
|
|
defer r.Finalise()
|
2019-06-17 16:34:30 +08:00
|
|
|
r.Mkdir(context.Background(), r.Fremote)
|
|
|
|
assert.NoError(t, r.Fremote.Mkdir(context.Background(), "subdir"))
|
2018-10-26 21:51:28 +08:00
|
|
|
|
2020-11-06 00:27:01 +08:00
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{}, []string{"subdir"}, fs.GetModifyWindow(ctx, r.Fremote))
|
2018-10-26 21:51:28 +08:00
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "subdir",
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
2020-11-06 00:27:01 +08:00
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{}, []string{}, fs.GetModifyWindow(ctx, r.Fremote))
|
2018-10-26 21:51:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// operations/rmdirs: Remove all the empty directories in the path
|
|
|
|
func TestRcRmdirs(t *testing.T) {
|
2020-11-06 00:27:01 +08:00
|
|
|
ctx := context.Background()
|
2018-10-26 21:51:28 +08:00
|
|
|
r, call := rcNewRun(t, "operations/rmdirs")
|
|
|
|
defer r.Finalise()
|
2019-06-17 16:34:30 +08:00
|
|
|
r.Mkdir(context.Background(), r.Fremote)
|
|
|
|
assert.NoError(t, r.Fremote.Mkdir(context.Background(), "subdir"))
|
|
|
|
assert.NoError(t, r.Fremote.Mkdir(context.Background(), "subdir/subsubdir"))
|
2018-10-26 21:51:28 +08:00
|
|
|
|
2020-11-06 00:27:01 +08:00
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{}, []string{"subdir", "subdir/subsubdir"}, fs.GetModifyWindow(ctx, r.Fremote))
|
2018-10-26 21:51:28 +08:00
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "subdir",
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
2020-11-06 00:27:01 +08:00
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{}, []string{}, fs.GetModifyWindow(ctx, r.Fremote))
|
2018-10-26 21:51:28 +08:00
|
|
|
|
2019-06-17 16:34:30 +08:00
|
|
|
assert.NoError(t, r.Fremote.Mkdir(context.Background(), "subdir"))
|
|
|
|
assert.NoError(t, r.Fremote.Mkdir(context.Background(), "subdir/subsubdir"))
|
2018-10-26 21:51:28 +08:00
|
|
|
|
|
|
|
in = rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "subdir",
|
|
|
|
"leaveRoot": true,
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err = call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params(nil), out)
|
|
|
|
|
2020-11-06 00:27:01 +08:00
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{}, []string{"subdir"}, fs.GetModifyWindow(ctx, r.Fremote))
|
2018-10-26 21:51:28 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// operations/size: Count the number of bytes and files in remote
|
|
|
|
func TestRcSize(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "operations/size")
|
|
|
|
defer r.Finalise()
|
2019-06-17 16:34:30 +08:00
|
|
|
file1 := r.WriteObject(context.Background(), "small", "1234567890", t2) // 10 bytes
|
|
|
|
file2 := r.WriteObject(context.Background(), "subdir/medium", "------------------------------------------------------------", t1) // 60 bytes
|
|
|
|
file3 := r.WriteObject(context.Background(), "subdir/subsubdir/large", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", t1) // 50 bytes
|
2018-10-26 21:51:28 +08:00
|
|
|
fstest.CheckItems(t, r.Fremote, file1, file2, file3)
|
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 21:51:28 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params{
|
|
|
|
"count": int64(3),
|
|
|
|
"bytes": int64(120),
|
|
|
|
}, out)
|
|
|
|
}
|
2019-03-12 00:59:27 +08:00
|
|
|
|
|
|
|
// operations/publiclink: Create or retrieve a public link to the given file or folder.
|
|
|
|
func TestRcPublicLink(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "operations/publiclink")
|
|
|
|
defer r.Finalise()
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "",
|
2020-06-01 05:18:01 +08:00
|
|
|
"expire": "5m",
|
|
|
|
"unlink": false,
|
2019-03-12 00:59:27 +08:00
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
_, err := call.Fn(context.Background(), in)
|
2019-03-12 00:59:27 +08:00
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "doesn't support public links")
|
|
|
|
}
|
2019-06-08 16:19:07 +08:00
|
|
|
|
|
|
|
// operations/fsinfo: Return information about the remote
|
|
|
|
func TestRcFsInfo(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "operations/fsinfo")
|
|
|
|
defer r.Finalise()
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
}
|
2019-06-17 16:34:30 +08:00
|
|
|
got, err := call.Fn(context.Background(), in)
|
2019-06-08 16:19:07 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
want := operations.GetFsInfo(r.Fremote)
|
|
|
|
assert.Equal(t, want.Name, got["Name"])
|
|
|
|
assert.Equal(t, want.Root, got["Root"])
|
|
|
|
assert.Equal(t, want.String, got["String"])
|
|
|
|
assert.Equal(t, float64(want.Precision), got["Precision"])
|
|
|
|
var hashes []interface{}
|
|
|
|
for _, hash := range want.Hashes {
|
|
|
|
hashes = append(hashes, hash)
|
|
|
|
}
|
|
|
|
assert.Equal(t, hashes, got["Hashes"])
|
|
|
|
var features = map[string]interface{}{}
|
|
|
|
for k, v := range want.Features {
|
|
|
|
features[k] = v
|
|
|
|
}
|
|
|
|
assert.Equal(t, features, got["Features"])
|
|
|
|
|
|
|
|
}
|
2020-04-28 20:02:19 +08:00
|
|
|
|
2020-06-18 18:49:35 +08:00
|
|
|
//operations/uploadfile : Tests if upload file succeeds
|
|
|
|
//
|
|
|
|
func TestUploadFile(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "operations/uploadfile")
|
|
|
|
defer r.Finalise()
|
2021-03-25 23:35:08 +08:00
|
|
|
ctx := context.Background()
|
2020-06-18 18:49:35 +08:00
|
|
|
|
|
|
|
testFileName := "test.txt"
|
|
|
|
testFileContent := "Hello World"
|
|
|
|
r.WriteFile(testFileName, testFileContent, t1)
|
2020-08-11 00:09:46 +08:00
|
|
|
testItem1 := fstest.NewItem(testFileName, testFileContent, t1)
|
|
|
|
testItem2 := fstest.NewItem(path.Join("subdir", testFileName), testFileContent, t1)
|
2020-06-18 18:49:35 +08:00
|
|
|
|
|
|
|
currentFile, err := os.Open(path.Join(r.LocalName, testFileName))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-25 23:35:08 +08:00
|
|
|
formReader, contentType, _, err := rest.MultipartUpload(ctx, currentFile, url.Values{}, "file", testFileName)
|
2020-06-18 18:49:35 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
httpReq := httptest.NewRequest("POST", "/", formReader)
|
|
|
|
httpReq.Header.Add("Content-Type", contentType)
|
|
|
|
|
|
|
|
in := rc.Params{
|
|
|
|
"_request": httpReq,
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-08-11 00:09:46 +08:00
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{testItem1}, nil, fs.ModTimeNotSupported)
|
|
|
|
|
|
|
|
assert.NoError(t, r.Fremote.Mkdir(context.Background(), "subdir"))
|
|
|
|
|
|
|
|
currentFile, err = os.Open(path.Join(r.LocalName, testFileName))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-25 23:35:08 +08:00
|
|
|
formReader, contentType, _, err = rest.MultipartUpload(ctx, currentFile, url.Values{}, "file", testFileName)
|
2020-08-11 00:09:46 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
httpReq = httptest.NewRequest("POST", "/", formReader)
|
|
|
|
httpReq.Header.Add("Content-Type", contentType)
|
|
|
|
|
|
|
|
in = rc.Params{
|
|
|
|
"_request": httpReq,
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"remote": "subdir",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
fstest.CheckListingWithPrecision(t, r.Fremote, []fstest.Item{testItem1, testItem2}, nil, fs.ModTimeNotSupported)
|
|
|
|
|
2020-06-18 18:49:35 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 20:02:19 +08:00
|
|
|
// operations/command: Runs a backend command
|
|
|
|
func TestRcCommand(t *testing.T) {
|
|
|
|
r, call := rcNewRun(t, "backend/command")
|
|
|
|
defer r.Finalise()
|
|
|
|
in := rc.Params{
|
|
|
|
"fs": r.FremoteName,
|
|
|
|
"command": "noop",
|
|
|
|
"opt": map[string]string{
|
|
|
|
"echo": "true",
|
|
|
|
"blue": "",
|
|
|
|
},
|
|
|
|
"arg": []string{
|
|
|
|
"path1",
|
|
|
|
"path2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
got, err := call.Fn(context.Background(), in)
|
|
|
|
if err != nil {
|
|
|
|
assert.False(t, r.Fremote.Features().IsLocal, "mustn't fail on local remote")
|
|
|
|
assert.Contains(t, err.Error(), "command not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
want := rc.Params{"result": map[string]interface{}{
|
|
|
|
"arg": []string{
|
|
|
|
"path1",
|
|
|
|
"path2",
|
|
|
|
},
|
|
|
|
"name": "noop",
|
|
|
|
"opt": map[string]string{
|
|
|
|
"blue": "",
|
|
|
|
"echo": "true",
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
errTxt := "explosion in the sausage factory"
|
|
|
|
in["opt"].(map[string]string)["error"] = errTxt
|
|
|
|
_, err = call.Fn(context.Background(), in)
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), errTxt)
|
|
|
|
}
|