From 4862d5b80078e8f03d298165dd73ad2007aaca26 Mon Sep 17 00:00:00 2001 From: Keepers <104464746+ryanfkeepers@users.noreply.github.com> Date: Thu, 28 Jul 2022 11:03:30 -0600 Subject: [PATCH] return errors if selectors reduce all entries (#427) * return errors if selectors reduce all entries During a `backup details` or `restore` command, if the selectors produce 0 results, return an error rather than running a no-op process. --- src/cli/backup/exchange.go | 5 ++- src/internal/operations/restore.go | 4 ++ src/internal/operations/restore_test.go | 55 +++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/cli/backup/exchange.go b/src/cli/backup/exchange.go index 5c0bea99a..49b7a6714 100644 --- a/src/cli/backup/exchange.go +++ b/src/cli/backup/exchange.go @@ -329,8 +329,11 @@ func detailsExchangeCmd(cmd *cobra.Command, args []string) error { } ds := sel.Reduce(d) - print.Entries(ds.Entries) + if len(ds.Entries) == 0 { + return errors.New("nothing to display: no items in the backup match the provided selectors") + } + print.Entries(ds.Entries) return nil } diff --git a/src/internal/operations/restore.go b/src/internal/operations/restore.go index 9ab5e8c56..1aade15ce 100644 --- a/src/internal/operations/restore.go +++ b/src/internal/operations/restore.go @@ -98,6 +98,10 @@ func (op *RestoreOperation) Run(ctx context.Context) error { // format the details and retrieve the items from kopia fds := er.Reduce(d) + if len(fds.Entries) == 0 { + return errors.New("nothing to restore: no items in the backup match the provided selectors") + } + // todo: use path pkg for this fdsPaths := fds.Paths() paths := make([][]string, len(fdsPaths)) diff --git a/src/internal/operations/restore_test.go b/src/internal/operations/restore_test.go index aa01a98b1..29a9d87ef 100644 --- a/src/internal/operations/restore_test.go +++ b/src/internal/operations/restore_test.go @@ -186,3 +186,58 @@ func (suite *RestoreOpIntegrationSuite) TestRestore_Run() { assert.Zero(t, ro.Results.WriteErrors, "errors while writing restore data") assert.Equal(t, bo.Results.ItemsWritten, ro.Results.ItemsWritten, "backup and restore wrote the same num of items") } + +func (suite *RestoreOpIntegrationSuite) TestRestore_Run_ErrorNoResults() { + t := suite.T() + ctx := context.Background() + + m365User := "lidiah@8qzvrj.onmicrosoft.com" + acct, err := ctesting.NewM365Account() + require.NoError(t, err) + + // need to initialize the repository before we can test connecting to it. + st, err := ctesting.NewPrefixedS3Storage(t) + require.NoError(t, err) + + k := kopia.NewConn(st) + require.NoError(t, k.Initialize(ctx)) + defer k.Close(ctx) + + w, err := kopia.NewWrapper(k) + require.NoError(t, err) + defer w.Close(ctx) + + ms, err := kopia.NewModelStore(k) + require.NoError(t, err) + defer ms.Close(ctx) + + sw := store.NewKopiaStore(ms) + + bsel := selectors.NewExchangeBackup() + bsel.Include(bsel.Users([]string{m365User})) + + bo, err := NewBackupOperation( + ctx, + Options{}, + w, + sw, + acct, + bsel.Selector) + require.NoError(t, err) + require.NoError(t, bo.Run(ctx)) + require.NotEmpty(t, bo.Results.BackupID) + + rsel := selectors.NewExchangeRestore() + rsel.Include(rsel.Users(selectors.None())) + + ro, err := NewRestoreOperation( + ctx, + Options{}, + w, + sw, + acct, + bo.Results.BackupID, + rsel.Selector) + require.NoError(t, err) + require.Error(t, ro.Run(ctx), "restoreOp.Run() should have 0 results") +}