2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-23 21:27:34 +00:00
restic/internal/repository/repair_pack_test.go

131 lines
4.4 KiB
Go
Raw Normal View History

2024-01-20 20:54:47 +00:00
package repository_test
import (
"context"
"math/rand"
"testing"
"time"
"github.com/restic/restic/internal/backend"
backendtest "github.com/restic/restic/internal/backend/test"
2024-01-20 20:54:47 +00:00
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/test"
rtest "github.com/restic/restic/internal/test"
"github.com/restic/restic/internal/ui/progress"
)
func listBlobs(repo restic.Repository) restic.BlobSet {
blobs := restic.NewBlobSet()
_ = repo.ListBlobs(context.TODO(), func(pb restic.PackedBlob) {
2024-01-20 20:54:47 +00:00
blobs.Insert(pb.BlobHandle)
})
return blobs
}
func replaceFile(t *testing.T, be backend.Backend, h backend.Handle, damage func([]byte) []byte) {
buf, err := backendtest.LoadAll(context.TODO(), be, h)
2024-01-20 20:54:47 +00:00
test.OK(t, err)
buf = damage(buf)
test.OK(t, be.Remove(context.TODO(), h))
test.OK(t, be.Save(context.TODO(), h, backend.NewByteReader(buf, be.Hasher())))
2024-01-20 20:54:47 +00:00
}
func TestRepairBrokenPack(t *testing.T) {
repository.TestAllVersions(t, testRepairBrokenPack)
}
func testRepairBrokenPack(t *testing.T, version uint) {
tests := []struct {
name string
2024-08-10 17:34:49 +00:00
damage func(t *testing.T, random *rand.Rand, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet)
2024-01-20 20:54:47 +00:00
}{
{
"valid pack",
2024-08-10 17:34:49 +00:00
func(t *testing.T, random *rand.Rand, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
2024-01-20 20:54:47 +00:00
return packsBefore, restic.NewBlobSet()
},
},
{
"broken pack",
2024-08-10 17:34:49 +00:00
func(t *testing.T, random *rand.Rand, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
wrongBlob := createRandomWrongBlob(t, random, repo)
2024-01-20 20:54:47 +00:00
damagedPacks := findPacksForBlobs(t, repo, restic.NewBlobSet(wrongBlob))
return damagedPacks, restic.NewBlobSet(wrongBlob)
},
},
{
"partially broken pack",
2024-08-10 17:34:49 +00:00
func(t *testing.T, random *rand.Rand, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
2024-01-20 20:54:47 +00:00
// damage one of the pack files
damagedID := packsBefore.List()[0]
replaceFile(t, be, backend.Handle{Type: backend.PackFile, Name: damagedID.String()},
2024-01-20 20:54:47 +00:00
func(buf []byte) []byte {
buf[0] ^= 0xff
return buf
})
// find blob that starts at offset 0
var damagedBlob restic.BlobHandle
for blobs := range repo.ListPacksFromIndex(context.TODO(), restic.NewIDSet(damagedID)) {
2024-01-20 20:54:47 +00:00
for _, blob := range blobs.Blobs {
if blob.Offset == 0 {
damagedBlob = blob.BlobHandle
}
}
}
return restic.NewIDSet(damagedID), restic.NewBlobSet(damagedBlob)
},
}, {
"truncated pack",
2024-08-10 17:34:49 +00:00
func(t *testing.T, random *rand.Rand, repo *repository.Repository, be backend.Backend, packsBefore restic.IDSet) (restic.IDSet, restic.BlobSet) {
2024-01-20 20:54:47 +00:00
// damage one of the pack files
damagedID := packsBefore.List()[0]
replaceFile(t, be, backend.Handle{Type: backend.PackFile, Name: damagedID.String()},
2024-01-20 20:54:47 +00:00
func(buf []byte) []byte {
buf = buf[0:10]
return buf
})
// all blobs in the file are broken
damagedBlobs := restic.NewBlobSet()
for blobs := range repo.ListPacksFromIndex(context.TODO(), restic.NewIDSet(damagedID)) {
2024-01-20 20:54:47 +00:00
for _, blob := range blobs.Blobs {
damagedBlobs.Insert(blob.BlobHandle)
}
}
return restic.NewIDSet(damagedID), damagedBlobs
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
// disable verification to allow adding corrupted blobs to the repository
repo, be := repository.TestRepositoryWithBackend(t, nil, version, repository.Options{NoExtraVerify: true})
2024-01-20 20:54:47 +00:00
seed := time.Now().UnixNano()
2024-08-10 17:34:49 +00:00
random := rand.New(rand.NewSource(seed))
2024-01-20 20:54:47 +00:00
t.Logf("rand seed is %v", seed)
2024-08-10 17:34:49 +00:00
createRandomBlobs(t, random, repo, 5, 0.7, true)
2024-01-20 20:54:47 +00:00
packsBefore := listPacks(t, repo)
blobsBefore := listBlobs(repo)
2024-08-10 17:34:49 +00:00
toRepair, damagedBlobs := test.damage(t, random, repo, be, packsBefore)
2024-01-20 20:54:47 +00:00
rtest.OK(t, repository.RepairPacks(context.TODO(), repo, toRepair, &progress.NoopPrinter{}))
// reload index
2024-01-20 20:58:28 +00:00
rtest.OK(t, repo.LoadIndex(context.TODO(), nil))
2024-01-20 20:54:47 +00:00
packsAfter := listPacks(t, repo)
blobsAfter := listBlobs(repo)
rtest.Assert(t, len(packsAfter.Intersect(toRepair)) == 0, "some damaged packs were not removed")
rtest.Assert(t, len(packsBefore.Sub(toRepair).Sub(packsAfter)) == 0, "not-damaged packs were removed")
rtest.Assert(t, blobsBefore.Sub(damagedBlobs).Equals(blobsAfter), "diverging blob lists")
})
}
}