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

repository: Remove empty cleanup functions in tests

TestRepository and its variants always returned no-op cleanup functions.
If they ever do need to do cleanup, using testing.T.Cleanup is easier
than passing these functions around.
This commit is contained in:
greatroar 2022-12-11 10:41:22 +01:00
parent f90bf84ba7
commit c0b5ec55ab
19 changed files with 91 additions and 190 deletions

View File

@ -25,13 +25,13 @@ import (
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
func prepareTempdirRepoSrc(t testing.TB, src TestDir) (tempdir string, repo restic.Repository, cleanup func()) { func prepareTempdirRepoSrc(t testing.TB, src TestDir) (string, restic.Repository) {
tempdir = restictest.TempDir(t) tempdir := restictest.TempDir(t)
repo, removeRepository := repository.TestRepository(t) repo := repository.TestRepository(t)
TestCreateFiles(t, tempdir, src) TestCreateFiles(t, tempdir, src)
return tempdir, repo, removeRepository return tempdir, repo
} }
func saveFile(t testing.TB, repo restic.Repository, filename string, filesystem fs.FS) (*restic.Node, ItemStats) { func saveFile(t testing.TB, repo restic.Repository, filename string, filesystem fs.FS) (*restic.Node, ItemStats) {
@ -139,9 +139,7 @@ func TestArchiverSaveFile(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, TestDir{"file": testfile}) tempdir, repo := prepareTempdirRepoSrc(t, TestDir{"file": testfile})
defer cleanup()
node, stats := saveFile(t, repo, filepath.Join(tempdir, "file"), fs.Track{FS: fs.Local{}}) node, stats := saveFile(t, repo, filepath.Join(tempdir, "file"), fs.Track{FS: fs.Local{}})
TestEnsureFileContent(ctx, t, repo, "file", node, testfile) TestEnsureFileContent(ctx, t, repo, "file", node, testfile)
@ -174,8 +172,7 @@ func TestArchiverSaveFileReaderFS(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
ts := time.Now() ts := time.Now()
filename := "xx" filename := "xx"
@ -217,8 +214,7 @@ func TestArchiverSave(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, TestDir{"file": testfile}) tempdir, repo := prepareTempdirRepoSrc(t, TestDir{"file": testfile})
defer cleanup()
wg, ctx := errgroup.WithContext(ctx) wg, ctx := errgroup.WithContext(ctx)
repo.StartPackUploader(ctx, wg) repo.StartPackUploader(ctx, wg)
@ -286,8 +282,7 @@ func TestArchiverSaveReaderFS(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
wg, ctx := errgroup.WithContext(ctx) wg, ctx := errgroup.WithContext(ctx)
repo.StartPackUploader(ctx, wg) repo.StartPackUploader(ctx, wg)
@ -362,7 +357,7 @@ func BenchmarkArchiverSaveFileSmall(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
b.StopTimer() b.StopTimer()
tempdir, repo, cleanup := prepareTempdirRepoSrc(b, d) tempdir, repo := prepareTempdirRepoSrc(b, d)
b.StartTimer() b.StartTimer()
_, stats := saveFile(b, repo, filepath.Join(tempdir, "file"), fs.Track{FS: fs.Local{}}) _, stats := saveFile(b, repo, filepath.Join(tempdir, "file"), fs.Track{FS: fs.Local{}})
@ -380,7 +375,6 @@ func BenchmarkArchiverSaveFileSmall(b *testing.B) {
if stats.TreeBlobs != 0 { if stats.TreeBlobs != 0 {
b.Errorf("wrong stats returned in DataBlobs, want 0, got %d", stats.DataBlobs) b.Errorf("wrong stats returned in DataBlobs, want 0, got %d", stats.DataBlobs)
} }
cleanup()
b.StartTimer() b.StartTimer()
} }
} }
@ -395,7 +389,7 @@ func BenchmarkArchiverSaveFileLarge(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
b.StopTimer() b.StopTimer()
tempdir, repo, cleanup := prepareTempdirRepoSrc(b, d) tempdir, repo := prepareTempdirRepoSrc(b, d)
b.StartTimer() b.StartTimer()
_, stats := saveFile(b, repo, filepath.Join(tempdir, "file"), fs.Track{FS: fs.Local{}}) _, stats := saveFile(b, repo, filepath.Join(tempdir, "file"), fs.Track{FS: fs.Local{}})
@ -413,7 +407,6 @@ func BenchmarkArchiverSaveFileLarge(b *testing.B) {
if stats.TreeBlobs != 0 { if stats.TreeBlobs != 0 {
b.Errorf("wrong stats returned in DataBlobs, want 0, got %d", stats.DataBlobs) b.Errorf("wrong stats returned in DataBlobs, want 0, got %d", stats.DataBlobs)
} }
cleanup()
b.StartTimer() b.StartTimer()
} }
} }
@ -467,11 +460,8 @@ func appendToFile(t testing.TB, filename string, data []byte) {
func TestArchiverSaveFileIncremental(t *testing.T) { func TestArchiverSaveFileIncremental(t *testing.T) {
tempdir := restictest.TempDir(t) tempdir := restictest.TempDir(t)
testRepo, removeRepository := repository.TestRepository(t)
defer removeRepository()
repo := &blobCountingRepo{ repo := &blobCountingRepo{
Repository: testRepo, Repository: repository.TestRepository(t),
saved: make(map[restic.BlobHandle]uint), saved: make(map[restic.BlobHandle]uint),
} }
@ -833,8 +823,7 @@ func TestArchiverSaveDir(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, test.src) tempdir, repo := prepareTempdirRepoSrc(t, test.src)
defer cleanup()
wg, ctx := errgroup.WithContext(context.Background()) wg, ctx := errgroup.WithContext(context.Background())
repo.StartPackUploader(ctx, wg) repo.StartPackUploader(ctx, wg)
@ -907,11 +896,8 @@ func TestArchiverSaveDir(t *testing.T) {
func TestArchiverSaveDirIncremental(t *testing.T) { func TestArchiverSaveDirIncremental(t *testing.T) {
tempdir := restictest.TempDir(t) tempdir := restictest.TempDir(t)
testRepo, removeRepository := repository.TestRepository(t)
defer removeRepository()
repo := &blobCountingRepo{ repo := &blobCountingRepo{
Repository: testRepo, Repository: repository.TestRepository(t),
saved: make(map[restic.BlobHandle]uint), saved: make(map[restic.BlobHandle]uint),
} }
@ -1089,8 +1075,7 @@ func TestArchiverSaveTree(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, test.src) tempdir, repo := prepareTempdirRepoSrc(t, test.src)
defer cleanup()
testFS := fs.Track{FS: fs.Local{}} testFS := fs.Track{FS: fs.Local{}}
@ -1391,8 +1376,7 @@ func TestArchiverSnapshot(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, test.src) tempdir, repo := prepareTempdirRepoSrc(t, test.src)
defer cleanup()
arch := New(repo, fs.Track{FS: fs.Local{}}, Options{}) arch := New(repo, fs.Track{FS: fs.Local{}}, Options{})
@ -1549,8 +1533,7 @@ func TestArchiverSnapshotSelect(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, test.src) tempdir, repo := prepareTempdirRepoSrc(t, test.src)
defer cleanup()
arch := New(repo, fs.Track{FS: fs.Local{}}, Options{}) arch := New(repo, fs.Track{FS: fs.Local{}}, Options{})
arch.Select = test.selFn arch.Select = test.selFn
@ -1652,8 +1635,7 @@ func TestArchiverParent(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, test.src) tempdir, repo := prepareTempdirRepoSrc(t, test.src)
defer cleanup()
testFS := &MockFS{ testFS := &MockFS{
FS: fs.Track{FS: fs.Local{}}, FS: fs.Track{FS: fs.Local{}},
@ -1819,8 +1801,7 @@ func TestArchiverErrorReporting(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, test.src) tempdir, repo := prepareTempdirRepoSrc(t, test.src)
defer cleanup()
back := restictest.Chdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
@ -1898,7 +1879,7 @@ func TestArchiverContextCanceled(t *testing.T) {
}) })
// Ensure that the archiver itself reports the canceled context and not just the backend // Ensure that the archiver itself reports the canceled context and not just the backend
repo, _ := repository.TestRepositoryWithBackend(t, &noCancelBackend{mem.New()}, 0) repo := repository.TestRepositoryWithBackend(t, &noCancelBackend{mem.New()}, 0)
back := restictest.Chdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
@ -2018,8 +1999,7 @@ func TestArchiverAbortEarlyOnError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, test.src) tempdir, repo := prepareTempdirRepoSrc(t, test.src)
defer cleanup()
back := restictest.Chdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
@ -2150,8 +2130,7 @@ func TestMetadataChanged(t *testing.T) {
}, },
} }
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, files) tempdir, repo := prepareTempdirRepoSrc(t, files)
defer cleanup()
back := restictest.Chdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
@ -2225,8 +2204,7 @@ func TestRacyFileSwap(t *testing.T) {
}, },
} }
tempdir, repo, cleanup := prepareTempdirRepoSrc(t, files) tempdir, repo := prepareTempdirRepoSrc(t, files)
defer cleanup()
back := restictest.Chdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()

