servetest: add tests for --auth-proxy
This commit is contained in:
parent
ee7101e6af
commit
23910ba53b
2 changed files with 79 additions and 6 deletions
35
cmd/serve/servetest/proxy_code.go
Normal file
35
cmd/serve/servetest/proxy_code.go
Normal file
|
@ -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 <root>", 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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,22 +8,27 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/rclone/rclone/cmd/serve/proxy/proxyflags"
|
||||||
"github.com/rclone/rclone/fs"
|
"github.com/rclone/rclone/fs"
|
||||||
"github.com/rclone/rclone/fs/config/configmap"
|
"github.com/rclone/rclone/fs/config/configmap"
|
||||||
"github.com/rclone/rclone/fstest"
|
"github.com/rclone/rclone/fstest"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StartFn describes the callback which should start the server,
|
// StartFn describes the callback which should start the server with
|
||||||
// return a config and a clean up function
|
// 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())
|
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.
|
// it.
|
||||||
func Run(t *testing.T, name string, start StartFn) {
|
func run(t *testing.T, name string, start StartFn, useProxy bool) {
|
||||||
fstest.Initialise()
|
fstest.Initialise()
|
||||||
|
|
||||||
fremote, _, clean, err := fstest.RandomRemote(*fstest.RemoteName, *fstest.SubDir)
|
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(), "")
|
err = fremote.Mkdir(context.Background(), "")
|
||||||
assert.NoError(t, err)
|
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()
|
defer cleanup()
|
||||||
|
|
||||||
// Change directory to run the tests
|
// Change directory to run the tests
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
require.NoError(t, err)
|
||||||
err = os.Chdir("../../../backend/" + name)
|
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
|
// Run the backend tests with an on the fly remote
|
||||||
args := []string{"test"}
|
args := []string{"test"}
|
||||||
|
@ -67,3 +94,14 @@ func Run(t *testing.T, name string, start StartFn) {
|
||||||
}
|
}
|
||||||
assert.NoError(t, err, "Running "+name+" integration tests")
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue