diff --git a/src/restic/archiver/archiver_test.go b/src/restic/archiver/archiver_test.go index 67a9ff8f6..6073b654d 100644 --- a/src/restic/archiver/archiver_test.go +++ b/src/restic/archiver/archiver_test.go @@ -10,6 +10,7 @@ import ( "restic/archiver" "restic/checker" "restic/crypto" + "restic/repository" . "restic/test" "restic/errors" @@ -47,7 +48,7 @@ func benchmarkChunkEncrypt(b testing.TB, buf, buf2 []byte, rd Rdr, key *crypto.K } func BenchmarkChunkEncrypt(b *testing.B) { - repo, cleanup := SetupRepo(b) + repo, cleanup := repository.TestRepository(b) defer cleanup() data := Random(23, 10<<20) // 10MiB @@ -79,7 +80,7 @@ func benchmarkChunkEncryptP(b *testing.PB, buf []byte, rd Rdr, key *crypto.Key) } func BenchmarkChunkEncryptParallel(b *testing.B) { - repo, cleanup := SetupRepo(b) + repo, cleanup := repository.TestRepository(b) defer cleanup() data := Random(23, 10<<20) // 10MiB @@ -98,7 +99,7 @@ func BenchmarkChunkEncryptParallel(b *testing.B) { } func archiveDirectory(b testing.TB) { - repo, cleanup := SetupRepo(b) + repo, cleanup := repository.TestRepository(b) defer cleanup() arch := archiver.New(repo) @@ -136,7 +137,7 @@ func countPacks(repo restic.Repository, t restic.FileType) (n uint) { } func archiveWithDedup(t testing.TB) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() if BenchArchiveDirectory == "" { @@ -208,7 +209,7 @@ func TestParallelSaveWithDuplication(t *testing.T) { } func testParallelSaveWithDuplication(t *testing.T, seed int) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() dataSizeMb := 128 diff --git a/src/restic/crypto/kdf.go b/src/restic/crypto/kdf.go index ccde35ace..158f462f1 100644 --- a/src/restic/crypto/kdf.go +++ b/src/restic/crypto/kdf.go @@ -4,9 +4,10 @@ import ( "crypto/rand" "time" + "restic/errors" + sscrypt "github.com/elithrar/simple-scrypt" "golang.org/x/crypto/scrypt" - "restic/errors" ) const saltLength = 64 diff --git a/src/restic/lock_test.go b/src/restic/lock_test.go index b60b9ea8a..a6854dbe6 100644 --- a/src/restic/lock_test.go +++ b/src/restic/lock_test.go @@ -6,11 +6,12 @@ import ( "time" "restic" + "restic/repository" . "restic/test" ) func TestLock(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() lock, err := restic.NewLock(repo) @@ -20,7 +21,7 @@ func TestLock(t *testing.T) { } func TestDoubleUnlock(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() lock, err := restic.NewLock(repo) @@ -34,7 +35,7 @@ func TestDoubleUnlock(t *testing.T) { } func TestMultipleLock(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() lock1, err := restic.NewLock(repo) @@ -48,7 +49,7 @@ func TestMultipleLock(t *testing.T) { } func TestLockExclusive(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() elock, err := restic.NewExclusiveLock(repo) @@ -57,7 +58,7 @@ func TestLockExclusive(t *testing.T) { } func TestLockOnExclusiveLockedRepo(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() elock, err := restic.NewExclusiveLock(repo) @@ -74,7 +75,7 @@ func TestLockOnExclusiveLockedRepo(t *testing.T) { } func TestExclusiveLockOnLockedRepo(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() elock, err := restic.NewLock(repo) @@ -168,7 +169,7 @@ func lockExists(repo restic.Repository, t testing.TB, id restic.ID) bool { } func TestLockWithStaleLock(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() id1, err := createFakeLock(repo, time.Now().Add(-time.Hour), os.Getpid()) @@ -193,7 +194,7 @@ func TestLockWithStaleLock(t *testing.T) { } func TestRemoveAllLocks(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() id1, err := createFakeLock(repo, time.Now().Add(-time.Hour), os.Getpid()) @@ -216,7 +217,7 @@ func TestRemoveAllLocks(t *testing.T) { } func TestLockRefresh(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() lock, err := restic.NewLock(repo) diff --git a/src/restic/repository/repository_test.go b/src/restic/repository/repository_test.go index 2a2d54aaa..a161e6509 100644 --- a/src/restic/repository/repository_test.go +++ b/src/restic/repository/repository_test.go @@ -18,7 +18,7 @@ import ( var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20} func TestSave(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() for _, size := range testSizes { @@ -54,7 +54,7 @@ func TestSave(t *testing.T) { } func TestSaveFrom(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() for _, size := range testSizes { @@ -88,7 +88,7 @@ func TestSaveFrom(t *testing.T) { } func BenchmarkSaveAndEncrypt(t *testing.B) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() size := 4 << 20 // 4MiB @@ -110,7 +110,7 @@ func BenchmarkSaveAndEncrypt(t *testing.B) { } func TestLoadTree(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() if BenchArchiveDirectory == "" { @@ -126,7 +126,7 @@ func TestLoadTree(t *testing.T) { } func BenchmarkLoadTree(t *testing.B) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() if BenchArchiveDirectory == "" { @@ -146,7 +146,7 @@ func BenchmarkLoadTree(t *testing.B) { } func TestLoadJSONUnpacked(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() if BenchArchiveDirectory == "" { @@ -207,7 +207,7 @@ func saveRandomDataBlobs(t testing.TB, repo restic.Repository, num int, sizeMax } func TestRepositoryIncrementalIndex(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() repository.IndexFull = func(*repository.Index) bool { return true } diff --git a/src/restic/test/backend.go b/src/restic/test/vars.go similarity index 57% rename from src/restic/test/backend.go rename to src/restic/test/vars.go index 055d7c10e..cde5f94a4 100644 --- a/src/restic/test/backend.go +++ b/src/restic/test/vars.go @@ -2,14 +2,7 @@ package test_helper import ( "fmt" - "io/ioutil" "os" - "path/filepath" - "testing" - - "restic" - "restic/backend/local" - "restic/repository" ) var ( @@ -47,40 +40,3 @@ func getBoolVar(name string, defaultValue bool) bool { return defaultValue } - -// SetupRepo returns a repo setup in a temp dir. -func SetupRepo(t testing.TB) (repo restic.Repository, cleanup func()) { - tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-") - if err != nil { - t.Fatal(err) - } - - // create repository below temp dir - b, err := local.Create(filepath.Join(tempdir, "repo")) - if err != nil { - t.Fatal(err) - } - - r := repository.New(b) - err = r.Init(TestPassword) - if err != nil { - t.Fatal(err) - } - repo = r - cleanup = func() { - if !TestCleanupTempDirs { - l := repo.Backend().(*local.Local) - fmt.Printf("leaving local backend at %s\n", l.Location()) - return - } - - if r, ok := repo.(restic.Deleter); ok { - err := r.Delete() - if err != nil { - t.Fatal(err) - } - } - } - - return repo, cleanup -} diff --git a/src/restic/tree_test.go b/src/restic/tree_test.go index 71c4441ed..1d23e9240 100644 --- a/src/restic/tree_test.go +++ b/src/restic/tree_test.go @@ -8,6 +8,7 @@ import ( "testing" "restic" + "restic/repository" . "restic/test" ) @@ -92,7 +93,7 @@ func TestNodeComparison(t *testing.T) { } func TestLoadTree(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() // save tree diff --git a/src/restic/walk/walk_test.go b/src/restic/walk/walk_test.go index 19874c3b0..d8416a65d 100644 --- a/src/restic/walk/walk_test.go +++ b/src/restic/walk/walk_test.go @@ -10,12 +10,13 @@ import ( "restic" "restic/archiver" "restic/pipe" + "restic/repository" . "restic/test" "restic/walk" ) func TestWalkTree(t *testing.T) { - repo, cleanup := SetupRepo(t) + repo, cleanup := repository.TestRepository(t) defer cleanup() dirs, err := filepath.Glob(TestWalkerPath)