forked from TrueCloudLab/restic
Allow specifying chunker polynomial for tests
This commit is contained in:
parent
d5323223f4
commit
4720a7d807
2 changed files with 27 additions and 4 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"restic/backend"
|
"restic/backend"
|
||||||
"restic/debug"
|
"restic/debug"
|
||||||
|
@ -63,6 +64,22 @@ func CreateConfig() (Config, error) {
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestCreateConfig creates a config for use within tests.
|
||||||
|
func TestCreateConfig(t testing.TB, pol chunker.Pol) (cfg Config) {
|
||||||
|
cfg.ChunkerPolynomial = pol
|
||||||
|
|
||||||
|
newID := make([]byte, repositoryIDSize)
|
||||||
|
_, err := io.ReadFull(rand.Reader, newID)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to create random ID: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.ID = hex.EncodeToString(newID)
|
||||||
|
cfg.Version = RepoVersion
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
// LoadConfig returns loads, checks and returns the config for a repository.
|
// LoadConfig returns loads, checks and returns the config for a repository.
|
||||||
func LoadConfig(r JSONUnpackedLoader) (Config, error) {
|
func LoadConfig(r JSONUnpackedLoader) (Config, error) {
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"restic/backend/local"
|
"restic/backend/local"
|
||||||
"restic/backend/mem"
|
"restic/backend/mem"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/restic/chunker"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestBackend returns a fully configured in-memory backend.
|
// TestBackend returns a fully configured in-memory backend.
|
||||||
|
@ -16,8 +18,11 @@ func TestBackend(t testing.TB) (be backend.Backend, cleanup func()) {
|
||||||
// TestPassword is used for all repositories created by the Test* functions.
|
// TestPassword is used for all repositories created by the Test* functions.
|
||||||
const TestPassword = "geheim"
|
const TestPassword = "geheim"
|
||||||
|
|
||||||
|
const testChunkerPol = chunker.Pol(0x3DA3358B4DC173)
|
||||||
|
|
||||||
// TestRepositoryWithBackend returns a repository initialized with a test
|
// TestRepositoryWithBackend returns a repository initialized with a test
|
||||||
// password. If be is nil, an in-memory backend is used.
|
// password. If be is nil, an in-memory backend is used. A constant polynomial
|
||||||
|
// is used for the chunker.
|
||||||
func TestRepositoryWithBackend(t testing.TB, be backend.Backend) (r *Repository, cleanup func()) {
|
func TestRepositoryWithBackend(t testing.TB, be backend.Backend) (r *Repository, cleanup func()) {
|
||||||
var beCleanup func()
|
var beCleanup func()
|
||||||
if be == nil {
|
if be == nil {
|
||||||
|
@ -26,9 +31,10 @@ func TestRepositoryWithBackend(t testing.TB, be backend.Backend) (r *Repository,
|
||||||
|
|
||||||
r = New(be)
|
r = New(be)
|
||||||
|
|
||||||
err := r.Init(TestPassword)
|
cfg := TestCreateConfig(t, testChunkerPol)
|
||||||
|
err := r.init(TestPassword, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("TestRepopository(): initialize repo failed: %v", err)
|
t.Fatalf("TestRepository(): initialize repo failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return r, func() {
|
return r, func() {
|
||||||
|
@ -41,7 +47,7 @@ func TestRepositoryWithBackend(t testing.TB, be backend.Backend) (r *Repository,
|
||||||
// TestRepository returns a repository initialized with a test password on an
|
// TestRepository returns a repository initialized with a test password on an
|
||||||
// in-memory backend. When the environment variable RESTIC_TEST_REPO is set to
|
// 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
|
// a non-existing directory, a local backend is created there and this is used
|
||||||
// instead. The directory is not removed.
|
// instead. The directory is not removed, but left there for inspection.
|
||||||
func TestRepository(t testing.TB) (r *Repository, cleanup func()) {
|
func TestRepository(t testing.TB) (r *Repository, cleanup func()) {
|
||||||
dir := os.Getenv("RESTIC_TEST_REPO")
|
dir := os.Getenv("RESTIC_TEST_REPO")
|
||||||
if dir != "" {
|
if dir != "" {
|
||||||
|
|
Loading…
Reference in a new issue