2016-04-10 14:52:15 +00:00
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"restic/backend"
|
|
|
|
"restic/pack"
|
|
|
|
"restic/repository"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/chunker"
|
|
|
|
)
|
|
|
|
|
|
|
|
// fakeFile returns a reader which yields deterministic pseudo-random data.
|
2016-08-02 20:11:55 +00:00
|
|
|
func fakeFile(t testing.TB, seed, size int64) io.Reader {
|
2016-07-31 08:29:53 +00:00
|
|
|
return io.LimitReader(repository.NewRandReader(rand.New(rand.NewSource(seed))), size)
|
2016-04-10 14:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// saveFile reads from rd and saves the blobs in the repository. The list of
|
|
|
|
// IDs is returned.
|
2016-08-02 20:11:55 +00:00
|
|
|
func saveFile(t testing.TB, repo *repository.Repository, rd io.Reader) (blobs backend.IDs) {
|
|
|
|
ch := chunker.New(rd, repo.Config.ChunkerPolynomial)
|
2016-04-10 14:52:15 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
chunk, err := ch.Next(getBuf())
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2016-08-02 20:11:55 +00:00
|
|
|
t.Fatalf("unabel to save chunk in repo: %v", err)
|
2016-04-10 14:52:15 +00:00
|
|
|
}
|
|
|
|
|
2016-08-02 20:11:55 +00:00
|
|
|
id, err := repo.SaveAndEncrypt(pack.Data, chunk.Data, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error saving chunk: %v", err)
|
2016-04-10 14:52:15 +00:00
|
|
|
}
|
|
|
|
blobs = append(blobs, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobs
|
|
|
|
}
|
|
|
|
|
2016-08-02 20:11:55 +00:00
|
|
|
const maxFileSize = 1500000
|
|
|
|
const maxSeed = 100
|
2016-04-10 15:25:32 +00:00
|
|
|
|
2016-08-02 20:11:55 +00:00
|
|
|
// 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 {
|
2016-04-10 14:52:15 +00:00
|
|
|
rnd := rand.NewSource(seed)
|
2016-08-02 20:11:55 +00:00
|
|
|
numNodes := int(rnd.Int63() % 64)
|
|
|
|
t.Logf("create %v nodes", numNodes)
|
2016-04-10 14:52:15 +00:00
|
|
|
|
|
|
|
var tree Tree
|
|
|
|
for i := 0; i < numNodes; i++ {
|
2016-08-02 20:11:55 +00:00
|
|
|
seed := rnd.Int63() % maxSeed
|
|
|
|
size := rnd.Int63() % maxFileSize
|
2016-04-10 15:25:32 +00:00
|
|
|
|
|
|
|
node := &Node{
|
2016-08-02 20:11:55 +00:00
|
|
|
Name: fmt.Sprintf("file-%v", seed),
|
2016-04-10 15:25:32 +00:00
|
|
|
Type: "file",
|
|
|
|
Mode: 0644,
|
2016-08-02 20:11:55 +00:00
|
|
|
Size: uint64(size),
|
2016-04-10 15:25:32 +00:00
|
|
|
}
|
2016-04-10 14:52:15 +00:00
|
|
|
|
2016-08-02 20:11:55 +00:00
|
|
|
node.Content = saveFile(t, repo, fakeFile(t, seed, size))
|
2016-04-10 14:52:15 +00:00
|
|
|
tree.Nodes = append(tree.Nodes, node)
|
|
|
|
}
|
|
|
|
|
2016-08-02 20:11:55 +00:00
|
|
|
id, err := repo.SaveJSON(pack.Tree, tree)
|
2016-04-10 14:52:15 +00:00
|
|
|
if err != nil {
|
2016-08-02 20:11:55 +00:00
|
|
|
t.Fatal(err)
|
2016-04-10 14:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2016-05-08 20:38:38 +00:00
|
|
|
// TestCreateSnapshot creates a snapshot filled with fake data. The
|
2016-04-10 14:52:15 +00:00
|
|
|
// fake data is generated deterministically from the timestamp `at`, which is
|
2016-08-02 20:11:55 +00:00
|
|
|
// also used as the snapshot's timestamp.
|
|
|
|
func TestCreateSnapshot(t testing.TB, repo *repository.Repository, at time.Time) backend.ID {
|
2016-04-10 14:52:15 +00:00
|
|
|
fakedir := fmt.Sprintf("fakedir-at-%v", at.Format("2006-01-02 15:04:05"))
|
|
|
|
snapshot, err := NewSnapshot([]string{fakedir})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
snapshot.Time = at
|
|
|
|
|
2016-08-02 20:11:55 +00:00
|
|
|
treeID := saveTree(t, repo, at.UnixNano())
|
2016-04-10 14:52:15 +00:00
|
|
|
snapshot.Tree = &treeID
|
|
|
|
|
|
|
|
id, err := repo.SaveJSONUnpacked(backend.Snapshot, snapshot)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("saved snapshot %v", id.Str())
|
|
|
|
|
|
|
|
err = repo.Flush()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.SaveIndex()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-08-02 20:11:55 +00:00
|
|
|
return id
|
2016-04-10 14:52:15 +00:00
|
|
|
}
|