View File

@ -465,8 +465,7 @@ func TestTestEnsureSnapshot(t *testing.T) {
back := restictest.Chdir(t, tempdir) back := restictest.Chdir(t, tempdir)
defer back() defer back()
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
arch := New(repo, fs.Local{}, Options{}) arch := New(repo, fs.Local{}, Options{})
opts := SnapshotOptions{ opts := SnapshotOptions{

View File

@ -340,9 +340,7 @@ func induceError(data []byte) {
} }
func TestCheckerModifiedData(t *testing.T) { func TestCheckerModifiedData(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
sn := archiver.TestSnapshot(t, repo, ".", nil) sn := archiver.TestSnapshot(t, repo, ".", nil)
t.Logf("archived as %v", sn.ID().Str()) t.Logf("archived as %v", sn.ID().Str())
@ -457,8 +455,7 @@ func TestCheckerBlobTypeConfusion(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel() defer cancel()
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
damagedNode := &restic.Node{ damagedNode := &restic.Node{
Name: "damaged", Name: "damaged",

View File

@ -12,13 +12,13 @@ import (
rtest "github.com/restic/restic/internal/test" rtest "github.com/restic/restic/internal/test"
) )
func prepareTempdirRepoSrc(t testing.TB, src archiver.TestDir) (tempdir string, repo restic.Repository, cleanup func()) { func prepareTempdirRepoSrc(t testing.TB, src archiver.TestDir) (string, restic.Repository) {
tempdir = rtest.TempDir(t) tempdir := rtest.TempDir(t)
repo, removeRepository := repository.TestRepository(t) repo := repository.TestRepository(t)
archiver.TestCreateFiles(t, tempdir, src) archiver.TestCreateFiles(t, tempdir, src)
return tempdir, repo, removeRepository return tempdir, repo
} }
type CheckDump func(t *testing.T, testDir string, testDump *bytes.Buffer) error type CheckDump func(t *testing.T, testDir string, testDump *bytes.Buffer) error
@ -72,9 +72,7 @@ func WriteTest(t *testing.T, format string, cd CheckDump) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
tmpdir, repo, cleanup := prepareTempdirRepoSrc(t, tt.args) tmpdir, repo := prepareTempdirRepoSrc(t, tt.args)
defer cleanup()
arch := archiver.New(repo, fs.Track{FS: fs.Local{}}, archiver.Options{}) arch := archiver.New(repo, fs.Track{FS: fs.Local{}}, archiver.Options{})
back := rtest.Chdir(t, tmpdir) back := rtest.Chdir(t, tmpdir)

View File

@ -65,8 +65,7 @@ func loadTree(t testing.TB, repo restic.Repository, id restic.ID) *restic.Tree {
} }
func TestFuseFile(t *testing.T) { func TestFuseFile(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
@ -148,8 +147,7 @@ func TestFuseFile(t *testing.T) {
} }
func TestFuseDir(t *testing.T) { func TestFuseDir(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
root := &Root{repo: repo, blobCache: bloblru.New(blobCacheSize)} root := &Root{repo: repo, blobCache: bloblru.New(blobCacheSize)}
@ -180,9 +178,7 @@ func TestFuseDir(t *testing.T) {
// Test top-level directories for their UID and GID. // Test top-level directories for their UID and GID.
func TestTopUIDGID(t *testing.T) { func TestTopUIDGID(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
restic.TestCreateSnapshot(t, repo, time.Unix(1460289341, 207401672), 0, 0) restic.TestCreateSnapshot(t, repo, time.Unix(1460289341, 207401672), 0, 0)
testTopUIDGID(t, Config{}, repo, uint32(os.Getuid()), uint32(os.Getgid())) testTopUIDGID(t, Config{}, repo, uint32(os.Getuid()), uint32(os.Getgid()))

View File

@ -328,14 +328,13 @@ var (
depth = 3 depth = 3
) )
func createFilledRepo(t testing.TB, snapshots int, dup float32, version uint) (restic.Repository, func()) { func createFilledRepo(t testing.TB, snapshots int, dup float32, version uint) restic.Repository {
repo, cleanup := repository.TestRepositoryWithVersion(t, version) repo := repository.TestRepositoryWithVersion(t, version)
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
restic.TestCreateSnapshot(t, repo, snapshotTime.Add(time.Duration(i)*time.Second), depth, dup) restic.TestCreateSnapshot(t, repo, snapshotTime.Add(time.Duration(i)*time.Second), depth, dup)
} }
return repo
return repo, cleanup
} }
func TestIndexSave(t *testing.T) { func TestIndexSave(t *testing.T) {
@ -343,8 +342,7 @@ func TestIndexSave(t *testing.T) {
} }
func testIndexSave(t *testing.T, version uint) { func testIndexSave(t *testing.T, version uint) {
repo, cleanup := createFilledRepo(t, 3, 0, version) repo := createFilledRepo(t, 3, 0, version)
defer cleanup()
err := repo.LoadIndex(context.TODO()) err := repo.LoadIndex(context.TODO())
if err != nil { if err != nil {

View File

@ -14,9 +14,7 @@ import (
) )
func TestUpgradeRepoV2(t *testing.T) { func TestUpgradeRepoV2(t *testing.T) {
repo, cleanup := repository.TestRepositoryWithVersion(t, 1) repo := repository.TestRepositoryWithVersion(t, 1)
defer cleanup()
if repo.Config().Version != 1 { if repo.Config().Version != 1 {
t.Fatal("test repo has wrong version") t.Fatal("test repo has wrong version")
} }
@ -63,8 +61,7 @@ func (be *failBackend) Save(ctx context.Context, h restic.Handle, rd restic.Rewi
} }
func TestUpgradeRepoV2Failure(t *testing.T) { func TestUpgradeRepoV2Failure(t *testing.T) {
be, cleanup := repository.TestBackend(t) be := repository.TestBackend(t)
defer cleanup()
// wrap backend so that it fails upgrading the config after the initial write // wrap backend so that it fails upgrading the config after the initial write
be = &failBackend{ be = &failBackend{
@ -72,9 +69,7 @@ func TestUpgradeRepoV2Failure(t *testing.T) {
Backend: be, Backend: be,
} }
repo, cleanup := repository.TestRepositoryWithBackend(t, be, 1) repo := repository.TestRepositoryWithBackend(t, be, 1)
defer cleanup()
if repo.Config().Version != 1 { if repo.Config().Version != 1 {
t.Fatal("test repo has wrong version") t.Fatal("test repo has wrong version")
} }

View File

@ -19,7 +19,7 @@ func FuzzSaveLoadBlob(f *testing.F) {
} }
id := restic.Hash(blob) id := restic.Hash(blob)
repo, _ := TestRepositoryWithBackend(t, mem.New(), 2) repo := TestRepositoryWithBackend(t, mem.New(), 2)
var wg errgroup.Group var wg errgroup.Group
repo.StartPackUploader(context.TODO(), &wg) repo.StartPackUploader(context.TODO(), &wg)

View File

@ -223,8 +223,7 @@ func TestRepack(t *testing.T) {
} }
func testRepack(t *testing.T, version uint) { func testRepack(t *testing.T, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(t, version) repo := repository.TestRepositoryWithVersion(t, version)
defer cleanup()
seed := time.Now().UnixNano() seed := time.Now().UnixNano()
rand.Seed(seed) rand.Seed(seed)
@ -302,10 +301,8 @@ func (r oneConnectionRepo) Connections() uint {
} }
func testRepackCopy(t *testing.T, version uint) { func testRepackCopy(t *testing.T, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(t, version) repo := repository.TestRepositoryWithVersion(t, version)
defer cleanup() dstRepo := repository.TestRepositoryWithVersion(t, version)
dstRepo, dstCleanup := repository.TestRepositoryWithVersion(t, version)
defer dstCleanup()
// test with minimal possible connection count // test with minimal possible connection count
repoWrapped := &oneConnectionRepo{repo} repoWrapped := &oneConnectionRepo{repo}
@ -349,8 +346,7 @@ func TestRepackWrongBlob(t *testing.T) {
} }
func testRepackWrongBlob(t *testing.T, version uint) { func testRepackWrongBlob(t *testing.T, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(t, version) repo := repository.TestRepositoryWithVersion(t, version)
defer cleanup()
seed := time.Now().UnixNano() seed := time.Now().UnixNano()
rand.Seed(seed) rand.Seed(seed)
@ -375,8 +371,7 @@ func TestRepackBlobFallback(t *testing.T) {
} }
func testRepackBlobFallback(t *testing.T, version uint) { func testRepackBlobFallback(t *testing.T, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(t, version) repo := repository.TestRepositoryWithVersion(t, version)
defer cleanup()
seed := time.Now().UnixNano() seed := time.Now().UnixNano()
rand.Seed(seed) rand.Seed(seed)

View File

@ -35,8 +35,7 @@ func TestSave(t *testing.T) {
} }
func testSave(t *testing.T, version uint) { func testSave(t *testing.T, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(t, version) repo := repository.TestRepositoryWithVersion(t, version)
defer cleanup()
for _, size := range testSizes { for _, size := range testSizes {
data := make([]byte, size) data := make([]byte, size)
@ -77,8 +76,7 @@ func TestSaveFrom(t *testing.T) {
} }
func testSaveFrom(t *testing.T, version uint) { func testSaveFrom(t *testing.T, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(t, version) repo := repository.TestRepositoryWithVersion(t, version)
defer cleanup()
for _, size := range testSizes { for _, size := range testSizes {
data := make([]byte, size) data := make([]byte, size)
@ -117,9 +115,7 @@ func BenchmarkSaveAndEncrypt(t *testing.B) {
} }
func benchmarkSaveAndEncrypt(t *testing.B, version uint) { func benchmarkSaveAndEncrypt(t *testing.B, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(t, version) repo := repository.TestRepositoryWithVersion(t, version)
defer cleanup()
size := 4 << 20 // 4MiB size := 4 << 20 // 4MiB
data := make([]byte, size) data := make([]byte, size)
@ -145,9 +141,7 @@ func TestLoadBlob(t *testing.T) {
} }
func testLoadBlob(t *testing.T, version uint) { func testLoadBlob(t *testing.T, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(t, version) repo := repository.TestRepositoryWithVersion(t, version)
defer cleanup()
length := 1000000 length := 1000000
buf := crypto.NewBlobBuffer(length) buf := crypto.NewBlobBuffer(length)
_, err := io.ReadFull(rnd, buf) _, err := io.ReadFull(rnd, buf)
@ -181,9 +175,7 @@ func BenchmarkLoadBlob(b *testing.B) {
} }
func benchmarkLoadBlob(b *testing.B, version uint) { func benchmarkLoadBlob(b *testing.B, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(b, version) repo := repository.TestRepositoryWithVersion(b, version)
defer cleanup()
length := 1000000 length := 1000000
buf := crypto.NewBlobBuffer(length) buf := crypto.NewBlobBuffer(length)
_, err := io.ReadFull(rnd, buf) _, err := io.ReadFull(rnd, buf)
@ -224,9 +216,7 @@ func BenchmarkLoadUnpacked(b *testing.B) {
} }
func benchmarkLoadUnpacked(b *testing.B, version uint) { func benchmarkLoadUnpacked(b *testing.B, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(b, version) repo := repository.TestRepositoryWithVersion(b, version)
defer cleanup()
length := 1000000 length := 1000000
buf := crypto.NewBlobBuffer(length) buf := crypto.NewBlobBuffer(length)
_, err := io.ReadFull(rnd, buf) _, err := io.ReadFull(rnd, buf)
@ -345,9 +335,7 @@ func BenchmarkLoadIndex(b *testing.B) {
func benchmarkLoadIndex(b *testing.B, version uint) { func benchmarkLoadIndex(b *testing.B, version uint) {
repository.TestUseLowSecurityKDFParameters(b) repository.TestUseLowSecurityKDFParameters(b)
repo, cleanup := repository.TestRepositoryWithVersion(b, version) repo := repository.TestRepositoryWithVersion(b, version)
defer cleanup()
idx := index.NewIndex() idx := index.NewIndex()
for i := 0; i < 5000; i++ { for i := 0; i < 5000; i++ {
@ -398,10 +386,7 @@ func TestRepositoryIncrementalIndex(t *testing.T) {
} }
func testRepositoryIncrementalIndex(t *testing.T, version uint) { func testRepositoryIncrementalIndex(t *testing.T, version uint) {
r, cleanup := repository.TestRepositoryWithVersion(t, version) repo := repository.TestRepositoryWithVersion(t, version).(*repository.Repository)
defer cleanup()
repo := r.(*repository.Repository)
index.IndexFull = func(*index.Index, bool) bool { return true } index.IndexFull = func(*index.Index, bool) bool { return true }

View File

@ -33,8 +33,8 @@ func TestUseLowSecurityKDFParameters(t logger) {
} }
// TestBackend returns a fully configured in-memory backend. // TestBackend returns a fully configured in-memory backend.
func TestBackend(t testing.TB) (be restic.Backend, cleanup func()) { func TestBackend(t testing.TB) restic.Backend {
return mem.New(), func() {} return mem.New()
} }
const TestChunkerPol = chunker.Pol(0x3DA3358B4DC173) const TestChunkerPol = chunker.Pol(0x3DA3358B4DC173)
@ -42,14 +42,13 @@ const TestChunkerPol = chunker.Pol(0x3DA3358B4DC173)
// TestRepositoryWithBackend returns a repository initialized with a test // TestRepositoryWithBackend returns a repository initialized with a test
// password. If be is nil, an in-memory backend is used. A constant polynomial // password. If be is nil, an in-memory backend is used. A constant polynomial
// is used for the chunker and low-security test parameters. // is used for the chunker and low-security test parameters.
func TestRepositoryWithBackend(t testing.TB, be restic.Backend, version uint) (r restic.Repository, cleanup func()) { func TestRepositoryWithBackend(t testing.TB, be restic.Backend, version uint) restic.Repository {
t.Helper() t.Helper()
TestUseLowSecurityKDFParameters(t) TestUseLowSecurityKDFParameters(t)
restic.TestDisableCheckPolynomial(t) restic.TestDisableCheckPolynomial(t)
var beCleanup func()
if be == nil { if be == nil {
be, beCleanup = TestBackend(t) be = TestBackend(t)
} }
repo, err := New(be, Options{}) repo, err := New(be, Options{})
@ -63,23 +62,19 @@ func TestRepositoryWithBackend(t testing.TB, be restic.Backend, version uint) (r
t.Fatalf("TestRepository(): initialize repo failed: %v", err) t.Fatalf("TestRepository(): initialize repo failed: %v", err)
} }
return repo, func() { return repo
if beCleanup != nil {
beCleanup()
}
}
} }
// TestRepository returns a repository initialized with a test password on an // TestRepository returns a repository initialized with a test password on an
// in-memory backend. When the environment variable RESTIC_TEST_REPO is set to // 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 // 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. // instead. The directory is not removed, but left there for inspection.
func TestRepository(t testing.TB) (r restic.Repository, cleanup func()) { func TestRepository(t testing.TB) restic.Repository {
t.Helper() t.Helper()
return TestRepositoryWithVersion(t, 0) return TestRepositoryWithVersion(t, 0)
} }
func TestRepositoryWithVersion(t testing.TB, version uint) (r restic.Repository, cleanup func()) { func TestRepositoryWithVersion(t testing.TB, version uint) restic.Repository {
t.Helper() t.Helper()
dir := os.Getenv("RESTIC_TEST_REPO") dir := os.Getenv("RESTIC_TEST_REPO")
if dir != "" { if dir != "" {

View File

@ -84,8 +84,7 @@ const (
var findTestTime = time.Unix(1469960361, 23) var findTestTime = time.Unix(1469960361, 23)
func TestFindUsedBlobs(t *testing.T) { func TestFindUsedBlobs(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
var snapshots []*restic.Snapshot var snapshots []*restic.Snapshot
for i := 0; i < findTestSnapshots; i++ { for i := 0; i < findTestSnapshots; i++ {
@ -128,8 +127,7 @@ func TestFindUsedBlobs(t *testing.T) {
} }
func TestMultiFindUsedBlobs(t *testing.T) { func TestMultiFindUsedBlobs(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
var snapshotTrees restic.IDs var snapshotTrees restic.IDs
for i := 0; i < findTestSnapshots; i++ { for i := 0; i < findTestSnapshots; i++ {
@ -177,8 +175,7 @@ func (r ForbiddenRepo) Connections() uint {
} }
func TestFindUsedBlobsSkipsSeenBlobs(t *testing.T) { func TestFindUsedBlobsSkipsSeenBlobs(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
snapshot := restic.TestCreateSnapshot(t, repo, findTestTime, findTestDepth, 0) snapshot := restic.TestCreateSnapshot(t, repo, findTestTime, findTestDepth, 0)
t.Logf("snapshot %v saved, tree %v", snapshot.ID().Str(), snapshot.Tree.Str()) t.Logf("snapshot %v saved, tree %v", snapshot.ID().Str(), snapshot.Tree.Str())
@ -196,8 +193,7 @@ func TestFindUsedBlobsSkipsSeenBlobs(t *testing.T) {
} }
func BenchmarkFindUsedBlobs(b *testing.B) { func BenchmarkFindUsedBlobs(b *testing.B) {
repo, cleanup := repository.TestRepository(b) repo := repository.TestRepository(b)
defer cleanup()
sn := restic.TestCreateSnapshot(b, repo, findTestTime, findTestDepth, 0) sn := restic.TestCreateSnapshot(b, repo, findTestTime, findTestDepth, 0)

View File

@ -15,8 +15,7 @@ import (
) )
func TestLock(t *testing.T) { func TestLock(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
lock, err := restic.NewLock(context.TODO(), repo) lock, err := restic.NewLock(context.TODO(), repo)
rtest.OK(t, err) rtest.OK(t, err)
@ -25,8 +24,7 @@ func TestLock(t *testing.T) {
} }
func TestDoubleUnlock(t *testing.T) { func TestDoubleUnlock(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
lock, err := restic.NewLock(context.TODO(), repo) lock, err := restic.NewLock(context.TODO(), repo)
rtest.OK(t, err) rtest.OK(t, err)
@ -39,8 +37,7 @@ func TestDoubleUnlock(t *testing.T) {
} }
func TestMultipleLock(t *testing.T) { func TestMultipleLock(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
lock1, err := restic.NewLock(context.TODO(), repo) lock1, err := restic.NewLock(context.TODO(), repo)
rtest.OK(t, err) rtest.OK(t, err)
@ -65,8 +62,7 @@ func (be *failLockLoadingBackend) Load(ctx context.Context, h restic.Handle, len
func TestMultipleLockFailure(t *testing.T) { func TestMultipleLockFailure(t *testing.T) {
be := &failLockLoadingBackend{Backend: mem.New()} be := &failLockLoadingBackend{Backend: mem.New()}
repo, cleanup := repository.TestRepositoryWithBackend(t, be, 0) repo := repository.TestRepositoryWithBackend(t, be, 0)
defer cleanup()
lock1, err := restic.NewLock(context.TODO(), repo) lock1, err := restic.NewLock(context.TODO(), repo)
rtest.OK(t, err) rtest.OK(t, err)
@ -78,8 +74,7 @@ func TestMultipleLockFailure(t *testing.T) {
} }
func TestLockExclusive(t *testing.T) { func TestLockExclusive(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
elock, err := restic.NewExclusiveLock(context.TODO(), repo) elock, err := restic.NewExclusiveLock(context.TODO(), repo)
rtest.OK(t, err) rtest.OK(t, err)
@ -87,8 +82,7 @@ func TestLockExclusive(t *testing.T) {
} }
func TestLockOnExclusiveLockedRepo(t *testing.T) { func TestLockOnExclusiveLockedRepo(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
elock, err := restic.NewExclusiveLock(context.TODO(), repo) elock, err := restic.NewExclusiveLock(context.TODO(), repo)
rtest.OK(t, err) rtest.OK(t, err)
@ -104,8 +98,7 @@ func TestLockOnExclusiveLockedRepo(t *testing.T) {
} }
func TestExclusiveLockOnLockedRepo(t *testing.T) { func TestExclusiveLockOnLockedRepo(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
elock, err := restic.NewLock(context.TODO(), repo) elock, err := restic.NewLock(context.TODO(), repo)
rtest.OK(t, err) rtest.OK(t, err)
@ -201,8 +194,7 @@ func lockExists(repo restic.Repository, t testing.TB, id restic.ID) bool {
} }
func TestLockWithStaleLock(t *testing.T) { func TestLockWithStaleLock(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
id1, err := createFakeLock(repo, time.Now().Add(-time.Hour), os.Getpid()) id1, err := createFakeLock(repo, time.Now().Add(-time.Hour), os.Getpid())
rtest.OK(t, err) rtest.OK(t, err)
@ -230,8 +222,7 @@ func TestLockWithStaleLock(t *testing.T) {
} }
func TestRemoveAllLocks(t *testing.T) { func TestRemoveAllLocks(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
id1, err := createFakeLock(repo, time.Now().Add(-time.Hour), os.Getpid()) id1, err := createFakeLock(repo, time.Now().Add(-time.Hour), os.Getpid())
rtest.OK(t, err) rtest.OK(t, err)
@ -257,8 +248,7 @@ func TestRemoveAllLocks(t *testing.T) {
} }
func TestLockRefresh(t *testing.T) { func TestLockRefresh(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
lock, err := restic.NewLock(context.TODO(), repo) lock, err := restic.NewLock(context.TODO(), repo)
rtest.OK(t, err) rtest.OK(t, err)

View File

@ -9,9 +9,7 @@ import (
) )
func TestFindLatestSnapshot(t *testing.T) { func TestFindLatestSnapshot(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
restic.TestCreateSnapshot(t, repo, parseTimeUTC("2015-05-05 05:05:05"), 1, 0) restic.TestCreateSnapshot(t, repo, parseTimeUTC("2015-05-05 05:05:05"), 1, 0)
restic.TestCreateSnapshot(t, repo, parseTimeUTC("2017-07-07 07:07:07"), 1, 0) restic.TestCreateSnapshot(t, repo, parseTimeUTC("2017-07-07 07:07:07"), 1, 0)
latestSnapshot := restic.TestCreateSnapshot(t, repo, parseTimeUTC("2019-09-09 09:09:09"), 1, 0) latestSnapshot := restic.TestCreateSnapshot(t, repo, parseTimeUTC("2019-09-09 09:09:09"), 1, 0)
@ -27,9 +25,7 @@ func TestFindLatestSnapshot(t *testing.T) {
} }
func TestFindLatestSnapshotWithMaxTimestamp(t *testing.T) { func TestFindLatestSnapshotWithMaxTimestamp(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
restic.TestCreateSnapshot(t, repo, parseTimeUTC("2015-05-05 05:05:05"), 1, 0) restic.TestCreateSnapshot(t, repo, parseTimeUTC("2015-05-05 05:05:05"), 1, 0)
desiredSnapshot := restic.TestCreateSnapshot(t, repo, parseTimeUTC("2017-07-07 07:07:07"), 1, 0) desiredSnapshot := restic.TestCreateSnapshot(t, repo, parseTimeUTC("2017-07-07 07:07:07"), 1, 0)
restic.TestCreateSnapshot(t, repo, parseTimeUTC("2019-09-09 09:09:09"), 1, 0) restic.TestCreateSnapshot(t, repo, parseTimeUTC("2019-09-09 09:09:09"), 1, 0)

View File

@ -32,8 +32,7 @@ func TestLoadJSONUnpacked(t *testing.T) {
} }
func testLoadJSONUnpacked(t *testing.T, version uint) { func testLoadJSONUnpacked(t *testing.T, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(t, version) repo := repository.TestRepositoryWithVersion(t, version)
defer cleanup()
// archive a snapshot // archive a snapshot
sn := restic.Snapshot{} sn := restic.Snapshot{}

View File

@ -37,9 +37,7 @@ func loadAllSnapshots(ctx context.Context, repo restic.Repository, excludeIDs re
} }
func TestCreateSnapshot(t *testing.T) { func TestCreateSnapshot(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
for i := 0; i < testCreateSnapshots; i++ { for i := 0; i < testCreateSnapshots; i++ {
restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth, 0) restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth, 0)
} }
@ -70,8 +68,7 @@ func TestCreateSnapshot(t *testing.T) {
} }
func BenchmarkTestCreateSnapshot(t *testing.B) { func BenchmarkTestCreateSnapshot(t *testing.B) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
t.ResetTimer() t.ResetTimer()

View File

@ -97,8 +97,7 @@ func TestNodeComparison(t *testing.T) {
} }
func TestEmptyLoadTree(t *testing.T) { func TestEmptyLoadTree(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
var wg errgroup.Group var wg errgroup.Group
repo.StartPackUploader(context.TODO(), &wg) repo.StartPackUploader(context.TODO(), &wg)
@ -177,14 +176,12 @@ func TestLoadTree(t *testing.T) {
} }
func testLoadTree(t *testing.T, version uint) { func testLoadTree(t *testing.T, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(t, version)
defer cleanup()
if rtest.BenchArchiveDirectory == "" { if rtest.BenchArchiveDirectory == "" {
t.Skip("benchdir not set, skipping") t.Skip("benchdir not set, skipping")
} }
// archive a few files // archive a few files
repo := repository.TestRepositoryWithVersion(t, version)
sn := archiver.TestSnapshot(t, repo, rtest.BenchArchiveDirectory, nil) sn := archiver.TestSnapshot(t, repo, rtest.BenchArchiveDirectory, nil)
rtest.OK(t, repo.Flush(context.Background())) rtest.OK(t, repo.Flush(context.Background()))
@ -197,14 +194,12 @@ func BenchmarkLoadTree(t *testing.B) {
} }
func benchmarkLoadTree(t *testing.B, version uint) { func benchmarkLoadTree(t *testing.B, version uint) {
repo, cleanup := repository.TestRepositoryWithVersion(t, version)
defer cleanup()
if rtest.BenchArchiveDirectory == "" { if rtest.BenchArchiveDirectory == "" {
t.Skip("benchdir not set, skipping") t.Skip("benchdir not set, skipping")
} }
// archive a few files // archive a few files
repo := repository.TestRepositoryWithVersion(t, version)
sn := archiver.TestSnapshot(t, repo, rtest.BenchArchiveDirectory, nil) sn := archiver.TestSnapshot(t, repo, rtest.BenchArchiveDirectory, nil)
rtest.OK(t, repo.Flush(context.Background())) rtest.OK(t, repo.Flush(context.Background()))

View File

@ -321,8 +321,7 @@ func TestRestorer(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
sn, id := saveSnapshot(t, repo, test.Snapshot) sn, id := saveSnapshot(t, repo, test.Snapshot)
t.Logf("snapshot saved as %v", id.Str()) t.Logf("snapshot saved as %v", id.Str())
@ -438,8 +437,7 @@ func TestRestorerRelative(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
sn, id := saveSnapshot(t, repo, test.Snapshot) sn, id := saveSnapshot(t, repo, test.Snapshot)
t.Logf("snapshot saved as %v", id.Str()) t.Logf("snapshot saved as %v", id.Str())
@ -447,7 +445,7 @@ func TestRestorerRelative(t *testing.T) {
res := NewRestorer(context.TODO(), repo, sn, false) res := NewRestorer(context.TODO(), repo, sn, false)
tempdir := rtest.TempDir(t) tempdir := rtest.TempDir(t)
cleanup = rtest.Chdir(t, tempdir) cleanup := rtest.Chdir(t, tempdir)
defer cleanup() defer cleanup()
errors := make(map[string]string) errors := make(map[string]string)
@ -670,8 +668,7 @@ func TestRestorerTraverseTree(t *testing.T) {
for _, test := range tests { for _, test := range tests {
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
sn, _ := saveSnapshot(t, repo, test.Snapshot) sn, _ := saveSnapshot(t, repo, test.Snapshot)
res := NewRestorer(context.TODO(), repo, sn, false) res := NewRestorer(context.TODO(), repo, sn, false)
@ -717,8 +714,7 @@ func checkConsistentInfo(t testing.TB, file string, fi os.FileInfo, modtime time
func TestRestorerConsistentTimestampsAndPermissions(t *testing.T) { func TestRestorerConsistentTimestampsAndPermissions(t *testing.T) {
timeForTest := time.Date(2019, time.January, 9, 1, 46, 40, 0, time.UTC) timeForTest := time.Date(2019, time.January, 9, 1, 46, 40, 0, time.UTC)
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
sn, _ := saveSnapshot(t, repo, Snapshot{ sn, _ := saveSnapshot(t, repo, Snapshot{
Nodes: map[string]Node{ Nodes: map[string]Node{
@ -803,9 +799,7 @@ func TestVerifyCancel(t *testing.T) {
}, },
} }
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
sn, _ := saveSnapshot(t, repo, snapshot) sn, _ := saveSnapshot(t, repo, snapshot)
res := NewRestorer(context.TODO(), repo, sn, false) res := NewRestorer(context.TODO(), repo, sn, false)
@ -832,8 +826,7 @@ func TestVerifyCancel(t *testing.T) {
} }
func TestRestorerSparseFiles(t *testing.T) { func TestRestorerSparseFiles(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
var zeros [1<<20 + 13]byte var zeros [1<<20 + 13]byte

View File

@ -16,8 +16,7 @@ import (
) )
func TestRestorerRestoreEmptyHardlinkedFileds(t *testing.T) { func TestRestorerRestoreEmptyHardlinkedFileds(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo := repository.TestRepository(t)
defer cleanup()
sn, _ := saveSnapshot(t, repo, Snapshot{ sn, _ := saveSnapshot(t, repo, Snapshot{
Nodes: map[string]Node{ Nodes: map[string]Node{