github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/cmd/serve/servetest/servetest.go (about) 1 // Package servetest provides infrastructure for running loopback 2 // tests of "rclone serve backend:" against the backend integration 3 // tests. 4 package servetest 5 6 import ( 7 "context" 8 "fmt" 9 "os" 10 "os/exec" 11 "path/filepath" 12 "strings" 13 "testing" 14 15 "github.com/rclone/rclone/cmd/serve/proxy/proxyflags" 16 "github.com/rclone/rclone/fs" 17 "github.com/rclone/rclone/fs/config/configmap" 18 "github.com/rclone/rclone/fstest" 19 "github.com/stretchr/testify/assert" 20 "github.com/stretchr/testify/require" 21 ) 22 23 // StartFn describes the callback which should start the server with 24 // the Fs passed in. 25 // It should return a config for the backend used to connect to the 26 // server and a clean up function 27 type StartFn func(f fs.Fs) (configmap.Simple, func()) 28 29 // run runs the server then runs the unit tests for the remote against 30 // it. 31 func run(t *testing.T, name string, start StartFn, useProxy bool) { 32 fstest.Initialise() 33 34 fremote, _, clean, err := fstest.RandomRemote() 35 assert.NoError(t, err) 36 defer clean() 37 38 err = fremote.Mkdir(context.Background(), "") 39 assert.NoError(t, err) 40 41 f := fremote 42 if useProxy { 43 // If using a proxy don't pass in the backend 44 f = nil 45 46 // the backend config will be made by the proxy 47 prog, err := filepath.Abs("../servetest/proxy_code.go") 48 require.NoError(t, err) 49 cmd := "go run " + prog + " " + fremote.Root() 50 51 // FIXME this is untidy setting a global variable! 52 proxyflags.Opt.AuthProxy = cmd 53 defer func() { 54 proxyflags.Opt.AuthProxy = "" 55 }() 56 } 57 config, cleanup := start(f) 58 defer cleanup() 59 60 // Change directory to run the tests 61 cwd, err := os.Getwd() 62 require.NoError(t, err) 63 err = os.Chdir("../../../backend/" + name) 64 require.NoError(t, err, "failed to cd to "+name+" backend") 65 defer func() { 66 // Change back to the old directory 67 require.NoError(t, os.Chdir(cwd)) 68 }() 69 70 // Run the backend tests with an on the fly remote 71 args := []string{"test"} 72 if testing.Verbose() { 73 args = append(args, "-v") 74 } 75 if *fstest.Verbose { 76 args = append(args, "-verbose") 77 } 78 remoteName := name + "test:" 79 args = append(args, "-remote", remoteName) 80 args = append(args, "-list-retries", fmt.Sprint(*fstest.ListRetries)) 81 cmd := exec.Command("go", args...) 82 83 // Configure the backend with environment variables 84 cmd.Env = os.Environ() 85 prefix := "RCLONE_CONFIG_" + strings.ToUpper(remoteName[:len(remoteName)-1]) + "_" 86 for k, v := range config { 87 cmd.Env = append(cmd.Env, prefix+strings.ToUpper(k)+"="+v) 88 } 89 90 // Run the test 91 out, err := cmd.CombinedOutput() 92 if len(out) != 0 { 93 t.Logf("\n----------\n%s----------\n", string(out)) 94 } 95 assert.NoError(t, err, "Running "+name+" integration tests") 96 } 97 98 // Run runs the server then runs the unit tests for the remote against 99 // it. 100 func Run(t *testing.T, name string, start StartFn) { 101 t.Run("Normal", func(t *testing.T) { 102 run(t, name, start, false) 103 }) 104 t.Run("AuthProxy", func(t *testing.T) { 105 run(t, name, start, true) 106 }) 107 }