2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-25 06:07:44 +00:00

test snapshot filtering with snapshot:path syntax

This commit is contained in:
Michael Eischer 2023-07-16 14:57:12 +02:00
parent 233b841ad9
commit c64d81063e
2 changed files with 51 additions and 1 deletions

View File

@ -123,6 +123,8 @@ func (f *SnapshotFilter) FindLatest(ctx context.Context, be Lister, loader Loade
type SnapshotFindCb func(string, *Snapshot, error) error
var ErrInvalidSnapshotSyntax = errors.New("snapshot:path syntax not allowed")
// FindAll yields Snapshots, either given explicitly by `snapshotIDs` or filtered from the list of all snapshots.
func (f *SnapshotFilter) FindAll(ctx context.Context, be Lister, loader LoaderUnpacked, snapshotIDs []string, fn SnapshotFindCb) error {
if len(snapshotIDs) != 0 {
@ -148,11 +150,13 @@ func (f *SnapshotFilter) FindAll(ctx context.Context, be Lister, loader LoaderUn
if sn != nil {
ids.Insert(*sn.ID())
}
} else if strings.HasPrefix(s, "latest:") {
err = ErrInvalidSnapshotSyntax
} else {
var subpath string
sn, subpath, err = FindSnapshot(ctx, be, loader, s)
if err == nil && subpath != "" {
err = errors.New("snapshot:path syntax not allowed")
err = ErrInvalidSnapshotSyntax
} else if err == nil {
if ids.Has(*sn.ID()) {
continue

View File

@ -6,6 +6,7 @@ import (
"github.com/restic/restic/internal/repository"
"github.com/restic/restic/internal/restic"
"github.com/restic/restic/internal/test"
)
func TestFindLatestSnapshot(t *testing.T) {
@ -43,3 +44,48 @@ func TestFindLatestSnapshotWithMaxTimestamp(t *testing.T) {
t.Errorf("FindLatest returned wrong snapshot ID: %v", *sn.ID())
}
}
func TestFindLatestWithSubpath(t *testing.T) {
repo := repository.TestRepository(t)
restic.TestCreateSnapshot(t, repo, parseTimeUTC("2015-05-05 05:05:05"), 1, 0)
desiredSnapshot := restic.TestCreateSnapshot(t, repo, parseTimeUTC("2017-07-07 07:07:07"), 1, 0)
for _, exp := range []struct {
query string
subpath string
}{
{"latest", ""},
{"latest:subpath", "subpath"},
{desiredSnapshot.ID().Str(), ""},
{desiredSnapshot.ID().Str() + ":subpath", "subpath"},
{desiredSnapshot.ID().String(), ""},
{desiredSnapshot.ID().String() + ":subpath", "subpath"},
} {
t.Run("", func(t *testing.T) {
sn, subpath, err := (&restic.SnapshotFilter{}).FindLatest(context.TODO(), repo.Backend(), repo, exp.query)
if err != nil {
t.Fatalf("FindLatest returned error: %v", err)
}
test.Assert(t, *sn.ID() == *desiredSnapshot.ID(), "FindLatest returned wrong snapshot ID: %v", *sn.ID())
test.Assert(t, subpath == exp.subpath, "FindLatest returned wrong path in snapshot: %v", subpath)
})
}
}
func TestFindAllSubpathError(t *testing.T) {
repo := repository.TestRepository(t)
desiredSnapshot := restic.TestCreateSnapshot(t, repo, parseTimeUTC("2017-07-07 07:07:07"), 1, 0)
count := 0
test.OK(t, (&restic.SnapshotFilter{}).FindAll(context.TODO(), repo.Backend(), repo,
[]string{"latest:subpath", desiredSnapshot.ID().Str() + ":subpath"},
func(id string, sn *restic.Snapshot, err error) error {
if err == restic.ErrInvalidSnapshotSyntax {
count++
return nil
}
return err
}))
test.Assert(t, count == 2, "unexpected number of subpath errors: %v, wanted %v", count, 2)
}