Allow specifying chunker polynomial for tests

This commit is contained in:
Alexander Neumann 2016-07-31 17:45:22 +02:00
parent d5323223f4
commit 4720a7d807
2 changed files with 27 additions and 4 deletions

View file

@ -6,6 +6,7 @@ import (
"encoding/hex"
"errors"
"io"
"testing"
"restic/backend"
"restic/debug"
@ -63,6 +64,22 @@ func CreateConfig() (Config, error) {
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.
func LoadConfig(r JSONUnpackedLoader) (Config, error) {
var (

View file

@ -6,6 +6,8 @@ import (
"restic/backend/local"
"restic/backend/mem"
"testing"
"github.com/restic/chunker"
)
// 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.
const TestPassword = "geheim"
const testChunkerPol = chunker.Pol(0x3DA3358B4DC173)
// 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()) {
var beCleanup func()
if be == nil {
@ -26,9 +31,10 @@ func TestRepositoryWithBackend(t testing.TB, be backend.Backend) (r *Repository,
r = New(be)
err := r.Init(TestPassword)
cfg := TestCreateConfig(t, testChunkerPol)
err := r.init(TestPassword, cfg)
if err != nil {
t.Fatalf("TestRepopository(): initialize repo failed: %v", err)
t.Fatalf("TestRepository(): initialize repo failed: %v", err)
}
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
// 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
// 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()) {
dir := os.Getenv("RESTIC_TEST_REPO")
if dir != "" {