From 554013ca9fd28c252042dedb128b3d4d4c9d8aa4 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 1 May 2017 23:08:16 +0200 Subject: [PATCH] rest: Convert to test suite --- src/restic/backend/rest/backend_test.go | 87 ---------------------- src/restic/backend/rest/rest_test.go | 96 ++++++++++++++++++++----- 2 files changed, 78 insertions(+), 105 deletions(-) delete mode 100644 src/restic/backend/rest/backend_test.go diff --git a/src/restic/backend/rest/backend_test.go b/src/restic/backend/rest/backend_test.go deleted file mode 100644 index 4274bfcb1..000000000 --- a/src/restic/backend/rest/backend_test.go +++ /dev/null @@ -1,87 +0,0 @@ -// DO NOT EDIT, AUTOMATICALLY GENERATED -package rest_test - -import ( - "testing" - - "restic/backend/test" -) - -var SkipMessage string - -func TestRestBackendCreate(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestCreate(t) -} - -func TestRestBackendOpen(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestOpen(t) -} - -func TestRestBackendCreateWithConfig(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestCreateWithConfig(t) -} - -func TestRestBackendLocation(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestLocation(t) -} - -func TestRestBackendConfig(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestConfig(t) -} - -func TestRestBackendLoad(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestLoad(t) -} - -func TestRestBackendSave(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestSave(t) -} - -func TestRestBackendSaveFilenames(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestSaveFilenames(t) -} - -func TestRestBackendBackend(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestBackend(t) -} - -func TestRestBackendDelete(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestDelete(t) -} - -func TestRestBackendCleanup(t *testing.T) { - if SkipMessage != "" { - t.Skip(SkipMessage) - } - test.TestCleanup(t) -} diff --git a/src/restic/backend/rest/rest_test.go b/src/restic/backend/rest/rest_test.go index f6f7676ef..d88c71e2b 100644 --- a/src/restic/backend/rest/rest_test.go +++ b/src/restic/backend/rest/rest_test.go @@ -1,39 +1,99 @@ package rest_test import ( - "fmt" + "bufio" + "io/ioutil" "net/url" "os" + "os/exec" "restic" + "strings" + "testing" "restic/backend/rest" "restic/backend/test" . "restic/test" ) -//go:generate go run ../test/generate_backend_tests.go - -func init() { - if TestRESTServer == "" { - SkipMessage = "REST test server not available" - return - } - - url, err := url.Parse(TestRESTServer) +func runRESTServer(t testing.TB, dir string) func() { + srv, err := exec.LookPath("rest-server") if err != nil { - fmt.Fprintf(os.Stderr, "invalid url: %v\n", err) - return + t.Skip(err) } - cfg := rest.Config{ - URL: url, + cmd := exec.Command(srv, "--path", dir) + cmd.Stdout = os.Stdout + stderr, err := cmd.StderrPipe() + if err != nil { + t.Fatal(err) } - test.CreateFn = func() (restic.Backend, error) { - return rest.Create(cfg) + if err := cmd.Start(); err != nil { + t.Fatal(err) } - test.OpenFn = func() (restic.Backend, error) { - return rest.Open(cfg) + sc := bufio.NewScanner(stderr) + for sc.Scan() { + if strings.HasPrefix(sc.Text(), "Starting server") { + break + } + } + + return func() { + if err := cmd.Process.Kill(); err != nil { + t.Fatal(err) + } + + // ignore errors, we've killed the process + _ = cmd.Wait() } } + +func TestBackend(t *testing.T) { + dir, cleanup := TempDir(t) + defer cleanup() + + cleanup = runRESTServer(t, dir) + defer cleanup() + + suite := test.Suite{ + // NewConfig returns a config for a new temporary backend that will be used in tests. + NewConfig: func() (interface{}, error) { + dir, err := ioutil.TempDir(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.Config{ + URL: url, + } + return cfg, nil + }, + + // CreateFn is a function that creates a temporary repository for the tests. + Create: func(config interface{}) (restic.Backend, error) { + cfg := config.(rest.Config) + return rest.Create(cfg) + }, + + // OpenFn is a function that opens a previously created temporary repository. + Open: func(config interface{}) (restic.Backend, error) { + cfg := config.(rest.Config) + return rest.Open(cfg) + }, + + // CleanupFn removes data created during the tests. + Cleanup: func(config interface{}) error { + return nil + }, + } + + suite.RunTests(t) +}