2018-09-30 08:15:18 +02:00
|
|
|
import logging
|
|
|
|
import sys
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
from borgmatic.borg import extract as module
|
2018-09-30 22:57:20 +02:00
|
|
|
from ..test_verbosity import insert_logging_mock
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
def insert_subprocess_mock(check_call_command, **kwargs):
|
|
|
|
subprocess = flexmock(module.subprocess)
|
|
|
|
subprocess.should_receive('check_call').with_args(check_call_command, **kwargs).once()
|
|
|
|
|
|
|
|
|
|
|
|
def insert_subprocess_never():
|
|
|
|
subprocess = flexmock(module.subprocess)
|
|
|
|
subprocess.should_receive('check_call').never()
|
|
|
|
|
|
|
|
|
|
|
|
def insert_subprocess_check_output_mock(check_output_command, result, **kwargs):
|
|
|
|
subprocess = flexmock(module.subprocess)
|
2018-09-30 07:45:00 +02:00
|
|
|
subprocess.should_receive('check_output').with_args(check_output_command, **kwargs).and_return(
|
|
|
|
result
|
|
|
|
).once()
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_extract_last_archive_dry_run_should_call_borg_with_last_archive():
|
|
|
|
flexmock(sys.stdout).encoding = 'utf-8'
|
|
|
|
insert_subprocess_check_output_mock(
|
2018-09-30 07:45:00 +02:00
|
|
|
('borg', 'list', '--short', 'repo'), result='archive1\narchive2\n'.encode('utf-8')
|
2017-08-06 01:21:39 +02:00
|
|
|
)
|
2018-09-30 07:45:00 +02:00
|
|
|
insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2'))
|
2017-08-06 01:21:39 +02:00
|
|
|
|
2018-09-30 07:45:00 +02:00
|
|
|
module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_extract_last_archive_dry_run_without_any_archives_should_bail():
|
|
|
|
flexmock(sys.stdout).encoding = 'utf-8'
|
|
|
|
insert_subprocess_check_output_mock(
|
2018-09-30 07:45:00 +02:00
|
|
|
('borg', 'list', '--short', 'repo'), result='\n'.encode('utf-8')
|
2017-08-06 01:21:39 +02:00
|
|
|
)
|
|
|
|
insert_subprocess_never()
|
|
|
|
|
2018-09-30 07:45:00 +02:00
|
|
|
module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
|
2018-09-08 22:53:37 +02:00
|
|
|
def test_extract_last_archive_dry_run_with_log_info_should_call_borg_with_info_parameter():
|
2017-08-06 01:21:39 +02:00
|
|
|
flexmock(sys.stdout).encoding = 'utf-8'
|
|
|
|
insert_subprocess_check_output_mock(
|
2018-09-30 07:45:00 +02:00
|
|
|
('borg', 'list', '--short', 'repo', '--info'), result='archive1\narchive2\n'.encode('utf-8')
|
2017-08-06 01:21:39 +02:00
|
|
|
)
|
2018-09-30 07:45:00 +02:00
|
|
|
insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2', '--info'))
|
2018-09-08 22:53:37 +02:00
|
|
|
insert_logging_mock(logging.INFO)
|
2017-08-06 01:21:39 +02:00
|
|
|
|
2018-09-30 07:45:00 +02:00
|
|
|
module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
|
2018-09-08 22:53:37 +02:00
|
|
|
def test_extract_last_archive_dry_run_with_log_debug_should_call_borg_with_debug_parameter():
|
2017-08-06 01:21:39 +02:00
|
|
|
flexmock(sys.stdout).encoding = 'utf-8'
|
|
|
|
insert_subprocess_check_output_mock(
|
2018-08-19 21:44:40 +02:00
|
|
|
('borg', 'list', '--short', 'repo', '--debug', '--show-rc'),
|
2017-08-06 01:21:39 +02:00
|
|
|
result='archive1\narchive2\n'.encode('utf-8'),
|
|
|
|
)
|
|
|
|
insert_subprocess_mock(
|
2018-09-30 07:45:00 +02:00
|
|
|
('borg', 'extract', '--dry-run', 'repo::archive2', '--debug', '--show-rc', '--list')
|
2017-08-06 01:21:39 +02:00
|
|
|
)
|
2018-09-08 22:53:37 +02:00
|
|
|
insert_logging_mock(logging.DEBUG)
|
2017-08-06 01:21:39 +02:00
|
|
|
|
2018-09-30 07:45:00 +02:00
|
|
|
module.extract_last_archive_dry_run(repository='repo', lock_wait=None)
|
2017-08-06 01:21:39 +02:00
|
|
|
|
|
|
|
|
2018-01-15 01:35:24 +01:00
|
|
|
def test_extract_last_archive_dry_run_should_call_borg_via_local_path():
|
|
|
|
flexmock(sys.stdout).encoding = 'utf-8'
|
|
|
|
insert_subprocess_check_output_mock(
|
2018-09-30 07:45:00 +02:00
|
|
|
('borg1', 'list', '--short', 'repo'), result='archive1\narchive2\n'.encode('utf-8')
|
2018-01-15 01:35:24 +01:00
|
|
|
)
|
2018-09-30 07:45:00 +02:00
|
|
|
insert_subprocess_mock(('borg1', 'extract', '--dry-run', 'repo::archive2'))
|
2018-01-15 01:35:24 +01:00
|
|
|
|
2018-09-30 07:45:00 +02:00
|
|
|
module.extract_last_archive_dry_run(repository='repo', lock_wait=None, local_path='borg1')
|
2018-01-15 01:35:24 +01:00
|
|
|
|
|
|
|
|
2017-08-06 01:21:39 +02:00
|
|
|
def test_extract_last_archive_dry_run_should_call_borg_with_remote_path_parameters():
|
|
|
|
flexmock(sys.stdout).encoding = 'utf-8'
|
|
|
|
insert_subprocess_check_output_mock(
|
|
|
|
('borg', 'list', '--short', 'repo', '--remote-path', 'borg1'),
|
|
|
|
result='archive1\narchive2\n'.encode('utf-8'),
|
|
|
|
)
|
|
|
|
insert_subprocess_mock(
|
2018-09-30 07:45:00 +02:00
|
|
|
('borg', 'extract', '--dry-run', 'repo::archive2', '--remote-path', 'borg1')
|
2017-08-06 01:21:39 +02:00
|
|
|
)
|
|
|
|
|
2018-09-30 07:45:00 +02:00
|
|
|
module.extract_last_archive_dry_run(repository='repo', lock_wait=None, remote_path='borg1')
|
2018-02-20 00:51:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_extract_last_archive_dry_run_should_call_borg_with_lock_wait_parameters():
|
|
|
|
flexmock(sys.stdout).encoding = 'utf-8'
|
|
|
|
insert_subprocess_check_output_mock(
|
|
|
|
('borg', 'list', '--short', 'repo', '--lock-wait', '5'),
|
|
|
|
result='archive1\narchive2\n'.encode('utf-8'),
|
|
|
|
)
|
2018-09-30 07:45:00 +02:00
|
|
|
insert_subprocess_mock(('borg', 'extract', '--dry-run', 'repo::archive2', '--lock-wait', '5'))
|
2018-02-20 00:51:04 +01:00
|
|
|
|
2018-09-30 07:45:00 +02:00
|
|
|
module.extract_last_archive_dry_run(repository='repo', lock_wait=5)
|