From 240b8f273a611378bd1376f3dc074b2561636b24 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 7 Aug 2016 18:45:25 +0200 Subject: [PATCH] Add more index tests --- src/restic/index/index.go | 15 +++---- src/restic/index/index_test.go | 73 +++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/src/restic/index/index.go b/src/restic/index/index.go index 9b7eed1da..9a4937011 100644 --- a/src/restic/index/index.go +++ b/src/restic/index/index.go @@ -18,12 +18,12 @@ type Pack struct { // Index contains information about blobs and packs stored in a repo. type Index struct { - Packs map[backend.ID]*Pack + Packs map[backend.ID]Pack } func newIndex() *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 { return nil, fmt.Errorf("pack %v processed twice", packID.Str()) } - p := &Pack{Entries: j.Entries} + p := Pack{Entries: j.Entries} idx.Packs[packID] = p } @@ -78,7 +78,7 @@ type indexJSON struct { } 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 err := repo.LoadJSONUnpacked(backend.Index, id, &idx) @@ -116,12 +116,13 @@ func Load(repo *repository.Repository) (*Index, error) { for _, jpack := range idx.Packs { P := Pack{} for _, blob := range jpack.Blobs { - P.Entries = append(P.Entries, pack.Blob{ + entry := pack.Blob{ ID: blob.ID, Type: blob.Type, Offset: blob.Offset, Length: blob.Length, - }) + } + P.Entries = append(P.Entries, entry) } res[jpack.ID] = P } @@ -139,7 +140,7 @@ func Load(repo *repository.Repository) (*Index, error) { idx := newIndex() for _, packs := range results { for id, pack := range packs { - idx.Packs[id] = &pack + idx.Packs[id] = pack } } diff --git a/src/restic/index/index_test.go b/src/restic/index/index_test.go index ad15a6a25..335b93051 100644 --- a/src/restic/index/index_test.go +++ b/src/restic/index/index_test.go @@ -2,6 +2,7 @@ package index import ( "restic" + "restic/backend" "restic/backend/local" "restic/repository" "testing" @@ -24,6 +25,14 @@ func createFilledRepo(t testing.TB, snapshots int) (*repository.Repository, func 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) { repo, cleanup := createFilledRepo(t, 3) defer cleanup() @@ -36,20 +45,80 @@ func TestIndexNew(t *testing.T) { if idx == nil { t.Fatalf("New() returned nil index") } + + validateIndex(t, repo, idx) } func TestIndexLoad(t *testing.T) { repo, cleanup := createFilledRepo(t, 3) defer cleanup() - idx, err := Load(repo) + loadIdx, err := Load(repo) if err != nil { t.Fatalf("Load() returned error %v", err) } - if idx == nil { + if loadIdx == nil { 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 {