2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-23 05:12:10 +00:00
restic/internal/backend/rest/rest_test.go

140 lines
2.9 KiB
Go
Raw Normal View History

package rest_test
import (
2017-05-11 20:40:50 +00:00
"context"
2017-05-01 21:08:16 +00:00
"io/ioutil"
"net"
"net/url"
"os"
2017-05-01 21:08:16 +00:00
"os/exec"
"testing"
"time"
"github.com/restic/restic/internal/backend"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/backend/rest"
"github.com/restic/restic/internal/backend/test"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2017-10-02 13:06:39 +00:00
rtest "github.com/restic/restic/internal/test"
)
2017-05-11 20:40:50 +00:00
func runRESTServer(ctx context.Context, t testing.TB, dir string) func() {
2017-05-01 21:08:16 +00:00
srv, err := exec.LookPath("rest-server")
if err != nil {
t.Skip(err)
}
2017-05-11 20:40:50 +00:00
cmd := exec.CommandContext(ctx, srv, "--path", dir)
2017-05-01 21:08:16 +00:00
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stdout
2017-05-01 21:08:16 +00:00
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
// wait until the TCP port is reachable
var success bool
for i := 0; i < 10; i++ {
time.Sleep(200 * time.Millisecond)
c, err := net.Dial("tcp", "localhost:8000")
if err != nil {
continue
2017-05-01 21:08:16 +00:00
}
success = true
if err := c.Close(); err != nil {
t.Fatal(err)
}
}
if !success {
t.Fatal("unable to connect to rest server")
return nil
}
2017-05-01 21:08:16 +00:00
return func() {
if err := cmd.Process.Kill(); err != nil {
t.Fatal(err)
}
// ignore errors, we've killed the process
_ = cmd.Wait()
}
}
2017-05-01 21:08:16 +00:00
2017-05-13 19:56:18 +00:00
func newTestSuite(ctx context.Context, t testing.TB) *test.Suite {
tr, err := backend.Transport(nil)
if err != nil {
t.Fatalf("cannot create transport for tests: %v", err)
}
2017-05-13 19:56:18 +00:00
return &test.Suite{
2017-05-01 21:08:16 +00:00
// NewConfig returns a config for a new temporary backend that will be used in tests.
NewConfig: func() (interface{}, error) {
2017-10-02 13:06:39 +00:00
dir, err := ioutil.TempDir(rtest.TestTempDir, "restic-test-rest-")
2017-05-01 21:08:16 +00:00
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)
}
2017-06-05 22:25:22 +00:00
cfg := rest.NewConfig()
cfg.URL = url
2017-05-01 21:08:16 +00:00
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, tr)
2017-05-01 21:08:16 +00:00
},
// 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, tr)
2017-05-01 21:08:16 +00:00
},
// CleanupFn removes data created during the tests.
Cleanup: func(config interface{}) error {
return nil
},
}
2017-05-13 19:56:18 +00:00
}
func TestBackendREST(t *testing.T) {
defer func() {
if t.Skipped() {
2017-10-02 13:06:39 +00:00
rtest.SkipDisallowed(t, "restic/backend/rest.TestBackendREST")
2017-05-13 19:56:18 +00:00
}
}()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2017-10-02 13:06:39 +00:00
dir, cleanup := rtest.TempDir(t)
2017-05-13 19:56:18 +00:00
defer cleanup()
cleanup = runRESTServer(ctx, t, dir)
defer cleanup()
newTestSuite(ctx, t).RunTests(t)
}
func BenchmarkBackendREST(t *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2017-10-02 13:06:39 +00:00
dir, cleanup := rtest.TempDir(t)
2017-05-13 19:56:18 +00:00
defer cleanup()
cleanup = runRESTServer(ctx, t, dir)
defer cleanup()
2017-05-01 21:08:16 +00:00
2017-05-13 19:56:18 +00:00
newTestSuite(ctx, t).RunBenchmarks(t)
2017-05-01 21:08:16 +00:00
}