forked from TrueCloudLab/restic
repository: convert test helper to return *repository.Repository
This commit is contained in:
parent
a1ca5e15c4
commit
d8b184b3d3
8 changed files with 25 additions and 25 deletions
|
@ -19,7 +19,7 @@ import (
|
||||||
|
|
||||||
type backendWrapper func(r backend.Backend) (backend.Backend, error)
|
type backendWrapper func(r backend.Backend) (backend.Backend, error)
|
||||||
|
|
||||||
func openLockTestRepo(t *testing.T, wrapper backendWrapper) restic.Repository {
|
func openLockTestRepo(t *testing.T, wrapper backendWrapper) *Repository {
|
||||||
be := backend.Backend(mem.New())
|
be := backend.Backend(mem.New())
|
||||||
// initialize repo
|
// initialize repo
|
||||||
TestRepositoryWithBackend(t, be, 0, Options{})
|
TestRepositoryWithBackend(t, be, 0, Options{})
|
||||||
|
@ -34,7 +34,7 @@ func openLockTestRepo(t *testing.T, wrapper backendWrapper) restic.Repository {
|
||||||
return TestOpenBackend(t, be)
|
return TestOpenBackend(t, be)
|
||||||
}
|
}
|
||||||
|
|
||||||
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 *Repository, lockerInst *locker, retryLock time.Duration) (*Unlocker, context.Context) {
|
||||||
lock, wrappedCtx, err := lockerInst.Lock(ctx, repo, false, retryLock, func(msg string) {}, func(format string, args ...interface{}) {})
|
lock, wrappedCtx, err := lockerInst.Lock(ctx, repo, false, retryLock, func(msg string) {}, func(format string, args ...interface{}) {})
|
||||||
test.OK(t, err)
|
test.OK(t, err)
|
||||||
test.OK(t, wrappedCtx.Err())
|
test.OK(t, wrappedCtx.Err())
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func testPrune(t *testing.T, opts repository.PruneOptions, errOnUnused bool) {
|
func testPrune(t *testing.T, opts repository.PruneOptions, errOnUnused bool) {
|
||||||
repo := repository.TestRepository(t).(*repository.Repository)
|
repo := repository.TestRepository(t)
|
||||||
createRandomBlobs(t, repo, 4, 0.5, true)
|
createRandomBlobs(t, repo, 4, 0.5, true)
|
||||||
createRandomBlobs(t, repo, 5, 0.5, true)
|
createRandomBlobs(t, repo, 5, 0.5, true)
|
||||||
keep, _ := selectBlobs(t, repo, 0.5)
|
keep, _ := selectBlobs(t, repo, 0.5)
|
||||||
|
@ -37,7 +37,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, repo.Backend()).(*repository.Repository)
|
repo = repository.TestOpenBackend(t, repo.Backend())
|
||||||
checker.TestCheckRepo(t, repo, true)
|
checker.TestCheckRepo(t, repo, true)
|
||||||
|
|
||||||
if errOnUnused {
|
if errOnUnused {
|
||||||
|
|
|
@ -17,7 +17,7 @@ func listIndex(t *testing.T, repo restic.Lister) restic.IDSet {
|
||||||
}
|
}
|
||||||
|
|
||||||
func testRebuildIndex(t *testing.T, readAllPacks bool, damage func(t *testing.T, repo *repository.Repository)) {
|
func testRebuildIndex(t *testing.T, readAllPacks bool, damage func(t *testing.T, repo *repository.Repository)) {
|
||||||
repo := repository.TestRepository(t).(*repository.Repository)
|
repo := repository.TestRepository(t)
|
||||||
createRandomBlobs(t, repo, 4, 0.5, true)
|
createRandomBlobs(t, repo, 4, 0.5, true)
|
||||||
createRandomBlobs(t, repo, 5, 0.5, true)
|
createRandomBlobs(t, repo, 5, 0.5, true)
|
||||||
indexes := listIndex(t, repo)
|
indexes := listIndex(t, repo)
|
||||||
|
@ -25,7 +25,7 @@ func testRebuildIndex(t *testing.T, readAllPacks bool, damage func(t *testing.T,
|
||||||
|
|
||||||
damage(t, repo)
|
damage(t, repo)
|
||||||
|
|
||||||
repo = repository.TestOpenBackend(t, repo.Backend()).(*repository.Repository)
|
repo = repository.TestOpenBackend(t, repo.Backend())
|
||||||
rtest.OK(t, repository.RepairIndex(context.TODO(), repo, repository.RepairIndexOptions{
|
rtest.OK(t, repository.RepairIndex(context.TODO(), repo, repository.RepairIndexOptions{
|
||||||
ReadAllPacks: readAllPacks,
|
ReadAllPacks: readAllPacks,
|
||||||
}, &progress.NoopPrinter{}))
|
}, &progress.NoopPrinter{}))
|
||||||
|
|
|
@ -24,7 +24,7 @@ func listBlobs(repo restic.Repository) restic.BlobSet {
|
||||||
return blobs
|
return blobs
|
||||||
}
|
}
|
||||||
|
|
||||||
func replaceFile(t *testing.T, repo restic.Repository, h backend.Handle, damage func([]byte) []byte) {
|
func replaceFile(t *testing.T, repo *repository.Repository, h backend.Handle, damage func([]byte) []byte) {
|
||||||
buf, err := backendtest.LoadAll(context.TODO(), repo.Backend(), h)
|
buf, err := backendtest.LoadAll(context.TODO(), repo.Backend(), h)
|
||||||
test.OK(t, err)
|
test.OK(t, err)
|
||||||
buf = damage(buf)
|
buf = damage(buf)
|
||||||
|
@ -39,17 +39,17 @@ func TestRepairBrokenPack(t *testing.T) {
|
||||||
func testRepairBrokenPack(t *testing.T, version uint) {
|
func testRepairBrokenPack(t *testing.T, version uint) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
damage func(t *testing.T, repo restic.Repository, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet)
|
damage func(t *testing.T, repo *repository.Repository, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet)
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"valid pack",
|
"valid pack",
|
||||||
func(t *testing.T, repo restic.Repository, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
func(t *testing.T, repo *repository.Repository, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
||||||
return packsBefore, restic.NewBlobSet()
|
return packsBefore, restic.NewBlobSet()
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"broken pack",
|
"broken pack",
|
||||||
func(t *testing.T, repo restic.Repository, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
func(t *testing.T, repo *repository.Repository, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
||||||
wrongBlob := createRandomWrongBlob(t, repo)
|
wrongBlob := createRandomWrongBlob(t, repo)
|
||||||
damagedPacks := findPacksForBlobs(t, repo, restic.NewBlobSet(wrongBlob))
|
damagedPacks := findPacksForBlobs(t, repo, restic.NewBlobSet(wrongBlob))
|
||||||
return damagedPacks, restic.NewBlobSet(wrongBlob)
|
return damagedPacks, restic.NewBlobSet(wrongBlob)
|
||||||
|
@ -57,7 +57,7 @@ func testRepairBrokenPack(t *testing.T, version uint) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"partially broken pack",
|
"partially broken pack",
|
||||||
func(t *testing.T, repo restic.Repository, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
func(t *testing.T, repo *repository.Repository, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
||||||
// damage one of the pack files
|
// damage one of the pack files
|
||||||
damagedID := packsBefore.List()[0]
|
damagedID := packsBefore.List()[0]
|
||||||
replaceFile(t, repo, backend.Handle{Type: backend.PackFile, Name: damagedID.String()},
|
replaceFile(t, repo, backend.Handle{Type: backend.PackFile, Name: damagedID.String()},
|
||||||
|
@ -80,7 +80,7 @@ func testRepairBrokenPack(t *testing.T, version uint) {
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
"truncated pack",
|
"truncated pack",
|
||||||
func(t *testing.T, repo restic.Repository, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
func(t *testing.T, repo *repository.Repository, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
|
||||||
// damage one of the pack files
|
// damage one of the pack files
|
||||||
damagedID := packsBefore.List()[0]
|
damagedID := packsBefore.List()[0]
|
||||||
replaceFile(t, repo, backend.Handle{Type: backend.PackFile, Name: damagedID.String()},
|
replaceFile(t, repo, backend.Handle{Type: backend.PackFile, Name: damagedID.String()},
|
||||||
|
|
|
@ -353,7 +353,7 @@ func testStreamPack(t *testing.T, version uint) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBlobVerification(t *testing.T) {
|
func TestBlobVerification(t *testing.T) {
|
||||||
repo := TestRepository(t).(*Repository)
|
repo := TestRepository(t)
|
||||||
|
|
||||||
type DamageType string
|
type DamageType string
|
||||||
const (
|
const (
|
||||||
|
@ -402,7 +402,7 @@ func TestBlobVerification(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnpackedVerification(t *testing.T) {
|
func TestUnpackedVerification(t *testing.T) {
|
||||||
repo := TestRepository(t).(*Repository)
|
repo := TestRepository(t)
|
||||||
|
|
||||||
type DamageType string
|
type DamageType string
|
||||||
const (
|
const (
|
||||||
|
|
|
@ -145,7 +145,7 @@ func testLoadBlob(t *testing.T, version uint) {
|
||||||
|
|
||||||
func TestLoadBlobBroken(t *testing.T) {
|
func TestLoadBlobBroken(t *testing.T) {
|
||||||
be := mem.New()
|
be := mem.New()
|
||||||
repo := repository.TestRepositoryWithBackend(t, &damageOnceBackend{Backend: be}, restic.StableRepoVersion, repository.Options{}).(*repository.Repository)
|
repo := repository.TestRepositoryWithBackend(t, &damageOnceBackend{Backend: be}, restic.StableRepoVersion, repository.Options{})
|
||||||
buf := test.Random(42, 1000)
|
buf := test.Random(42, 1000)
|
||||||
|
|
||||||
var wg errgroup.Group
|
var wg errgroup.Group
|
||||||
|
@ -374,7 +374,7 @@ func TestRepositoryIncrementalIndex(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func testRepositoryIncrementalIndex(t *testing.T, version uint) {
|
func testRepositoryIncrementalIndex(t *testing.T, version uint) {
|
||||||
repo := repository.TestRepositoryWithVersion(t, version).(*repository.Repository)
|
repo := repository.TestRepositoryWithVersion(t, version)
|
||||||
|
|
||||||
index.IndexFull = func(*index.Index, bool) bool { return true }
|
index.IndexFull = func(*index.Index, bool) bool { return true }
|
||||||
|
|
||||||
|
@ -425,7 +425,7 @@ func TestInvalidCompression(t *testing.T) {
|
||||||
|
|
||||||
func TestListPack(t *testing.T) {
|
func TestListPack(t *testing.T) {
|
||||||
be := mem.New()
|
be := mem.New()
|
||||||
repo := repository.TestRepositoryWithBackend(t, &damageOnceBackend{Backend: be}, restic.StableRepoVersion, repository.Options{}).(*repository.Repository)
|
repo := repository.TestRepositoryWithBackend(t, &damageOnceBackend{Backend: be}, restic.StableRepoVersion, repository.Options{})
|
||||||
buf := test.Random(42, 1000)
|
buf := test.Random(42, 1000)
|
||||||
|
|
||||||
var wg errgroup.Group
|
var wg errgroup.Group
|
||||||
|
|
|
@ -46,7 +46,7 @@ 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 backend.Backend, version uint, opts Options) restic.Repository {
|
func TestRepositoryWithBackend(t testing.TB, be backend.Backend, version uint, opts Options) *Repository {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
TestUseLowSecurityKDFParameters(t)
|
TestUseLowSecurityKDFParameters(t)
|
||||||
restic.TestDisableCheckPolynomial(t)
|
restic.TestDisableCheckPolynomial(t)
|
||||||
|
@ -76,12 +76,12 @@ func TestRepositoryWithBackend(t testing.TB, be backend.Backend, version uint, o
|
||||||
// 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) restic.Repository {
|
func TestRepository(t testing.TB) *Repository {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
return TestRepositoryWithVersion(t, 0)
|
return TestRepositoryWithVersion(t, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRepositoryWithVersion(t testing.TB, version uint) restic.Repository {
|
func TestRepositoryWithVersion(t testing.TB, version uint) *Repository {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
dir := os.Getenv("RESTIC_TEST_REPO")
|
dir := os.Getenv("RESTIC_TEST_REPO")
|
||||||
opts := Options{}
|
opts := Options{}
|
||||||
|
@ -103,7 +103,7 @@ 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()) {
|
func TestFromFixture(t testing.TB, repoFixture string) (*Repository, func()) {
|
||||||
repodir, cleanup := test.Env(t, repoFixture)
|
repodir, cleanup := test.Env(t, repoFixture)
|
||||||
repo := TestOpenLocal(t, repodir)
|
repo := TestOpenLocal(t, repodir)
|
||||||
|
|
||||||
|
@ -111,7 +111,7 @@ func TestFromFixture(t testing.TB, repoFixture string) (restic.Repository, func(
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestOpenLocal opens a local repository.
|
// TestOpenLocal opens a local repository.
|
||||||
func TestOpenLocal(t testing.TB, dir string) restic.Repository {
|
func TestOpenLocal(t testing.TB, dir string) *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 {
|
||||||
|
@ -123,7 +123,7 @@ func TestOpenLocal(t testing.TB, dir string) restic.Repository {
|
||||||
return TestOpenBackend(t, be)
|
return TestOpenBackend(t, be)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOpenBackend(t testing.TB, be backend.Backend) restic.Repository {
|
func TestOpenBackend(t testing.TB, be backend.Backend) *Repository {
|
||||||
repo, err := New(be, Options{})
|
repo, err := New(be, Options{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
|
@ -18,7 +18,7 @@ func TestUpgradeRepoV2(t *testing.T) {
|
||||||
t.Fatal("test repo has wrong version")
|
t.Fatal("test repo has wrong version")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := UpgradeRepo(context.Background(), repo.(*Repository))
|
err := UpgradeRepo(context.Background(), repo)
|
||||||
rtest.OK(t, err)
|
rtest.OK(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ func TestUpgradeRepoV2Failure(t *testing.T) {
|
||||||
t.Fatal("test repo has wrong version")
|
t.Fatal("test repo has wrong version")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := UpgradeRepo(context.Background(), repo.(*Repository))
|
err := UpgradeRepo(context.Background(), repo)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected error returned from Apply(), got nil")
|
t.Fatal("expected error returned from Apply(), got nil")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue