2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 23:06:32 +00:00

Add trees recursively to test snapshot

This commit is contained in:
Alexander Neumann 2016-07-31 10:58:09 +02:00
parent 952f124238
commit d9012b4a64
2 changed files with 46 additions and 17 deletions

View File

@ -43,28 +43,50 @@ func saveFile(t testing.TB, repo *repository.Repository, rd io.Reader) (blobs ba
return blobs return blobs
} }
const maxFileSize = 1500000 const (
const maxSeed = 100 maxFileSize = 1500000
maxSeed = 20
maxNodes = 32
)
// saveTree saves a tree of fake files in the repo and returns the ID. // saveTree saves a tree of fake files in the repo and returns the ID.
func saveTree(t testing.TB, repo *repository.Repository, seed int64) backend.ID { func saveTree(t testing.TB, repo *repository.Repository, seed int64, depth int) backend.ID {
t.Logf("create fake tree with seed %d, depth %d", seed, depth)
rnd := rand.NewSource(seed) rnd := rand.NewSource(seed)
numNodes := int(rnd.Int63() % 64) numNodes := int(rnd.Int63() % maxNodes)
t.Logf("create %v nodes", numNodes) t.Logf("create %v nodes", numNodes)
var tree Tree var tree Tree
for i := 0; i < numNodes; i++ { for i := 0; i < numNodes; i++ {
seed := rnd.Int63() % maxSeed
size := rnd.Int63() % maxFileSize // randomly select the type of the node, either tree (p = 1/4) or file (p = 3/4).
if depth > 1 && rnd.Int63()%4 == 0 {
treeSeed := rnd.Int63() % maxSeed
id := saveTree(t, repo, treeSeed, depth-1)
node := &Node{ node := &Node{
Name: fmt.Sprintf("file-%v", seed), Name: fmt.Sprintf("dir-%v", treeSeed),
Type: "file", Type: "dir",
Mode: 0644, Mode: 0755,
Size: uint64(size), Subtree: &id,
} }
node.Content = saveFile(t, repo, fakeFile(t, seed, size)) tree.Nodes = append(tree.Nodes, node)
continue
}
fileSeed := rnd.Int63() % maxSeed
fileSize := rnd.Int63() % maxFileSize
node := &Node{
Name: fmt.Sprintf("file-%v", fileSeed),
Type: "file",
Mode: 0644,
Size: uint64(fileSize),
}
node.Content = saveFile(t, repo, fakeFile(t, fileSeed, fileSize))
tree.Nodes = append(tree.Nodes, node) tree.Nodes = append(tree.Nodes, node)
} }
@ -78,8 +100,12 @@ func saveTree(t testing.TB, repo *repository.Repository, seed int64) backend.ID
// TestCreateSnapshot creates a snapshot filled with fake data. The // TestCreateSnapshot creates a snapshot filled with fake data. The
// fake data is generated deterministically from the timestamp `at`, which is // fake data is generated deterministically from the timestamp `at`, which is
// also used as the snapshot's timestamp. // also used as the snapshot's timestamp. The tree's depth can be specified
func TestCreateSnapshot(t testing.TB, repo *repository.Repository, at time.Time) backend.ID { // with the parameter depth.
func TestCreateSnapshot(t testing.TB, repo *repository.Repository, at time.Time, depth int) backend.ID {
seed := at.Unix()
t.Logf("create fake snapshot at %s with seed %d", at, seed)
fakedir := fmt.Sprintf("fakedir-at-%v", at.Format("2006-01-02 15:04:05")) fakedir := fmt.Sprintf("fakedir-at-%v", at.Format("2006-01-02 15:04:05"))
snapshot, err := NewSnapshot([]string{fakedir}) snapshot, err := NewSnapshot([]string{fakedir})
if err != nil { if err != nil {
@ -87,7 +113,7 @@ func TestCreateSnapshot(t testing.TB, repo *repository.Repository, at time.Time)
} }
snapshot.Time = at snapshot.Time = at
treeID := saveTree(t, repo, at.UnixNano()) treeID := saveTree(t, repo, seed, depth)
snapshot.Tree = &treeID snapshot.Tree = &treeID
id, err := repo.SaveJSONUnpacked(backend.Snapshot, snapshot) id, err := repo.SaveJSONUnpacked(backend.Snapshot, snapshot)

View File

@ -10,14 +10,17 @@ import (
var testSnapshotTime = time.Unix(1460289341, 207401672) var testSnapshotTime = time.Unix(1460289341, 207401672)
const testCreateSnapshots = 3 const (
testCreateSnapshots = 3
testDepth = 2
)
func TestCreateSnapshot(t *testing.T) { func TestCreateSnapshot(t *testing.T) {
repo, cleanup := repository.TestRepository(t) repo, cleanup := repository.TestRepository(t)
defer cleanup() defer cleanup()
for i := 0; i < testCreateSnapshots; i++ { for i := 0; i < testCreateSnapshots; i++ {
restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second)) restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth)
} }
snapshots, err := restic.LoadAllSnapshots(repo) snapshots, err := restic.LoadAllSnapshots(repo)