2018-02-17 23:25:59 +00:00
|
|
|
// Serve restic tests set up a server and run the integration tests
|
|
|
|
// for restic against it.
|
2018-11-26 14:07:25 +00:00
|
|
|
|
2018-02-17 23:25:59 +00:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
2019-06-17 08:34:30 +00:00
|
|
|
"context"
|
2018-02-17 23:25:59 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
_ "github.com/rclone/rclone/backend/all"
|
|
|
|
"github.com/rclone/rclone/cmd/serve/httplib"
|
|
|
|
"github.com/rclone/rclone/fstest"
|
2018-02-17 23:25:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-05-01 10:16:22 +00:00
|
|
|
testBindAddress = "localhost:0"
|
2018-02-17 23:25:59 +00:00
|
|
|
resticSource = "../../../../../restic/restic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestRestic runs the restic server then runs the unit tests for the
|
|
|
|
// restic remote against it.
|
|
|
|
func TestRestic(t *testing.T) {
|
|
|
|
_, err := os.Stat(resticSource)
|
|
|
|
if err != nil {
|
|
|
|
t.Skipf("Skipping test as restic source not found: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
opt := httplib.DefaultOpt
|
|
|
|
opt.ListenAddr = testBindAddress
|
|
|
|
|
|
|
|
fstest.Initialise()
|
|
|
|
|
2019-08-08 18:58:02 +00:00
|
|
|
fremote, _, clean, err := fstest.RandomRemote()
|
2018-02-17 23:25:59 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
defer clean()
|
|
|
|
|
2019-06-17 08:34:30 +00:00
|
|
|
err = fremote.Mkdir(context.Background(), "")
|
2018-02-17 23:25:59 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Start the server
|
2020-07-26 11:06:47 +00:00
|
|
|
w := NewServer(fremote, &opt)
|
2018-11-01 17:16:31 +00:00
|
|
|
assert.NoError(t, w.Serve())
|
|
|
|
defer func() {
|
|
|
|
w.Close()
|
|
|
|
w.Wait()
|
|
|
|
}()
|
2018-02-17 23:25:59 +00:00
|
|
|
|
|
|
|
// Change directory to run the tests
|
|
|
|
err = os.Chdir(resticSource)
|
|
|
|
assert.NoError(t, err, "failed to cd to restic source code")
|
|
|
|
|
|
|
|
// 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(),
|
2019-05-01 10:16:22 +00:00
|
|
|
"RESTIC_TEST_REST_REPOSITORY=rest:"+w.Server.URL()+path,
|
2020-03-18 16:48:27 +00:00
|
|
|
"GO111MODULE=on",
|
2018-02-17 23:25:59 +00: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"},
|
|
|
|
} {
|
|
|
|
got := makeRemote(test.in)
|
|
|
|
assert.Equal(t, test.want, got, test.in)
|
|
|
|
}
|
|
|
|
}
|