repository: unify repository initialization in tests

Tests should use a helper from internal/repository/testing.go to
construct a Repository object.
This commit is contained in:
Michael Eischer 2024-02-24 21:45:24 +01:00
parent 3ba1fa3cee
commit dc441c57a7
5 changed files with 27 additions and 46 deletions

View File

@ -72,11 +72,9 @@ func assertOnlyMixedPackHints(t *testing.T, hints []error) {
} }
func TestCheckRepo(t *testing.T) { func TestCheckRepo(t *testing.T) {
repodir, cleanup := test.Env(t, checkerTestData) repo, cleanup := repository.TestFromFixture(t, checkerTestData)
defer cleanup() defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
chkr := checker.New(repo, false) chkr := checker.New(repo, false)
hints, errs := chkr.LoadIndex(context.TODO(), nil) hints, errs := chkr.LoadIndex(context.TODO(), nil)
if len(errs) > 0 { if len(errs) > 0 {
@ -92,11 +90,9 @@ func TestCheckRepo(t *testing.T) {
} }
func TestMissingPack(t *testing.T) { func TestMissingPack(t *testing.T) {
repodir, cleanup := test.Env(t, checkerTestData) repo, cleanup := repository.TestFromFixture(t, checkerTestData)
defer cleanup() defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
packHandle := backend.Handle{ packHandle := backend.Handle{
Type: restic.PackFile, Type: restic.PackFile,
Name: "657f7fb64f6a854fff6fe9279998ee09034901eded4e6db9bcee0e59745bbce6", Name: "657f7fb64f6a854fff6fe9279998ee09034901eded4e6db9bcee0e59745bbce6",
@ -123,11 +119,9 @@ func TestMissingPack(t *testing.T) {
} }
func TestUnreferencedPack(t *testing.T) { func TestUnreferencedPack(t *testing.T) {
repodir, cleanup := test.Env(t, checkerTestData) repo, cleanup := repository.TestFromFixture(t, checkerTestData)
defer cleanup() defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
// index 3f1a only references pack 60e0 // index 3f1a only references pack 60e0
packID := "60e0438dcb978ec6860cc1f8c43da648170ee9129af8f650f876bad19f8f788e" packID := "60e0438dcb978ec6860cc1f8c43da648170ee9129af8f650f876bad19f8f788e"
indexHandle := backend.Handle{ indexHandle := backend.Handle{
@ -156,11 +150,9 @@ func TestUnreferencedPack(t *testing.T) {
} }
func TestUnreferencedBlobs(t *testing.T) { func TestUnreferencedBlobs(t *testing.T) {
repodir, cleanup := test.Env(t, checkerTestData) repo, cleanup := repository.TestFromFixture(t, checkerTestData)
defer cleanup() defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
snapshotHandle := backend.Handle{ snapshotHandle := backend.Handle{
Type: restic.SnapshotFile, Type: restic.SnapshotFile,
Name: "51d249d28815200d59e4be7b3f21a157b864dc343353df9d8e498220c2499b02", Name: "51d249d28815200d59e4be7b3f21a157b864dc343353df9d8e498220c2499b02",
@ -195,11 +187,9 @@ func TestUnreferencedBlobs(t *testing.T) {
} }
func TestModifiedIndex(t *testing.T) { func TestModifiedIndex(t *testing.T) {
repodir, cleanup := test.Env(t, checkerTestData) repo, cleanup := repository.TestFromFixture(t, checkerTestData)
defer cleanup() defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
done := make(chan struct{}) done := make(chan struct{})
defer close(done) defer close(done)
@ -274,11 +264,9 @@ func TestModifiedIndex(t *testing.T) {
var checkerDuplicateIndexTestData = filepath.Join("testdata", "duplicate-packs-in-index-test-repo.tar.gz") var checkerDuplicateIndexTestData = filepath.Join("testdata", "duplicate-packs-in-index-test-repo.tar.gz")
func TestDuplicatePacksInIndex(t *testing.T) { func TestDuplicatePacksInIndex(t *testing.T) {
repodir, cleanup := test.Env(t, checkerDuplicateIndexTestData) repo, cleanup := repository.TestFromFixture(t, checkerDuplicateIndexTestData)
defer cleanup() defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
chkr := checker.New(repo, false) chkr := checker.New(repo, false)
hints, errs := chkr.LoadIndex(context.TODO(), nil) hints, errs := chkr.LoadIndex(context.TODO(), nil)
if len(hints) == 0 { if len(hints) == 0 {
@ -342,9 +330,7 @@ func TestCheckerModifiedData(t *testing.T) {
t.Logf("archived as %v", sn.ID().Str()) t.Logf("archived as %v", sn.ID().Str())
beError := &errorBackend{Backend: repo.Backend()} beError := &errorBackend{Backend: repo.Backend()}
checkRepo, err := repository.New(beError, repository.Options{}) checkRepo := repository.TestOpenBackend(t, beError)
test.OK(t, err)
test.OK(t, checkRepo.SearchKey(context.TODO(), test.TestPassword, 5, ""))
chkr := checker.New(checkRepo, false) chkr := checker.New(checkRepo, false)
@ -399,10 +385,8 @@ func (r *loadTreesOnceRepository) LoadTree(ctx context.Context, id restic.ID) (*
} }
func TestCheckerNoDuplicateTreeDecodes(t *testing.T) { func TestCheckerNoDuplicateTreeDecodes(t *testing.T) {
repodir, cleanup := test.Env(t, checkerTestData) repo, cleanup := repository.TestFromFixture(t, checkerTestData)
defer cleanup() defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
checkRepo := &loadTreesOnceRepository{ checkRepo := &loadTreesOnceRepository{
Repository: repo, Repository: repo,
loadedTrees: restic.NewIDSet(), loadedTrees: restic.NewIDSet(),
@ -549,9 +533,7 @@ func TestCheckerBlobTypeConfusion(t *testing.T) {
} }
func loadBenchRepository(t *testing.B) (*checker.Checker, restic.Repository, func()) { func loadBenchRepository(t *testing.B) (*checker.Checker, restic.Repository, func()) {
repodir, cleanup := test.Env(t, checkerTestData) repo, cleanup := repository.TestFromFixture(t, checkerTestData)
repo := repository.TestOpenLocal(t, repodir)
chkr := checker.New(repo, false) chkr := checker.New(repo, false)
hints, errs := chkr.LoadIndex(context.TODO(), nil) hints, errs := chkr.LoadIndex(context.TODO(), nil)

View File

@ -15,11 +15,9 @@ import (
var repoFixture = filepath.Join("..", "repository", "testdata", "test-repo.tar.gz") var repoFixture = filepath.Join("..", "repository", "testdata", "test-repo.tar.gz")
func TestRepositoryForAllIndexes(t *testing.T) { func TestRepositoryForAllIndexes(t *testing.T) {
repodir, cleanup := rtest.Env(t, repoFixture) repo, cleanup := repository.TestFromFixture(t, repoFixture)
defer cleanup() defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
expectedIndexIDs := restic.NewIDSet() expectedIndexIDs := restic.NewIDSet()
rtest.OK(t, repo.List(context.TODO(), restic.IndexFile, func(id restic.ID, size int64) error { rtest.OK(t, repo.List(context.TODO(), restic.IndexFile, func(id restic.ID, size int64) error {
expectedIndexIDs.Insert(id) expectedIndexIDs.Insert(id)

View File

@ -31,10 +31,7 @@ func openLockTestRepo(t *testing.T, wrapper backendWrapper) restic.Repository {
rtest.OK(t, err) rtest.OK(t, err)
} }
repo, err := New(be, Options{}) return TestOpenBackend(t, be)
rtest.OK(t, err)
rtest.OK(t, repo.SearchKey(context.TODO(), test.TestPassword, 1, ""))
return repo
} }
func checkedLockRepo(ctx context.Context, t *testing.T, repo restic.Repository, lockerInst *locker, retryLock time.Duration) (*Unlocker, context.Context) { func checkedLockRepo(ctx context.Context, t *testing.T, repo restic.Repository, lockerInst *locker, retryLock time.Duration) (*Unlocker, context.Context) {
@ -77,9 +74,7 @@ func TestLockCancel(t *testing.T) {
func TestLockConflict(t *testing.T) { func TestLockConflict(t *testing.T) {
t.Parallel() t.Parallel()
repo := openLockTestRepo(t, nil) repo := openLockTestRepo(t, nil)
repo2, err := New(repo.Backend(), Options{}) repo2 := TestOpenBackend(t, repo.Backend())
test.OK(t, err)
test.OK(t, repo2.SearchKey(context.TODO(), test.TestPassword, 1, ""))
lock, _, err := Lock(context.Background(), repo, true, 0, func(msg string) {}, func(format string, args ...interface{}) {}) lock, _, err := Lock(context.Background(), repo, true, 0, func(msg string) {}, func(format string, args ...interface{}) {})
test.OK(t, err) test.OK(t, err)

View File

@ -221,10 +221,9 @@ func benchmarkLoadUnpacked(b *testing.B, version uint) {
var repoFixture = filepath.Join("testdata", "test-repo.tar.gz") var repoFixture = filepath.Join("testdata", "test-repo.tar.gz")
func TestRepositoryLoadIndex(t *testing.T) { func TestRepositoryLoadIndex(t *testing.T) {
repodir, cleanup := rtest.Env(t, repoFixture) repo, cleanup := repository.TestFromFixture(t, repoFixture)
defer cleanup() defer cleanup()
repo := repository.TestOpenLocal(t, repodir)
rtest.OK(t, repo.LoadIndex(context.TODO(), nil)) rtest.OK(t, repo.LoadIndex(context.TODO(), nil))
} }
@ -243,7 +242,7 @@ func loadIndex(ctx context.Context, repo restic.LoaderUnpacked, id restic.ID) (*
} }
func TestRepositoryLoadUnpackedBroken(t *testing.T) { func TestRepositoryLoadUnpackedBroken(t *testing.T) {
repodir, cleanup := rtest.Env(t, repoFixture) repo, cleanup := repository.TestFromFixture(t, repoFixture)
defer cleanup() defer cleanup()
data := rtest.Random(23, 12345) data := rtest.Random(23, 12345)
@ -252,7 +251,6 @@ func TestRepositoryLoadUnpackedBroken(t *testing.T) {
// damage buffer // damage buffer
data[0] ^= 0xff data[0] ^= 0xff
repo := repository.TestOpenLocal(t, repodir)
// store broken file // store broken file
err := repo.Backend().Save(context.TODO(), h, backend.NewByteReader(data, nil)) err := repo.Backend().Save(context.TODO(), h, backend.NewByteReader(data, nil))
rtest.OK(t, err) rtest.OK(t, err)
@ -289,10 +287,7 @@ func TestRepositoryLoadUnpackedRetryBroken(t *testing.T) {
be, err := local.Open(context.TODO(), local.Config{Path: repodir, Connections: 2}) be, err := local.Open(context.TODO(), local.Config{Path: repodir, Connections: 2})
rtest.OK(t, err) rtest.OK(t, err)
repo, err := repository.New(&damageOnceBackend{Backend: be}, repository.Options{}) repo := repository.TestOpenBackend(t, &damageOnceBackend{Backend: be})
rtest.OK(t, err)
err = repo.SearchKey(context.TODO(), rtest.TestPassword, 10, "")
rtest.OK(t, err)
rtest.OK(t, repo.LoadIndex(context.TODO(), nil)) rtest.OK(t, repo.LoadIndex(context.TODO(), nil))
} }

View File

@ -95,8 +95,15 @@ func TestRepositoryWithVersion(t testing.TB, version uint) restic.Repository {
return TestRepositoryWithBackend(t, nil, version, opts) return TestRepositoryWithBackend(t, nil, version, opts)
} }
func TestFromFixture(t testing.TB, repoFixture string) (restic.Repository, func()) {
repodir, cleanup := test.Env(t, repoFixture)
repo := TestOpenLocal(t, repodir)
return repo, cleanup
}
// TestOpenLocal opens a local repository. // TestOpenLocal opens a local repository.
func TestOpenLocal(t testing.TB, dir string) (r restic.Repository) { func TestOpenLocal(t testing.TB, dir string) restic.Repository {
var be backend.Backend var be backend.Backend
be, err := local.Open(context.TODO(), local.Config{Path: dir, Connections: 2}) be, err := local.Open(context.TODO(), local.Config{Path: dir, Connections: 2})
if err != nil { if err != nil {
@ -105,6 +112,10 @@ func TestOpenLocal(t testing.TB, dir string) (r restic.Repository) {
be = retry.New(be, 3, nil, nil) be = retry.New(be, 3, nil, nil)
return TestOpenBackend(t, be)
}
func TestOpenBackend(t testing.TB, be backend.Backend) restic.Repository {
repo, err := New(be, Options{}) repo, err := New(be, Options{})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)