2018-07-28 23:21:38 +02:00
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
|
2018-07-29 00:02:17 +02:00
|
|
|
from flexmock import flexmock
|
|
|
|
|
2018-12-26 00:23:54 +01:00
|
|
|
from borgmatic.commands import borgmatic as module
|
2018-07-29 00:02:17 +02:00
|
|
|
|
2018-07-28 23:21:38 +02:00
|
|
|
|
2018-10-14 00:19:16 +02:00
|
|
|
def test_run_commands_handles_multiple_json_outputs_in_array():
|
2018-07-29 00:02:17 +02:00
|
|
|
(
|
2018-12-26 00:23:54 +01:00
|
|
|
flexmock(module)
|
2018-07-29 00:02:17 +02:00
|
|
|
.should_receive('_run_commands_on_repository')
|
|
|
|
.times(3)
|
|
|
|
.replace_with(
|
2018-09-30 07:45:00 +02:00
|
|
|
lambda args, consistency, json_results, local_path, location, remote_path, retention, storage, unexpanded_repository: json_results.append(
|
|
|
|
{"whatever": unexpanded_repository}
|
|
|
|
)
|
2018-07-29 00:02:17 +02:00
|
|
|
)
|
|
|
|
)
|
2018-07-28 23:21:38 +02:00
|
|
|
|
2018-07-29 00:02:17 +02:00
|
|
|
(
|
|
|
|
flexmock(sys.stdout)
|
|
|
|
.should_call("write")
|
|
|
|
.with_args(
|
|
|
|
json.dumps(
|
|
|
|
json.loads(
|
|
|
|
'''
|
|
|
|
[
|
|
|
|
{"whatever": "fake_repo1"},
|
|
|
|
{"whatever": "fake_repo2"},
|
|
|
|
{"whatever": "fake_repo3"}
|
|
|
|
]
|
2018-09-30 07:45:00 +02:00
|
|
|
'''
|
2018-07-29 00:02:17 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2018-07-28 23:21:38 +02:00
|
|
|
|
2018-12-26 00:23:54 +01:00
|
|
|
module._run_commands(
|
2018-07-29 00:02:17 +02:00
|
|
|
args=flexmock(json=True),
|
|
|
|
consistency=None,
|
|
|
|
local_path=None,
|
2018-09-30 07:45:00 +02:00
|
|
|
location={'repositories': ['fake_repo1', 'fake_repo2', 'fake_repo3']},
|
2018-07-29 00:02:17 +02:00
|
|
|
remote_path=None,
|
|
|
|
retention=None,
|
|
|
|
storage=None,
|
|
|
|
)
|
2018-12-26 00:23:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_collect_configuration_run_summary_logs_info_for_success():
|
2019-02-19 06:43:30 +01:00
|
|
|
flexmock(module.validate).should_receive('parse_configuration').and_return({'test.yaml': {}})
|
|
|
|
flexmock(module).should_receive('run_configuration')
|
|
|
|
args = flexmock(extract=False)
|
|
|
|
|
|
|
|
logs = tuple(module.collect_configuration_run_summary_logs(('test.yaml',), args=args))
|
|
|
|
|
|
|
|
assert any(log for log in logs if log.levelno == module.logging.INFO)
|
|
|
|
|
|
|
|
|
2019-02-19 06:52:56 +01:00
|
|
|
def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
|
2019-02-18 18:30:34 +01:00
|
|
|
flexmock(module.validate).should_receive('parse_configuration').and_return({'test.yaml': {}})
|
2019-02-18 21:58:39 +01:00
|
|
|
flexmock(module.validate).should_receive('guard_configuration_contains_repository')
|
2018-12-26 00:23:54 +01:00
|
|
|
flexmock(module).should_receive('run_configuration')
|
2019-02-19 06:43:30 +01:00
|
|
|
args = flexmock(extract=True, repository='repo')
|
2018-12-26 00:23:54 +01:00
|
|
|
|
2019-02-18 21:58:39 +01:00
|
|
|
logs = tuple(module.collect_configuration_run_summary_logs(('test.yaml',), args=args))
|
2018-12-26 00:23:54 +01:00
|
|
|
|
|
|
|
assert any(log for log in logs if log.levelno == module.logging.INFO)
|
|
|
|
|
|
|
|
|
2019-02-19 06:52:56 +01:00
|
|
|
def test_collect_configuration_run_summary_logs_critical_for_extract_with_repository_error():
|
|
|
|
flexmock(module.validate).should_receive('parse_configuration').and_return({'test.yaml': {}})
|
|
|
|
flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
|
|
|
|
ValueError
|
|
|
|
)
|
|
|
|
args = flexmock(extract=True, repository='repo')
|
|
|
|
|
|
|
|
logs = tuple(module.collect_configuration_run_summary_logs(('test.yaml',), args=args))
|
|
|
|
|
|
|
|
assert any(log for log in logs if log.levelno == module.logging.CRITICAL)
|
|
|
|
|
|
|
|
|
2019-02-18 18:52:56 +01:00
|
|
|
def test_collect_configuration_run_summary_logs_critical_for_parse_error():
|
|
|
|
flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
|
2019-02-19 06:43:30 +01:00
|
|
|
args = flexmock(extract=False)
|
2019-02-18 18:52:56 +01:00
|
|
|
|
2019-02-18 21:58:39 +01:00
|
|
|
logs = tuple(module.collect_configuration_run_summary_logs(('test.yaml',), args=args))
|
2019-02-18 18:52:56 +01:00
|
|
|
|
|
|
|
assert any(log for log in logs if log.levelno == module.logging.CRITICAL)
|
|
|
|
|
|
|
|
|
|
|
|
def test_collect_configuration_run_summary_logs_critical_for_run_error():
|
2019-02-18 18:30:34 +01:00
|
|
|
flexmock(module.validate).should_receive('parse_configuration').and_return({'test.yaml': {}})
|
2019-02-18 21:58:39 +01:00
|
|
|
flexmock(module.validate).should_receive('guard_configuration_contains_repository')
|
2018-12-26 00:23:54 +01:00
|
|
|
flexmock(module).should_receive('run_configuration').and_raise(ValueError)
|
2019-02-19 06:43:30 +01:00
|
|
|
args = flexmock(extract=False)
|
2018-12-26 00:23:54 +01:00
|
|
|
|
2019-02-18 21:58:39 +01:00
|
|
|
logs = tuple(module.collect_configuration_run_summary_logs(('test.yaml',), args=args))
|
2018-12-26 00:23:54 +01:00
|
|
|
|
|
|
|
assert any(log for log in logs if log.levelno == module.logging.CRITICAL)
|
|
|
|
|
|
|
|
|
|
|
|
def test_collect_configuration_run_summary_logs_critical_for_missing_configs():
|
2019-02-18 18:30:34 +01:00
|
|
|
flexmock(module.validate).should_receive('parse_configuration').and_return({'test.yaml': {}})
|
2019-02-18 21:58:39 +01:00
|
|
|
flexmock(module).should_receive('run_configuration')
|
2019-02-19 06:43:30 +01:00
|
|
|
args = flexmock(config_paths=(), extract=False)
|
2019-02-18 18:30:34 +01:00
|
|
|
|
2019-02-18 21:58:39 +01:00
|
|
|
logs = tuple(module.collect_configuration_run_summary_logs(config_filenames=(), args=args))
|
2018-12-26 00:23:54 +01:00
|
|
|
|
|
|
|
assert any(log for log in logs if log.levelno == module.logging.CRITICAL)
|