2024-04-14 09:53:08 +00:00
|
|
|
package repository_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"github.com/restic/restic/internal/ui/progress"
|
|
|
|
)
|
|
|
|
|
|
|
|
func listIndex(t *testing.T, repo restic.Lister) restic.IDSet {
|
|
|
|
return listFiles(t, repo, restic.IndexFile)
|
|
|
|
}
|
|
|
|
|
2024-05-10 14:59:09 +00:00
|
|
|
func testRebuildIndex(t *testing.T, readAllPacks bool, damage func(t *testing.T, repo *repository.Repository, be backend.Backend)) {
|
|
|
|
repo, be := repository.TestRepositoryWithVersion(t, 0)
|
2024-04-14 09:53:08 +00:00
|
|
|
createRandomBlobs(t, repo, 4, 0.5, true)
|
|
|
|
createRandomBlobs(t, repo, 5, 0.5, true)
|
|
|
|
indexes := listIndex(t, repo)
|
|
|
|
t.Logf("old indexes %v", indexes)
|
|
|
|
|
2024-05-10 14:59:09 +00:00
|
|
|
damage(t, repo, be)
|
2024-04-14 09:53:08 +00:00
|
|
|
|
2024-05-10 14:59:09 +00:00
|
|
|
repo = repository.TestOpenBackend(t, be)
|
2024-04-14 09:53:08 +00:00
|
|
|
rtest.OK(t, repository.RepairIndex(context.TODO(), repo, repository.RepairIndexOptions{
|
|
|
|
ReadAllPacks: readAllPacks,
|
|
|
|
}, &progress.NoopPrinter{}))
|
|
|
|
|
|
|
|
newIndexes := listIndex(t, repo)
|
|
|
|
old := indexes.Intersect(newIndexes)
|
|
|
|
rtest.Assert(t, len(old) == 0, "expected old indexes to be removed, found %v", old)
|
|
|
|
|
|
|
|
checker.TestCheckRepo(t, repo, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRebuildIndex(t *testing.T) {
|
|
|
|
for _, test := range []struct {
|
|
|
|
name string
|
2024-05-10 14:59:09 +00:00
|
|
|
damage func(t *testing.T, repo *repository.Repository, be backend.Backend)
|
2024-04-14 09:53:08 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"valid index",
|
2024-05-10 14:59:09 +00:00
|
|
|
func(t *testing.T, repo *repository.Repository, be backend.Backend) {},
|
2024-04-14 09:53:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"damaged index",
|
2024-05-10 14:59:09 +00:00
|
|
|
func(t *testing.T, repo *repository.Repository, be backend.Backend) {
|
2024-04-14 09:53:08 +00:00
|
|
|
index := listIndex(t, repo).List()[0]
|
2024-05-10 14:59:09 +00:00
|
|
|
replaceFile(t, be, backend.Handle{Type: restic.IndexFile, Name: index.String()}, func(b []byte) []byte {
|
2024-04-14 09:53:08 +00:00
|
|
|
b[0] ^= 0xff
|
|
|
|
return b
|
|
|
|
})
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"missing index",
|
2024-05-10 14:59:09 +00:00
|
|
|
func(t *testing.T, repo *repository.Repository, be backend.Backend) {
|
2024-04-14 09:53:08 +00:00
|
|
|
index := listIndex(t, repo).List()[0]
|
2024-05-10 14:59:09 +00:00
|
|
|
rtest.OK(t, be.Remove(context.TODO(), backend.Handle{Type: restic.IndexFile, Name: index.String()}))
|
2024-04-14 09:53:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"missing pack",
|
2024-05-10 14:59:09 +00:00
|
|
|
func(t *testing.T, repo *repository.Repository, be backend.Backend) {
|
2024-04-14 09:53:08 +00:00
|
|
|
pack := listPacks(t, repo).List()[0]
|
2024-05-10 14:59:09 +00:00
|
|
|
rtest.OK(t, be.Remove(context.TODO(), backend.Handle{Type: restic.PackFile, Name: pack.String()}))
|
2024-04-14 09:53:08 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
testRebuildIndex(t, false, test.damage)
|
|
|
|
testRebuildIndex(t, true, test.damage)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|