2016-08-01 16:31:44 +00:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
2016-08-01 16:40:08 +00:00
|
|
|
"bufio"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
2016-08-01 16:31:44 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-08-01 16:40:08 +00:00
|
|
|
"restic/backend"
|
2016-08-01 16:31:44 +00:00
|
|
|
"restic/repository"
|
|
|
|
)
|
|
|
|
|
2016-08-01 16:40:08 +00:00
|
|
|
func loadIDSet(t testing.TB, filename string) backend.IDSet {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("unable to open golden file %v: %v", filename, err)
|
|
|
|
return backend.IDSet{}
|
|
|
|
}
|
|
|
|
|
|
|
|
sc := bufio.NewScanner(f)
|
|
|
|
|
|
|
|
ids := backend.NewIDSet()
|
|
|
|
for sc.Scan() {
|
|
|
|
id, err := backend.ParseID(sc.Text())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("file %v contained invalid id: %v", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ids.Insert(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = f.Close(); err != nil {
|
|
|
|
t.Errorf("closing file %v failed with error %v", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveIDSet(t testing.TB, filename string, s backend.IDSet) {
|
|
|
|
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0644)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to update golden file %v: %v", filename, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var ids backend.IDs
|
|
|
|
for id := range s {
|
|
|
|
ids = append(ids, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(ids)
|
|
|
|
for _, id := range ids {
|
|
|
|
fmt.Fprintf(f, "%s\n", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = f.Close(); err != nil {
|
|
|
|
t.Fatalf("close file %v returned error: %v", filename, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var updateGoldenFiles = flag.Bool("update", false, "update golden files in testdata/")
|
|
|
|
|
2016-08-01 16:31:44 +00:00
|
|
|
const (
|
2016-08-01 18:44:02 +00:00
|
|
|
findTestSnapshots = 3
|
|
|
|
findTestDepth = 2
|
2016-08-01 16:31:44 +00:00
|
|
|
)
|
|
|
|
|
2016-08-01 18:44:02 +00:00
|
|
|
var findTestTime = time.Unix(1469960361, 23)
|
2016-08-01 16:31:44 +00:00
|
|
|
|
|
|
|
func TestFindUsedBlobs(t *testing.T) {
|
|
|
|
repo, cleanup := repository.TestRepository(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
var snapshots []*Snapshot
|
2016-08-01 18:44:02 +00:00
|
|
|
for i := 0; i < findTestSnapshots; i++ {
|
|
|
|
sn := TestCreateSnapshot(t, repo, findTestTime.Add(time.Duration(i)*time.Second), findTestDepth)
|
2016-08-01 16:31:44 +00:00
|
|
|
t.Logf("snapshot %v saved, tree %v", sn.ID().Str(), sn.Tree.Str())
|
|
|
|
snapshots = append(snapshots, sn)
|
|
|
|
}
|
|
|
|
|
2016-08-01 16:40:08 +00:00
|
|
|
for i, sn := range snapshots {
|
2016-08-01 18:44:02 +00:00
|
|
|
usedBlobs := backend.NewIDSet()
|
|
|
|
err := FindUsedBlobs(repo, *sn.Tree, usedBlobs)
|
2016-08-01 16:31:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("FindUsedBlobs returned error: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(usedBlobs) == 0 {
|
|
|
|
t.Errorf("FindUsedBlobs returned an empty set")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-08-01 16:40:08 +00:00
|
|
|
goldenFilename := filepath.Join("testdata", fmt.Sprintf("used_blobs_snapshot%d", i))
|
|
|
|
want := loadIDSet(t, goldenFilename)
|
|
|
|
|
|
|
|
if !want.Equals(usedBlobs) {
|
|
|
|
t.Errorf("snapshot %d: wrong list of blobs returned:\n missing blobs: %v\n extra blobs: %v",
|
|
|
|
i, want.Sub(usedBlobs), usedBlobs.Sub(want))
|
|
|
|
}
|
|
|
|
|
|
|
|
if *updateGoldenFiles {
|
|
|
|
saveIDSet(t, goldenFilename, usedBlobs)
|
|
|
|
}
|
2016-08-01 16:31:44 +00:00
|
|
|
}
|
|
|
|
}
|