Merge pull request #1569 from restic/ext-rest-backend-test

REST: refactor backend tests
This commit is contained in:
Alexander Neumann 2018-01-21 18:02:09 +01:00
commit a3d43a92b3

View file

@ -2,7 +2,6 @@ package rest_test
import ( import (
"context" "context"
"io/ioutil"
"net" "net"
"net/url" "net/url"
"os" "os"
@ -17,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)
@ -48,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)
} }
@ -59,29 +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) {
dir, err := ioutil.TempDir(rtest.TestTempDir, "restic-test-rest-")
if err != nil {
t.Fatal(err)
}
t.Logf("create new backend at %v", dir)
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
@ -119,10 +115,29 @@ 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 TestBackendRESTExternalServer(t *testing.T) {
repostr := os.Getenv("RESTIC_TEST_REST_REPOSITORY")
if repostr == "" {
t.Skipf("environment variable %v not set", "RESTIC_TEST_REST_REPOSITORY")
}
cfg, err := rest.ParseConfig(repostr)
if err != nil {
t.Fatal(err)
}
c := cfg.(rest.Config)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
newTestSuite(ctx, t, c.URL, true).RunTests(t)
} }
func BenchmarkBackendREST(t *testing.B) { func BenchmarkBackendREST(t *testing.B) {
@ -132,8 +147,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)
} }