2016-04-10 14:52:15 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"restic/backend"
|
|
|
|
"restic/backend/local"
|
|
|
|
"restic/backend/mem"
|
|
|
|
"testing"
|
2016-07-31 15:45:22 +00:00
|
|
|
|
|
|
|
"github.com/restic/chunker"
|
2016-04-10 14:52:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestBackend returns a fully configured in-memory backend.
|
|
|
|
func TestBackend(t testing.TB) (be backend.Backend, cleanup func()) {
|
|
|
|
return mem.New(), func() {}
|
|
|
|
}
|
|
|
|
|
2016-05-08 11:51:33 +00:00
|
|
|
// TestPassword is used for all repositories created by the Test* functions.
|
2016-04-10 14:52:15 +00:00
|
|
|
const TestPassword = "geheim"
|
|
|
|
|
2016-07-31 15:45:22 +00:00
|
|
|
const testChunkerPol = chunker.Pol(0x3DA3358B4DC173)
|
|
|
|
|
2016-04-10 14:52:15 +00:00
|
|
|
// TestRepositoryWithBackend returns a repository initialized with a test
|
2016-07-31 15:45:22 +00:00
|
|
|
// password. If be is nil, an in-memory backend is used. A constant polynomial
|
|
|
|
// is used for the chunker.
|
2016-04-10 14:52:15 +00:00
|
|
|
func TestRepositoryWithBackend(t testing.TB, be backend.Backend) (r *Repository, cleanup func()) {
|
|
|
|
var beCleanup func()
|
|
|
|
if be == nil {
|
|
|
|
be, beCleanup = TestBackend(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
r = New(be)
|
|
|
|
|
2016-07-31 15:45:22 +00:00
|
|
|
cfg := TestCreateConfig(t, testChunkerPol)
|
|
|
|
err := r.init(TestPassword, cfg)
|
2016-04-10 14:52:15 +00:00
|
|
|
if err != nil {
|
2016-07-31 15:45:22 +00:00
|
|
|
t.Fatalf("TestRepository(): initialize repo failed: %v", err)
|
2016-04-10 14:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r, func() {
|
|
|
|
if beCleanup != nil {
|
|
|
|
beCleanup()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestRepository returns a repository initialized with a test password on an
|
|
|
|
// in-memory backend. When the environment variable RESTIC_TEST_REPO is set to
|
|
|
|
// a non-existing directory, a local backend is created there and this is used
|
2016-07-31 15:45:22 +00:00
|
|
|
// instead. The directory is not removed, but left there for inspection.
|
2016-04-10 14:52:15 +00:00
|
|
|
func TestRepository(t testing.TB) (r *Repository, cleanup func()) {
|
|
|
|
dir := os.Getenv("RESTIC_TEST_REPO")
|
|
|
|
if dir != "" {
|
|
|
|
_, err := os.Stat(dir)
|
|
|
|
if err != nil {
|
|
|
|
be, err := local.Create(dir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating local backend at %v: %v", dir, err)
|
|
|
|
}
|
|
|
|
return TestRepositoryWithBackend(t, be)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
t.Logf("directory at %v already exists, using mem backend", dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TestRepositoryWithBackend(t, nil)
|
|
|
|
}
|