// Package servetest provides infrastructure for running loopback // tests of "rclone serve backend:" against the backend integration // tests. package servetest import ( "context" "fmt" "os" "os/exec" "strings" "testing" "github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs/config/configmap" "github.com/rclone/rclone/fstest" "github.com/stretchr/testify/assert" ) // StartFn describes the callback which should start the server, // return a config and a clean up function type StartFn func(f fs.Fs) (configmap.Simple, func()) // Run runs the server then runs the unit tests for the remote against // it. func Run(t *testing.T, name string, start StartFn) { fstest.Initialise() fremote, _, clean, err := fstest.RandomRemote(*fstest.RemoteName, *fstest.SubDir) assert.NoError(t, err) defer clean() err = fremote.Mkdir(context.Background(), "") assert.NoError(t, err) config, cleanup := start(fremote) defer cleanup() // Change directory to run the tests err = os.Chdir("../../../backend/" + name) assert.NoError(t, err, "failed to cd to "+name+" backend") // 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:" 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") }