From cdbe6cdf3ab5d26d0d688ddc9b44bf90a383c3fa Mon Sep 17 00:00:00 2001 From: Nain Date: Wed, 15 Mar 2023 12:39:54 -0400 Subject: [PATCH 1/7] Add "--repository" flag to the "prune" action part of ticket #564 --- borgmatic/actions/prune.py | 6 +++ borgmatic/commands/arguments.py | 4 ++ tests/unit/actions/test_prune.py | 72 +++++++++++++++++++++++++++++++- 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/borgmatic/actions/prune.py b/borgmatic/actions/prune.py index 2d214a1..ca098ce 100644 --- a/borgmatic/actions/prune.py +++ b/borgmatic/actions/prune.py @@ -1,6 +1,7 @@ import logging import borgmatic.borg.prune +import borgmatic.config.validate import borgmatic.hooks.command logger = logging.getLogger(__name__) @@ -23,6 +24,11 @@ def run_prune( ''' Run the "prune" action for the given repository. ''' + if prune_arguments.repository and not borgmatic.config.validate.repositories_match( + repository, prune_arguments.repository + ): + return + borgmatic.hooks.command.execute_hook( hooks.get('before_prune'), hooks.get('umask'), diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index 3f56b7c..a8e8aac 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -333,6 +333,10 @@ def make_parsers(): add_help=False, ) prune_group = prune_parser.add_argument_group('prune arguments') + prune_group.add_argument( + '--repository', + help='Path of specific existing repository to prune (must be already specified in a borgmatic configuration file)', + ) prune_group.add_argument( '--stats', dest='stats', diff --git a/tests/unit/actions/test_prune.py b/tests/unit/actions/test_prune.py index d34a9c8..21e8260 100644 --- a/tests/unit/actions/test_prune.py +++ b/tests/unit/actions/test_prune.py @@ -7,7 +7,77 @@ def test_run_prune_calls_hooks(): flexmock(module.logger).answer = lambda message: None flexmock(module.borgmatic.borg.prune).should_receive('prune_archives') flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2) - prune_arguments = flexmock(stats=flexmock(), list_archives=flexmock()) + prune_arguments = flexmock(repository=None, stats=flexmock(), list_archives=flexmock()) + global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) + + module.run_prune( + config_filename='test.yaml', + repository='repo', + storage={}, + retention={}, + hooks={}, + hook_context={}, + local_borg_version=None, + prune_arguments=prune_arguments, + global_arguments=global_arguments, + dry_run_label='', + local_path=None, + remote_path=None, + ) + + +def test_run_prune_runs_with_no_explicit_repository(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.borg.prune).should_receive('prune_archives') + prune_arguments = flexmock(repository=None, stats=flexmock(), list_archives=flexmock()) + global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) + + module.run_prune( + config_filename='test.yaml', + repository='repo', + storage={}, + retention={}, + hooks={}, + hook_context={}, + local_borg_version=None, + prune_arguments=prune_arguments, + global_arguments=global_arguments, + dry_run_label='', + local_path=None, + remote_path=None, + ) + + +def test_run_prune_runs_with_select_repository(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True) + flexmock(module.borgmatic.borg.prune).should_receive('prune_archives') + prune_arguments = flexmock(repository=flexmock(), stats=flexmock(), list_archives=flexmock()) + global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) + + module.run_prune( + config_filename='test.yaml', + repository='repo', + storage={}, + retention={}, + hooks={}, + hook_context={}, + local_borg_version=None, + prune_arguments=prune_arguments, + global_arguments=global_arguments, + dry_run_label='', + local_path=None, + remote_path=None, + ) + + +def test_run_prune_bails_if_repository_does_not_match(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return( + False + ) + flexmock(module.borgmatic.borg.prune).should_receive('prune_archives').never() + prune_arguments = flexmock(repository=flexmock(), stats=flexmock(), list_archives=flexmock()) global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) module.run_prune( From 7de9260b0d14a7b68aee31404b5b757d8460d0a9 Mon Sep 17 00:00:00 2001 From: Nain Date: Wed, 15 Mar 2023 14:59:12 -0400 Subject: [PATCH 2/7] Remove test now that --repository isn't expected to error As discussed #652#issuecomment-5579 --- tests/integration/commands/test_arguments.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/integration/commands/test_arguments.py b/tests/integration/commands/test_arguments.py index cb001c1..754564b 100644 --- a/tests/integration/commands/test_arguments.py +++ b/tests/integration/commands/test_arguments.py @@ -254,13 +254,6 @@ def test_parse_arguments_allows_init_and_create(): module.parse_arguments('--config', 'myconfig', 'init', '--encryption', 'repokey', 'create') -def test_parse_arguments_disallows_repository_unless_action_consumes_it(): - flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default']) - - with pytest.raises(SystemExit): - module.parse_arguments('--config', 'myconfig', '--repository', 'test.borg') - - def test_parse_arguments_allows_repository_with_extract(): flexmock(module.collect).should_receive('get_default_config_paths').and_return(['default']) From ce0ce4cd1c7016e302f7066f249d873a9b579e56 Mon Sep 17 00:00:00 2001 From: Nain Date: Thu, 16 Mar 2023 08:23:21 -0400 Subject: [PATCH 3/7] Merge mostly repetetive tests --- tests/unit/actions/test_prune.py | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/tests/unit/actions/test_prune.py b/tests/unit/actions/test_prune.py index 21e8260..6c09e0c 100644 --- a/tests/unit/actions/test_prune.py +++ b/tests/unit/actions/test_prune.py @@ -3,7 +3,7 @@ from flexmock import flexmock from borgmatic.actions import prune as module -def test_run_prune_calls_hooks(): +def test_run_prune_calls_hooks_of_configured_repository(): flexmock(module.logger).answer = lambda message: None flexmock(module.borgmatic.borg.prune).should_receive('prune_archives') flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2) @@ -26,28 +26,6 @@ def test_run_prune_calls_hooks(): ) -def test_run_prune_runs_with_no_explicit_repository(): - flexmock(module.logger).answer = lambda message: None - flexmock(module.borgmatic.borg.prune).should_receive('prune_archives') - prune_arguments = flexmock(repository=None, stats=flexmock(), list_archives=flexmock()) - global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) - - module.run_prune( - config_filename='test.yaml', - repository='repo', - storage={}, - retention={}, - hooks={}, - hook_context={}, - local_borg_version=None, - prune_arguments=prune_arguments, - global_arguments=global_arguments, - dry_run_label='', - local_path=None, - remote_path=None, - ) - - def test_run_prune_runs_with_select_repository(): flexmock(module.logger).answer = lambda message: None flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True) From 480addd7ce9f34996712f3f250d56d8d20dd3abd Mon Sep 17 00:00:00 2001 From: Nain Date: Thu, 16 Mar 2023 08:34:52 -0400 Subject: [PATCH 4/7] Add "--repository" flag to the "check" action --- borgmatic/actions/check.py | 6 ++++ borgmatic/commands/arguments.py | 4 +++ tests/unit/actions/test_check.py | 60 ++++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/borgmatic/actions/check.py b/borgmatic/actions/check.py index 580c70e..f357239 100644 --- a/borgmatic/actions/check.py +++ b/borgmatic/actions/check.py @@ -1,6 +1,7 @@ import logging import borgmatic.borg.check +import borgmatic.config.validate import borgmatic.hooks.command logger = logging.getLogger(__name__) @@ -23,6 +24,11 @@ def run_check( ''' Run the "check" action for the given repository. ''' + if check_arguments.repository and not borgmatic.config.validate.repositories_match( + repository, check_arguments.repository + ): + return + borgmatic.hooks.command.execute_hook( hooks.get('before_check'), hooks.get('umask'), diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index a8e8aac..fbf7168 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -419,6 +419,10 @@ def make_parsers(): add_help=False, ) check_group = check_parser.add_argument_group('check arguments') + check_group.add_argument( + '--repository', + help='Path of specific existing repository to check (must be already specified in a borgmatic configuration file)', + ) check_group.add_argument( '--progress', dest='progress', diff --git a/tests/unit/actions/test_check.py b/tests/unit/actions/test_check.py index 0007ee3..db03140 100644 --- a/tests/unit/actions/test_check.py +++ b/tests/unit/actions/test_check.py @@ -3,7 +3,7 @@ from flexmock import flexmock from borgmatic.actions import check as module -def test_run_check_calls_hooks(): +def test_run_check_calls_hooks_for_configured_repository(): flexmock(module.logger).answer = lambda message: None flexmock(module.borgmatic.config.checks).should_receive( 'repository_enabled_for_checks' @@ -11,7 +11,63 @@ def test_run_check_calls_hooks(): flexmock(module.borgmatic.borg.check).should_receive('check_archives') flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2) check_arguments = flexmock( - progress=flexmock(), repair=flexmock(), only=flexmock(), force=flexmock() + repository=None, progress=flexmock(), repair=flexmock(), only=flexmock(), force=flexmock() + ) + global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) + + module.run_check( + config_filename='test.yaml', + repository='repo', + location={'repositories': ['repo']}, + storage={}, + consistency={}, + hooks={}, + hook_context={}, + local_borg_version=None, + check_arguments=check_arguments, + global_arguments=global_arguments, + local_path=None, + remote_path=None, + ) + + +def test_run_check_runs_with_select_repository(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True) + flexmock(module.borgmatic.borg.check).should_receive('check_archives') + check_arguments = flexmock( + repository='repo', progress=flexmock(), repair=flexmock(), only=flexmock(), force=flexmock() + ) + global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) + + module.run_check( + config_filename='test.yaml', + repository='repo', + location={'repositories': ['repo']}, + storage={}, + consistency={}, + hooks={}, + hook_context={}, + local_borg_version=None, + check_arguments=check_arguments, + global_arguments=global_arguments, + local_path=None, + remote_path=None, + ) + + +def test_run_check_bails_if_repository_does_not_match(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return( + False + ) + flexmock(module.borgmatic.borg.check).should_receive('check_archives').never() + check_arguments = flexmock( + repository='repo2', + progress=flexmock(), + repair=flexmock(), + only=flexmock(), + force=flexmock(), ) global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) From a8aeace5b52e9d3bfff4a7860f34388f35454e77 Mon Sep 17 00:00:00 2001 From: Nain Date: Thu, 16 Mar 2023 11:13:45 -0400 Subject: [PATCH 5/7] Add "--repository" flag to the "compact" action --- borgmatic/actions/compact.py | 6 ++++ borgmatic/commands/arguments.py | 4 +++ tests/unit/actions/test_compact.py | 58 ++++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/borgmatic/actions/compact.py b/borgmatic/actions/compact.py index 00585b0..7a25b82 100644 --- a/borgmatic/actions/compact.py +++ b/borgmatic/actions/compact.py @@ -2,6 +2,7 @@ import logging import borgmatic.borg.compact import borgmatic.borg.feature +import borgmatic.config.validate import borgmatic.hooks.command logger = logging.getLogger(__name__) @@ -24,6 +25,11 @@ def run_compact( ''' Run the "compact" action for the given repository. ''' + if compact_arguments.repository and not borgmatic.config.validate.repositories_match( + repository, compact_arguments.repository + ): + return + borgmatic.hooks.command.execute_hook( hooks.get('before_compact'), hooks.get('umask'), diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index fbf7168..6991db5 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -357,6 +357,10 @@ def make_parsers(): add_help=False, ) compact_group = compact_parser.add_argument_group('compact arguments') + compact_group.add_argument( + '--repository', + help='Path of specific existing repository to compact (must be already specified in a borgmatic configuration file)', + ) compact_group.add_argument( '--progress', dest='progress', diff --git a/tests/unit/actions/test_compact.py b/tests/unit/actions/test_compact.py index bc2b940..fab21f3 100644 --- a/tests/unit/actions/test_compact.py +++ b/tests/unit/actions/test_compact.py @@ -3,13 +3,67 @@ from flexmock import flexmock from borgmatic.actions import compact as module -def test_compact_actions_calls_hooks(): +def test_compact_actions_calls_hooks_for_configured_repository(): flexmock(module.logger).answer = lambda message: None flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True) flexmock(module.borgmatic.borg.compact).should_receive('compact_segments') flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2) compact_arguments = flexmock( - progress=flexmock(), cleanup_commits=flexmock(), threshold=flexmock() + repository=None, progress=flexmock(), cleanup_commits=flexmock(), threshold=flexmock() + ) + global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) + + module.run_compact( + config_filename='test.yaml', + repository='repo', + storage={}, + retention={}, + hooks={}, + hook_context={}, + local_borg_version=None, + compact_arguments=compact_arguments, + global_arguments=global_arguments, + dry_run_label='', + local_path=None, + remote_path=None, + ) + + +def test_compact_runs_with_select_repository(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True) + flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True) + flexmock(module.borgmatic.borg.compact).should_receive('compact_segments') + compact_arguments = flexmock( + repository='repo', progress=flexmock(), cleanup_commits=flexmock(), threshold=flexmock() + ) + global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) + + module.run_compact( + config_filename='test.yaml', + repository='repo', + storage={}, + retention={}, + hooks={}, + hook_context={}, + local_borg_version=None, + compact_arguments=compact_arguments, + global_arguments=global_arguments, + dry_run_label='', + local_path=None, + remote_path=None, + ) + + +def test_compact_bails_if_repository_does_not_match(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True) + flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return( + False + ) + flexmock(module.borgmatic.borg.compact).should_receive('compact_segments').never() + compact_arguments = flexmock( + repository='repo2', progress=flexmock(), cleanup_commits=flexmock(), threshold=flexmock() ) global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) From 5f87ea3ec5146f07ce639580d1752344646ff925 Mon Sep 17 00:00:00 2001 From: Nain Date: Thu, 16 Mar 2023 13:15:49 -0400 Subject: [PATCH 6/7] Add "--repository" flag to the "create" action --- borgmatic/actions/create.py | 6 +++ borgmatic/commands/arguments.py | 4 ++ tests/unit/actions/test_create.py | 74 ++++++++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/borgmatic/actions/create.py b/borgmatic/actions/create.py index c882032..96a4852 100644 --- a/borgmatic/actions/create.py +++ b/borgmatic/actions/create.py @@ -2,6 +2,7 @@ import json import logging import borgmatic.borg.create +import borgmatic.config.validate import borgmatic.hooks.command import borgmatic.hooks.dispatch import borgmatic.hooks.dump @@ -28,6 +29,11 @@ def run_create( If create_arguments.json is True, yield the JSON output from creating the archive. ''' + if create_arguments.repository and not borgmatic.config.validate.repositories_match( + repository, create_arguments.repository + ): + return + borgmatic.hooks.command.execute_hook( hooks.get('before_backup'), hooks.get('umask'), diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index 6991db5..d5dc6af 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -393,6 +393,10 @@ def make_parsers(): add_help=False, ) create_group = create_parser.add_argument_group('create arguments') + create_group.add_argument( + '--repository', + help='Path of specific existing repository to backup to (must be already specified in a borgmatic configuration file)', + ) create_group.add_argument( '--progress', dest='progress', diff --git a/tests/unit/actions/test_create.py b/tests/unit/actions/test_create.py index 915f0ae..04d7abc 100644 --- a/tests/unit/actions/test_create.py +++ b/tests/unit/actions/test_create.py @@ -5,14 +5,84 @@ from borgmatic.actions import create as module def test_run_create_executes_and_calls_hooks(): flexmock(module.logger).answer = lambda message: None - flexmock(module.borgmatic.borg.create).should_receive('create_archive') + flexmock(module.borgmatic.borg.create).should_receive('create_archive').once() flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2) flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return({}) flexmock(module.borgmatic.hooks.dispatch).should_receive( 'call_hooks_even_if_unconfigured' ).and_return({}) create_arguments = flexmock( - progress=flexmock(), stats=flexmock(), json=flexmock(), list_files=flexmock() + repository=flexmock(), + progress=flexmock(), + stats=flexmock(), + json=flexmock(), + list_files=flexmock(), + ) + global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) + + list( + module.run_create( + config_filename='test.yaml', + repository='repo', + location={}, + storage={}, + hooks={}, + hook_context={}, + local_borg_version=None, + create_arguments=create_arguments, + global_arguments=global_arguments, + dry_run_label='', + local_path=None, + remote_path=None, + ) + ) + + +def test_run_create_runs_with_select_repository(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.config.validate).should_receive( + 'repositories_match' + ).once().and_return(True) + flexmock(module.borgmatic.borg.create).should_receive('create_archive').once() + create_arguments = flexmock( + repository=flexmock(), + progress=flexmock(), + stats=flexmock(), + json=flexmock(), + list_files=flexmock(), + ) + global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) + + list( + module.run_create( + config_filename='test.yaml', + repository='repo', + location={}, + storage={}, + hooks={}, + hook_context={}, + local_borg_version=None, + create_arguments=create_arguments, + global_arguments=global_arguments, + dry_run_label='', + local_path=None, + remote_path=None, + ) + ) + + +def test_run_create_bails_if_repository_does_not_match(): + flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.config.validate).should_receive( + 'repositories_match' + ).once().and_return(False) + flexmock(module.borgmatic.borg.create).should_receive('create_archive').never() + create_arguments = flexmock( + repository=flexmock(), + progress=flexmock(), + stats=flexmock(), + json=flexmock(), + list_files=flexmock(), ) global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) From 3e2241461331283d4332d08675611c6294288532 Mon Sep 17 00:00:00 2001 From: Nain Date: Thu, 16 Mar 2023 14:01:29 -0400 Subject: [PATCH 7/7] Update tests Make them more explicit. Also formatting. --- tests/unit/actions/test_check.py | 27 +++++++++++++++++---------- tests/unit/actions/test_compact.py | 19 +++++++++++-------- tests/unit/actions/test_create.py | 5 +++-- tests/unit/actions/test_prune.py | 15 +++++++++------ 4 files changed, 40 insertions(+), 26 deletions(-) diff --git a/tests/unit/actions/test_check.py b/tests/unit/actions/test_check.py index db03140..bd0cf1c 100644 --- a/tests/unit/actions/test_check.py +++ b/tests/unit/actions/test_check.py @@ -8,10 +8,11 @@ def test_run_check_calls_hooks_for_configured_repository(): flexmock(module.borgmatic.config.checks).should_receive( 'repository_enabled_for_checks' ).and_return(True) - flexmock(module.borgmatic.borg.check).should_receive('check_archives') + flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never() + flexmock(module.borgmatic.borg.check).should_receive('check_archives').once() flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2) check_arguments = flexmock( - repository=None, progress=flexmock(), repair=flexmock(), only=flexmock(), force=flexmock() + repository=None, progress=flexmock(), repair=flexmock(), only=flexmock(), force=flexmock(), ) global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) @@ -33,16 +34,22 @@ def test_run_check_calls_hooks_for_configured_repository(): def test_run_check_runs_with_select_repository(): flexmock(module.logger).answer = lambda message: None - flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True) - flexmock(module.borgmatic.borg.check).should_receive('check_archives') + flexmock(module.borgmatic.config.validate).should_receive( + 'repositories_match' + ).once().and_return(True) + flexmock(module.borgmatic.borg.check).should_receive('check_archives').once() check_arguments = flexmock( - repository='repo', progress=flexmock(), repair=flexmock(), only=flexmock(), force=flexmock() + repository=flexmock(), + progress=flexmock(), + repair=flexmock(), + only=flexmock(), + force=flexmock(), ) global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) module.run_check( config_filename='test.yaml', - repository='repo', + repository=flexmock(), location={'repositories': ['repo']}, storage={}, consistency={}, @@ -58,12 +65,12 @@ def test_run_check_runs_with_select_repository(): def test_run_check_bails_if_repository_does_not_match(): flexmock(module.logger).answer = lambda message: None - flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return( - False - ) + flexmock(module.borgmatic.config.validate).should_receive( + 'repositories_match' + ).once().and_return(False) flexmock(module.borgmatic.borg.check).should_receive('check_archives').never() check_arguments = flexmock( - repository='repo2', + repository=flexmock(), progress=flexmock(), repair=flexmock(), only=flexmock(), diff --git a/tests/unit/actions/test_compact.py b/tests/unit/actions/test_compact.py index fab21f3..97f3d5b 100644 --- a/tests/unit/actions/test_compact.py +++ b/tests/unit/actions/test_compact.py @@ -6,7 +6,8 @@ from borgmatic.actions import compact as module def test_compact_actions_calls_hooks_for_configured_repository(): flexmock(module.logger).answer = lambda message: None flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True) - flexmock(module.borgmatic.borg.compact).should_receive('compact_segments') + flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never() + flexmock(module.borgmatic.borg.compact).should_receive('compact_segments').once() flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2) compact_arguments = flexmock( repository=None, progress=flexmock(), cleanup_commits=flexmock(), threshold=flexmock() @@ -31,11 +32,13 @@ def test_compact_actions_calls_hooks_for_configured_repository(): def test_compact_runs_with_select_repository(): flexmock(module.logger).answer = lambda message: None - flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True) + flexmock(module.borgmatic.config.validate).should_receive( + 'repositories_match' + ).once().and_return(True) flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True) - flexmock(module.borgmatic.borg.compact).should_receive('compact_segments') + flexmock(module.borgmatic.borg.compact).should_receive('compact_segments').once() compact_arguments = flexmock( - repository='repo', progress=flexmock(), cleanup_commits=flexmock(), threshold=flexmock() + repository=flexmock(), progress=flexmock(), cleanup_commits=flexmock(), threshold=flexmock() ) global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) @@ -58,12 +61,12 @@ def test_compact_runs_with_select_repository(): def test_compact_bails_if_repository_does_not_match(): flexmock(module.logger).answer = lambda message: None flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True) - flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return( - False - ) + flexmock(module.borgmatic.config.validate).should_receive( + 'repositories_match' + ).once().and_return(False) flexmock(module.borgmatic.borg.compact).should_receive('compact_segments').never() compact_arguments = flexmock( - repository='repo2', progress=flexmock(), cleanup_commits=flexmock(), threshold=flexmock() + repository=flexmock(), progress=flexmock(), cleanup_commits=flexmock(), threshold=flexmock() ) global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) diff --git a/tests/unit/actions/test_create.py b/tests/unit/actions/test_create.py index 04d7abc..6eb18c2 100644 --- a/tests/unit/actions/test_create.py +++ b/tests/unit/actions/test_create.py @@ -3,8 +3,9 @@ from flexmock import flexmock from borgmatic.actions import create as module -def test_run_create_executes_and_calls_hooks(): +def test_run_create_executes_and_calls_hooks_for_configured_repository(): flexmock(module.logger).answer = lambda message: None + flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never() flexmock(module.borgmatic.borg.create).should_receive('create_archive').once() flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2) flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return({}) @@ -12,7 +13,7 @@ def test_run_create_executes_and_calls_hooks(): 'call_hooks_even_if_unconfigured' ).and_return({}) create_arguments = flexmock( - repository=flexmock(), + repository=None, progress=flexmock(), stats=flexmock(), json=flexmock(), diff --git a/tests/unit/actions/test_prune.py b/tests/unit/actions/test_prune.py index 6c09e0c..b911989 100644 --- a/tests/unit/actions/test_prune.py +++ b/tests/unit/actions/test_prune.py @@ -5,7 +5,8 @@ from borgmatic.actions import prune as module def test_run_prune_calls_hooks_of_configured_repository(): flexmock(module.logger).answer = lambda message: None - flexmock(module.borgmatic.borg.prune).should_receive('prune_archives') + flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never() + flexmock(module.borgmatic.borg.prune).should_receive('prune_archives').once() flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2) prune_arguments = flexmock(repository=None, stats=flexmock(), list_archives=flexmock()) global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) @@ -28,8 +29,10 @@ def test_run_prune_calls_hooks_of_configured_repository(): def test_run_prune_runs_with_select_repository(): flexmock(module.logger).answer = lambda message: None - flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True) - flexmock(module.borgmatic.borg.prune).should_receive('prune_archives') + flexmock(module.borgmatic.config.validate).should_receive( + 'repositories_match' + ).once().and_return(True) + flexmock(module.borgmatic.borg.prune).should_receive('prune_archives').once() prune_arguments = flexmock(repository=flexmock(), stats=flexmock(), list_archives=flexmock()) global_arguments = flexmock(monitoring_verbosity=1, dry_run=False) @@ -51,9 +54,9 @@ def test_run_prune_runs_with_select_repository(): def test_run_prune_bails_if_repository_does_not_match(): flexmock(module.logger).answer = lambda message: None - flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return( - False - ) + flexmock(module.borgmatic.config.validate).should_receive( + 'repositories_match' + ).once().and_return(False) flexmock(module.borgmatic.borg.prune).should_receive('prune_archives').never() prune_arguments = flexmock(repository=flexmock(), stats=flexmock(), list_archives=flexmock()) global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)