2015-04-26 12:46:15 +00:00
|
|
|
package test_helper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/restic/restic"
|
|
|
|
"github.com/restic/restic/backend"
|
|
|
|
"github.com/restic/restic/backend/local"
|
2015-05-09 21:52:03 +00:00
|
|
|
"github.com/restic/restic/repository"
|
2015-04-26 12:46:15 +00:00
|
|
|
)
|
|
|
|
|
2015-05-09 12:45:09 +00:00
|
|
|
var TestPassword = flag.String("test.password", "geheim", `use this password for repositories created during tests (default: "geheim")`)
|
2015-04-26 12:46:15 +00:00
|
|
|
var TestCleanup = flag.Bool("test.cleanup", true, "clean up after running tests (remove local backend directory with all content)")
|
|
|
|
var TestTempDir = flag.String("test.tempdir", "", "use this directory for temporary storage (default: system temp dir)")
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func SetupRepo(t testing.TB) *repository.Repository {
|
2015-04-26 12:46:15 +00:00
|
|
|
tempdir, err := ioutil.TempDir(*TestTempDir, "restic-test-")
|
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
// create repository below temp dir
|
|
|
|
b, err := local.Create(filepath.Join(tempdir, "repo"))
|
|
|
|
OK(t, err)
|
|
|
|
|
2015-05-09 21:52:03 +00:00
|
|
|
repo := repository.New(b)
|
2015-05-09 11:32:52 +00:00
|
|
|
OK(t, repo.Init(*TestPassword))
|
|
|
|
return repo
|
2015-04-26 12:46:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func TeardownRepo(t testing.TB, repo *repository.Repository) {
|
2015-04-26 12:46:15 +00:00
|
|
|
if !*TestCleanup {
|
2015-05-09 11:32:52 +00:00
|
|
|
l := repo.Backend().(*local.Local)
|
2015-04-26 12:46:15 +00:00
|
|
|
t.Logf("leaving local backend at %s\n", l.Location())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-09 11:32:52 +00:00
|
|
|
OK(t, repo.Delete())
|
2015-04-26 12:46:15 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 21:59:58 +00:00
|
|
|
func SnapshotDir(t testing.TB, repo *repository.Repository, path string, parent backend.ID) *restic.Snapshot {
|
2015-05-09 11:32:52 +00:00
|
|
|
arch := restic.NewArchiver(repo)
|
2015-04-26 12:46:15 +00:00
|
|
|
sn, _, err := arch.Snapshot(nil, []string{path}, parent)
|
|
|
|
OK(t, err)
|
|
|
|
return sn
|
|
|
|
}
|