|
@@ -4,6 +4,7 @@ import sys
|
|
|
import os
|
|
|
|
|
|
from flexmock import flexmock
|
|
|
+import pytest
|
|
|
|
|
|
from borgmatic import borg as module
|
|
|
from borgmatic.verbosity import VERBOSITY_SOME, VERBOSITY_LOTS
|
|
@@ -46,17 +47,23 @@ def test_write_exclude_file_with_empty_exclude_patterns_does_not_raise():
|
|
|
|
|
|
|
|
|
def insert_subprocess_mock(check_call_command, **kwargs):
|
|
|
- subprocess = flexmock(STDOUT=STDOUT)
|
|
|
+ subprocess = flexmock(module.subprocess)
|
|
|
subprocess.should_receive('check_call').with_args(check_call_command, **kwargs).once()
|
|
|
flexmock(module).subprocess = subprocess
|
|
|
|
|
|
|
|
|
def insert_subprocess_never():
|
|
|
- subprocess = flexmock()
|
|
|
+ subprocess = flexmock(module.subprocess)
|
|
|
subprocess.should_receive('check_call').never()
|
|
|
flexmock(module).subprocess = subprocess
|
|
|
|
|
|
|
|
|
+def insert_subprocess_check_output_mock(check_output_command, result, **kwargs):
|
|
|
+ subprocess = flexmock(module.subprocess)
|
|
|
+ subprocess.should_receive('check_output').with_args(check_output_command, **kwargs).and_return(result).once()
|
|
|
+ flexmock(module).subprocess = subprocess
|
|
|
+
|
|
|
+
|
|
|
def insert_platform_mock():
|
|
|
flexmock(module.platform).should_receive('node').and_return('host')
|
|
|
|
|
@@ -395,9 +402,15 @@ def test_parse_checks_with_disabled_returns_no_checks():
|
|
|
|
|
|
|
|
|
def test_make_check_flags_with_checks_returns_flags():
|
|
|
- flags = module._make_check_flags(('foo', 'bar'))
|
|
|
+ flags = module._make_check_flags(('repository',))
|
|
|
+
|
|
|
+ assert flags == ('--repository-only',)
|
|
|
+
|
|
|
|
|
|
- assert flags == ('--foo-only', '--bar-only')
|
|
|
+def test_make_check_flags_with_extract_check_does_not_make_extract_flag():
|
|
|
+ flags = module._make_check_flags(('extract',))
|
|
|
+
|
|
|
+ assert flags == ()
|
|
|
|
|
|
|
|
|
def test_make_check_flags_with_default_checks_returns_no_flags():
|
|
@@ -407,19 +420,27 @@ def test_make_check_flags_with_default_checks_returns_no_flags():
|
|
|
|
|
|
|
|
|
def test_make_check_flags_with_checks_and_last_returns_flags_including_last():
|
|
|
- flags = module._make_check_flags(('foo', 'bar'), check_last=3)
|
|
|
+ flags = module._make_check_flags(('repository',), check_last=3)
|
|
|
|
|
|
- assert flags == ('--foo-only', '--bar-only', '--last', '3')
|
|
|
+ assert flags == ('--repository-only', '--last', '3')
|
|
|
|
|
|
|
|
|
-def test_make_check_flags_with_last_returns_last_flag():
|
|
|
+def test_make_check_flags_with_default_checks_and_last_returns_last_flag():
|
|
|
flags = module._make_check_flags(module.DEFAULT_CHECKS, check_last=3)
|
|
|
|
|
|
assert flags == ('--last', '3')
|
|
|
|
|
|
|
|
|
-def test_check_archives_should_call_borg_with_parameters():
|
|
|
- checks = flexmock()
|
|
|
+@pytest.mark.parametrize(
|
|
|
+ 'checks',
|
|
|
+ (
|
|
|
+ ('repository',),
|
|
|
+ ('archives',),
|
|
|
+ ('repository', 'archives'),
|
|
|
+ ('repository', 'archives', 'other'),
|
|
|
+ ),
|
|
|
+)
|
|
|
+def test_check_archives_should_call_borg_with_parameters(checks):
|
|
|
check_last = flexmock()
|
|
|
consistency_config = flexmock().should_receive('get').and_return(check_last).mock
|
|
|
flexmock(module).should_receive('_parse_checks').and_return(checks)
|
|
@@ -442,9 +463,27 @@ def test_check_archives_should_call_borg_with_parameters():
|
|
|
)
|
|
|
|
|
|
|
|
|
+def test_check_archives_with_extract_check_should_call_extract_only():
|
|
|
+ checks = ('extract',)
|
|
|
+ check_last = flexmock()
|
|
|
+ consistency_config = flexmock().should_receive('get').and_return(check_last).mock
|
|
|
+ flexmock(module).should_receive('_parse_checks').and_return(checks)
|
|
|
+ flexmock(module).should_receive('_make_check_flags').never()
|
|
|
+ flexmock(module).should_receive('extract_last_archive_dry_run').once()
|
|
|
+ insert_subprocess_never()
|
|
|
+
|
|
|
+ module.check_archives(
|
|
|
+ verbosity=None,
|
|
|
+ repository='repo',
|
|
|
+ consistency_config=consistency_config,
|
|
|
+ command='borg',
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
def test_check_archives_with_verbosity_some_should_call_borg_with_info_parameter():
|
|
|
+ checks = ('repository',)
|
|
|
consistency_config = flexmock().should_receive('get').and_return(None).mock
|
|
|
- flexmock(module).should_receive('_parse_checks').and_return(flexmock())
|
|
|
+ flexmock(module).should_receive('_parse_checks').and_return(checks)
|
|
|
flexmock(module).should_receive('_make_check_flags').and_return(())
|
|
|
insert_subprocess_mock(
|
|
|
('borg', 'check', 'repo', '--info'),
|
|
@@ -462,8 +501,9 @@ def test_check_archives_with_verbosity_some_should_call_borg_with_info_parameter
|
|
|
|
|
|
|
|
|
def test_check_archives_with_verbosity_lots_should_call_borg_with_debug_parameter():
|
|
|
+ checks = ('repository',)
|
|
|
consistency_config = flexmock().should_receive('get').and_return(None).mock
|
|
|
- flexmock(module).should_receive('_parse_checks').and_return(flexmock())
|
|
|
+ flexmock(module).should_receive('_parse_checks').and_return(checks)
|
|
|
flexmock(module).should_receive('_make_check_flags').and_return(())
|
|
|
insert_subprocess_mock(
|
|
|
('borg', 'check', 'repo', '--debug'),
|
|
@@ -494,7 +534,7 @@ def test_check_archives_without_any_checks_should_bail():
|
|
|
|
|
|
|
|
|
def test_check_archives_with_remote_path_should_call_borg_with_remote_path_parameters():
|
|
|
- checks = flexmock()
|
|
|
+ checks = ('repository',)
|
|
|
check_last = flexmock()
|
|
|
consistency_config = flexmock().should_receive('get').and_return(check_last).mock
|
|
|
flexmock(module).should_receive('_parse_checks').and_return(checks)
|
|
@@ -516,3 +556,87 @@ def test_check_archives_with_remote_path_should_call_borg_with_remote_path_param
|
|
|
command='borg',
|
|
|
remote_path='borg1',
|
|
|
)
|
|
|
+
|
|
|
+
|
|
|
+def test_extract_last_archive_dry_run_should_call_borg_with_last_archive():
|
|
|
+ flexmock(sys.stdout).encoding = 'utf-8'
|
|
|
+ insert_subprocess_check_output_mock(
|
|
|
+ ('borg', 'list', '--short', 'repo'),
|
|
|
+ result='archive1\narchive2\n'.encode('utf-8'),
|
|
|
+ )
|
|
|
+ insert_subprocess_mock(
|
|
|
+ ('borg', 'extract', '--dry-run', 'repo::archive2'),
|
|
|
+ )
|
|
|
+
|
|
|
+ module.extract_last_archive_dry_run(
|
|
|
+ verbosity=None,
|
|
|
+ repository='repo',
|
|
|
+ command='borg',
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+def test_extract_last_archive_dry_run_without_any_archives_should_bail():
|
|
|
+ flexmock(sys.stdout).encoding = 'utf-8'
|
|
|
+ insert_subprocess_check_output_mock(
|
|
|
+ ('borg', 'list', '--short', 'repo'),
|
|
|
+ result='\n'.encode('utf-8'),
|
|
|
+ )
|
|
|
+ insert_subprocess_never()
|
|
|
+
|
|
|
+ module.extract_last_archive_dry_run(
|
|
|
+ verbosity=None,
|
|
|
+ repository='repo',
|
|
|
+ command='borg',
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+def test_extract_last_archive_dry_run_with_verbosity_some_should_call_borg_with_info_parameter():
|
|
|
+ flexmock(sys.stdout).encoding = 'utf-8'
|
|
|
+ insert_subprocess_check_output_mock(
|
|
|
+ ('borg', 'list', '--short', 'repo', '--info'),
|
|
|
+ result='archive1\narchive2\n'.encode('utf-8'),
|
|
|
+ )
|
|
|
+ insert_subprocess_mock(
|
|
|
+ ('borg', 'extract', '--dry-run', 'repo::archive2', '--info'),
|
|
|
+ )
|
|
|
+
|
|
|
+ module.extract_last_archive_dry_run(
|
|
|
+ verbosity=VERBOSITY_SOME,
|
|
|
+ repository='repo',
|
|
|
+ command='borg',
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+def test_extract_last_archive_dry_run_with_verbosity_lots_should_call_borg_with_debug_parameter():
|
|
|
+ flexmock(sys.stdout).encoding = 'utf-8'
|
|
|
+ insert_subprocess_check_output_mock(
|
|
|
+ ('borg', 'list', '--short', 'repo', '--debug'),
|
|
|
+ result='archive1\narchive2\n'.encode('utf-8'),
|
|
|
+ )
|
|
|
+ insert_subprocess_mock(
|
|
|
+ ('borg', 'extract', '--dry-run', 'repo::archive2', '--debug', '--list'),
|
|
|
+ )
|
|
|
+
|
|
|
+ module.extract_last_archive_dry_run(
|
|
|
+ verbosity=VERBOSITY_LOTS,
|
|
|
+ repository='repo',
|
|
|
+ command='borg',
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
+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(
|
|
|
+ ('borg', 'extract', '--dry-run', 'repo::archive2', '--remote-path', 'borg1'),
|
|
|
+ )
|
|
|
+
|
|
|
+ module.extract_last_archive_dry_run(
|
|
|
+ verbosity=None,
|
|
|
+ repository='repo',
|
|
|
+ command='borg',
|
|
|
+ remote_path='borg1',
|
|
|
+ )
|