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