From 23910ba53b8035e651db8f5e0d26615f3822e566 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 2 Aug 2019 13:47:45 +0100 Subject: [PATCH] servetest: add tests for --auth-proxy --- cmd/serve/servetest/proxy_code.go | 35 ++++++++++++++++++++++ cmd/serve/servetest/servetest.go | 50 +++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 cmd/serve/servetest/proxy_code.go diff --git a/cmd/serve/servetest/proxy_code.go b/cmd/serve/servetest/proxy_code.go new file mode 100644 index 000000000..5340e5ee0 --- /dev/null +++ b/cmd/serve/servetest/proxy_code.go @@ -0,0 +1,35 @@ +// +build ignore + +// A simple auth proxy for testing purposes +package main + +import ( + "encoding/json" + "log" + "os" +) + +func main() { + if len(os.Args) < 2 { + log.Fatalf("Syntax: %s ", os.Args[0]) + } + root := os.Args[1] + + // Read the input + var in map[string]string + err := json.NewDecoder(os.Stdin).Decode(&in) + if err != nil { + log.Fatal(err) + } + + // Write the output + var out = map[string]string{ + "type": "local", + "_root": root, + "_obscure": "pass", + } + json.NewEncoder(os.Stdout).Encode(&out) + if err != nil { + log.Fatal(err) + } +} diff --git a/cmd/serve/servetest/servetest.go b/cmd/serve/servetest/servetest.go index f964b17dd..99cf33a32 100644 --- a/cmd/serve/servetest/servetest.go +++ b/cmd/serve/servetest/servetest.go @@ -8,22 +8,27 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "strings" "testing" + "github.com/rclone/rclone/cmd/serve/proxy/proxyflags" "github.com/rclone/rclone/fs" "github.com/rclone/rclone/fs/config/configmap" "github.com/rclone/rclone/fstest" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) -// StartFn describes the callback which should start the server, -// return a config and a clean up function +// 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 type StartFn func(f fs.Fs) (configmap.Simple, func()) -// Run runs the server then runs the unit tests for the remote against +// run runs the server then runs the unit tests for the remote against // it. -func Run(t *testing.T, name string, start StartFn) { +func run(t *testing.T, name string, start StartFn, useProxy bool) { fstest.Initialise() fremote, _, clean, err := fstest.RandomRemote(*fstest.RemoteName, *fstest.SubDir) @@ -33,12 +38,34 @@ func Run(t *testing.T, name string, start StartFn) { err = fremote.Mkdir(context.Background(), "") assert.NoError(t, err) - config, cleanup := start(fremote) + 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) defer cleanup() // Change directory to run the tests + cwd, err := os.Getwd() + require.NoError(t, err) err = os.Chdir("../../../backend/" + name) - assert.NoError(t, err, "failed to cd to "+name+" backend") + require.NoError(t, err, "failed to cd to "+name+" backend") + defer func() { + // Change back to the old directory + require.NoError(t, os.Chdir(cwd)) + }() // Run the backend tests with an on the fly remote args := []string{"test"} @@ -67,3 +94,14 @@ func Run(t *testing.T, name string, start StartFn) { } assert.NoError(t, err, "Running "+name+" integration tests") } + +// Run runs the server then runs the unit tests for the remote against +// it. +func Run(t *testing.T, name string, start StartFn) { + t.Run("Normal", func(t *testing.T) { + run(t, name, start, false) + }) + t.Run("AuthProxy", func(t *testing.T) { + run(t, name, start, true) + }) +}