2018-02-18 00:25:59 +01:00
|
|
|
// Serve restic tests set up a server and run the integration tests
|
|
|
|
// for restic against it.
|
2018-11-26 15:07:25 +01:00
|
|
|
|
2018-02-18 00:25:59 +01:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2021-05-02 09:56:24 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-02-18 00:25:59 +01:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
_ "github.com/rclone/rclone/backend/all"
|
|
|
|
"github.com/rclone/rclone/fstest"
|
2018-02-18 00:25:59 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-05-02 09:56:24 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-18 00:25:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-05-01 12:16:22 +02:00
|
|
|
testBindAddress = "localhost:0"
|
2018-02-18 00:25:59 +01:00
|
|
|
resticSource = "../../../../../restic/restic"
|
|
|
|
)
|
|
|
|
|
2021-05-02 09:56:24 +02:00
|
|
|
func newOpt() Options {
|
|
|
|
opt := DefaultOpt
|
|
|
|
opt.HTTP.ListenAddr = []string{testBindAddress}
|
|
|
|
return opt
|
|
|
|
}
|
|
|
|
|
2018-02-18 00:25:59 +01:00
|
|
|
// TestRestic runs the restic server then runs the unit tests for the
|
|
|
|
// restic remote against it.
|
2021-05-02 09:56:24 +02:00
|
|
|
//
|
|
|
|
// Requires the restic source code in the location indicated by resticSource.
|
|
|
|
func TestResticIntegration(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2018-02-18 00:25:59 +01:00
|
|
|
_, err := os.Stat(resticSource)
|
|
|
|
if err != nil {
|
|
|
|
t.Skipf("Skipping test as restic source not found: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-05-02 09:56:24 +02:00
|
|
|
opt := newOpt()
|
2018-02-18 00:25:59 +01:00
|
|
|
|
|
|
|
fstest.Initialise()
|
|
|
|
|
2019-08-08 20:58:02 +02:00
|
|
|
fremote, _, clean, err := fstest.RandomRemote()
|
2018-02-18 00:25:59 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer clean()
|
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
err = fremote.Mkdir(context.Background(), "")
|
2018-02-18 00:25:59 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Start the server
|
2021-05-02 09:56:24 +02:00
|
|
|
s, err := newServer(ctx, fremote, &opt)
|
|
|
|
require.NoError(t, err)
|
|
|
|
testURL := s.Server.URLs()[0]
|
2018-11-01 18:16:31 +01:00
|
|
|
defer func() {
|
2021-05-02 09:56:24 +02:00
|
|
|
_ = s.Shutdown()
|
2018-11-01 18:16:31 +01:00
|
|
|
}()
|
2018-02-18 00:25:59 +01:00
|
|
|
|
|
|
|
// Change directory to run the tests
|
|
|
|
err = os.Chdir(resticSource)
|
2021-05-02 09:56:24 +02:00
|
|
|
require.NoError(t, err, "failed to cd to restic source code")
|
2018-02-18 00:25:59 +01:00
|
|
|
|
|
|
|
// Run the restic tests
|
|
|
|
runTests := func(path string) {
|
|
|
|
args := []string{"test", "./internal/backend/rest", "-run", "TestBackendRESTExternalServer", "-count=1"}
|
|
|
|
if testing.Verbose() {
|
|
|
|
args = append(args, "-v")
|
|
|
|
}
|
|
|
|
cmd := exec.Command("go", args...)
|
|
|
|
cmd.Env = append(os.Environ(),
|
2021-05-02 09:56:24 +02:00
|
|
|
"RESTIC_TEST_REST_REPOSITORY=rest:"+testURL+path,
|
2020-03-18 17:48:27 +01:00
|
|
|
"GO111MODULE=on",
|
2018-02-18 00:25:59 +01:00
|
|
|
)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if len(out) != 0 {
|
|
|
|
t.Logf("\n----------\n%s----------\n", string(out))
|
|
|
|
}
|
|
|
|
assert.NoError(t, err, "Running restic integration tests")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run the tests with no path
|
|
|
|
runTests("")
|
|
|
|
//... and again with a path
|
|
|
|
runTests("potato/sausage/")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeRemote(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
in, want string
|
|
|
|
}{
|
|
|
|
{"/", ""},
|
|
|
|
{"/data", "data"},
|
|
|
|
{"/data/", "data"},
|
|
|
|
{"/data/1", "data/1"},
|
|
|
|
{"/data/12", "data/12/12"},
|
|
|
|
{"/data/123", "data/12/123"},
|
|
|
|
{"/data/123/", "data/12/123"},
|
|
|
|
{"/keys", "keys"},
|
|
|
|
{"/keys/1", "keys/1"},
|
|
|
|
{"/keys/12", "keys/12"},
|
|
|
|
{"/keys/123", "keys/123"},
|
|
|
|
} {
|
2021-05-02 09:56:24 +02:00
|
|
|
r := httptest.NewRequest("GET", test.in, nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
next := http.HandlerFunc(func(_ http.ResponseWriter, request *http.Request) {
|
|
|
|
remote, ok := request.Context().Value(ContextRemoteKey).(string)
|
|
|
|
assert.True(t, ok, "Failed to get remote from context")
|
|
|
|
assert.Equal(t, test.want, remote, test.in)
|
|
|
|
})
|
|
|
|
got := WithRemote(next)
|
|
|
|
got.ServeHTTP(w, r)
|
2018-02-18 00:25:59 +01:00
|
|
|
}
|
|
|
|
}
|