2018-02-18 07:25:59 +08:00
|
|
|
// Serve restic tests set up a server and run the integration tests
|
|
|
|
// for restic against it.
|
2018-11-26 22:07:25 +08:00
|
|
|
|
2018-02-18 07:25:59 +08:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2024-01-30 01:54:23 +08:00
|
|
|
"errors"
|
2021-05-02 15:56:24 +08:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2018-02-18 07:25:59 +08:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
_ "github.com/rclone/rclone/backend/all"
|
2024-01-30 01:54:23 +08:00
|
|
|
"github.com/rclone/rclone/cmd"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fstest"
|
2018-02-18 07:25:59 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-05-02 15:56:24 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-18 07:25:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-05-01 18:16:22 +08:00
|
|
|
testBindAddress = "localhost:0"
|
2018-02-18 07:25:59 +08:00
|
|
|
resticSource = "../../../../../restic/restic"
|
|
|
|
)
|
|
|
|
|
2021-05-02 15:56:24 +08:00
|
|
|
func newOpt() Options {
|
|
|
|
opt := DefaultOpt
|
|
|
|
opt.HTTP.ListenAddr = []string{testBindAddress}
|
|
|
|
return opt
|
|
|
|
}
|
|
|
|
|
2018-02-18 07:25:59 +08:00
|
|
|
// TestRestic runs the restic server then runs the unit tests for the
|
|
|
|
// restic remote against it.
|
2021-05-02 15:56:24 +08:00
|
|
|
//
|
|
|
|
// Requires the restic source code in the location indicated by resticSource.
|
|
|
|
func TestResticIntegration(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2018-02-18 07:25:59 +08:00
|
|
|
_, err := os.Stat(resticSource)
|
|
|
|
if err != nil {
|
|
|
|
t.Skipf("Skipping test as restic source not found: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-05-02 15:56:24 +08:00
|
|
|
opt := newOpt()
|
2018-02-18 07:25:59 +08:00
|
|
|
|
|
|
|
fstest.Initialise()
|
|
|
|
|
2019-08-09 02:58:02 +08:00
|
|
|
fremote, _, clean, err := fstest.RandomRemote()
|
2018-02-18 07:25:59 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer clean()
|
|
|
|
|
2019-06-17 16:34:30 +08:00
|
|
|
err = fremote.Mkdir(context.Background(), "")
|
2018-02-18 07:25:59 +08:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Start the server
|
2021-05-02 15:56:24 +08:00
|
|
|
s, err := newServer(ctx, fremote, &opt)
|
|
|
|
require.NoError(t, err)
|
|
|
|
testURL := s.Server.URLs()[0]
|
2018-11-02 01:16:31 +08:00
|
|
|
defer func() {
|
2021-05-02 15:56:24 +08:00
|
|
|
_ = s.Shutdown()
|
2018-11-02 01:16:31 +08:00
|
|
|
}()
|
2018-02-18 07:25:59 +08:00
|
|
|
|
|
|
|
// Change directory to run the tests
|
|
|
|
err = os.Chdir(resticSource)
|
2021-05-02 15:56:24 +08:00
|
|
|
require.NoError(t, err, "failed to cd to restic source code")
|
2018-02-18 07:25:59 +08: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 15:56:24 +08:00
|
|
|
"RESTIC_TEST_REST_REPOSITORY=rest:"+testURL+path,
|
2020-03-19 00:48:27 +08:00
|
|
|
"GO111MODULE=on",
|
2018-02-18 07:25:59 +08: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 15:56:24 +08: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 07:25:59 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-30 01:54:23 +08:00
|
|
|
|
|
|
|
type listErrorFs struct {
|
|
|
|
fs.Fs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *listErrorFs) List(ctx context.Context, dir string) (entries fs.DirEntries, err error) {
|
|
|
|
return fs.DirEntries{}, errors.New("oops")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListErrors(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
// setup rclone with a local backend in a temporary directory
|
|
|
|
tempdir := t.TempDir()
|
|
|
|
opt := newOpt()
|
|
|
|
|
|
|
|
// make a new file system in the temp dir
|
|
|
|
f := &listErrorFs{Fs: cmd.NewFsSrc([]string{tempdir})}
|
|
|
|
s, err := newServer(ctx, f, &opt)
|
|
|
|
require.NoError(t, err)
|
|
|
|
router := s.Server.Router()
|
|
|
|
|
|
|
|
req := newRequest(t, "GET", "/test/snapshots/", nil)
|
|
|
|
checkRequest(t, router.ServeHTTP, req, []wantFunc{wantCode(http.StatusInternalServerError)})
|
|
|
|
}
|
|
|
|
|
|
|
|
type newObjectErrorFs struct {
|
|
|
|
fs.Fs
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *newObjectErrorFs) NewObject(ctx context.Context, remote string) (fs.Object, error) {
|
|
|
|
return nil, f.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServeErrors(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
// setup rclone with a local backend in a temporary directory
|
|
|
|
tempdir := t.TempDir()
|
|
|
|
opt := newOpt()
|
|
|
|
|
|
|
|
// make a new file system in the temp dir
|
|
|
|
f := &newObjectErrorFs{Fs: cmd.NewFsSrc([]string{tempdir})}
|
|
|
|
s, err := newServer(ctx, f, &opt)
|
|
|
|
require.NoError(t, err)
|
|
|
|
router := s.Server.Router()
|
|
|
|
|
|
|
|
f.err = errors.New("oops")
|
|
|
|
req := newRequest(t, "GET", "/test/config", nil)
|
|
|
|
checkRequest(t, router.ServeHTTP, req, []wantFunc{wantCode(http.StatusInternalServerError)})
|
|
|
|
|
|
|
|
f.err = fs.ErrorObjectNotFound
|
|
|
|
checkRequest(t, router.ServeHTTP, req, []wantFunc{wantCode(http.StatusNotFound)})
|
|
|
|
}
|