Do not create duplicate content for tests

This commit is contained in:
Alexander Neumann 2016-08-01 21:30:46 +02:00
parent f1bc181c5b
commit 035d0aeb31

View file

@ -1,6 +1,7 @@
package restic
import (
"encoding/json"
"fmt"
"io"
"math/rand"
@ -34,10 +35,14 @@ func saveFile(t testing.TB, repo *repository.Repository, rd io.Reader) (blobs ba
t.Fatalf("unable to save chunk in repo: %v", err)
}
id, err := repo.SaveAndEncrypt(pack.Data, chunk.Data, nil)
id := backend.Hash(chunk.Data)
if !repo.Index().Has(id) {
_, err := repo.SaveAndEncrypt(pack.Data, chunk.Data, &id)
if err != nil {
t.Fatalf("error saving chunk: %v", err)
}
}
blobs = append(blobs, id)
}
@ -50,6 +55,23 @@ const (
maxNodes = 32
)
func treeIsKnown(t testing.TB, repo *repository.Repository, tree *Tree) (bool, backend.ID) {
data, err := json.Marshal(tree)
if err != nil {
t.Fatalf("json.Marshal(tree) returned error: %v", err)
return false, backend.ID{}
}
data = append(data, '\n')
// check if tree has been saved before
id := backend.Hash(data)
if repo.Index().Has(id) {
return true, id
}
return false, 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, depth int) backend.ID {
rnd := rand.NewSource(seed)
@ -88,6 +110,10 @@ func saveTree(t testing.TB, repo *repository.Repository, seed int64, depth int)
tree.Nodes = append(tree.Nodes, node)
}
if known, id := treeIsKnown(t, repo, &tree); known {
return id
}
id, err := repo.SaveJSON(pack.Tree, tree)
if err != nil {
t.Fatal(err)