1
0
Fork 0
mirror of https://github.com/restic/restic.git synced 2024-12-22 15:57:07 +00:00

index.New: Test various error conditions

This commit is contained in:
Alexander Neumann 2018-10-28 14:02:31 +01:00
parent 3ae2a79bdf
commit 0b600d6cef

View file

@ -2,6 +2,7 @@ package index
import ( import (
"context" "context"
"sync"
"testing" "testing"
"time" "time"
@ -65,18 +66,21 @@ func TestIndexNew(t *testing.T) {
validateIndex(t, repo, idx) validateIndex(t, repo, idx)
} }
type ListErrorRepo struct { type ErrorRepo struct {
restic.Repository restic.Repository
Max int MaxListFiles int
MaxPacks int
MaxPacksMutex sync.Mutex
} }
// List returns an error after repo.Max files // List returns an error after repo.MaxListFiles files.
func (repo ListErrorRepo) List(ctx context.Context, t restic.FileType, fn func(restic.ID, int64) error) error { func (repo *ErrorRepo) List(ctx context.Context, t restic.FileType, fn func(restic.ID, int64) error) error {
if repo.Max == 0 { if repo.MaxListFiles == 0 {
return errors.New("test error, max is zero") return errors.New("test error, max is zero")
} }
max := repo.Max max := repo.MaxListFiles
return repo.Repository.List(ctx, t, func(id restic.ID, size int64) error { return repo.Repository.List(ctx, t, func(id restic.ID, size int64) error {
if max == 0 { if max == 0 {
return errors.New("test error, max reached zero") return errors.New("test error, max reached zero")
@ -87,14 +91,54 @@ func (repo ListErrorRepo) List(ctx context.Context, t restic.FileType, fn func(r
}) })
} }
func TestIndexNewErrors(t *testing.T) { // ListPack returns an error after repo.MaxPacks files.
func (repo *ErrorRepo) ListPack(ctx context.Context, id restic.ID, size int64) ([]restic.Blob, int64, error) {
repo.MaxPacksMutex.Lock()
max := repo.MaxPacks
if max > 0 {
repo.MaxPacks--
}
repo.MaxPacksMutex.Unlock()
if max == 0 {
return nil, 0, errors.New("test list pack error")
}
return repo.Repository.ListPack(ctx, id, size)
}
func TestIndexNewListErrors(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0) repo, cleanup := createFilledRepo(t, 3, 0)
defer cleanup() defer cleanup()
for _, max := range []int{0, 3, 5} { for _, max := range []int{0, 3, 5} {
errRepo := ListErrorRepo{ errRepo := &ErrorRepo{
Repository: repo, Repository: repo,
Max: max, MaxListFiles: max,
}
idx, invalid, err := New(context.TODO(), errRepo, restic.NewIDSet(), nil)
if err == nil {
t.Errorf("expected error not found, got nil")
}
if idx != nil {
t.Errorf("expected nil index, got %v", idx)
}
if len(invalid) != 0 {
t.Errorf("expected empty invalid list, got %v", invalid)
}
}
}
func TestIndexNewPackErrors(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0)
defer cleanup()
for _, max := range []int{0, 3, 5} {
errRepo := &ErrorRepo{
Repository: repo,
MaxPacks: max,
} }
idx, invalid, err := New(context.TODO(), errRepo, restic.NewIDSet(), nil) idx, invalid, err := New(context.TODO(), errRepo, restic.NewIDSet(), nil)
if err == nil { if err == nil {
@ -237,7 +281,7 @@ func BenchmarkIndexSave(b *testing.B) {
} }
func TestIndexDuplicateBlobs(t *testing.T) { func TestIndexDuplicateBlobs(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0.01) repo, cleanup := createFilledRepo(t, 3, 0.05)
defer cleanup() defer cleanup()
idx, _, err := New(context.TODO(), repo, restic.NewIDSet(), nil) idx, _, err := New(context.TODO(), repo, restic.NewIDSet(), nil)