From f7c0893f76a99be35c8bf58aa1c9ab354773fe22 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 28 Oct 2018 13:16:36 +0100 Subject: [PATCH] index: Add tests for error conditions --- internal/index/index_test.go | 53 +++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/internal/index/index_test.go b/internal/index/index_test.go index 1459263ee..83d20069f 100644 --- a/internal/index/index_test.go +++ b/internal/index/index_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/restic/restic/internal/checker" + "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/test" @@ -48,7 +49,7 @@ func TestIndexNew(t *testing.T) { repo, cleanup := createFilledRepo(t, 3, 0) defer cleanup() - idx, _, err := New(context.TODO(), repo, restic.NewIDSet(), nil) + idx, invalid, err := New(context.TODO(), repo, restic.NewIDSet(), nil) if err != nil { t.Fatalf("New() returned error %v", err) } @@ -57,9 +58,59 @@ func TestIndexNew(t *testing.T) { t.Fatalf("New() returned nil index") } + if len(invalid) > 0 { + t.Fatalf("New() returned invalid files: %v", invalid) + } + 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) { repo, cleanup := createFilledRepo(t, 3, 0) defer cleanup()