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) TestEnsureSnapshot(t, repo, snapshotID, want)
checker.TestCheckRepo(t, repo, false) checker.TestCheckRepo(t, repo)
// check that the snapshot contains the targets with absolute paths // check that the snapshot contains the targets with absolute paths
for i, target := range sn.Paths { for i, target := range sn.Paths {
@@ -1641,7 +1641,7 @@ func TestArchiverSnapshotSelect(t *testing.T) {
} }
TestEnsureSnapshot(t, repo, snapshotID, want) 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) 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) 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 // make sure the content matches
TestEnsureFileContent(context.Background(), t, repo, "testfile", node3, files["testfile"].(TestFile)) TestEnsureFileContent(context.Background(), t, repo, "testfile", node3, files["testfile"].(TestFile))
checker.TestCheckRepo(t, repo, false) checker.TestCheckRepo(t, repo)
} }
func TestRacyFileTypeSwap(t *testing.T) { func TestRacyFileTypeSwap(t *testing.T) {

View File

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

View File

@@ -46,7 +46,7 @@ func TestCreateSnapshot(t *testing.T) {
t.Fatalf("snapshot has zero tree ID") t.Fatalf("snapshot has zero tree ID")
} }
checker.TestCheckRepo(t, repo, false) checker.TestCheckRepo(t, repo)
} }
func BenchmarkTestCreateSnapshot(t *testing.B) { 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") 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 // remove pack files to make check happy
rtest.OK(t, restic.ParallelRemove(context.TODO(), unpacked, newPacks, restic.PackFile, nil, nil)) 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 { func listPacks(t testing.TB, repo restic.Lister) restic.IDSet {

View File

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

View File

@@ -7,7 +7,6 @@ import (
"time" "time"
"github.com/restic/restic/internal/backend" "github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/checker"
"github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test" 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, ReadAllPacks: readAllPacks,
}, &progress.NoopPrinter{})) }, &progress.NoopPrinter{}))
checker.TestCheckRepo(t, repo, true) repository.TestCheckRepo(t, repo)
} }
func TestRebuildIndex(t *testing.T) { 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/cache"
"github.com/restic/restic/internal/backend/local" "github.com/restic/restic/internal/backend/local"
"github.com/restic/restic/internal/backend/mem" "github.com/restic/restic/internal/backend/mem"
"github.com/restic/restic/internal/checker"
"github.com/restic/restic/internal/crypto" "github.com/restic/restic/internal/crypto"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository" "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") rtest.Equals(t, expectedPacks, packs, "unexpected number of pack files")
checker.TestCheckRepo(t, repo, true) repository.TestCheckRepo(t, repo)
} }
func BenchmarkSaveAndEncrypt(t *testing.B) { 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 // TODO get rid of this test helper
return restic.NewLock(context.TODO(), &internalRepository{repo}, exclusive) 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)
}
}