2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-01 17:23:57 +00:00

rest: Refactor backend tests

This commit is contained in:
Alexander Neumann 2018-01-20 10:16:50 +01:00
parent 1046eabf95
commit 67da240068

View File

@ -16,7 +16,7 @@ import (
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
) )
func runRESTServer(ctx context.Context, t testing.TB, dir string) func() { func runRESTServer(ctx context.Context, t testing.TB, dir string) (*url.URL, func()) {
srv, err := exec.LookPath("rest-server") srv, err := exec.LookPath("rest-server")
if err != nil { if err != nil {
t.Skip(err) t.Skip(err)
@ -47,10 +47,15 @@ func runRESTServer(ctx context.Context, t testing.TB, dir string) func() {
if !success { if !success {
t.Fatal("unable to connect to rest server") t.Fatal("unable to connect to rest server")
return nil return nil, nil
} }
return func() { url, err := url.Parse("http://localhost:8000/restic-test")
if err != nil {
t.Fatal(err)
}
cleanup := func() {
if err := cmd.Process.Kill(); err != nil { if err := cmd.Process.Kill(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -58,22 +63,21 @@ func runRESTServer(ctx context.Context, t testing.TB, dir string) func() {
// ignore errors, we've killed the process // ignore errors, we've killed the process
_ = cmd.Wait() _ = cmd.Wait()
} }
return url, cleanup
} }
func newTestSuite(ctx context.Context, t testing.TB) *test.Suite { func newTestSuite(ctx context.Context, t testing.TB, url *url.URL, minimalData bool) *test.Suite {
tr, err := backend.Transport(nil) tr, err := backend.Transport(nil)
if err != nil { if err != nil {
t.Fatalf("cannot create transport for tests: %v", err) t.Fatalf("cannot create transport for tests: %v", err)
} }
return &test.Suite{ return &test.Suite{
MinimalData: minimalData,
// NewConfig returns a config for a new temporary backend that will be used in tests. // NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (interface{}, error) { NewConfig: func() (interface{}, error) {
url, err := url.Parse("http://localhost:8000/restic-test")
if err != nil {
t.Fatal(err)
}
cfg := rest.NewConfig() cfg := rest.NewConfig()
cfg.URL = url cfg.URL = url
return cfg, nil return cfg, nil
@ -111,10 +115,10 @@ func TestBackendREST(t *testing.T) {
dir, cleanup := rtest.TempDir(t) dir, cleanup := rtest.TempDir(t)
defer cleanup() defer cleanup()
cleanup = runRESTServer(ctx, t, dir) serverURL, cleanup := runRESTServer(ctx, t, dir)
defer cleanup() defer cleanup()
newTestSuite(ctx, t).RunTests(t) newTestSuite(ctx, t, serverURL, false).RunTests(t)
} }
func BenchmarkBackendREST(t *testing.B) { func BenchmarkBackendREST(t *testing.B) {
@ -124,8 +128,8 @@ func BenchmarkBackendREST(t *testing.B) {
dir, cleanup := rtest.TempDir(t) dir, cleanup := rtest.TempDir(t)
defer cleanup() defer cleanup()
cleanup = runRESTServer(ctx, t, dir) serverURL, cleanup := runRESTServer(ctx, t, dir)
defer cleanup() defer cleanup()
newTestSuite(ctx, t).RunBenchmarks(t) newTestSuite(ctx, t, serverURL, false).RunBenchmarks(t)
} }