diff --git a/archiver_test.go b/archiver_test.go index 9aa9bffb4..5f8ee6bd0 100644 --- a/archiver_test.go +++ b/archiver_test.go @@ -48,12 +48,12 @@ func benchmarkChunkEncrypt(b testing.TB, buf, buf2 []byte, rd Rdr, key *crypto.K } func BenchmarkChunkEncrypt(b *testing.B) { + repo := SetupRepo() + defer TeardownRepo(repo) + data := Random(23, 10<<20) // 10MiB rd := bytes.NewReader(data) - s := SetupRepo(b) - defer TeardownRepo(b, s) - buf := make([]byte, chunker.MaxSize) buf2 := make([]byte, chunker.MaxSize) @@ -61,7 +61,7 @@ func BenchmarkChunkEncrypt(b *testing.B) { b.SetBytes(int64(len(data))) for i := 0; i < b.N; i++ { - benchmarkChunkEncrypt(b, buf, buf2, rd, s.Key()) + benchmarkChunkEncrypt(b, buf, buf2, rd, repo.Key()) } } @@ -82,8 +82,8 @@ func benchmarkChunkEncryptP(b *testing.PB, buf []byte, rd Rdr, key *crypto.Key) } func BenchmarkChunkEncryptParallel(b *testing.B) { - s := SetupRepo(b) - defer TeardownRepo(b, s) + repo := SetupRepo() + defer TeardownRepo(repo) data := Random(23, 10<<20) // 10MiB @@ -95,14 +95,14 @@ func BenchmarkChunkEncryptParallel(b *testing.B) { b.RunParallel(func(pb *testing.PB) { for pb.Next() { rd := bytes.NewReader(data) - benchmarkChunkEncryptP(pb, buf, rd, s.Key()) + benchmarkChunkEncryptP(pb, buf, rd, repo.Key()) } }) } func archiveDirectory(b testing.TB) { - repo := SetupRepo(b) - defer TeardownRepo(b, repo) + repo := SetupRepo() + defer TeardownRepo(repo) arch := restic.NewArchiver(repo) @@ -131,13 +131,13 @@ func BenchmarkArchiveDirectory(b *testing.B) { } func archiveWithDedup(t testing.TB) { + repo := SetupRepo() + defer TeardownRepo(repo) + if *benchArchiveDirectory == "" { t.Skip("benchdir not set, skipping TestArchiverDedup") } - repo := SetupRepo(t) - defer TeardownRepo(t, repo) - var cnt struct { before, after, after2 struct { packs, dataBlobs, treeBlobs uint @@ -195,15 +195,15 @@ func TestArchiveDedup(t *testing.T) { } func BenchmarkLoadTree(t *testing.B) { + repo := SetupRepo() + defer TeardownRepo(repo) + if *benchArchiveDirectory == "" { t.Skip("benchdir not set, skipping TestArchiverDedup") } - s := SetupRepo(t) - defer TeardownRepo(t, s) - // archive a few files - arch := restic.NewArchiver(s) + arch := restic.NewArchiver(repo) sn, _, err := arch.Snapshot(nil, []string{*benchArchiveDirectory}, nil) OK(t, err) t.Logf("archived snapshot %v", sn.ID()) @@ -211,7 +211,7 @@ func BenchmarkLoadTree(t *testing.B) { list := make([]backend.ID, 0, 10) done := make(chan struct{}) - for blob := range s.Index().Each(done) { + for blob := range repo.Index().Each(done) { if blob.Type != pack.Tree { continue } @@ -228,7 +228,7 @@ func BenchmarkLoadTree(t *testing.B) { for i := 0; i < t.N; i++ { for _, id := range list { - _, err := restic.LoadTree(s, id) + _, err := restic.LoadTree(repo, id) OK(t, err) } } diff --git a/cache_test.go b/cache_test.go index 6f342dc39..9e39e86d9 100644 --- a/cache_test.go +++ b/cache_test.go @@ -8,8 +8,8 @@ import ( ) func TestCache(t *testing.T) { - repo := SetupRepo(t) - defer TeardownRepo(t, repo) + repo := SetupRepo() + defer TeardownRepo(repo) _, err := restic.NewCache(repo, "") OK(t, err) diff --git a/repository/repository_test.go b/repository/repository_test.go index fe9f5c9d4..2ccb0b284 100644 --- a/repository/repository_test.go +++ b/repository/repository_test.go @@ -28,8 +28,8 @@ var repoTests = []testJSONStruct{ } func TestSaveJSON(t *testing.T) { - repo := SetupRepo(t) - defer TeardownRepo(t, repo) + repo := SetupRepo() + defer TeardownRepo(repo) for _, obj := range repoTests { data, err := json.Marshal(obj) @@ -47,8 +47,8 @@ func TestSaveJSON(t *testing.T) { } func BenchmarkSaveJSON(t *testing.B) { - repo := SetupRepo(t) - defer TeardownRepo(t, repo) + repo := SetupRepo() + defer TeardownRepo(repo) obj := repoTests[0] @@ -72,8 +72,8 @@ func BenchmarkSaveJSON(t *testing.B) { var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20} func TestSave(t *testing.T) { - repo := SetupRepo(t) - defer TeardownRepo(t, repo) + repo := SetupRepo() + defer TeardownRepo(repo) for _, size := range testSizes { data := make([]byte, size) @@ -104,8 +104,8 @@ func TestSave(t *testing.T) { } func TestSaveFrom(t *testing.T) { - repo := SetupRepo(t) - defer TeardownRepo(t, repo) + repo := SetupRepo() + defer TeardownRepo(repo) for _, size := range testSizes { data := make([]byte, size) @@ -134,8 +134,8 @@ func TestSaveFrom(t *testing.T) { } func BenchmarkSaveFrom(t *testing.B) { - repo := SetupRepo(t) - defer TeardownRepo(t, repo) + repo := SetupRepo() + defer TeardownRepo(repo) size := 4 << 20 // 4MiB @@ -156,13 +156,13 @@ func BenchmarkSaveFrom(t *testing.B) { } func TestLoadJSONPack(t *testing.T) { + repo := SetupRepo() + defer TeardownRepo(repo) + if *benchTestDir == "" { t.Skip("benchdir not set, skipping") } - repo := SetupRepo(t) - defer TeardownRepo(t, repo) - // archive a few files sn := SnapshotDir(t, repo, *benchTestDir, nil) OK(t, repo.Flush()) @@ -173,13 +173,13 @@ func TestLoadJSONPack(t *testing.T) { } func TestLoadJSONUnpacked(t *testing.T) { + repo := SetupRepo() + defer TeardownRepo(repo) + if *benchTestDir == "" { t.Skip("benchdir not set, skipping") } - repo := SetupRepo(t) - defer TeardownRepo(t, repo) - // archive a snapshot sn := restic.Snapshot{} sn.Hostname = "foobar" diff --git a/snapshot_test.go b/snapshot_test.go index 00bb7a038..649e9a1f4 100644 --- a/snapshot_test.go +++ b/snapshot_test.go @@ -8,9 +8,6 @@ import ( ) func TestNewSnapshot(t *testing.T) { - s := SetupRepo(t) - defer TeardownRepo(t, s) - paths := []string{"/home/foobar"} _, err := restic.NewSnapshot(paths) diff --git a/test/backend.go b/test/backend.go index bcf874c0f..60aaa1249 100644 --- a/test/backend.go +++ b/test/backend.go @@ -45,27 +45,38 @@ func getBoolVar(name string, defaultValue bool) bool { return defaultValue } -func SetupRepo(t testing.TB) *repository.Repository { +func SetupRepo() *repository.Repository { tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-") - OK(t, err) + if err != nil { + panic(err) + } // create repository below temp dir b, err := local.Create(filepath.Join(tempdir, "repo")) - OK(t, err) + if err != nil { + panic(err) + } repo := repository.New(b) - OK(t, repo.Init(TestPassword)) + err = repo.Init(TestPassword) + if err != nil { + panic(err) + } + return repo } -func TeardownRepo(t testing.TB, repo *repository.Repository) { +func TeardownRepo(repo *repository.Repository) { if !TestCleanup { l := repo.Backend().(*local.Local) - t.Logf("leaving local backend at %s\n", l.Location()) + fmt.Printf("leaving local backend at %s\n", l.Location()) return } - OK(t, repo.Delete()) + err := repo.Delete() + if err != nil { + panic(err) + } } func SnapshotDir(t testing.TB, repo *repository.Repository, path string, parent backend.ID) *restic.Snapshot { @@ -74,3 +85,9 @@ func SnapshotDir(t testing.TB, repo *repository.Repository, path string, parent OK(t, err) return sn } + +func WithRepo(t testing.TB, f func(*repository.Repository)) { + repo := SetupRepo() + f(repo) + TeardownRepo(repo) +} diff --git a/tree_test.go b/tree_test.go index b46f01b0a..2d9cb7b7e 100644 --- a/tree_test.go +++ b/tree_test.go @@ -93,8 +93,8 @@ func TestNodeComparison(t *testing.T) { } func TestLoadTree(t *testing.T) { - repo := SetupRepo(t) - defer TeardownRepo(t, repo) + repo := SetupRepo() + defer TeardownRepo(repo) // save tree tree := restic.NewTree() diff --git a/walk_test.go b/walk_test.go index 384e605df..15ff87f01 100644 --- a/walk_test.go +++ b/walk_test.go @@ -13,12 +13,12 @@ import ( var testWalkDirectory = flag.String("test.walkdir", ".", "test walking a directory (globbing pattern, default: .)") func TestWalkTree(t *testing.T) { + repo := SetupRepo() + defer TeardownRepo(repo) + dirs, err := filepath.Glob(*testWalkDirectory) OK(t, err) - repo := SetupRepo(t) - defer TeardownRepo(t, repo) - // archive a few files arch := restic.NewArchiver(repo) sn, _, err := arch.Snapshot(nil, dirs, nil)