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.
This commit is contained in:
Keepers 2022-07-28 11:03:30 -06:00 committed by GitHub
parent 99a210b6da
commit 4862d5b800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 1 deletions

View File

@ -329,8 +329,11 @@ func detailsExchangeCmd(cmd *cobra.Command, args []string) error {
} }
ds := sel.Reduce(d) 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 return nil
} }

View File

@ -98,6 +98,10 @@ func (op *RestoreOperation) Run(ctx context.Context) error {
// format the details and retrieve the items from kopia // format the details and retrieve the items from kopia
fds := er.Reduce(d) 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 // todo: use path pkg for this
fdsPaths := fds.Paths() fdsPaths := fds.Paths()
paths := make([][]string, len(fdsPaths)) paths := make([][]string, len(fdsPaths))

View File

@ -186,3 +186,58 @@ func (suite *RestoreOpIntegrationSuite) TestRestore_Run() {
assert.Zero(t, ro.Results.WriteErrors, "errors while writing restore data") 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") 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")
}