2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-30 13:40:36 +00:00
restic/repository/repository_test.go

220 lines
4.3 KiB
Go
Raw Normal View History

package repository_test
import (
"bytes"
2015-02-16 19:00:23 +00:00
"crypto/rand"
"crypto/sha256"
"encoding/json"
2015-02-16 19:00:23 +00:00
"io"
2015-07-04 14:52:17 +00:00
"path/filepath"
"testing"
"github.com/restic/restic"
"github.com/restic/restic/backend"
"github.com/restic/restic/pack"
2015-07-04 14:52:17 +00:00
"github.com/restic/restic/repository"
2015-04-09 19:15:48 +00:00
. "github.com/restic/restic/test"
)
type testJSONStruct struct {
Foo uint32
Bar string
Baz []byte
}
2015-05-09 11:32:52 +00:00
var repoTests = []testJSONStruct{
testJSONStruct{Foo: 23, Bar: "Teststring", Baz: []byte("xx")},
}
func TestSaveJSON(t *testing.T) {
repo := SetupRepo()
defer TeardownRepo(repo)
2015-05-09 11:32:52 +00:00
for _, obj := range repoTests {
data, err := json.Marshal(obj)
2015-04-09 19:15:48 +00:00
OK(t, err)
data = append(data, '\n')
h := sha256.Sum256(data)
2015-05-09 11:32:52 +00:00
id, err := repo.SaveJSON(pack.Tree, obj)
2015-04-09 19:15:48 +00:00
OK(t, err)
Assert(t, h == id,
"TestSaveJSON: wrong plaintext ID: expected %02x, got %02x",
h, id)
}
}
func BenchmarkSaveJSON(t *testing.B) {
repo := SetupRepo()
defer TeardownRepo(repo)
2015-05-09 11:32:52 +00:00
obj := repoTests[0]
data, err := json.Marshal(obj)
2015-04-09 19:15:48 +00:00
OK(t, err)
data = append(data, '\n')
h := sha256.Sum256(data)
2015-02-16 19:00:23 +00:00
t.ResetTimer()
for i := 0; i < t.N; i++ {
2015-05-09 11:32:52 +00:00
id, err := repo.SaveJSON(pack.Tree, obj)
2015-04-09 19:15:48 +00:00
OK(t, err)
Assert(t, h == id,
"TestSaveJSON: wrong plaintext ID: expected %02x, got %02x",
h, id)
}
}
2015-02-16 19:00:23 +00:00
var testSizes = []int{5, 23, 2<<18 + 23, 1 << 20}
func TestSave(t *testing.T) {
repo := SetupRepo()
defer TeardownRepo(repo)
for _, size := range testSizes {
data := make([]byte, size)
_, err := io.ReadFull(rand.Reader, data)
OK(t, err)
id := backend.Hash(data)
// save
sid, err := repo.SaveAndEncrypt(pack.Data, data, nil)
OK(t, err)
Equals(t, id, sid)
2015-05-09 11:32:52 +00:00
OK(t, repo.Flush())
// read back
buf, err := repo.LoadBlob(pack.Data, id, make([]byte, size))
Assert(t, len(buf) == len(data),
"number of bytes read back does not match: expected %d, got %d",
len(data), len(buf))
Assert(t, bytes.Equal(buf, data),
"data does not match: expected %02x, got %02x",
data, buf)
}
}
2015-02-16 19:00:23 +00:00
func TestSaveFrom(t *testing.T) {
repo := SetupRepo()
defer TeardownRepo(repo)
2015-02-16 19:00:23 +00:00
for _, size := range testSizes {
data := make([]byte, size)
_, err := io.ReadFull(rand.Reader, data)
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-02-16 19:00:23 +00:00
id := backend.Hash(data)
2015-02-16 19:00:23 +00:00
// save
err = repo.SaveFrom(pack.Data, &id, uint(size), bytes.NewReader(data))
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-02-16 19:00:23 +00:00
2015-05-09 11:32:52 +00:00
OK(t, repo.Flush())
2015-02-16 19:00:23 +00:00
// read back
buf, err := repo.LoadBlob(pack.Data, id, make([]byte, size))
2015-02-16 19:00:23 +00:00
2015-04-09 19:15:48 +00:00
Assert(t, len(buf) == len(data),
2015-02-16 19:00:23 +00:00
"number of bytes read back does not match: expected %d, got %d",
len(data), len(buf))
2015-04-09 19:15:48 +00:00
Assert(t, bytes.Equal(buf, data),
2015-02-16 19:00:23 +00:00
"data does not match: expected %02x, got %02x",
data, buf)
}
}
func BenchmarkSaveFrom(t *testing.B) {
repo := SetupRepo()
defer TeardownRepo(repo)
2015-02-16 19:00:23 +00:00
size := 4 << 20 // 4MiB
data := make([]byte, size)
_, err := io.ReadFull(rand.Reader, data)
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-02-16 19:00:23 +00:00
id := backend.ID(sha256.Sum256(data))
2015-02-16 19:00:23 +00:00
t.ResetTimer()
t.SetBytes(int64(size))
for i := 0; i < t.N; i++ {
// save
err = repo.SaveFrom(pack.Data, &id, uint(size), bytes.NewReader(data))
2015-04-09 19:15:48 +00:00
OK(t, err)
2015-02-16 19:00:23 +00:00
}
}
2015-02-17 22:05:23 +00:00
func TestLoadJSONPack(t *testing.T) {
repo := SetupRepo()
defer TeardownRepo(repo)
2015-06-28 11:15:35 +00:00
if BenchArchiveDirectory == "" {
2015-05-09 11:32:52 +00:00
t.Skip("benchdir not set, skipping")
}
// archive a few files
2015-06-28 11:15:35 +00:00
sn := SnapshotDir(t, repo, BenchArchiveDirectory, nil)
2015-05-09 11:32:52 +00:00
OK(t, repo.Flush())
tree := restic.NewTree()
err := repo.LoadJSONPack(pack.Tree, *sn.Tree, &tree)
2015-04-09 19:15:48 +00:00
OK(t, err)
}
func TestLoadJSONUnpacked(t *testing.T) {
repo := SetupRepo()
defer TeardownRepo(repo)
2015-06-28 11:15:35 +00:00
if BenchArchiveDirectory == "" {
2015-05-09 11:32:52 +00:00
t.Skip("benchdir not set, skipping")
}
// archive a snapshot
sn := restic.Snapshot{}
sn.Hostname = "foobar"
sn.Username = "test!"
2015-05-09 11:32:52 +00:00
id, err := repo.SaveJSONUnpacked(backend.Snapshot, &sn)
OK(t, err)
var sn2 restic.Snapshot
// restore
2015-05-09 11:32:52 +00:00
err = repo.LoadJSONUnpacked(backend.Snapshot, id, &sn2)
OK(t, err)
Equals(t, sn.Hostname, sn2.Hostname)
Equals(t, sn.Username, sn2.Username)
}
2015-07-04 14:52:17 +00:00
var repoFixture = filepath.Join("testdata", "test-repo.tar.gz")
func TestLoadIndex(t *testing.T) {
WithTestEnvironment(t, repoFixture, func(repodir string) {
repo := OpenLocalRepo(t, repodir)
OK(t, repo.LoadIndex())
})
}
func BenchmarkLoadIndex(b *testing.B) {
WithTestEnvironment(b, repoFixture, func(repodir string) {
repo := OpenLocalRepo(b, repodir)
b.ResetTimer()
for i := 0; i < b.N; i++ {
repo.SetIndex(repository.NewIndex())
OK(b, repo.LoadIndex())
}
})
}