2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-04 20:37:49 +00:00

Add test for FindUsedBlobs

This commit is contained in:
Alexander Neumann 2016-07-31 21:26:56 +02:00
parent 4720a7d807
commit 22aa17091b
2 changed files with 57 additions and 14 deletions

View File

@ -174,3 +174,9 @@ func RebuildIndex(repo *Repository) error {
return nil return nil
} }
// FindUsedBlobs traverses the tree ID and returns a set of all blobs
// encountered.
func FindUsedBlobs(repo *Repository, treeID backend.ID) (backend.IDSet, error) {
return nil, nil
}

View File

@ -1,11 +1,14 @@
package repository package repository_test
import ( import (
"io" "io"
"math/rand" "math/rand"
"restic"
"restic/backend" "restic/backend"
"restic/pack" "restic/pack"
"restic/repository"
"testing" "testing"
"time"
) )
func randomSize(min, max int) int { func randomSize(min, max int) int {
@ -13,7 +16,7 @@ func randomSize(min, max int) int {
} }
func random(t *testing.T, length int) []byte { func random(t *testing.T, length int) []byte {
rd := NewRandReader(rand.New(rand.NewSource(int64(length)))) rd := repository.NewRandReader(rand.New(rand.NewSource(int64(length))))
buf := make([]byte, length) buf := make([]byte, length)
_, err := io.ReadFull(rd, buf) _, err := io.ReadFull(rd, buf)
if err != nil { if err != nil {
@ -23,7 +26,7 @@ func random(t *testing.T, length int) []byte {
return buf return buf
} }
func createRandomBlobs(t *testing.T, repo *Repository, blobs int, pData float32) { func createRandomBlobs(t *testing.T, repo *repository.Repository, blobs int, pData float32) {
for i := 0; i < blobs; i++ { for i := 0; i < blobs; i++ {
var ( var (
tpe pack.BlobType tpe pack.BlobType
@ -57,7 +60,7 @@ func createRandomBlobs(t *testing.T, repo *Repository, blobs int, pData float32)
// selectBlobs splits the list of all blobs randomly into two lists. A blob // selectBlobs splits the list of all blobs randomly into two lists. A blob
// will be contained in the firstone ith probability p. // will be contained in the firstone ith probability p.
func selectBlobs(t *testing.T, repo *Repository, p float32) (list1, list2 backend.IDSet) { func selectBlobs(t *testing.T, repo *repository.Repository, p float32) (list1, list2 backend.IDSet) {
done := make(chan struct{}) done := make(chan struct{})
defer close(done) defer close(done)
@ -82,7 +85,7 @@ func selectBlobs(t *testing.T, repo *Repository, p float32) (list1, list2 backen
return list1, list2 return list1, list2
} }
func listPacks(t *testing.T, repo *Repository) backend.IDSet { func listPacks(t *testing.T, repo *repository.Repository) backend.IDSet {
done := make(chan struct{}) done := make(chan struct{})
defer close(done) defer close(done)
@ -94,7 +97,7 @@ func listPacks(t *testing.T, repo *Repository) backend.IDSet {
return list return list
} }
func findPacksForBlobs(t *testing.T, repo *Repository, blobs backend.IDSet) backend.IDSet { func findPacksForBlobs(t *testing.T, repo *repository.Repository, blobs backend.IDSet) backend.IDSet {
packs := backend.NewIDSet() packs := backend.NewIDSet()
idx := repo.Index() idx := repo.Index()
@ -110,34 +113,34 @@ func findPacksForBlobs(t *testing.T, repo *Repository, blobs backend.IDSet) back
return packs return packs
} }
func repack(t *testing.T, repo *Repository, packs, blobs backend.IDSet) { func repack(t *testing.T, repo *repository.Repository, packs, blobs backend.IDSet) {
err := Repack(repo, packs, blobs) err := repository.Repack(repo, packs, blobs)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
func saveIndex(t *testing.T, repo *Repository) { func saveIndex(t *testing.T, repo *repository.Repository) {
if err := repo.SaveIndex(); err != nil { if err := repo.SaveIndex(); err != nil {
t.Fatalf("repo.SaveIndex() %v", err) t.Fatalf("repo.SaveIndex() %v", err)
} }
} }
func rebuildIndex(t *testing.T, repo *Repository) { func rebuildIndex(t *testing.T, repo *repository.Repository) {
if err := RebuildIndex(repo); err != nil { if err := repository.RebuildIndex(repo); err != nil {
t.Fatalf("error rebuilding index: %v", err) t.Fatalf("error rebuilding index: %v", err)
} }
} }
func reloadIndex(t *testing.T, repo *Repository) { func reloadIndex(t *testing.T, repo *repository.Repository) {
repo.SetIndex(NewMasterIndex()) repo.SetIndex(repository.NewMasterIndex())
if err := repo.LoadIndex(); err != nil { if err := repo.LoadIndex(); err != nil {
t.Fatalf("error loading new index: %v", err) t.Fatalf("error loading new index: %v", err)
} }
} }
func TestRepack(t *testing.T) { func TestRepack(t *testing.T) {
repo, cleanup := TestRepository(t) repo, cleanup := repository.TestRepository(t)
defer cleanup() defer cleanup()
createRandomBlobs(t, repo, rand.Intn(400), 0.7) createRandomBlobs(t, repo, rand.Intn(400), 0.7)
@ -189,3 +192,37 @@ func TestRepack(t *testing.T) {
} }
} }
} }
const (
testSnapshots = 3
testDepth = 2
)
var testTime = time.Unix(1469960361, 23)
func TestFindUsedBlobs(t *testing.T) {
repo, cleanup := repository.TestRepository(t)
defer cleanup()
var snapshots []*restic.Snapshot
for i := 0; i < testSnapshots; i++ {
sn := restic.TestCreateSnapshot(t, repo, testTime.Add(time.Duration(i)*time.Second), testDepth)
t.Logf("snapshot %v saved, tree %v", sn.ID().Str(), sn.Tree.Str())
snapshots = append(snapshots, sn)
}
for _, sn := range snapshots {
usedBlobs, err := repository.FindUsedBlobs(repo, *sn.Tree)
if err != nil {
t.Errorf("FindUsedBlobs returned error: %v", err)
continue
}
if len(usedBlobs) == 0 {
t.Errorf("FindUsedBlobs returned an empty set")
continue
}
t.Logf("used blobs from snapshot %v (tree %v): %v", sn.ID().Str(), sn.Tree.Str(), usedBlobs)
}
}