repository: add dedicated test helper

This commit is contained in:
Michael Eischer
2025-09-28 14:25:49 +02:00
parent 82971ad7f0
commit 189b295c30
8 changed files with 56 additions and 31 deletions

View File

@@ -1421,7 +1421,7 @@ func TestArchiverSnapshot(t *testing.T) {
}
TestEnsureSnapshot(t, repo, snapshotID, want)
checker.TestCheckRepo(t, repo, false)
checker.TestCheckRepo(t, repo)
// check that the snapshot contains the targets with absolute paths
for i, target := range sn.Paths {
@@ -1641,7 +1641,7 @@ func TestArchiverSnapshotSelect(t *testing.T) {
}
TestEnsureSnapshot(t, repo, snapshotID, want)
checker.TestCheckRepo(t, repo, false)
checker.TestCheckRepo(t, repo)
})
}
}
@@ -1866,7 +1866,7 @@ func TestArchiverParent(t *testing.T) {
t.Logf("testfs: %v", testFS)
}
checker.TestCheckRepo(t, repo, false)
checker.TestCheckRepo(t, repo)
})
}
}
@@ -2021,7 +2021,7 @@ func TestArchiverErrorReporting(t *testing.T) {
}
TestEnsureSnapshot(t, repo, snapshotID, want)
checker.TestCheckRepo(t, repo, false)
checker.TestCheckRepo(t, repo)
})
}
}
@@ -2384,7 +2384,7 @@ func TestMetadataChanged(t *testing.T) {
// make sure the content matches
TestEnsureFileContent(context.Background(), t, repo, "testfile", node3, files["testfile"].(TestFile))
checker.TestCheckRepo(t, repo, false)
checker.TestCheckRepo(t, repo)
}
func TestRacyFileTypeSwap(t *testing.T) {

View File

@@ -8,7 +8,7 @@ import (
)
// TestCheckRepo runs the checker on repo.
func TestCheckRepo(t testing.TB, repo restic.Repository, skipStructure bool) {
func TestCheckRepo(t testing.TB, repo restic.Repository) {
chkr := New(repo, true)
hints, errs := chkr.LoadIndex(context.TODO(), nil)
@@ -33,7 +33,6 @@ func TestCheckRepo(t testing.TB, repo restic.Repository, skipStructure bool) {
t.Error(err)
}
if !skipStructure {
// structure
errChan = make(chan error)
go chkr.Structure(context.TODO(), nil, errChan)
@@ -50,7 +49,6 @@ func TestCheckRepo(t testing.TB, repo restic.Repository, skipStructure bool) {
if len(blobs) > 0 {
t.Errorf("unused blobs found: %v", blobs)
}
}
// read data
errChan = make(chan error)

View File

@@ -46,7 +46,7 @@ func TestCreateSnapshot(t *testing.T) {
t.Fatalf("snapshot has zero tree ID")
}
checker.TestCheckRepo(t, repo, false)
checker.TestCheckRepo(t, repo)
}
func BenchmarkTestCreateSnapshot(t *testing.B) {

View File

@@ -402,7 +402,7 @@ func testIndexSave(t *testing.T, version uint) {
}))
rtest.Equals(t, 0, len(blobs), "saved index is missing blobs")
checker.TestCheckRepo(t, repo, false)
checker.TestCheckRepo(t, repo)
})
}
}
@@ -449,7 +449,7 @@ func testIndexSavePartial(t *testing.T, version uint) {
// remove pack files to make check happy
rtest.OK(t, restic.ParallelRemove(context.TODO(), unpacked, newPacks, restic.PackFile, nil, nil))
checker.TestCheckRepo(t, repo, false)
checker.TestCheckRepo(t, repo)
}
func listPacks(t testing.TB, repo restic.Lister) restic.IDSet {

View File

@@ -8,7 +8,6 @@ import (
"testing"
"time"
"github.com/restic/restic/internal/checker"
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/repository/pack"
"github.com/restic/restic/internal/restic"
@@ -49,7 +48,7 @@ func testPrune(t *testing.T, opts repository.PruneOptions, errOnUnused bool) {
rtest.OK(t, plan.Execute(context.TODO(), &progress.NoopPrinter{}))
repo = repository.TestOpenBackend(t, be)
checker.TestCheckRepo(t, repo, true)
repository.TestCheckRepo(t, repo)
if errOnUnused {
existing := listBlobs(repo)
@@ -181,7 +180,7 @@ func TestPruneSmall(t *testing.T) {
// repopen repository
repo = repository.TestOpenBackend(t, be)
checker.TestCheckRepo(t, repo, true)
repository.TestCheckRepo(t, repo)
// load all blobs
for blob := range keep {

View File

@@ -7,7 +7,6 @@ import (
"time"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/checker"
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test"
@@ -36,7 +35,7 @@ func testRebuildIndex(t *testing.T, readAllPacks bool, damage func(t *testing.T,
ReadAllPacks: readAllPacks,
}, &progress.NoopPrinter{}))
checker.TestCheckRepo(t, repo, true)
repository.TestCheckRepo(t, repo)
}
func TestRebuildIndex(t *testing.T) {

View File

@@ -16,7 +16,6 @@ import (
"github.com/restic/restic/internal/backend/cache"
"github.com/restic/restic/internal/backend/local"
"github.com/restic/restic/internal/backend/mem"
"github.com/restic/restic/internal/checker"
"github.com/restic/restic/internal/crypto"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository"
@@ -131,7 +130,7 @@ func testSavePackMerging(t *testing.T, targetPercentage int, expectedPacks int)
}))
rtest.Equals(t, expectedPacks, packs, "unexpected number of pack files")
checker.TestCheckRepo(t, repo, true)
repository.TestCheckRepo(t, repo)
}
func BenchmarkSaveAndEncrypt(t *testing.B) {

View File

@@ -162,3 +162,33 @@ func TestNewLock(_ *testing.T, repo *Repository, exclusive bool) (*restic.Lock,
// TODO get rid of this test helper
return restic.NewLock(context.TODO(), &internalRepository{repo}, exclusive)
}
// TestCheckRepo runs the checker on repo.
func TestCheckRepo(t testing.TB, repo restic.Repository) {
chkr := NewChecker(repo)
hints, errs := chkr.LoadIndex(context.TODO(), nil)
if len(errs) != 0 {
t.Fatalf("errors loading index: %v", errs)
}
if len(hints) != 0 {
t.Fatalf("errors loading index: %v", hints)
}
// packs
errChan := make(chan error)
go chkr.Packs(context.TODO(), errChan)
for err := range errChan {
t.Error(err)
}
// read data
errChan = make(chan error)
go chkr.ReadData(context.TODO(), errChan)
for err := range errChan {
t.Error(err)
}
}