Rename WithTestEnvironment -> Env

This commit is contained in:
Alexander Neumann 2016-09-04 14:29:04 +02:00
parent 6ab425f130
commit 512a92895f
7 changed files with 207 additions and 200 deletions

View file

@ -1,7 +1,6 @@
package checker_test
import (
"fmt"
"math/rand"
"path/filepath"
"sort"
@ -12,7 +11,7 @@ import (
"restic/backend/mem"
"restic/checker"
"restic/repository"
. "restic/test"
"restic/test"
)
var checkerTestData = filepath.Join("testdata", "checker-test-repo.tar.gz")
@ -60,8 +59,10 @@ func checkData(chkr *checker.Checker) []error {
}
func TestCheckRepo(t *testing.T) {
WithTestEnvironment(t, checkerTestData, func(repodir string) {
repo := OpenLocalRepo(t, repodir)
repodir, cleanup := test.Env(t, checkerTestData)
defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
@ -73,17 +74,18 @@ func TestCheckRepo(t *testing.T) {
t.Errorf("expected no hints, got %v: %v", len(hints), hints)
}
OKs(t, checkPacks(chkr))
OKs(t, checkStruct(chkr))
})
test.OKs(t, checkPacks(chkr))
test.OKs(t, checkStruct(chkr))
}
func TestMissingPack(t *testing.T) {
WithTestEnvironment(t, checkerTestData, func(repodir string) {
repo := OpenLocalRepo(t, repodir)
repodir, cleanup := test.Env(t, checkerTestData)
defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
packID := "657f7fb64f6a854fff6fe9279998ee09034901eded4e6db9bcee0e59745bbce6"
OK(t, repo.Backend().Remove(restic.DataFile, packID))
test.OK(t, repo.Backend().Remove(restic.DataFile, packID))
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
@ -97,25 +99,26 @@ func TestMissingPack(t *testing.T) {
errs = checkPacks(chkr)
Assert(t, len(errs) == 1,
test.Assert(t, len(errs) == 1,
"expected exactly one error, got %v", len(errs))
if err, ok := errs[0].(checker.PackError); ok {
Equals(t, packID, err.ID.String())
test.Equals(t, packID, err.ID.String())
} else {
t.Errorf("expected error returned by checker.Packs() to be PackError, got %v", err)
}
})
}
func TestUnreferencedPack(t *testing.T) {
WithTestEnvironment(t, checkerTestData, func(repodir string) {
repo := OpenLocalRepo(t, repodir)
repodir, cleanup := test.Env(t, checkerTestData)
defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
// index 3f1a only references pack 60e0
indexID := "3f1abfcb79c6f7d0a3be517d2c83c8562fba64ef2c8e9a3544b4edaf8b5e3b44"
packID := "60e0438dcb978ec6860cc1f8c43da648170ee9129af8f650f876bad19f8f788e"
OK(t, repo.Backend().Remove(restic.IndexFile, indexID))
test.OK(t, repo.Backend().Remove(restic.IndexFile, indexID))
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
@ -129,31 +132,32 @@ func TestUnreferencedPack(t *testing.T) {
errs = checkPacks(chkr)
Assert(t, len(errs) == 1,
test.Assert(t, len(errs) == 1,
"expected exactly one error, got %v", len(errs))
if err, ok := errs[0].(checker.PackError); ok {
Equals(t, packID, err.ID.String())
test.Equals(t, packID, err.ID.String())
} else {
t.Errorf("expected error returned by checker.Packs() to be PackError, got %v", err)
}
})
}
func TestUnreferencedBlobs(t *testing.T) {
WithTestEnvironment(t, checkerTestData, func(repodir string) {
repo := OpenLocalRepo(t, repodir)
repodir, cleanup := test.Env(t, checkerTestData)
defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
snID := "51d249d28815200d59e4be7b3f21a157b864dc343353df9d8e498220c2499b02"
OK(t, repo.Backend().Remove(restic.SnapshotFile, snID))
test.OK(t, repo.Backend().Remove(restic.SnapshotFile, snID))
unusedBlobsBySnapshot := restic.IDs{
ParseID("58c748bbe2929fdf30c73262bd8313fe828f8925b05d1d4a87fe109082acb849"),
ParseID("988a272ab9768182abfd1fe7d7a7b68967825f0b861d3b36156795832c772235"),
ParseID("c01952de4d91da1b1b80bc6e06eaa4ec21523f4853b69dc8231708b9b7ec62d8"),
ParseID("bec3a53d7dc737f9a9bee68b107ec9e8ad722019f649b34d474b9982c3a3fec7"),
ParseID("2a6f01e5e92d8343c4c6b78b51c5a4dc9c39d42c04e26088c7614b13d8d0559d"),
ParseID("18b51b327df9391732ba7aaf841a4885f350d8a557b2da8352c9acf8898e3f10"),
test.ParseID("58c748bbe2929fdf30c73262bd8313fe828f8925b05d1d4a87fe109082acb849"),
test.ParseID("988a272ab9768182abfd1fe7d7a7b68967825f0b861d3b36156795832c772235"),
test.ParseID("c01952de4d91da1b1b80bc6e06eaa4ec21523f4853b69dc8231708b9b7ec62d8"),
test.ParseID("bec3a53d7dc737f9a9bee68b107ec9e8ad722019f649b34d474b9982c3a3fec7"),
test.ParseID("2a6f01e5e92d8343c4c6b78b51c5a4dc9c39d42c04e26088c7614b13d8d0559d"),
test.ParseID("18b51b327df9391732ba7aaf841a4885f350d8a557b2da8352c9acf8898e3f10"),
}
sort.Sort(unusedBlobsBySnapshot)
@ -168,21 +172,22 @@ func TestUnreferencedBlobs(t *testing.T) {
t.Errorf("expected no hints, got %v: %v", len(hints), hints)
}
OKs(t, checkPacks(chkr))
OKs(t, checkStruct(chkr))
test.OKs(t, checkPacks(chkr))
test.OKs(t, checkStruct(chkr))
blobs := chkr.UnusedBlobs()
sort.Sort(blobs)
Equals(t, unusedBlobsBySnapshot, blobs)
})
test.Equals(t, unusedBlobsBySnapshot, blobs)
}
var checkerDuplicateIndexTestData = filepath.Join("testdata", "duplicate-packs-in-index-test-repo.tar.gz")
func TestDuplicatePacksInIndex(t *testing.T) {
WithTestEnvironment(t, checkerDuplicateIndexTestData, func(repodir string) {
repo := OpenLocalRepo(t, repodir)
repodir, cleanup := test.Env(t, checkerDuplicateIndexTestData)
defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
@ -206,8 +211,6 @@ func TestDuplicatePacksInIndex(t *testing.T) {
if len(errs) > 0 {
t.Errorf("expected no errors, got %v: %v", len(errs), errs)
}
})
}
// errorBackend randomly modifies data after reading.
@ -217,7 +220,6 @@ type errorBackend struct {
}
func (b errorBackend) Load(h restic.Handle, p []byte, off int64) (int, error) {
fmt.Printf("load %v\n", h)
n, err := b.Backend.Load(h, p, off)
if b.ProduceErrors {
@ -242,16 +244,16 @@ func TestCheckerModifiedData(t *testing.T) {
repository.TestUseLowSecurityKDFParameters(t)
repo := repository.New(be)
OK(t, repo.Init(TestPassword))
test.OK(t, repo.Init(test.TestPassword))
arch := archiver.New(repo)
_, id, err := arch.Snapshot(nil, []string{"."}, nil)
OK(t, err)
test.OK(t, err)
t.Logf("archived as %v", id.Str())
beError := &errorBackend{Backend: be}
checkRepo := repository.New(beError)
OK(t, checkRepo.SearchKey(TestPassword, 5))
test.OK(t, checkRepo.SearchKey(test.TestPassword, 5))
chkr := checker.New(checkRepo)

View file

@ -174,22 +174,24 @@ func TestLoadJSONUnpacked(t *testing.T) {
var repoFixture = filepath.Join("testdata", "test-repo.tar.gz")
func TestRepositoryLoadIndex(t *testing.T) {
WithTestEnvironment(t, repoFixture, func(repodir string) {
repo := OpenLocalRepo(t, repodir)
repodir, cleanup := Env(t, repoFixture)
defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
OK(t, repo.LoadIndex())
})
}
func BenchmarkLoadIndex(b *testing.B) {
WithTestEnvironment(b, repoFixture, func(repodir string) {
repo := OpenLocalRepo(b, repodir)
repodir, cleanup := Env(b, repoFixture)
defer cleanup()
repo := repository.TestOpenLocal(b, repodir)
b.ResetTimer()
for i := 0; i < b.N; i++ {
repo.SetIndex(repository.NewMasterIndex())
OK(b, repo.LoadIndex())
}
})
}
// saveRandomDataBlobs generates random data blobs and saves them to the repository.

View file

@ -37,7 +37,7 @@ const testChunkerPol = chunker.Pol(0x3DA3358B4DC173)
// TestRepositoryWithBackend returns a repository initialized with a test
// password. If be is nil, an in-memory backend is used. A constant polynomial
// is used for the chunker and low-security test parameters.
func TestRepositoryWithBackend(t testing.TB, be restic.Backend) (r *Repository, cleanup func()) {
func TestRepositoryWithBackend(t testing.TB, be restic.Backend) (r restic.Repository, cleanup func()) {
TestUseLowSecurityKDFParameters(t)
var beCleanup func()
@ -45,15 +45,15 @@ func TestRepositoryWithBackend(t testing.TB, be restic.Backend) (r *Repository,
be, beCleanup = TestBackend(t)
}
r = New(be)
repo := New(be)
cfg := restic.TestCreateConfig(t, testChunkerPol)
err := r.init(TestPassword, cfg)
err := repo.init(TestPassword, cfg)
if err != nil {
t.Fatalf("TestRepository(): initialize repo failed: %v", err)
}
return r, func() {
return repo, func() {
if beCleanup != nil {
beCleanup()
}
@ -64,7 +64,7 @@ func TestRepositoryWithBackend(t testing.TB, be restic.Backend) (r *Repository,
// 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, but left there for inspection.
func TestRepository(t testing.TB) (r *Repository, cleanup func()) {
func TestRepository(t testing.TB) (r restic.Repository, cleanup func()) {
dir := os.Getenv("RESTIC_TEST_REPO")
if dir != "" {
_, err := os.Stat(dir)
@ -83,3 +83,19 @@ func TestRepository(t testing.TB) (r *Repository, cleanup func()) {
return TestRepositoryWithBackend(t, nil)
}
// TestOpenLocal opens a local repository.
func TestOpenLocal(t testing.TB, dir string) (r restic.Repository) {
be, err := local.Open(dir)
if err != nil {
t.Fatal(err)
}
repo := New(be)
err = repo.SearchKey(TestPassword, 10)
if err != nil {
t.Fatal(err)
}
return repo
}

View file

@ -1,2 +1,2 @@
// Package test_helper provides helper functions for writing tests for restic.
package test_helper
// Package test provides helper functions for writing tests for restic.
package test

View file

@ -1,4 +1,4 @@
package test_helper
package test
import (
"compress/bzip2"
@ -16,9 +16,6 @@ import (
"testing"
mrand "math/rand"
"restic/backend/local"
"restic/repository"
)
// Assert fails the test if the condition is false.
@ -184,40 +181,28 @@ func SetupTarTestFixture(t testing.TB, outputDir, tarFile string) {
OK(t, cmd.Run())
}
// WithTestEnvironment creates a test environment, extracts the repository
// fixture and and calls f with the repository dir.
func WithTestEnvironment(t testing.TB, repoFixture string, f func(repodir string)) {
// Env creates a test environment and extracts the repository fixture.
// Returned is the repo path and a cleanup function.
func Env(t testing.TB, repoFixture string) (repodir string, cleanup func()) {
tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-")
OK(t, err)
fd, err := os.Open(repoFixture)
if err != nil {
panic(err)
t.Fatal(err)
}
OK(t, fd.Close())
SetupTarTestFixture(t, tempdir, repoFixture)
f(filepath.Join(tempdir, "repo"))
return filepath.Join(tempdir, "repo"), func() {
if !TestCleanupTempDirs {
t.Logf("leaving temporary directory %v used for test", tempdir)
return
}
RemoveAll(t, tempdir)
}
// OpenLocalRepo opens the local repository located at dir.
func OpenLocalRepo(t testing.TB, dir string) restic.Repository {
be, err := local.Open(dir)
OK(t, err)
repo := repository.New(be)
err = repo.SearchKey(TestPassword, 10)
OK(t, err)
return repo
}
}
func isFile(fi os.FileInfo) bool {

View file

@ -1,4 +1,4 @@
package test_helper
package test
import (
"fmt"

View file

@ -1341,8 +1341,10 @@ var walktreeTestItems = []string{
}
func TestDelayedWalkTree(t *testing.T) {
WithTestEnvironment(t, repoFixture, func(repodir string) {
repo := OpenLocalRepo(t, repodir)
repodir, cleanup := Env(t, repoFixture)
defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
OK(t, repo.LoadIndex())
root, err := restic.ParseID("937a2f64f736c64ee700c6ab06f840c68c94799c288146a0e81e07f4c94254da")
@ -1366,12 +1368,13 @@ func TestDelayedWalkTree(t *testing.T) {
if i != len(walktreeTestItems) {
t.Fatalf("got %d items, expected %v", i, len(walktreeTestItems))
}
})
}
func BenchmarkDelayedWalkTree(t *testing.B) {
WithTestEnvironment(t, repoFixture, func(repodir string) {
repo := OpenLocalRepo(t, repodir)
repodir, cleanup := Env(t, repoFixture)
defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
OK(t, repo.LoadIndex())
root, err := restic.ParseID("937a2f64f736c64ee700c6ab06f840c68c94799c288146a0e81e07f4c94254da")
@ -1389,5 +1392,4 @@ func BenchmarkDelayedWalkTree(t *testing.B) {
for _ = range treeJobs {
}
}
})
}