2016-08-31 20:39:36 +00:00
|
|
|
package archiver_test
|
2014-11-16 20:41:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-06-05 21:56:59 +00:00
|
|
|
"context"
|
2014-11-16 20:41:05 +00:00
|
|
|
"io"
|
2017-09-04 19:39:21 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-11-16 20:41:05 +00:00
|
|
|
"testing"
|
2015-12-09 20:09:49 +00:00
|
|
|
"time"
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/archiver"
|
|
|
|
"github.com/restic/restic/internal/crypto"
|
2017-12-15 23:01:28 +00:00
|
|
|
"github.com/restic/restic/internal/fs"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/repository"
|
2017-07-24 15:42:25 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2017-07-23 12:21:03 +00:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/errors"
|
2016-09-03 11:34:04 +00:00
|
|
|
|
|
|
|
"github.com/restic/chunker"
|
2014-11-16 20:41:05 +00:00
|
|
|
)
|
|
|
|
|
2015-04-05 22:22:19 +00:00
|
|
|
var testPol = chunker.Pol(0x3DA3358B4DC173)
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2015-02-17 19:02:43 +00:00
|
|
|
type Rdr interface {
|
2015-02-09 22:39:16 +00:00
|
|
|
io.ReadSeeker
|
|
|
|
io.ReaderAt
|
2015-02-17 19:02:43 +00:00
|
|
|
}
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-05-04 22:45:29 +00:00
|
|
|
func benchmarkChunkEncrypt(b testing.TB, buf, buf2 []byte, rd Rdr, key *crypto.Key) {
|
2015-02-09 22:39:16 +00:00
|
|
|
rd.Seek(0, 0)
|
2016-02-22 20:09:21 +00:00
|
|
|
ch := chunker.New(rd, testPol)
|
2017-10-29 10:33:57 +00:00
|
|
|
nonce := crypto.NewRandomNonce()
|
2015-02-09 22:39:16 +00:00
|
|
|
|
|
|
|
for {
|
2016-02-22 20:09:21 +00:00
|
|
|
chunk, err := ch.Next(buf)
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) == io.EOF {
|
2015-02-09 22:39:16 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(b, err)
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.Assert(b, uint(len(chunk.Data)) == chunk.Length,
|
2016-02-22 20:09:21 +00:00
|
|
|
"invalid length: got %d, expected %d", len(chunk.Data), chunk.Length)
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2017-10-29 10:33:57 +00:00
|
|
|
_ = key.Seal(buf2[:0], nonce, chunk.Data, nil)
|
2015-02-09 22:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-16 20:41:05 +00:00
|
|
|
func BenchmarkChunkEncrypt(b *testing.B) {
|
2016-09-04 11:24:51 +00:00
|
|
|
repo, cleanup := repository.TestRepository(b)
|
2016-09-04 10:52:43 +00:00
|
|
|
defer cleanup()
|
2015-06-26 20:12:04 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
data := rtest.Random(23, 10<<20) // 10MiB
|
2015-02-08 21:54:45 +00:00
|
|
|
rd := bytes.NewReader(data)
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2015-05-04 22:14:07 +00:00
|
|
|
buf := make([]byte, chunker.MaxSize)
|
|
|
|
buf2 := make([]byte, chunker.MaxSize)
|
2015-02-17 19:02:43 +00:00
|
|
|
|
2014-11-16 20:41:05 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(len(data)))
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-06-26 20:12:04 +00:00
|
|
|
benchmarkChunkEncrypt(b, buf, buf2, rd, repo.Key())
|
2015-02-09 22:39:16 +00:00
|
|
|
}
|
2015-02-17 19:02:43 +00:00
|
|
|
}
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-05-04 22:45:29 +00:00
|
|
|
func benchmarkChunkEncryptP(b *testing.PB, buf []byte, rd Rdr, key *crypto.Key) {
|
2016-02-22 20:09:21 +00:00
|
|
|
ch := chunker.New(rd, testPol)
|
2017-10-29 10:33:57 +00:00
|
|
|
nonce := crypto.NewRandomNonce()
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
for {
|
2016-02-22 20:09:21 +00:00
|
|
|
chunk, err := ch.Next(buf)
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) == io.EOF {
|
2015-02-09 22:39:16 +00:00
|
|
|
break
|
|
|
|
}
|
2014-11-16 20:41:05 +00:00
|
|
|
|
2017-10-29 10:33:57 +00:00
|
|
|
_ = key.Seal(chunk.Data[:0], nonce, chunk.Data, nil)
|
2015-02-09 22:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-08 21:54:45 +00:00
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
func BenchmarkChunkEncryptParallel(b *testing.B) {
|
2016-09-04 11:24:51 +00:00
|
|
|
repo, cleanup := repository.TestRepository(b)
|
2016-09-04 10:52:43 +00:00
|
|
|
defer cleanup()
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
data := rtest.Random(23, 10<<20) // 10MiB
|
2015-02-09 22:39:16 +00:00
|
|
|
|
2015-05-04 22:14:07 +00:00
|
|
|
buf := make([]byte, chunker.MaxSize)
|
2015-02-17 19:02:43 +00:00
|
|
|
|
2015-02-09 22:39:16 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
b.SetBytes(int64(len(data)))
|
|
|
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
rd := bytes.NewReader(data)
|
2015-06-26 20:12:04 +00:00
|
|
|
benchmarkChunkEncryptP(pb, buf, rd, repo.Key())
|
2014-11-16 20:41:05 +00:00
|
|
|
}
|
2015-02-09 22:39:16 +00:00
|
|
|
})
|
2014-11-16 20:41:05 +00:00
|
|
|
}
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func archiveDirectory(b testing.TB) {
|
2016-09-04 11:24:51 +00:00
|
|
|
repo, cleanup := repository.TestRepository(b)
|
2016-09-04 10:52:43 +00:00
|
|
|
defer cleanup()
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2016-08-31 21:07:50 +00:00
|
|
|
arch := archiver.New(repo)
|
2015-02-10 20:59:18 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
_, id, err := arch.Snapshot(context.TODO(), nil, []string{rtest.BenchArchiveDirectory}, nil, "localhost", nil, time.Now())
|
|
|
|
rtest.OK(b, err)
|
2015-02-10 20:59:18 +00:00
|
|
|
|
|
|
|
b.Logf("snapshot archived as %v", id)
|
|
|
|
}
|
2015-02-17 21:39:44 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func TestArchiveDirectory(t *testing.T) {
|
2017-10-02 13:06:39 +00:00
|
|
|
if rtest.BenchArchiveDirectory == "" {
|
2015-04-26 15:44:38 +00:00
|
|
|
t.Skip("benchdir not set, skipping TestArchiveDirectory")
|
|
|
|
}
|
|
|
|
|
|
|
|
archiveDirectory(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkArchiveDirectory(b *testing.B) {
|
2017-10-02 13:06:39 +00:00
|
|
|
if rtest.BenchArchiveDirectory == "" {
|
2015-04-26 15:44:38 +00:00
|
|
|
b.Skip("benchdir not set, skipping BenchmarkArchiveDirectory")
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:58:50 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
archiveDirectory(b)
|
|
|
|
}
|
2015-02-17 21:39:44 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 16:25:36 +00:00
|
|
|
func countPacks(t testing.TB, repo restic.Repository, tpe restic.FileType) (n uint) {
|
|
|
|
err := repo.Backend().List(context.TODO(), tpe, func(restic.FileInfo) error {
|
2016-09-03 11:34:04 +00:00
|
|
|
n++
|
2018-01-21 16:25:36 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2016-09-03 11:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func archiveWithDedup(t testing.TB) {
|
2016-09-04 11:24:51 +00:00
|
|
|
repo, cleanup := repository.TestRepository(t)
|
2016-09-04 10:52:43 +00:00
|
|
|
defer cleanup()
|
2015-06-26 20:12:04 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
if rtest.BenchArchiveDirectory == "" {
|
2015-04-26 15:44:38 +00:00
|
|
|
t.Skip("benchdir not set, skipping TestArchiverDedup")
|
2015-02-17 22:40:37 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
var cnt struct {
|
|
|
|
before, after, after2 struct {
|
|
|
|
packs, dataBlobs, treeBlobs uint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 21:39:44 +00:00
|
|
|
// archive a few files
|
2017-10-02 13:06:39 +00:00
|
|
|
sn := archiver.TestSnapshot(t, repo, rtest.BenchArchiveDirectory, nil)
|
2015-03-09 21:58:17 +00:00
|
|
|
t.Logf("archived snapshot %v", sn.ID().Str())
|
2015-02-17 21:39:44 +00:00
|
|
|
|
|
|
|
// get archive stats
|
2018-01-21 16:25:36 +00:00
|
|
|
cnt.before.packs = countPacks(t, repo, restic.DataFile)
|
2016-08-31 20:39:36 +00:00
|
|
|
cnt.before.dataBlobs = repo.Index().Count(restic.DataBlob)
|
|
|
|
cnt.before.treeBlobs = repo.Index().Count(restic.TreeBlob)
|
2015-04-26 15:44:38 +00:00
|
|
|
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
|
|
|
cnt.before.packs, cnt.before.dataBlobs, cnt.before.treeBlobs)
|
2015-02-17 21:39:44 +00:00
|
|
|
|
2015-03-09 21:58:17 +00:00
|
|
|
// archive the same files again, without parent snapshot
|
2017-10-02 13:06:39 +00:00
|
|
|
sn2 := archiver.TestSnapshot(t, repo, rtest.BenchArchiveDirectory, nil)
|
2015-03-09 21:58:17 +00:00
|
|
|
t.Logf("archived snapshot %v", sn2.ID().Str())
|
2015-02-17 21:39:44 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// get archive stats again
|
2018-01-21 16:25:36 +00:00
|
|
|
cnt.after.packs = countPacks(t, repo, restic.DataFile)
|
2016-08-31 20:39:36 +00:00
|
|
|
cnt.after.dataBlobs = repo.Index().Count(restic.DataBlob)
|
|
|
|
cnt.after.treeBlobs = repo.Index().Count(restic.TreeBlob)
|
2015-04-26 15:44:38 +00:00
|
|
|
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
|
|
|
cnt.after.packs, cnt.after.dataBlobs, cnt.after.treeBlobs)
|
|
|
|
|
2015-05-04 20:11:53 +00:00
|
|
|
// if there are more data blobs, something is wrong
|
2015-04-26 15:44:38 +00:00
|
|
|
if cnt.after.dataBlobs > cnt.before.dataBlobs {
|
|
|
|
t.Fatalf("TestArchiverDedup: too many data blobs in repository: before %d, after %d",
|
|
|
|
cnt.before.dataBlobs, cnt.after.dataBlobs)
|
|
|
|
}
|
2015-03-09 21:58:17 +00:00
|
|
|
|
|
|
|
// archive the same files again, with a parent snapshot
|
2017-10-02 13:06:39 +00:00
|
|
|
sn3 := archiver.TestSnapshot(t, repo, rtest.BenchArchiveDirectory, sn2.ID())
|
2015-03-09 21:58:17 +00:00
|
|
|
t.Logf("archived snapshot %v, parent %v", sn3.ID().Str(), sn2.ID().Str())
|
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
// get archive stats again
|
2018-01-21 16:25:36 +00:00
|
|
|
cnt.after2.packs = countPacks(t, repo, restic.DataFile)
|
2016-08-31 20:39:36 +00:00
|
|
|
cnt.after2.dataBlobs = repo.Index().Count(restic.DataBlob)
|
|
|
|
cnt.after2.treeBlobs = repo.Index().Count(restic.TreeBlob)
|
2015-04-26 15:44:38 +00:00
|
|
|
t.Logf("packs %v, data blobs %v, tree blobs %v",
|
|
|
|
cnt.after2.packs, cnt.after2.dataBlobs, cnt.after2.treeBlobs)
|
|
|
|
|
2015-05-04 20:11:53 +00:00
|
|
|
// if there are more data blobs, something is wrong
|
2015-04-26 15:44:38 +00:00
|
|
|
if cnt.after2.dataBlobs > cnt.before.dataBlobs {
|
|
|
|
t.Fatalf("TestArchiverDedup: too many data blobs in repository: before %d, after %d",
|
|
|
|
cnt.before.dataBlobs, cnt.after2.dataBlobs)
|
2015-02-18 21:46:09 +00:00
|
|
|
}
|
2015-02-17 21:39:44 +00:00
|
|
|
}
|
2015-02-21 23:09:57 +00:00
|
|
|
|
2015-04-26 15:44:38 +00:00
|
|
|
func TestArchiveDedup(t *testing.T) {
|
|
|
|
archiveWithDedup(t)
|
|
|
|
}
|
|
|
|
|
2017-03-07 10:17:15 +00:00
|
|
|
func TestArchiveEmptySnapshot(t *testing.T) {
|
|
|
|
repo, cleanup := repository.TestRepository(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
arch := archiver.New(repo)
|
|
|
|
|
2017-09-02 17:28:09 +00:00
|
|
|
sn, id, err := arch.Snapshot(context.TODO(), nil, []string{"file-does-not-exist-123123213123", "file2-does-not-exist-too-123123123"}, nil, "localhost", nil, time.Now())
|
2017-03-07 10:17:15 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("expected error for empty snapshot, got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !id.IsNull() {
|
|
|
|
t.Errorf("expected null ID for empty snapshot, got %v", id.Str())
|
|
|
|
}
|
|
|
|
|
|
|
|
if sn != nil {
|
|
|
|
t.Errorf("expected null snapshot for empty snapshot, got %v", sn)
|
|
|
|
}
|
|
|
|
}
|
2017-09-04 19:39:21 +00:00
|
|
|
|
|
|
|
func TestArchiveNameCollision(t *testing.T) {
|
|
|
|
repo, cleanup := repository.TestRepository(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
dir, cleanup := rtest.TempDir(t)
|
2017-09-04 19:39:21 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
root := filepath.Join(dir, "root")
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, os.MkdirAll(root, 0755))
|
2017-09-04 19:39:21 +00:00
|
|
|
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, ioutil.WriteFile(filepath.Join(dir, "testfile"), []byte("testfile1"), 0644))
|
|
|
|
rtest.OK(t, ioutil.WriteFile(filepath.Join(dir, "root", "testfile"), []byte("testfile2"), 0644))
|
2017-09-04 19:39:21 +00:00
|
|
|
|
2017-12-15 23:01:28 +00:00
|
|
|
defer fs.TestChdir(t, root)()
|
2017-09-04 19:39:21 +00:00
|
|
|
|
|
|
|
arch := archiver.New(repo)
|
|
|
|
|
2017-09-09 12:58:07 +00:00
|
|
|
sn, id, err := arch.Snapshot(context.TODO(), nil, []string{"testfile", filepath.Join("..", "testfile")}, nil, "localhost", nil, time.Now())
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2017-09-04 19:39:21 +00:00
|
|
|
|
|
|
|
t.Logf("snapshot archived as %v", id)
|
|
|
|
|
|
|
|
tree, err := repo.LoadTree(context.TODO(), *sn.Tree)
|
2017-10-02 13:06:39 +00:00
|
|
|
rtest.OK(t, err)
|
2017-09-04 19:39:21 +00:00
|
|
|
|
|
|
|
if len(tree.Nodes) != 2 {
|
|
|
|
t.Fatalf("tree has %d nodes, wanted 2: %v", len(tree.Nodes), tree.Nodes)
|
|
|
|
}
|
|
|
|
}
|