Tests for extract_archive().

This commit is contained in:
Dan Helfman 2019-02-18 10:31:52 -08:00
parent 9de5083a7e
commit 2a4d4247e3
3 changed files with 90 additions and 12 deletions

View File

@ -57,28 +57,29 @@ def extract_archive(
repository, repository,
archive, archive,
restore_paths, restore_paths,
location_config,
storage_config, storage_config,
local_path=None, local_path='borg',
remote_path=None, remote_path=None,
progress=False,
): ):
''' '''
Given a dry-run flag, a local or remote repository path, an archive name, zero or more paths to Given a dry-run flag, a local or remote repository path, an archive name, zero or more paths to
restore from the archive, a location configuration dict, and a storage configuration dict, restore from the archive, and a storage configuration dict, extract the archive into the current
extract the archive into the current directory. directory.
''' '''
umask = storage_config.get('umask', None) umask = storage_config.get('umask', None)
lock_wait = storage_config.get('lock_wait', None) lock_wait = storage_config.get('lock_wait', None)
full_command = ( full_command = (
(local_path, 'extract', '::'.join(repository, archive)) (local_path, 'extract', '::'.join((repository, archive)))
+ restore_paths + (restore_paths if restore_paths else ())
+ (('--remote-path', remote_path) if remote_path else ()) + (('--remote-path', remote_path) if remote_path else ())
+ (('--umask', str(umask)) if umask else ()) + (('--umask', str(umask)) if umask else ())
+ (('--lock-wait', str(lock_wait)) if lock_wait else ()) + (('--lock-wait', str(lock_wait)) if lock_wait else ())
+ (('--info',) if logger.getEffectiveLevel() == logging.INFO else ()) + (('--info',) if logger.getEffectiveLevel() == logging.INFO else ())
+ (('--debug', '--list', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ()) + (('--debug', '--list', '--show-rc') if logger.isEnabledFor(logging.DEBUG) else ())
+ (('--dry-run',) if dry_run else ()) + (('--dry-run',) if dry_run else ())
+ (('--progress',) if progress else ())
) )
logger.debug(' '.join(full_command)) logger.debug(' '.join(full_command))

View File

@ -357,6 +357,7 @@ def _run_commands_on_repository(
storage, storage,
local_path=local_path, local_path=local_path,
remote_path=remote_path, remote_path=remote_path,
progress=args.progress,
) )
if args.list: if args.list:
logger.info('{}: Listing archives'.format(repository)) logger.info('{}: Listing archives'.format(repository))

View File

