mirror of
https://github.com/octoleo/restic.git
synced 2024-12-23 11:28:54 +00:00
Add more index tests
This commit is contained in:
parent
6808523d34
commit
240b8f273a
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user