some tests remaining

This commit is contained in:
Divyansh Singh 2023-06-07 23:56:02 +05:30
parent dc56fd33a0
commit dcb90bba50
3 changed files with 76 additions and 7 deletions

View file

@ -3,7 +3,33 @@ from flexmock import flexmock
from borgmatic.actions.config import bootstrap as module from borgmatic.actions.config import bootstrap as module
def test_run_bootstrap(): def test_get_config_paths_returns_list_of_config_paths():
bootstrap_arguments = flexmock(
borgmatic_source_directory=None,
repository='repo',
archive='archive',
)
global_arguments = flexmock(
dry_run=False,
)
local_borg_version = flexmock()
extract_process = flexmock(
stdout=flexmock(
read=lambda: '{"config_paths": ["/borgmatic/config.yaml"]}',
),
)
flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
extract_process
)
flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
'archive'
)
assert module.get_config_paths(
bootstrap_arguments, global_arguments, local_borg_version
) == ['/borgmatic/config.yaml']
def test_run_bootstrap_does_not_raise():
bootstrap_arguments = flexmock( bootstrap_arguments = flexmock(
repository='repo', repository='repo',
archive='archive', archive='archive',
@ -23,11 +49,8 @@ def test_run_bootstrap():
) )
flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return( flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
extract_process extract_process
) ).twice()
flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return( flexmock(module.borgmatic.borg.rlist).should_receive('resolve_archive_name').and_return(
'archive' 'archive'
) )
flexmock(module.borgmatic.borg.extract).should_receive('extract_archive').and_return(
extract_process
)
module.run_bootstrap(bootstrap_arguments, global_arguments, local_borg_version) module.run_bootstrap(bootstrap_arguments, global_arguments, local_borg_version)

View file

@ -1,3 +1,4 @@
import sys
from flexmock import flexmock from flexmock import flexmock
from borgmatic.actions import create as module from borgmatic.actions import create as module
@ -7,6 +8,7 @@ def test_run_create_executes_and_calls_hooks_for_configured_repository():
flexmock(module.logger).answer = lambda message: None flexmock(module.logger).answer = lambda message: None
flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never() flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
flexmock(module.borgmatic.borg.create).should_receive('create_archive').once() flexmock(module.borgmatic.borg.create).should_receive('create_archive').once()
flexmock(module).should_receive('create_borgmatic_manifest').once()
flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2) 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').and_return({})
flexmock(module.borgmatic.hooks.dispatch).should_receive( flexmock(module.borgmatic.hooks.dispatch).should_receive(
@ -45,6 +47,7 @@ def test_run_create_runs_with_selected_repository():
'repositories_match' 'repositories_match'
).once().and_return(True) ).once().and_return(True)
flexmock(module.borgmatic.borg.create).should_receive('create_archive').once() flexmock(module.borgmatic.borg.create).should_receive('create_archive').once()
flexmock(module).should_receive('create_borgmatic_manifest').once()
create_arguments = flexmock( create_arguments = flexmock(
repository=flexmock(), repository=flexmock(),
progress=flexmock(), progress=flexmock(),
@ -78,6 +81,7 @@ def test_run_create_bails_if_repository_does_not_match():
'repositories_match' 'repositories_match'
).once().and_return(False) ).once().and_return(False)
flexmock(module.borgmatic.borg.create).should_receive('create_archive').never() flexmock(module.borgmatic.borg.create).should_receive('create_archive').never()
flexmock(module).should_receive('create_borgmatic_manifest').never()
create_arguments = flexmock( create_arguments = flexmock(
repository=flexmock(), repository=flexmock(),
progress=flexmock(), progress=flexmock(),
@ -106,16 +110,40 @@ def test_run_create_bails_if_repository_does_not_match():
def test_create_borgmatic_manifest_creates_manifest_file(): def test_create_borgmatic_manifest_creates_manifest_file():
flexmock(module.os.path).should_receive('expanduser').and_return('/home/user')
flexmock(module.os.path).should_receive('join').and_return('/home/user/bootstrap/manifest.json')
flexmock(module.os.path).should_receive('exists').and_return(False) flexmock(module.os.path).should_receive('exists').and_return(False)
flexmock(module.os).should_receive('makedirs').and_return(True) flexmock(module.os).should_receive('makedirs').and_return(True)
flexmock(module.importlib_metadata).should_receive('version').and_return('1.0.0')
flexmock(module.json).should_receive('dump').and_return(True) flexmock(module.json).should_receive('dump').and_return(True)
module.create_borgmatic_manifest({}, 'test.yaml', False) module.create_borgmatic_manifest({}, 'test.yaml', False)
def test_create_borgmatic_manifest_creates_manifest_file_with_custom_borgmatic_source_directory():
flexmock(module.os.path).should_receive('join').with_args(
'/borgmatic', 'bootstrap', 'manifest.json'
).and_return('/borgmatic/bootstrap/manifest.json')
flexmock(module.os.path).should_receive('exists').and_return(False)
flexmock(module.os).should_receive('makedirs').and_return(True)
flexmock(module.importlib_metadata).should_receive('version').and_return('1.0.0')
flexmock(sys.modules['builtins']).should_receive('open').with_args(
'/borgmatic/bootstrap/manifest.json', 'w'
).and_return(
flexmock(
__enter__=lambda *args: flexmock(
write=lambda *args: None, close=lambda *args: None
),
__exit__=lambda *args: None,
)
)
flexmock(module.json).should_receive('dump').and_return(True)
module.create_borgmatic_manifest(
{'borgmatic_source_directory': '/borgmatic'}, 'test.yaml', False
)
def test_create_borgmatic_manifest_does_not_create_manifest_file_on_dry_run(): def test_create_borgmatic_manifest_does_not_create_manifest_file_on_dry_run():
flexmock(module.os.path).should_receive('expanduser').never() flexmock(module.os.path).should_receive('expanduser').never()

View file

@ -1002,6 +1002,24 @@ def test_collect_configuration_run_summary_logs_info_for_success_with_bootstrap(
) )
assert {log.levelno for log in logs} == {logging.INFO} assert {log.levelno for log in logs} == {logging.INFO}
def test_collect_configuration_run_summary_logs_error_on_bootstrap_failure():
flexmock(module.validate).should_receive('guard_single_repository_selected').never()
flexmock(module.validate).should_receive('guard_configuration_contains_repository').never()
flexmock(module).should_receive('run_configuration').never()
flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
ValueError
)
arguments = {
'bootstrap': flexmock(repository='repo'),
'global': flexmock(monitoring_verbosity=1, dry_run=False),
}
logs = tuple(
module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
)
assert {log.levelno for log in logs} == {logging.CRITICAL}
def test_collect_configuration_run_summary_logs_extract_with_repository_error(): def test_collect_configuration_run_summary_logs_extract_with_repository_error():
flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise( flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(