restic/test/backend.go
Alexander Neumann 5cdcc99eba Use array instead of hash for backend.ID
Since backend.ID is always a slice of constant length, use an array
instead of a slice. Mostly, arrays behave as slices, except that an
array cannot be nil, so use `*backend.ID` insteaf of `backend.ID` in
places where the absence of an ID is possible (e.g. for the Subtree of a
Node, which may not present when the node is a file node).

This change allows to directly use backend.ID as the the key for a map,
so that arbitrary data structures (e.g. a Set implemented as a
map[backend.ID]struct{}) can easily be formed.
2015-07-25 18:01:57 +02:00

95 lines
2.1 KiB
Go

package test_helper
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/restic/restic"
"github.com/restic/restic/backend"
"github.com/restic/restic/backend/local"
"github.com/restic/restic/repository"
)
var (
TestPassword = getStringVar("RESTIC_TEST_PASSWORD", "geheim")
TestCleanup = getBoolVar("RESTIC_TEST_CLEANUP", true)
TestTempDir = getStringVar("RESTIC_TEST_TMPDIR", "")
RunIntegrationTest = getBoolVar("RESTIC_TEST_INTEGRATION", true)
RunFuseTest = getBoolVar("RESTIC_TEST_FUSE", true)
TestSFTPPath = getStringVar("RESTIC_TEST_SFTPPATH", "/usr/lib/ssh:/usr/lib/openssh")
TestWalkerPath = getStringVar("RESTIC_TEST_PATH", ".")
BenchArchiveDirectory = getStringVar("RESTIC_BENCH_DIR", ".")
)
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
}
func SetupRepo() *repository.Repository {
tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-")
if err != nil {
panic(err)
}
// create repository below temp dir
b, err := local.Create(filepath.Join(tempdir, "repo"))
if err != nil {
panic(err)
}
repo := repository.New(b)
err = repo.Init(TestPassword)
if err != nil {
panic(err)
}
return repo
}
func TeardownRepo(repo *repository.Repository) {
if !TestCleanup {
l := repo.Backend().(*local.Local)
fmt.Printf("leaving local backend at %s\n", l.Location())
return
}
err := repo.Delete()
if err != nil {
panic(err)
}
}
func SnapshotDir(t testing.TB, repo *repository.Repository, path string, parent *backend.ID) *restic.Snapshot {
arch := restic.NewArchiver(repo)
sn, _, err := arch.Snapshot(nil, []string{path}, parent)
OK(t, err)
return sn
}
func WithRepo(t testing.TB, f func(*repository.Repository)) {
repo := SetupRepo()
f(repo)
TeardownRepo(repo)
}