Add more index tests
This commit is contained in:
parent
6808523d34
commit
240b8f273a
2 changed files with 79 additions and 9 deletions
|
@ -18,12 +18,12 @@ type Pack struct {
|
||||||
|
|
||||||
// Index contains information about blobs and packs stored in a repo.
|
// Index contains information about blobs and packs stored in a repo.
|
||||||
type Index struct {
|
type Index struct {
|
||||||
Packs map[backend.ID]*Pack
|
Packs map[backend.ID]Pack
|
||||||
}
|
}
|
||||||
|
|
||||||
func newIndex() *Index {
|
func newIndex() *Index {
|
||||||
return &Index{
|
return &Index{
|
||||||
Packs: make(map[backend.ID]*Pack),
|
Packs: make(map[backend.ID]Pack),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ func New(repo *repository.Repository) (*Index, error) {
|
||||||
if _, ok := idx.Packs[packID]; ok {
|
if _, ok := idx.Packs[packID]; ok {
|
||||||
return nil, fmt.Errorf("pack %v processed twice", packID.Str())
|
return nil, fmt.Errorf("pack %v processed twice", packID.Str())
|
||||||
}
|
}
|
||||||
p := &Pack{Entries: j.Entries}
|
p := Pack{Entries: j.Entries}
|
||||||
idx.Packs[packID] = p
|
idx.Packs[packID] = p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ type indexJSON struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadIndexJSON(repo *repository.Repository, id backend.ID) (*indexJSON, error) {
|
func loadIndexJSON(repo *repository.Repository, id backend.ID) (*indexJSON, error) {
|
||||||
fmt.Printf("process index %v\n", id.Str())
|
debug.Log("index.loadIndexJSON", "process index %v\n", id.Str())
|
||||||
|
|
||||||
var idx indexJSON
|
var idx indexJSON
|
||||||
err := repo.LoadJSONUnpacked(backend.Index, id, &idx)
|
err := repo.LoadJSONUnpacked(backend.Index, id, &idx)
|
||||||
|
@ -116,12 +116,13 @@ func Load(repo *repository.Repository) (*Index, error) {
|
||||||
for _, jpack := range idx.Packs {
|
for _, jpack := range idx.Packs {
|
||||||
P := Pack{}
|
P := Pack{}
|
||||||
for _, blob := range jpack.Blobs {
|
for _, blob := range jpack.Blobs {
|
||||||
P.Entries = append(P.Entries, pack.Blob{
|
entry := pack.Blob{
|
||||||
ID: blob.ID,
|
ID: blob.ID,
|
||||||
Type: blob.Type,
|
Type: blob.Type,
|
||||||
Offset: blob.Offset,
|
Offset: blob.Offset,
|
||||||
Length: blob.Length,
|
Length: blob.Length,
|
||||||
})
|
}
|
||||||
|
P.Entries = append(P.Entries, entry)
|
||||||
}
|
}
|
||||||
res[jpack.ID] = P
|
res[jpack.ID] = P
|
||||||
}
|
}
|
||||||
|
@ -139,7 +140,7 @@ func Load(repo *repository.Repository) (*Index, error) {
|
||||||
idx := newIndex()
|
idx := newIndex()
|
||||||
for _, packs := range results {
|
for _, packs := range results {
|
||||||
for id, pack := range packs {
|
for id, pack := range packs {
|
||||||
idx.Packs[id] = &pack
|
idx.Packs[id] = pack
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package index
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"restic"
|
"restic"
|
||||||
|
"restic/backend"
|
||||||
"restic/backend/local"
|
"restic/backend/local"
|
||||||
"restic/repository"
|
"restic/repository"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -24,6 +25,14 @@ func createFilledRepo(t testing.TB, snapshots int) (*repository.Repository, func
|
||||||
return repo, cleanup
|
return repo, cleanup
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateIndex(t testing.TB, repo *repository.Repository, idx *Index) {
|
||||||
|
for id := range repo.List(backend.Data, nil) {
|
||||||
|
if _, ok := idx.Packs[id]; !ok {
|
||||||
|
t.Errorf("pack %v missing from index", id.Str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestIndexNew(t *testing.T) {
|
func TestIndexNew(t *testing.T) {
|
||||||
repo, cleanup := createFilledRepo(t, 3)
|
repo, cleanup := createFilledRepo(t, 3)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
@ -36,20 +45,80 @@ func TestIndexNew(t *testing.T) {
|
||||||
if idx == nil {
|
if idx == nil {
|
||||||
t.Fatalf("New() returned nil index")
|
t.Fatalf("New() returned nil index")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
validateIndex(t, repo, idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIndexLoad(t *testing.T) {
|
func TestIndexLoad(t *testing.T) {
|
||||||
repo, cleanup := createFilledRepo(t, 3)
|
repo, cleanup := createFilledRepo(t, 3)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
idx, err := Load(repo)
|
loadIdx, err := Load(repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Load() returned error %v", err)
|
t.Fatalf("Load() returned error %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if idx == nil {
|
if loadIdx == nil {
|
||||||
t.Fatalf("Load() returned nil index")
|
t.Fatalf("Load() returned nil index")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
validateIndex(t, repo, loadIdx)
|
||||||
|
|
||||||
|
newIdx, err := New(repo)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New() returned error %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(loadIdx.Packs) != len(newIdx.Packs) {
|
||||||
|
t.Errorf("number of packs does not match: want %v, got %v",
|
||||||
|
len(loadIdx.Packs), len(newIdx.Packs))
|
||||||
|
}
|
||||||
|
|
||||||
|
validateIndex(t, repo, newIdx)
|
||||||
|
|
||||||
|
for packID, packNew := range newIdx.Packs {
|
||||||
|
packLoad, ok := loadIdx.Packs[packID]
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("loaded index does not list pack %v", packID.Str())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(packNew.Entries) != len(packLoad.Entries) {
|
||||||
|
t.Errorf(" number of entries in pack %v does not match: %d != %d\n %v\n %v",
|
||||||
|
packID.Str(), len(packNew.Entries), len(packLoad.Entries),
|
||||||
|
packNew.Entries, packLoad.Entries)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entryNew := range packNew.Entries {
|
||||||
|
found := false
|
||||||
|
for _, entryLoad := range packLoad.Entries {
|
||||||
|
if !entryLoad.ID.Equal(entryNew.ID) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if entryLoad.Type != entryNew.Type {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if entryLoad.Offset != entryNew.Offset {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if entryLoad.Length != entryNew.Length {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
t.Errorf("blob not found in loaded index: %v", entryNew)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func openRepo(t testing.TB, dir, password string) *repository.Repository {
|
func openRepo(t testing.TB, dir, password string) *repository.Repository {
|
||||||
|
|
Loading…
Reference in a new issue