From 67da24006896f88c53917d66900271eae6d73c5a Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sat, 20 Jan 2018 10:16:50 +0100 Subject: [PATCH] rest: Refactor backend tests --- internal/backend/rest/rest_test.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/internal/backend/rest/rest_test.go b/internal/backend/rest/rest_test.go index 3119baf5d..f8ad17c27 100644 --- a/internal/backend/rest/rest_test.go +++ b/internal/backend/rest/rest_test.go @@ -16,7 +16,7 @@ import ( 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") if err != nil { t.Skip(err) @@ -47,10 +47,15 @@ func runRESTServer(ctx context.Context, t testing.TB, dir string) func() { if !success { 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 { 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 _ = 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) if err != nil { t.Fatalf("cannot create transport for tests: %v", err) } return &test.Suite{ + MinimalData: minimalData, + // NewConfig returns a config for a new temporary backend that will be used in tests. NewConfig: func() (interface{}, error) { - url, err := url.Parse("http://localhost:8000/restic-test") - if err != nil { - t.Fatal(err) - } - cfg := rest.NewConfig() cfg.URL = url return cfg, nil @@ -111,10 +115,10 @@ func TestBackendREST(t *testing.T) { dir, cleanup := rtest.TempDir(t) defer cleanup() - cleanup = runRESTServer(ctx, t, dir) + serverURL, cleanup := runRESTServer(ctx, t, dir) defer cleanup() - newTestSuite(ctx, t).RunTests(t) + newTestSuite(ctx, t, serverURL, false).RunTests(t) } func BenchmarkBackendREST(t *testing.B) { @@ -124,8 +128,8 @@ func BenchmarkBackendREST(t *testing.B) { dir, cleanup := rtest.TempDir(t) defer cleanup() - cleanup = runRESTServer(ctx, t, dir) + serverURL, cleanup := runRESTServer(ctx, t, dir) defer cleanup() - newTestSuite(ctx, t).RunBenchmarks(t) + newTestSuite(ctx, t, serverURL, false).RunBenchmarks(t) }