2019-08-02 17:56:27 +08:00
|
|
|
// Package servetest provides infrastructure for running loopback
|
|
|
|
// tests of "rclone serve backend:" against the backend integration
|
|
|
|
// tests.
|
|
|
|
package servetest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-11-11 00:45:47 +08:00
|
|
|
"flag"
|
2019-08-02 17:56:27 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2019-08-02 20:47:45 +08:00
|
|
|
"path/filepath"
|
2019-08-02 17:56:27 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-02 20:47:45 +08:00
|
|
|
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags"
|
2019-08-02 17:56:27 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/configmap"
|
|
|
|
"github.com/rclone/rclone/fstest"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-08-02 20:47:45 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-02 17:56:27 +08:00
|
|
|
)
|
|
|
|
|
2020-11-11 00:45:47 +08:00
|
|
|
var subRun = flag.String("sub-run", "", "pass this to the -run command of the backend tests")
|
|
|
|
|
2019-08-02 20:47:45 +08:00
|
|
|
// StartFn describes the callback which should start the server with
|
|
|
|
// the Fs passed in.
|
|
|
|
// It should return a config for the backend used to connect to the
|
|
|
|
// server and a clean up function
|
2019-08-02 17:56:27 +08:00
|
|
|
type StartFn func(f fs.Fs) (configmap.Simple, func())
|
|
|
|
|
2019-08-02 20:47:45 +08:00
|
|
|
// run runs the server then runs the unit tests for the remote against
|
2019-08-02 17:56:27 +08:00
|
|
|
// it.
|
2019-08-02 20:47:45 +08:00
|
|
|
func run(t *testing.T, name string, start StartFn, useProxy bool) {
|
2019-08-09 02:58:02 +08:00
|
|
|
fremote, _, clean, err := fstest.RandomRemote()
|
2019-08-02 17:56:27 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer clean()
|
|
|
|
|
|
|
|
err = fremote.Mkdir(context.Background(), "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2019-08-02 20:47:45 +08:00
|
|
|
f := fremote
|
|
|
|
if useProxy {
|
|
|
|
// If using a proxy don't pass in the backend
|
|
|
|
f = nil
|
|
|
|
|
|
|
|
// the backend config will be made by the proxy
|
|
|
|
prog, err := filepath.Abs("../servetest/proxy_code.go")
|
|
|
|
require.NoError(t, err)
|
|
|
|
cmd := "go run " + prog + " " + fremote.Root()
|
|
|
|
|
|
|
|
// FIXME this is untidy setting a global variable!
|
|
|
|
proxyflags.Opt.AuthProxy = cmd
|
|
|
|
defer func() {
|
|
|
|
proxyflags.Opt.AuthProxy = ""
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
config, cleanup := start(f)
|
2019-08-02 17:56:27 +08:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Change directory to run the tests
|
2019-08-02 20:47:45 +08:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
require.NoError(t, err)
|
2019-08-02 17:56:27 +08:00
|
|
|
err = os.Chdir("../../../backend/" + name)
|
2019-08-02 20:47:45 +08:00
|
|
|
require.NoError(t, err, "failed to cd to "+name+" backend")
|
|
|
|
defer func() {
|
|
|
|
// Change back to the old directory
|
|
|
|
require.NoError(t, os.Chdir(cwd))
|
|
|
|
}()
|
2019-08-02 17:56:27 +08:00
|
|
|
|
|
|
|
// Run the backend tests with an on the fly remote
|
|
|
|
args := []string{"test"}
|
|
|
|
if testing.Verbose() {
|
|
|
|
args = append(args, "-v")
|
|
|
|
}
|
|
|
|
if *fstest.Verbose {
|
|
|
|
args = append(args, "-verbose")
|
|
|
|
}
|
|
|
|
remoteName := name + "test:"
|
2020-11-11 00:45:47 +08:00
|
|
|
if *subRun != "" {
|
|
|
|
args = append(args, "-run", *subRun)
|
|
|
|
}
|
2019-08-02 17:56:27 +08:00
|
|
|
args = append(args, "-remote", remoteName)
|
|
|
|
args = append(args, "-list-retries", fmt.Sprint(*fstest.ListRetries))
|
|
|
|
cmd := exec.Command("go", args...)
|
|
|
|
|
|
|
|
// Configure the backend with environment variables
|
|
|
|
cmd.Env = os.Environ()
|
|
|
|
prefix := "RCLONE_CONFIG_" + strings.ToUpper(remoteName[:len(remoteName)-1]) + "_"
|
|
|
|
for k, v := range config {
|
|
|
|
cmd.Env = append(cmd.Env, prefix+strings.ToUpper(k)+"="+v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the test
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if len(out) != 0 {
|
|
|
|
t.Logf("\n----------\n%s----------\n", string(out))
|
|
|
|
}
|
|
|
|
assert.NoError(t, err, "Running "+name+" integration tests")
|
|
|
|
}
|
2019-08-02 20:47:45 +08:00
|
|
|
|
|
|
|
// Run runs the server then runs the unit tests for the remote against
|
|
|
|
// it.
|
|
|
|
func Run(t *testing.T, name string, start StartFn) {
|
2020-06-16 04:15:15 +08:00
|
|
|
fstest.Initialise()
|
2019-08-02 20:47:45 +08:00
|
|
|
t.Run("Normal", func(t *testing.T) {
|
|
|
|
run(t, name, start, false)
|
|
|
|
})
|
|
|
|
t.Run("AuthProxy", func(t *testing.T) {
|
|
|
|
run(t, name, start, true)
|
|
|
|
})
|
|
|
|
}
|