2015-04-26 12:46:15 +00:00
|
|
|
package test_helper
|
|
|
|
|
|
|
|
import (
|
2015-06-13 10:35:19 +00:00
|
|
|
"fmt"
|
2015-04-26 12:46:15 +00:00
|
|
|
"io/ioutil"
|
2015-06-13 10:35:19 +00:00
|
|
|
"os"
|
2015-04-26 12:46:15 +00:00
|
|
|
"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-06-05 20:33:39 +00:00
|
|
|
var (
|
2015-06-28 11:15:35 +00:00
|
|
|
TestPassword = getStringVar("RESTIC_TEST_PASSWORD", "geheim")
|
2016-01-23 18:12:02 +00:00
|
|
|
TestCleanupTempDirs = getBoolVar("RESTIC_TEST_CLEANUP", true)
|
2015-06-28 11:15:35 +00:00
|
|
|
TestTempDir = getStringVar("RESTIC_TEST_TMPDIR", "")
|
|
|
|
RunIntegrationTest = getBoolVar("RESTIC_TEST_INTEGRATION", true)
|
2015-07-18 21:19:50 +00:00
|
|
|
RunFuseTest = getBoolVar("RESTIC_TEST_FUSE", true)
|
2015-06-28 11:15:35 +00:00
|
|
|
TestSFTPPath = getStringVar("RESTIC_TEST_SFTPPATH", "/usr/lib/ssh:/usr/lib/openssh")
|
|
|
|
TestWalkerPath = getStringVar("RESTIC_TEST_PATH", ".")
|
|
|
|
BenchArchiveDirectory = getStringVar("RESTIC_BENCH_DIR", ".")
|
2015-12-19 16:21:45 +00:00
|
|
|
TestS3Server = getStringVar("RESTIC_TEST_S3_SERVER", "")
|
2015-06-05 20:33:39 +00:00
|
|
|
)
|
2015-04-26 12:46:15 +00:00
|
|
|
|
2015-06-13 10:35:19 +00:00
|
|
|
func getStringVar(name, defaultValue string) string {
|
|
|
|
if e := os.Getenv(name); e != "" {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBoolVar(name string, defaultValue bool) bool {
|
|
|
|
if e := os.Getenv(name); e != "" {
|
|
|
|
switch e {
|
|
|
|
case "1":
|
|
|
|
return true
|
|
|
|
case "0":
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "invalid value for variable %q, using default\n", name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
2015-06-26 20:12:04 +00:00
|
|
|
func SetupRepo() *repository.Repository {
|
2015-06-13 10:35:19 +00:00
|
|
|
tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-")
|
2015-06-26 20:12:04 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-04-26 12:46:15 +00:00
|
|
|
|
|
|
|
// create repository below temp dir
|
|
|
|
b, err := local.Create(filepath.Join(tempdir, "repo"))
|
2015-06-26 20:12:04 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-04-26 12:46:15 +00:00
|
|
|
|
2015-05-09 21:52:03 +00:00
|
|
|
repo := repository.New(b)
|
2015-06-26 20:12:04 +00:00
|
|
|
err = repo.Init(TestPassword)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2015-05-09 11:32:52 +00:00
|
|
|
return repo
|
2015-04-26 12:46:15 +00:00
|
|
|
}
|
|
|
|
|
2015-06-26 20:12:04 +00:00
|
|
|
func TeardownRepo(repo *repository.Repository) {
|
2016-01-23 18:12:02 +00:00
|
|
|
if !TestCleanupTempDirs {
|
2015-05-09 11:32:52 +00:00
|
|
|
l := repo.Backend().(*local.Local)
|
2015-06-26 20:12:04 +00:00
|
|
|
fmt.Printf("leaving local backend at %s\n", l.Location())
|
2015-04-26 12:46:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-26 20:12:04 +00:00
|
|
|
err := repo.Delete()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-04-26 12:46:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 15:05:45 +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
|
|
|
|
}
|
2015-06-26 20:12:04 +00:00
|
|
|
|
|
|
|
func WithRepo(t testing.TB, f func(*repository.Repository)) {
|
|
|
|
repo := SetupRepo()
|
|
|
|
f(repo)
|
|
|
|
TeardownRepo(repo)
|
|
|
|
}
|