@ -24,7 +24,7 @@ def insert_subprocess_check_output_mock(check_output_command, result, **kwargs):
).once() ).once()
def test_extract_last_archive_dry_run_should_call_borg_with_last_archive(): def test_extract_last_archive_dry_run_calls_borg_with_last_archive():
flexmock(sys.stdout).encoding = 'utf-8' flexmock(sys.stdout).encoding = 'utf-8'
insert_subprocess_check_output_mock( insert_subprocess_check_output_mock(
('borg', 'list', '--short', 'repo'), result='archive1\narchive2\n'.encode('utf-8') ('borg', 'list', '--short', 'repo'), result='archive1\narchive2\n'.encode('utf-8')
@ -44,7 +44,7 @@ def test_extract_last_archive_dry_run_without_any_archives_should_bail():
module.extract_last_archive_dry_run(repository='repo', lock_wait=None) module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
def test_extract_last_archive_dry_run_with_log_info_should_call_borg_with_info_parameter(): def test_extract_last_archive_dry_run_with_log_info_calls_borg_with_info_parameter():
flexmock(sys.stdout).encoding = 'utf-8' flexmock(sys.stdout).encoding = 'utf-8'
insert_subprocess_check_output_mock( insert_subprocess_check_output_mock(
('borg', 'list', '--short', 'repo', '--info'), result='archive1\narchive2\n'.encode('utf-8') ('borg', 'list', '--short', 'repo', '--info'), result='archive1\narchive2\n'.encode('utf-8')
@ -55,7 +55,7 @@ def test_extract_last_archive_dry_run_with_log_info_should_call_borg_with_info_p
module.extract_last_archive_dry_run(repository='repo', lock_wait=None) module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
def test_extract_last_archive_dry_run_with_log_debug_should_call_borg_with_debug_parameter(): def test_extract_last_archive_dry_run_with_log_debug_calls_borg_with_debug_parameter():
flexmock(sys.stdout).encoding = 'utf-8' flexmock(sys.stdout).encoding = 'utf-8'
insert_subprocess_check_output_mock( insert_subprocess_check_output_mock(
('borg', 'list', '--short', 'repo', '--debug', '--show-rc'), ('borg', 'list', '--short', 'repo', '--debug', '--show-rc'),
@ -69,7 +69,7 @@ def test_extract_last_archive_dry_run_with_log_debug_should_call_borg_with_debug
module.extract_last_archive_dry_run(repository='repo', lock_wait=None) module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
def test_extract_last_archive_dry_run_should_call_borg_via_local_path(): def test_extract_last_archive_dry_run_calls_borg_via_local_path():
flexmock(sys.stdout).encoding = 'utf-8' flexmock(sys.stdout).encoding = 'utf-8'
insert_subprocess_check_output_mock( insert_subprocess_check_output_mock(
('borg1', 'list', '--short', 'repo'), result='archive1\narchive2\n'.encode('utf-8') ('borg1', 'list', '--short', 'repo'), result='archive1\narchive2\n'.encode('utf-8')
@ -79,7 +79,7 @@ def test_extract_last_archive_dry_run_should_call_borg_via_local_path():
module.extract_last_archive_dry_run(repository='repo', lock_wait=None, local_path='borg1') module.extract_last_archive_dry_run(repository='repo', lock_wait=None, local_path='borg1')
def test_extract_last_archive_dry_run_should_call_borg_with_remote_path_parameters(): def test_extract_last_archive_dry_run_calls_borg_with_remote_path_parameters():
flexmock(sys.stdout).encoding = 'utf-8' flexmock(sys.stdout).encoding = 'utf-8'
insert_subprocess_check_output_mock( insert_subprocess_check_output_mock(
('borg', 'list', '--short', 'repo', '--remote-path', 'borg1'), ('borg', 'list', '--short', 'repo', '--remote-path', 'borg1'),
@ -92,7 +92,7 @@ def test_extract_last_archive_dry_run_should_call_borg_with_remote_path_paramete
module.extract_last_archive_dry_run(repository='repo', lock_wait=None, remote_path='borg1') module.extract_last_archive_dry_run(repository='repo', lock_wait=None, remote_path='borg1')
def test_extract_last_archive_dry_run_should_call_borg_with_lock_wait_parameters(): def test_extract_last_archive_dry_run_calls_borg_with_lock_wait_parameters():
flexmock(sys.stdout).encoding = 'utf-8' flexmock(sys.stdout).encoding = 'utf-8'
insert_subprocess_check_output_mock( insert_subprocess_check_output_mock(
('borg', 'list', '--short', 'repo', '--lock-wait', '5'), ('borg', 'list', '--short', 'repo', '--lock-wait', '5'),
@ -101,3 +101,79 @@ def test_extract_last_archive_dry_run_should_call_borg_with_lock_wait_parameters
insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2', '--lock-wait', '5')) insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2', '--lock-wait', '5'))
module.extract_last_archive_dry_run(repository='repo', lock_wait=5) module.extract_last_archive_dry_run(repository='repo', lock_wait=5)
def test_extract_archive_calls_borg_with_remote_path_parameters():
insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--remote-path', 'borg1'))
module.extract_archive(
dry_run=False,
repository='repo',
archive='archive',
restore_paths=None,
storage_config={},
remote_path='borg1',
)
def test_extract_archive_calls_borg_with_umask_parameters():
insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--umask', '0770'))
module.extract_archive(
dry_run=False,
repository='repo',
archive='archive',
restore_paths=None,
storage_config={'umask': '0770'},
)
def test_extract_archive_calls_borg_with_lock_wait_parameters():
insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--lock-wait', '5'))
module.extract_archive(
dry_run=False,
repository='repo',
archive='archive',
restore_paths=None,
storage_config={'lock_wait': '5'},
)
def test_extract_archive_with_log_info_calls_borg_with_info_parameter():
insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--info'))
insert_logging_mock(logging.INFO)
module.extract_archive(
dry_run=False, repository='repo', archive='archive', restore_paths=None, storage_config={}
)
def test_extract_archive_with_log_debug_calls_borg_with_debug_parameters():
insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--debug', '--list', '--show-rc'))
insert_logging_mock(logging.DEBUG)
module.extract_archive(
dry_run=False, repository='repo', archive='archive', restore_paths=None, storage_config={}
)
def test_extract_archive_calls_borg_with_dry_run_parameter():
insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--dry-run'))
module.extract_archive(
dry_run=True, repository='repo', archive='archive', restore_paths=None, storage_config={}
)
def test_extract_archive_calls_borg_with_progress_parameter():
insert_subprocess_mock(('borg', 'extract', 'repo::archive', '--progress'))
module.extract_archive(
dry_run=False,
repository='repo',
archive='archive',
restore_paths=None,
storage_config={},
progress=True,
)