2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-10 07:11:06 +00:00

rest: Convert to test suite

This commit is contained in:
Alexander Neumann 2017-05-01 23:08:16 +02:00
parent 3250fdc2ca
commit 554013ca9f
2 changed files with 78 additions and 105 deletions

View File

@ -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)
}

View File

@ -1,39 +1,99 @@
package rest_test package rest_test
import ( import (
"fmt" "bufio"
"io/ioutil"
"net/url" "net/url"
"os" "os"
"os/exec"
"restic" "restic"
"strings"
"testing"
"restic/backend/rest" "restic/backend/rest"
"restic/backend/test" "restic/backend/test"
. "restic/test" . "restic/test"
) )
//go:generate go run ../test/generate_backend_tests.go func runRESTServer(t testing.TB, dir string) func() {
srv, err := exec.LookPath("rest-server")
func init() {
if TestRESTServer == "" {
SkipMessage = "REST test server not available"
return
}
url, err := url.Parse(TestRESTServer)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "invalid url: %v\n", err) t.Skip(err)
return
} }
cfg := rest.Config{ cmd := exec.Command(srv, "--path", dir)
URL: url, cmd.Stdout = os.Stdout
stderr, err := cmd.StderrPipe()
if err != nil {
t.Fatal(err)
} }
test.CreateFn = func() (restic.Backend, error) { if err := cmd.Start(); err != nil {
return rest.Create(cfg) t.Fatal(err)
} }
test.OpenFn = func() (restic.Backend, error) { sc := bufio.NewScanner(stderr)
return rest.Open(cfg) 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)
}