2
2
mirror of https://github.com/octoleo/restic.git synced 2024-12-22 19:08:55 +00:00

index: Add tests for error conditions

This commit is contained in:
Alexander Neumann 2018-10-28 13:16:36 +01:00
parent c3de301fc8
commit f7c0893f76

View File

@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/restic/restic/internal/checker" "github.com/restic/restic/internal/checker"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/test" "github.com/restic/restic/internal/test"
@ -48,7 +49,7 @@ func TestIndexNew(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0) repo, cleanup := createFilledRepo(t, 3, 0)
defer cleanup() defer cleanup()
idx, _, err := New(context.TODO(), repo, restic.NewIDSet(), nil) idx, invalid, err := New(context.TODO(), repo, restic.NewIDSet(), nil)
if err != nil { if err != nil {
t.Fatalf("New() returned error %v", err) t.Fatalf("New() returned error %v", err)
} }
@ -57,9 +58,59 @@ func TestIndexNew(t *testing.T) {
t.Fatalf("New() returned nil index") t.Fatalf("New() returned nil index")
} }
if len(invalid) > 0 {
t.Fatalf("New() returned invalid files: %v", invalid)
}
validateIndex(t, repo, idx) validateIndex(t, repo, idx)
} }
type ListErrorRepo struct {
restic.Repository
Max int
}
// List returns an error after repo.Max files
func (repo ListErrorRepo) List(ctx context.Context, t restic.FileType, fn func(restic.ID, int64) error) error {
if repo.Max == 0 {
return errors.New("test error, max is zero")
}
max := repo.Max
return repo.Repository.List(ctx, t, func(id restic.ID, size int64) error {
if max == 0 {
return errors.New("test error, max reached zero")
}
max--
return fn(id, size)
})
}
func TestIndexNewErrors(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0)
defer cleanup()
for _, max := range []int{0, 3, 5} {
errRepo := ListErrorRepo{
Repository: repo,
Max: 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 TestIndexLoad(t *testing.T) { func TestIndexLoad(t *testing.T) {
repo, cleanup := createFilledRepo(t, 3, 0) repo, cleanup := createFilledRepo(t, 3, 0)
defer cleanup() defer cleanup()