2016-02-20 21:05:48 +00:00
|
|
|
package rest_test
|
|
|
|
|
|
|
|
import (
|
2017-05-11 20:40:50 +00:00
|
|
|
"context"
|
2017-05-01 21:08:16 +00:00
|
|
|
"io/ioutil"
|
2017-05-12 19:33:34 +00:00
|
|
|
"net"
|
2016-02-20 21:05:48 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2017-05-01 21:08:16 +00:00
|
|
|
"os/exec"
|
2016-08-31 20:39:36 +00:00
|
|
|
"restic"
|
2017-05-01 21:08:16 +00:00
|
|
|
"testing"
|
2017-05-12 19:33:34 +00:00
|
|
|
"time"
|
2016-02-20 21:05:48 +00:00
|
|
|
|
|
|
|
"restic/backend/rest"
|
|
|
|
"restic/backend/test"
|
|
|
|
. "restic/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)
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
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
|
2017-05-12 19:33:34 +00:00
|
|
|
cmd.Stderr = os.Stdout
|
2017-05-01 21:08:16 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
2017-05-12 19:33:34 +00:00
|
|
|
// wait until the TCP port is reachable
|
|
|
|
var success bool
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
c, err := net.Dial("tcp", "localhost:8000")
|
|
|
|
if err != nil {
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
continue
|
2017-05-01 21:08:16 +00:00
|
|
|
}
|
2017-05-12 19:33:34 +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
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2016-02-20 21:05:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-01 21:08:16 +00:00
|
|
|
|
|
|
|
func TestBackend(t *testing.T) {
|
2017-05-11 20:40:50 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2017-05-01 21:08:16 +00:00
|
|
|
dir, cleanup := TempDir(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-05-11 20:40:50 +00:00
|
|
|
cleanup = runRESTServer(ctx, t, dir)
|
2017-05-01 21:08:16 +00:00
|
|
|
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)
|
|
|
|
}
|