2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-31 16:10:49 +00:00
restic/src/restic/repository/index_test.go

367 lines
8.3 KiB
Go
Raw Normal View History

package repository_test
2015-04-26 15:10:31 +00:00
import (
"bytes"
2016-08-31 18:29:54 +00:00
"restic"
2015-04-26 15:10:31 +00:00
"testing"
"restic/backend"
"restic/pack"
"restic/repository"
. "restic/test"
2015-04-26 15:10:31 +00:00
)
func TestIndexSerialize(t *testing.T) {
type testEntry struct {
2016-08-31 18:29:54 +00:00
id restic.ID
pack restic.ID
2015-04-26 15:10:31 +00:00
tpe pack.BlobType
offset, length uint
}
tests := []testEntry{}
idx := repository.NewIndex()
2015-04-26 15:10:31 +00:00
// create 50 packs with 20 blobs each
for i := 0; i < 50; i++ {
2016-08-07 10:36:24 +00:00
packID := backend.RandomID()
2015-04-26 15:10:31 +00:00
pos := uint(0)
for j := 0; j < 20; j++ {
2016-08-07 10:36:24 +00:00
id := backend.RandomID()
2015-04-26 15:10:31 +00:00
length := uint(i*100 + j)
idx.Store(repository.PackedBlob{
Type: pack.Data,
ID: id,
PackID: packID,
Offset: pos,
Length: length,
})
2015-04-26 15:10:31 +00:00
tests = append(tests, testEntry{
id: id,
pack: packID,
tpe: pack.Data,
offset: pos,
length: length,
})
pos += length
}
}
wr := bytes.NewBuffer(nil)
err := idx.Encode(wr)
OK(t, err)
idx2, err := repository.DecodeIndex(wr)
2015-04-26 15:10:31 +00:00
OK(t, err)
Assert(t, idx2 != nil,
"nil returned for decoded index")
wr2 := bytes.NewBuffer(nil)
err = idx2.Encode(wr2)
OK(t, err)
for _, testBlob := range tests {
list, err := idx.Lookup(testBlob.id, testBlob.tpe)
2015-04-26 15:10:31 +00:00
OK(t, err)
if len(list) != 1 {
t.Errorf("expected one result for blob %v, got %v: %v", testBlob.id.Str(), len(list), list)
}
result := list[0]
Equals(t, testBlob.pack, result.PackID)
Equals(t, testBlob.tpe, result.Type)
Equals(t, testBlob.offset, result.Offset)
Equals(t, testBlob.length, result.Length)
2015-04-26 15:10:31 +00:00
list2, err := idx2.Lookup(testBlob.id, testBlob.tpe)
2015-04-26 15:10:31 +00:00
OK(t, err)
if len(list2) != 1 {
t.Errorf("expected one result for blob %v, got %v: %v", testBlob.id.Str(), len(list2), list2)
}
result2 := list2[0]
Equals(t, testBlob.pack, result2.PackID)
Equals(t, testBlob.tpe, result2.Type)
Equals(t, testBlob.offset, result2.Offset)
Equals(t, testBlob.length, result2.Length)
2015-04-26 15:10:31 +00:00
}
2015-10-12 20:34:12 +00:00
// add more blobs to idx
2015-04-26 15:10:31 +00:00
newtests := []testEntry{}
for i := 0; i < 10; i++ {
2016-08-07 10:36:24 +00:00
packID := backend.RandomID()
2015-04-26 15:10:31 +00:00
pos := uint(0)
for j := 0; j < 10; j++ {
2016-08-07 10:36:24 +00:00
id := backend.RandomID()
2015-04-26 15:10:31 +00:00
length := uint(i*100 + j)
idx.Store(repository.PackedBlob{
Type: pack.Data,
ID: id,
PackID: packID,
Offset: pos,
Length: length,
})
2015-04-26 15:10:31 +00:00
newtests = append(newtests, testEntry{
id: id,
pack: packID,
tpe: pack.Data,
offset: pos,
length: length,
})
pos += length
}
}
2015-10-12 20:34:12 +00:00
// serialize idx, unserialize to idx3
2015-04-26 15:10:31 +00:00
wr3 := bytes.NewBuffer(nil)
2015-10-12 20:34:12 +00:00
err = idx.Finalize(wr3)
2015-04-26 15:10:31 +00:00
OK(t, err)
2015-10-12 20:34:12 +00:00
Assert(t, idx.Final(),
"index not final after encoding")
2016-08-07 10:36:24 +00:00
id := backend.RandomID()
2015-11-18 19:33:20 +00:00
OK(t, idx.SetID(id))
2015-11-02 17:51:45 +00:00
id2, err := idx.ID()
Assert(t, id2.Equal(id),
"wrong ID returned: want %v, got %v", id, id2)
idx3, err := repository.DecodeIndex(wr3)
2015-04-26 15:10:31 +00:00
OK(t, err)
Assert(t, idx3 != nil,
"nil returned for decoded index")
2015-10-12 20:34:12 +00:00
Assert(t, idx3.Final(),
"decoded index is not final")
2015-04-26 15:10:31 +00:00
// all new blobs must be in the index
for _, testBlob := range newtests {
list, err := idx3.Lookup(testBlob.id, testBlob.tpe)
2015-04-26 15:10:31 +00:00
OK(t, err)
if len(list) != 1 {
t.Errorf("expected one result for blob %v, got %v: %v", testBlob.id.Str(), len(list), list)
}
blob := list[0]
Equals(t, testBlob.pack, blob.PackID)
Equals(t, testBlob.tpe, blob.Type)
Equals(t, testBlob.offset, blob.Offset)
Equals(t, testBlob.length, blob.Length)
2015-04-26 15:10:31 +00:00
}
}
func TestIndexSize(t *testing.T) {
idx := repository.NewIndex()
2015-04-26 15:10:31 +00:00
packs := 200
blobs := 100
for i := 0; i < packs; i++ {
2016-08-07 10:36:24 +00:00
packID := backend.RandomID()
2015-04-26 15:10:31 +00:00
pos := uint(0)
for j := 0; j < blobs; j++ {
2016-08-07 10:36:24 +00:00
id := backend.RandomID()
2015-04-26 15:10:31 +00:00
length := uint(i*100 + j)
idx.Store(repository.PackedBlob{
Type: pack.Data,
ID: id,
PackID: packID,
Offset: pos,
Length: length,
})
2015-04-26 15:10:31 +00:00
pos += length
}
}
wr := bytes.NewBuffer(nil)
err := idx.Encode(wr)
OK(t, err)
t.Logf("Index file size for %d blobs in %d packs is %d", blobs*packs, packs, wr.Len())
}
// example index serialization from doc/Design.md
var docExample = []byte(`
2015-07-25 22:40:00 +00:00
{
"supersedes": [
"ed54ae36197f4745ebc4b54d10e0f623eaaaedd03013eb7ae90df881b7781452"
],
"packs": [
{
"id": "73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c",
"blobs": [
{
"id": "3ec79977ef0cf5de7b08cd12b874cd0f62bbaf7f07f3497a5b1bbcc8cb39b1ce",
"type": "data",
"offset": 0,
"length": 25
},{
"id": "9ccb846e60d90d4eb915848add7aa7ea1e4bbabfc60e573db9f7bfb2789afbae",
"type": "tree",
"offset": 38,
"length": 100
},
{
"id": "d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66",
"type": "data",
"offset": 150,
"length": 123
}
]
}
]
}
`)
var docOldExample = []byte(`
2015-04-26 15:10:31 +00:00
[ {
"id": "73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c",
"blobs": [
2015-07-25 22:40:00 +00:00
{
"id": "3ec79977ef0cf5de7b08cd12b874cd0f62bbaf7f07f3497a5b1bbcc8cb39b1ce",
"type": "data",
"offset": 0,
"length": 25
},{
"id": "9ccb846e60d90d4eb915848add7aa7ea1e4bbabfc60e573db9f7bfb2789afbae",
"type": "tree",
"offset": 38,
"length": 100
},
{
"id": "d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66",
"type": "data",
"offset": 150,
"length": 123
}
2015-04-26 15:10:31 +00:00
]
} ]
`)
var exampleTests = []struct {
2016-08-31 18:29:54 +00:00
id, packID restic.ID
2015-04-26 15:10:31 +00:00
tpe pack.BlobType
offset, length uint
}{
{
ParseID("3ec79977ef0cf5de7b08cd12b874cd0f62bbaf7f07f3497a5b1bbcc8cb39b1ce"),
ParseID("73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c"),
pack.Data, 0, 25,
}, {
ParseID("9ccb846e60d90d4eb915848add7aa7ea1e4bbabfc60e573db9f7bfb2789afbae"),
ParseID("73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c"),
pack.Tree, 38, 100,
}, {
ParseID("d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66"),
ParseID("73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c"),
pack.Data, 150, 123,
},
}
2015-11-01 21:32:28 +00:00
var exampleLookupTest = struct {
2016-08-31 18:29:54 +00:00
packID restic.ID
blobs map[restic.ID]pack.BlobType
2015-11-01 21:32:28 +00:00
}{
ParseID("73d04e6125cf3c28a299cc2f3cca3b78ceac396e4fcf9575e34536b26782413c"),
2016-08-31 18:29:54 +00:00
map[restic.ID]pack.BlobType{
ParseID("3ec79977ef0cf5de7b08cd12b874cd0f62bbaf7f07f3497a5b1bbcc8cb39b1ce"): pack.Data,
ParseID("9ccb846e60d90d4eb915848add7aa7ea1e4bbabfc60e573db9f7bfb2789afbae"): pack.Tree,
ParseID("d3dc577b4ffd38cc4b32122cabf8655a0223ed22edfd93b353dc0c3f2b0fdf66"): pack.Data,
2015-11-01 21:32:28 +00:00
},
}
2015-04-26 15:10:31 +00:00
func TestIndexUnserialize(t *testing.T) {
2016-08-31 18:29:54 +00:00
oldIdx := restic.IDs{ParseID("ed54ae36197f4745ebc4b54d10e0f623eaaaedd03013eb7ae90df881b7781452")}
2015-07-25 22:40:00 +00:00
idx, err := repository.DecodeIndex(bytes.NewReader(docExample))
2015-04-26 15:10:31 +00:00
OK(t, err)
for _, test := range exampleTests {
list, err := idx.Lookup(test.id, test.tpe)
2015-04-26 15:10:31 +00:00
OK(t, err)
if len(list) != 1 {
t.Errorf("expected one result for blob %v, got %v: %v", test.id.Str(), len(list), list)
}
blob := list[0]
t.Logf("looking for blob %v/%v, got %v", test.tpe, test.id.Str(), blob)
Equals(t, test.packID, blob.PackID)
Equals(t, test.tpe, blob.Type)
Equals(t, test.offset, blob.Offset)
Equals(t, test.length, blob.Length)
2015-04-26 15:10:31 +00:00
}
2015-07-25 22:40:00 +00:00
Equals(t, oldIdx, idx.Supersedes())
2015-11-01 21:32:28 +00:00
blobs := idx.ListPack(exampleLookupTest.packID)
if len(blobs) != len(exampleLookupTest.blobs) {
t.Fatalf("expected %d blobs in pack, got %d", len(exampleLookupTest.blobs), len(blobs))
}
for _, blob := range blobs {
b, ok := exampleLookupTest.blobs[blob.ID]
if !ok {
2015-11-01 21:32:28 +00:00
t.Errorf("unexpected blob %v found", blob.ID.Str())
}
if blob.Type != b {
t.Errorf("unexpected type for blob %v: want %v, got %v", blob.ID.Str(), b, blob.Type)
}
2015-11-01 21:32:28 +00:00
}
2015-07-25 22:40:00 +00:00
}
func TestIndexUnserializeOld(t *testing.T) {
idx, err := repository.DecodeOldIndex(bytes.NewReader(docOldExample))
2015-07-25 22:40:00 +00:00
OK(t, err)
for _, test := range exampleTests {
list, err := idx.Lookup(test.id, test.tpe)
2015-07-25 22:40:00 +00:00
OK(t, err)
if len(list) != 1 {
t.Errorf("expected one result for blob %v, got %v: %v", test.id.Str(), len(list), list)
}
blob := list[0]
Equals(t, test.packID, blob.PackID)
Equals(t, test.tpe, blob.Type)
Equals(t, test.offset, blob.Offset)
Equals(t, test.length, blob.Length)
2015-07-25 22:40:00 +00:00
}
Equals(t, 0, len(idx.Supersedes()))
}
2015-10-25 14:28:01 +00:00
func TestIndexPacks(t *testing.T) {
idx := repository.NewIndex()
2016-08-31 18:29:54 +00:00
packs := restic.NewIDSet()
2015-10-25 14:28:01 +00:00
for i := 0; i < 20; i++ {
2016-08-07 10:36:24 +00:00
packID := backend.RandomID()
idx.Store(repository.PackedBlob{
Type: pack.Data,
2016-08-07 10:36:24 +00:00
ID: backend.RandomID(),
PackID: packID,
Offset: 0,
Length: 23,
})
2015-10-25 14:28:01 +00:00
packs.Insert(packID)
}
idxPacks := idx.Packs()
Assert(t, packs.Equals(idxPacks), "packs in index do not match packs added to index")
}