From 68ee9687f5e01bd5abbedb43e88f8fb02d1bcd5d Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 27 Apr 2023 22:27:23 +0530 Subject: [PATCH] Added tests for all subcommands and used black formatter Signed-off-by: Chirag Aggarwal --- borgmatic/borg/mount.py | 2 - borgmatic/borg/prune.py | 3 - borgmatic/commands/arguments.py | 80 ++++++++++++++++++------- tests/integration/borg/test_commands.py | 1 + tests/unit/borg/test_info.py | 49 +++++++++++++++ tests/unit/borg/test_mount.py | 55 ++++++++++++++++- tests/unit/borg/test_rlist.py | 42 +++++++++++++ tests/unit/borg/test_transfer.py | 47 +++++++++++++++ 8 files changed, 252 insertions(+), 27 deletions(-) diff --git a/borgmatic/borg/mount.py b/borgmatic/borg/mount.py index 764f72e..3302617 100644 --- a/borgmatic/borg/mount.py +++ b/borgmatic/borg/mount.py @@ -31,12 +31,10 @@ def mount_archive( + (('--lock-wait', str(lock_wait)) if lock_wait else ()) + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ()) + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) - + flags.make_flags_from_arguments( mount_arguments, excludes=('repository', 'archive', 'mount_point', 'paths', 'options'), ) - + (('-o', mount_arguments.options) if mount_arguments.options else ()) + ( ( diff --git a/borgmatic/borg/prune.py b/borgmatic/borg/prune.py index ba41fba..c7d1393 100644 --- a/borgmatic/borg/prune.py +++ b/borgmatic/borg/prune.py @@ -75,16 +75,13 @@ def prune_archives( + (('--lock-wait', str(lock_wait)) if lock_wait else ()) + (('--stats',) if prune_arguments.stats and not dry_run else ()) + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ()) - + flags.make_flags_from_arguments( prune_arguments, excludes=('repository', 'stats', 'list_archives'), ) + (('--list',) if prune_arguments.list_archives else ()) - + (('--debug', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (('--dry-run',) if dry_run else ()) - + (tuple(extra_borg_options.split(' ')) if extra_borg_options else ()) + flags.make_repository_flags(repository_path, local_borg_version) ) diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index 7711958..4b19135 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -327,16 +327,24 @@ def make_parsers(): '--last', metavar='N', help='Only transfer last N archives after other filters are applied' ) transfer_group.add_argument( - '--oldest', metavar='TIMESPAN', help='Transfer archives within a specified time range starting from the timestamp of the oldest archive (e.g. 7d or 12m) [Borg 2.x+ only]' + '--oldest', + metavar='TIMESPAN', + help='Transfer archives within a specified time range starting from the timestamp of the oldest archive (e.g. 7d or 12m) [Borg 2.x+ only]', ) transfer_group.add_argument( - '--newest', metavar='TIMESPAN', help='Transfer archives within a time range that ends at timestamp of the newest archive and starts a specified time range ago (e.g. 7d or 12m) [Borg 2.x+ only]' + '--newest', + metavar='TIMESPAN', + help='Transfer archives within a time range that ends at timestamp of the newest archive and starts a specified time range ago (e.g. 7d or 12m) [Borg 2.x+ only]', ) transfer_group.add_argument( - '--older', metavar='TIMESPAN', help='Transfer archives that are older than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]' + '--older', + metavar='TIMESPAN', + help='Transfer archives that are older than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]', ) transfer_group.add_argument( - '--newer', metavar='TIMESPAN', help='Transfer archives that are newer than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]' + '--newer', + metavar='TIMESPAN', + help='Transfer archives that are newer than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]', ) transfer_group.add_argument( '-h', '--help', action='help', help='Show this help message and exit' @@ -365,16 +373,24 @@ def make_parsers(): '--list', dest='list_archives', action='store_true', help='List archives kept/pruned' ) prune_group.add_argument( - '--oldest', metavar='TIMESPAN', help='Prune archives within a specified time range starting from the timestamp of the oldest archive (e.g. 7d or 12m) [Borg 2.x+ only]' + '--oldest', + metavar='TIMESPAN', + help='Prune archives within a specified time range starting from the timestamp of the oldest archive (e.g. 7d or 12m) [Borg 2.x+ only]', ) prune_group.add_argument( - '--newest', metavar='TIMESPAN', help='Prune archives within a time range that ends at timestamp of the newest archive and starts a specified time range ago (e.g. 7d or 12m) [Borg 2.x+ only]' + '--newest', + metavar='TIMESPAN', + help='Prune archives within a time range that ends at timestamp of the newest archive and starts a specified time range ago (e.g. 7d or 12m) [Borg 2.x+ only]', ) prune_group.add_argument( - '--older', metavar='TIMESPAN', help='Prune archives that are older than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]' + '--older', + metavar='TIMESPAN', + help='Prune archives that are older than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]', ) prune_group.add_argument( - '--newer', metavar='TIMESPAN', help='Prune archives that are newer than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]' + '--newer', + metavar='TIMESPAN', + help='Prune archives that are newer than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]', ) prune_group.add_argument('-h', '--help', action='help', help='Show this help message and exit') @@ -625,16 +641,24 @@ def make_parsers(): '--last', metavar='N', help='Mount last N archives after other filters are applied' ) mount_group.add_argument( - '--oldest', metavar='TIMESPAN', help='Mount archives within a specified time range starting from the timestamp of the oldest archive (e.g. 7d or 12m) [Borg 2.x+ only]' + '--oldest', + metavar='TIMESPAN', + help='Mount archives within a specified time range starting from the timestamp of the oldest archive (e.g. 7d or 12m) [Borg 2.x+ only]', ) mount_group.add_argument( - '--newest', metavar='TIMESPAN', help='Mount archives within a time range that ends at timestamp of the newest archive and starts a specified time range ago (e.g. 7d or 12m) [Borg 2.x+ only]' + '--newest', + metavar='TIMESPAN', + help='Mount archives within a time range that ends at timestamp of the newest archive and starts a specified time range ago (e.g. 7d or 12m) [Borg 2.x+ only]', ) mount_group.add_argument( - '--older', metavar='TIMESPAN', help='Mount archives that are older than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]' + '--older', + metavar='TIMESPAN', + help='Mount archives that are older than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]', ) mount_group.add_argument( - '--newer', metavar='TIMESPAN', help='Mount archives that are newer than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]' + '--newer', + metavar='TIMESPAN', + help='Mount archives that are newer than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]', ) mount_group.add_argument('--options', dest='options', help='Extra Borg mount options') mount_group.add_argument('-h', '--help', action='help', help='Show this help message and exit') @@ -728,16 +752,24 @@ def make_parsers(): '--last', metavar='N', help='List last N archives after other filters are applied' ) rlist_group.add_argument( - '--oldest', metavar='TIMESPAN', help='List archives within a specified time range starting from the timestamp of the oldest archive (e.g. 7d or 12m) [Borg 2.x+ only]' + '--oldest', + metavar='TIMESPAN', + help='List archives within a specified time range starting from the timestamp of the oldest archive (e.g. 7d or 12m) [Borg 2.x+ only]', ) rlist_group.add_argument( - '--newest', metavar='TIMESPAN', help='List archives within a time range that ends at timestamp of the newest archive and starts a specified time range ago (e.g. 7d or 12m) [Borg 2.x+ only]' + '--newest', + metavar='TIMESPAN', + help='List archives within a time range that ends at timestamp of the newest archive and starts a specified time range ago (e.g. 7d or 12m) [Borg 2.x+ only]', ) rlist_group.add_argument( - '--older', metavar='TIMESPAN', help='List archives that are older than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]' + '--older', + metavar='TIMESPAN', + help='List archives that are older than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]', ) rlist_group.add_argument( - '--newer', metavar='TIMESPAN', help='List archives that are newer than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]' + '--newer', + metavar='TIMESPAN', + help='List archives that are newer than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]', ) rlist_group.add_argument('-h', '--help', action='help', help='Show this help message and exit') @@ -871,16 +903,24 @@ def make_parsers(): '--last', metavar='N', help='Show info for last N archives after other filters are applied' ) info_group.add_argument( - '--oldest', metavar='TIMESPAN', help='Show info for archives within a specified time range starting from the timestamp of the oldest archive (e.g. 7d or 12m) [Borg 2.x+ only]' + '--oldest', + metavar='TIMESPAN', + help='Show info for archives within a specified time range starting from the timestamp of the oldest archive (e.g. 7d or 12m) [Borg 2.x+ only]', ) info_group.add_argument( - '--newest', metavar='TIMESPAN', help='Show info for archives within a time range that ends at timestamp of the newest archive and starts a specified time range ago (e.g. 7d or 12m) [Borg 2.x+ only]' + '--newest', + metavar='TIMESPAN', + help='Show info for archives within a time range that ends at timestamp of the newest archive and starts a specified time range ago (e.g. 7d or 12m) [Borg 2.x+ only]', ) info_group.add_argument( - '--older', metavar='TIMESPAN', help='Show info for archives that are older than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]' + '--older', + metavar='TIMESPAN', + help='Show info for archives that are older than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]', ) info_group.add_argument( - '--newer', metavar='TIMESPAN', help='Show info for archives that are newer than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]' + '--newer', + metavar='TIMESPAN', + help='Show info for archives that are newer than the specified time range (e.g. 7d or 12m) from the current time [Borg 2.x+ only]', ) info_group.add_argument('-h', '--help', action='help', help='Show this help message and exit') diff --git a/tests/integration/borg/test_commands.py b/tests/integration/borg/test_commands.py index 6049a26..6c330d6 100644 --- a/tests/integration/borg/test_commands.py +++ b/tests/integration/borg/test_commands.py @@ -107,6 +107,7 @@ def test_display_archives_info_command_does_not_duplicate_flags_or_raise(): 'repo', {}, '2.3.4', fuzz_argument(arguments, argument_name) ) + def test_prune_archives_command_does_not_duplicate_flags_or_raise(): arguments = borgmatic.commands.arguments.parse_arguments('prune')['prune'] flexmock(borgmatic.borg.prune).should_receive('execute_command').replace_with( diff --git a/tests/unit/borg/test_info.py b/tests/unit/borg/test_info.py index 2eed4fe..d7bf731 100644 --- a/tests/unit/borg/test_info.py +++ b/tests/unit/borg/test_info.py @@ -433,3 +433,52 @@ def test_display_archives_info_passes_through_arguments_to_borg(argument_name): archive=None, json=False, prefix=None, match_archives=None, **{argument_name: 'value'} ), ) + + +def test_display_archives_info_with_date_based_matching_calls_borg_with_date_based_flags(): + flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels') + flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER + flexmock(module.flags).should_receive('make_flags').and_return(()) + flexmock(module.flags).should_receive('make_match_archives_flags').with_args( + None, None, '2.3.4' + ).and_return(()) + flexmock(module.flags).should_receive('make_flags_from_arguments').and_return( + ('--newer', '1d', '--newest', '1y', '--older', '1m', '--oldest', '1w') + ) + flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo')) + flexmock(module.environment).should_receive('make_environment') + flexmock(module).should_receive('execute_command').with_args( + ( + 'borg', + 'info', + '--newer', + '1d', + '--newest', + '1y', + '--older', + '1m', + '--oldest', + '1w', + '--repo', + 'repo', + ), + output_log_level=module.borgmatic.logger.ANSWER, + borg_local_path='borg', + extra_environment=None, + ) + info_arguments = flexmock( + archive=None, + json=False, + prefix=None, + match_archives=None, + newer='1d', + newest='1y', + older='1m', + oldest='1w', + ) + module.display_archives_info( + repository_path='repo', + storage_config={}, + local_borg_version='2.3.4', + info_arguments=info_arguments, + ) diff --git a/tests/unit/borg/test_mount.py b/tests/unit/borg/test_mount.py index 1438ebe..05ff72e 100644 --- a/tests/unit/borg/test_mount.py +++ b/tests/unit/borg/test_mount.py @@ -77,7 +77,9 @@ def test_mount_archive_calls_borg_with_path_flags(): ) insert_execute_command_mock(('borg', 'mount', 'repo::archive', '/mnt', 'path1', 'path2')) - mount_arguments = flexmock(mount_point='/mnt', options=None, paths=['path1', 'path2'], foreground=False) + mount_arguments = flexmock( + mount_point='/mnt', options=None, paths=['path1', 'path2'], foreground=False + ) module.mount_archive( repository_path='repo', archive='archive', @@ -207,7 +209,9 @@ def test_mount_archive_calls_borg_with_options_flags(): ) insert_execute_command_mock(('borg', 'mount', '-o', 'super_mount', 'repo::archive', '/mnt')) - mount_arguments = flexmock(mount_point='/mnt', options='super_mount', paths=None, foreground=False) + mount_arguments = flexmock( + mount_point='/mnt', options='super_mount', paths=None, foreground=False + ) module.mount_archive( repository_path='repo', archive='archive', @@ -215,3 +219,50 @@ def test_mount_archive_calls_borg_with_options_flags(): storage_config={}, local_borg_version='1.2.3', ) + + +def test_mount_archive_with_date_based_matching_calls_borg_with_date_based_flags(): + flexmock(module.feature).should_receive('available').and_return(True) + flexmock(module.flags).should_receive('make_repository_flags').and_return( + ( + '--repo', + 'repo', + ) + ) + insert_execute_command_mock( + ( + 'borg', + 'mount', + '--newer', + '1d', + '--newest', + '1y', + '--older', + '1m', + '--oldest', + '1w', + '--repo', + 'repo', + '--match-archives', + None, + '/mnt', + ) + ) + + mount_arguments = flexmock( + mount_point='/mnt', + options=None, + paths=None, + foreground=False, + newer='1d', + newest='1y', + older='1m', + oldest='1w', + ) + module.mount_archive( + repository_path='repo', + archive=None, + mount_arguments=mount_arguments, + storage_config={}, + local_borg_version='1.2.3', + ) diff --git a/tests/unit/borg/test_rlist.py b/tests/unit/borg/test_rlist.py index b83ba61..b6fadd1 100644 --- a/tests/unit/borg/test_rlist.py +++ b/tests/unit/borg/test_rlist.py @@ -506,3 +506,45 @@ def test_list_repository_with_json_returns_borg_output(): ) == json_output ) + + +def test_make_rlist_command_with_date_based_matching_calls_borg_with_date_based_flags(): + flexmock(module.flags).should_receive('make_flags').and_return(()) + flexmock(module.flags).should_receive('make_match_archives_flags').with_args( + None, None, '1.2.3' + ).and_return(()) + flexmock(module.flags).should_receive('make_flags_from_arguments').and_return( + ('--newer', '1d', '--newest', '1y', '--older', '1m', '--oldest', '1w') + ) + flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',)) + + command = module.make_rlist_command( + repository_path='repo', + storage_config={}, + local_borg_version='1.2.3', + rlist_arguments=flexmock( + archive=None, + paths=None, + json=False, + prefix=None, + match_archives=None, + newer='1d', + newest='1y', + older='1m', + oldest='1w', + ), + ) + + assert command == ( + 'borg', + 'list', + '--newer', + '1d', + '--newest', + '1y', + '--older', + '1m', + '--oldest', + '1w', + 'repo', + ) diff --git a/tests/unit/borg/test_transfer.py b/tests/unit/borg/test_transfer.py index 8f41bf5..fd6cb19 100644 --- a/tests/unit/borg/test_transfer.py +++ b/tests/unit/borg/test_transfer.py @@ -386,3 +386,50 @@ def test_transfer_archives_with_source_repository_calls_borg_with_other_repo_fla archive=None, progress=None, match_archives=None, source_repository='other' ), ) + + +def test_transfer_archives_with_date_based_matching_calls_borg_with_date_based_flags(): + flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels') + flexmock(module.flags).should_receive('make_flags').and_return(()) + flexmock(module.flags).should_receive('make_match_archives_flags').and_return(()) + flexmock(module.flags).should_receive('make_flags_from_arguments').and_return( + ('--newer', '1d', '--newest', '1y', '--older', '1m', '--oldest', '1w') + ) + flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo')) + flexmock(module.environment).should_receive('make_environment') + flexmock(module).should_receive('execute_command').with_args( + ( + 'borg', + 'transfer', + '--newer', + '1d', + '--newest', + '1y', + '--older', + '1m', + '--oldest', + '1w', + '--repo', + 'repo', + ), + output_log_level=module.borgmatic.logger.ANSWER, + output_file=None, + borg_local_path='borg', + extra_environment=None, + ) + + module.transfer_archives( + dry_run=False, + repository_path='repo', + storage_config={}, + local_borg_version='2.3.4', + transfer_arguments=flexmock( + archive=None, + progress=None, + source_repository='other', + newer='1d', + newest='1y', + older='1m', + oldest='1w', + ), + )