2018-10-26 15:48:22 +02:00
|
|
|
package rc
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2020-07-27 18:35:43 +02:00
|
|
|
"fmt"
|
2020-07-27 20:17:09 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-07-27 18:35:43 +02:00
|
|
|
"os"
|
2018-11-04 17:16:43 +01:00
|
|
|
"runtime"
|
2021-01-09 14:26:06 +01:00
|
|
|
"strings"
|
2018-10-26 15:48:22 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-10-17 16:04:22 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
2018-10-26 15:48:22 +02:00
|
|
|
)
|
|
|
|
|
2020-07-27 18:35:43 +02:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
// Pretend to be rclone version if we have a version string parameter
|
|
|
|
if os.Args[len(os.Args)-1] == "version" {
|
|
|
|
fmt.Printf("rclone %s\n", fs.Version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2021-01-09 14:26:06 +01:00
|
|
|
// Pretend to error if we have an unknown command
|
|
|
|
if os.Args[len(os.Args)-1] == "unknown_command" {
|
|
|
|
fmt.Printf("rclone %s\n", fs.Version)
|
|
|
|
fmt.Fprintf(os.Stderr, "Unknown command\n")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-07-27 18:35:43 +02:00
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2018-10-26 15:48:22 +02:00
|
|
|
func TestInternalNoop(t *testing.T) {
|
|
|
|
call := Calls.Get("rc/noop")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in := Params{
|
|
|
|
"String": "hello",
|
|
|
|
"Int": 42,
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 15:48:22 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
assert.Equal(t, in, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInternalError(t *testing.T) {
|
|
|
|
call := Calls.Get("rc/error")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in := Params{}
|
2019-06-17 10:34:30 +02:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 15:48:22 +02:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInternalList(t *testing.T) {
|
|
|
|
call := Calls.Get("rc/list")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in := Params{}
|
2019-06-17 10:34:30 +02:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 15:48:22 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
assert.Equal(t, Params{"commands": Calls.List()}, out)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCorePid(t *testing.T) {
|
|
|
|
call := Calls.Get("core/pid")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in := Params{}
|
2019-06-17 10:34:30 +02:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 15:48:22 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
pid := out["pid"]
|
|
|
|
assert.NotEqual(t, nil, pid)
|
|
|
|
_, ok := pid.(int)
|
|
|
|
assert.Equal(t, true, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCoreMemstats(t *testing.T) {
|
|
|
|
call := Calls.Get("core/memstats")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in := Params{}
|
2019-06-17 10:34:30 +02:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 15:48:22 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
sys := out["Sys"]
|
|
|
|
assert.NotEqual(t, nil, sys)
|
|
|
|
_, ok := sys.(uint64)
|
|
|
|
assert.Equal(t, true, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCoreGC(t *testing.T) {
|
|
|
|
call := Calls.Get("core/gc")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in := Params{}
|
2019-06-17 10:34:30 +02:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-10-26 15:48:22 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, out)
|
|
|
|
assert.Equal(t, Params(nil), out)
|
|
|
|
}
|
2018-11-04 17:16:43 +01:00
|
|
|
|
|
|
|
func TestCoreVersion(t *testing.T) {
|
|
|
|
call := Calls.Get("core/version")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in := Params{}
|
2019-06-17 10:34:30 +02:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-11-04 17:16:43 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
assert.Equal(t, fs.Version, out["version"])
|
|
|
|
assert.Equal(t, runtime.GOOS, out["os"])
|
|
|
|
assert.Equal(t, runtime.GOARCH, out["arch"])
|
|
|
|
assert.Equal(t, runtime.Version(), out["goVersion"])
|
|
|
|
_ = out["isGit"].(bool)
|
2020-08-18 22:21:53 +02:00
|
|
|
v := out["decomposed"].([]int64)
|
2018-11-04 17:16:43 +01:00
|
|
|
assert.True(t, len(v) >= 2)
|
|
|
|
}
|
2018-11-04 17:30:56 +01:00
|
|
|
|
|
|
|
func TestCoreObscure(t *testing.T) {
|
|
|
|
call := Calls.Get("core/obscure")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in := Params{
|
|
|
|
"clear": "potato",
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
out, err := call.Fn(context.Background(), in)
|
2018-11-04 17:30:56 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, out)
|
|
|
|
assert.Equal(t, in["clear"], obscure.MustReveal(out["obscured"].(string)))
|
|
|
|
}
|
2019-10-17 16:04:22 +02:00
|
|
|
|
|
|
|
func TestCoreQuit(t *testing.T) {
|
|
|
|
//The call should return an error if param exitCode is not parsed to int
|
|
|
|
call := Calls.Get("core/quit")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
in := Params{
|
|
|
|
"exitCode": "potato",
|
|
|
|
}
|
|
|
|
_, err := call.Fn(context.Background(), in)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
2020-07-14 20:09:59 +02:00
|
|
|
|
|
|
|
// core/command: Runs a raw rclone command
|
|
|
|
func TestCoreCommand(t *testing.T) {
|
|
|
|
call := Calls.Get("core/command")
|
|
|
|
|
2021-01-09 14:26:06 +01:00
|
|
|
test := func(command string, returnType string, wantOutput string, fail bool) {
|
|
|
|
var rec = httptest.NewRecorder()
|
|
|
|
var w http.ResponseWriter = rec
|
|
|
|
|
|
|
|
in := Params{
|
|
|
|
"command": command,
|
|
|
|
"opt": map[string]string{},
|
|
|
|
"arg": []string{},
|
|
|
|
"_response": w,
|
|
|
|
}
|
|
|
|
if returnType != "" {
|
|
|
|
in["returnType"] = returnType
|
|
|
|
} else {
|
|
|
|
returnType = "COMBINED_OUTPUT"
|
|
|
|
}
|
|
|
|
stream := strings.HasPrefix(returnType, "STREAM")
|
|
|
|
got, err := call.Fn(context.Background(), in)
|
|
|
|
if stream && fail {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !stream {
|
|
|
|
assert.Equal(t, wantOutput, got["result"])
|
|
|
|
assert.Equal(t, fail, got["error"])
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, wantOutput, rec.Body.String())
|
|
|
|
}
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Result().StatusCode)
|
2020-07-14 20:09:59 +02:00
|
|
|
}
|
|
|
|
|
2021-01-09 14:26:06 +01:00
|
|
|
version := fmt.Sprintf("rclone %s\n", fs.Version)
|
|
|
|
errorString := "Unknown command\n"
|
|
|
|
t.Run("OK", func(t *testing.T) {
|
|
|
|
test("version", "", version, false)
|
|
|
|
})
|
|
|
|
t.Run("Fail", func(t *testing.T) {
|
|
|
|
test("unknown_command", "", version+errorString, true)
|
|
|
|
})
|
|
|
|
t.Run("Combined", func(t *testing.T) {
|
|
|
|
test("unknown_command", "COMBINED_OUTPUT", version+errorString, true)
|
|
|
|
})
|
|
|
|
t.Run("Stderr", func(t *testing.T) {
|
|
|
|
test("unknown_command", "STREAM_ONLY_STDERR", errorString, true)
|
|
|
|
})
|
|
|
|
t.Run("Stdout", func(t *testing.T) {
|
|
|
|
test("unknown_command", "STREAM_ONLY_STDOUT", version, true)
|
|
|
|
})
|
|
|
|
t.Run("Stream", func(t *testing.T) {
|
|
|
|
test("unknown_command", "STREAM", version+errorString, true)
|
|
|
|
})
|
2020-07-14 20:09:59 +02:00
|
|
|
}
|