2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 12:55:18 +00:00

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,154 +59,158 @@ 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()
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
if len(errs) > 0 {
t.Fatalf("expected no errors, got %v: %v", len(errs), errs)
}
repo := repository.TestOpenLocal(t, repodir)
if len(hints) > 0 {
t.Errorf("expected no hints, got %v: %v", len(hints), hints)
}
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
if len(errs) > 0 {
t.Fatalf("expected no errors, got %v: %v", len(errs), errs)
}
OKs(t, checkPacks(chkr))
OKs(t, checkStruct(chkr))
})
if len(hints) > 0 {
t.Errorf("expected no hints, got %v: %v", len(hints), hints)
}
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()
packID := "657f7fb64f6a854fff6fe9279998ee09034901eded4e6db9bcee0e59745bbce6"
OK(t, repo.Backend().Remove(restic.DataFile, packID))
repo := repository.TestOpenLocal(t, repodir)
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
if len(errs) > 0 {
t.Fatalf("expected no errors, got %v: %v", len(errs), errs)
}
packID := "657f7fb64f6a854fff6fe9279998ee09034901eded4e6db9bcee0e59745bbce6"
test.OK(t, repo.Backend().Remove(restic.DataFile, packID))
if len(hints) > 0 {
t.Errorf("expected no hints, got %v: %v", len(hints), hints)
}
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
if len(errs) > 0 {
t.Fatalf("expected no errors, got %v: %v", len(errs), errs)
}
errs = checkPacks(chkr)
if len(hints) > 0 {
t.Errorf("expected no hints, got %v: %v", len(hints), hints)
}
Assert(t, len(errs) == 1,
"expected exactly one error, got %v", len(errs))
errs = checkPacks(chkr)
if err, ok := errs[0].(checker.PackError); ok {
Equals(t, packID, err.ID.String())
} else {
t.Errorf("expected error returned by checker.Packs() to be PackError, got %v", err)
}
})
test.Assert(t, len(errs) == 1,
"expected exactly one error, got %v", len(errs))
if err, ok := errs[0].(checker.PackError); ok {
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()
// index 3f1a only references pack 60e0
indexID := "3f1abfcb79c6f7d0a3be517d2c83c8562fba64ef2c8e9a3544b4edaf8b5e3b44"
packID := "60e0438dcb978ec6860cc1f8c43da648170ee9129af8f650f876bad19f8f788e"
OK(t, repo.Backend().Remove(restic.IndexFile, indexID))
repo := repository.TestOpenLocal(t, repodir)
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
if len(errs) > 0 {
t.Fatalf("expected no errors, got %v: %v", len(errs), errs)
}
// index 3f1a only references pack 60e0
indexID := "3f1abfcb79c6f7d0a3be517d2c83c8562fba64ef2c8e9a3544b4edaf8b5e3b44"
packID := "60e0438dcb978ec6860cc1f8c43da648170ee9129af8f650f876bad19f8f788e"
test.OK(t, repo.Backend().Remove(restic.IndexFile, indexID))
if len(hints) > 0 {
t.Errorf("expected no hints, got %v: %v", len(hints), hints)
}
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
if len(errs) > 0 {
t.Fatalf("expected no errors, got %v: %v", len(errs), errs)
}
errs = checkPacks(chkr)
if len(hints) > 0 {
t.Errorf("expected no hints, got %v: %v", len(hints), hints)
}
Assert(t, len(errs) == 1,
"expected exactly one error, got %v", len(errs))
errs = checkPacks(chkr)
if err, ok := errs[0].(checker.PackError); ok {
Equals(t, packID, err.ID.String())
} else {
t.Errorf("expected error returned by checker.Packs() to be PackError, got %v", err)
}
})
test.Assert(t, len(errs) == 1,
"expected exactly one error, got %v", len(errs))
if err, ok := errs[0].(checker.PackError); ok {
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()
snID := "51d249d28815200d59e4be7b3f21a157b864dc343353df9d8e498220c2499b02"
OK(t, repo.Backend().Remove(restic.SnapshotFile, snID))
repo := repository.TestOpenLocal(t, repodir)
unusedBlobsBySnapshot := restic.IDs{
ParseID("58c748bbe2929fdf30c73262bd8313fe828f8925b05d1d4a87fe109082acb849"),
ParseID("988a272ab9768182abfd1fe7d7a7b68967825f0b861d3b36156795832c772235"),
ParseID("c01952de4d91da1b1b80bc6e06eaa4ec21523f4853b69dc8231708b9b7ec62d8"),
ParseID("bec3a53d7dc737f9a9bee68b107ec9e8ad722019f649b34d474b9982c3a3fec7"),
ParseID("2a6f01e5e92d8343c4c6b78b51c5a4dc9c39d42c04e26088c7614b13d8d0559d"),
ParseID("18b51b327df9391732ba7aaf841a4885f350d8a557b2da8352c9acf8898e3f10"),
}
snID := "51d249d28815200d59e4be7b3f21a157b864dc343353df9d8e498220c2499b02"
test.OK(t, repo.Backend().Remove(restic.SnapshotFile, snID))
sort.Sort(unusedBlobsBySnapshot)
unusedBlobsBySnapshot := restic.IDs{
test.ParseID("58c748bbe2929fdf30c73262bd8313fe828f8925b05d1d4a87fe109082acb849"),
test.ParseID("988a272ab9768182abfd1fe7d7a7b68967825f0b861d3b36156795832c772235"),
test.ParseID("c01952de4d91da1b1b80bc6e06eaa4ec21523f4853b69dc8231708b9b7ec62d8"),
test.ParseID("bec3a53d7dc737f9a9bee68b107ec9e8ad722019f649b34d474b9982c3a3fec7"),
test.ParseID("2a6f01e5e92d8343c4c6b78b51c5a4dc9c39d42c04e26088c7614b13d8d0559d"),
test.ParseID("18b51b327df9391732ba7aaf841a4885f350d8a557b2da8352c9acf8898e3f10"),
}
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
if len(errs) > 0 {
t.Fatalf("expected no errors, got %v: %v", len(errs), errs)
}
sort.Sort(unusedBlobsBySnapshot)
if len(hints) > 0 {
t.Errorf("expected no hints, got %v: %v", len(hints), hints)
}
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
if len(errs) > 0 {
t.Fatalf("expected no errors, got %v: %v", len(errs), errs)
}
OKs(t, checkPacks(chkr))
OKs(t, checkStruct(chkr))
if len(hints) > 0 {
t.Errorf("expected no hints, got %v: %v", len(hints), hints)
}
blobs := chkr.UnusedBlobs()
sort.Sort(blobs)
test.OKs(t, checkPacks(chkr))
test.OKs(t, checkStruct(chkr))
Equals(t, unusedBlobsBySnapshot, blobs)
})
blobs := chkr.UnusedBlobs()
sort.Sort(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()
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
if len(hints) == 0 {
t.Fatalf("did not get expected checker hints for duplicate packs in indexes")
repo := repository.TestOpenLocal(t, repodir)
chkr := checker.New(repo)
hints, errs := chkr.LoadIndex()
if len(hints) == 0 {
t.Fatalf("did not get expected checker hints for duplicate packs in indexes")
}
found := false
for _, hint := range hints {
if _, ok := hint.(checker.ErrDuplicatePacks); ok {
found = true
} else {
t.Errorf("got unexpected hint: %v", hint)
}
}
found := false
for _, hint := range hints {
if _, ok := hint.(checker.ErrDuplicatePacks); ok {
found = true
} else {
t.Errorf("got unexpected hint: %v", hint)
}
}
if !found {
t.Fatalf("did not find hint ErrDuplicatePacks")
}
if !found {
t.Fatalf("did not find hint ErrDuplicatePacks")
}
if len(errs) > 0 {
t.Errorf("expected no errors, got %v: %v", len(errs), errs)
}
})
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)
OK(t, repo.LoadIndex())
})
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)
b.ResetTimer()
repodir, cleanup := Env(b, repoFixture)
defer cleanup()
for i := 0; i < b.N; i++ {
repo.SetIndex(repository.NewMasterIndex())
OK(b, repo.LoadIndex())
}
})
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
}
if !TestCleanupTempDirs {
t.Logf("leaving temporary directory %v used for test", tempdir)
return
RemoveAll(t, tempdir)
}
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,53 +1341,55 @@ var walktreeTestItems = []string{
}
func TestDelayedWalkTree(t *testing.T) {
WithTestEnvironment(t, repoFixture, func(repodir string) {
repo := OpenLocalRepo(t, repodir)
OK(t, repo.LoadIndex())
repodir, cleanup := Env(t, repoFixture)
defer cleanup()
root, err := restic.ParseID("937a2f64f736c64ee700c6ab06f840c68c94799c288146a0e81e07f4c94254da")
OK(t, err)
repo := repository.TestOpenLocal(t, repodir)
OK(t, repo.LoadIndex())
dr := delayRepo{repo, 100 * time.Millisecond}
root, err := restic.ParseID("937a2f64f736c64ee700c6ab06f840c68c94799c288146a0e81e07f4c94254da")
OK(t, err)
dr := delayRepo{repo, 100 * time.Millisecond}
// start tree walker
treeJobs := make(chan walk.TreeJob)
go walk.Tree(dr, root, nil, treeJobs)
i := 0
for job := range treeJobs {
expectedPath := filepath.Join(strings.Split(walktreeTestItems[i], "/")...)
if job.Path != expectedPath {
t.Fatalf("expected path %q (%v), got %q", walktreeTestItems[i], i, job.Path)
}
i++
}
if i != len(walktreeTestItems) {
t.Fatalf("got %d items, expected %v", i, len(walktreeTestItems))
}
}
func BenchmarkDelayedWalkTree(t *testing.B) {
repodir, cleanup := Env(t, repoFixture)
defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
OK(t, repo.LoadIndex())
root, err := restic.ParseID("937a2f64f736c64ee700c6ab06f840c68c94799c288146a0e81e07f4c94254da")
OK(t, err)
dr := delayRepo{repo, 10 * time.Millisecond}
t.ResetTimer()
for i := 0; i < t.N; i++ {
// start tree walker
treeJobs := make(chan walk.TreeJob)
go walk.Tree(dr, root, nil, treeJobs)
i := 0
for job := range treeJobs {
expectedPath := filepath.Join(strings.Split(walktreeTestItems[i], "/")...)
if job.Path != expectedPath {
t.Fatalf("expected path %q (%v), got %q", walktreeTestItems[i], i, job.Path)
}
i++
for _ = range treeJobs {
}
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)
OK(t, repo.LoadIndex())
root, err := restic.ParseID("937a2f64f736c64ee700c6ab06f840c68c94799c288146a0e81e07f4c94254da")
OK(t, err)
dr := delayRepo{repo, 10 * time.Millisecond}
t.ResetTimer()
for i := 0; i < t.N; i++ {
// start tree walker
treeJobs := make(chan walk.TreeJob)
go walk.Tree(dr, root, nil, treeJobs)
for _ = range treeJobs {
}
}
})
}
}