2023-06-04 06:50:35 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from io import StringIO
|
|
|
|
|
2017-10-30 03:36:26 +01:00
|
|
|
import pytest
|
2019-12-07 01:29:41 +01:00
|
|
|
from flexmock import flexmock
|
2017-10-30 03:36:26 +01:00
|
|
|
|
|
|
|
from borgmatic.config import validate as module
|
|
|
|
|
|
|
|
|
2023-04-15 06:21:25 +02:00
|
|
|
def test_schema_filename_finds_schema_path():
|
2023-06-04 06:50:35 +02:00
|
|
|
schema_path = '/var/borgmatic/config/schema.yaml'
|
|
|
|
|
2023-06-04 07:07:24 +02:00
|
|
|
flexmock(os.path).should_receive('dirname').and_return('/var/borgmatic/config')
|
2023-06-04 06:50:35 +02:00
|
|
|
builtins = flexmock(sys.modules['builtins'])
|
|
|
|
builtins.should_receive('open').with_args(schema_path).and_return(StringIO())
|
|
|
|
assert module.schema_filename() == schema_path
|
|
|
|
|
|
|
|
|
|
|
|
def test_schema_filename_raises_filenotfounderror():
|
|
|
|
schema_path = '/var/borgmatic/config/schema.yaml'
|
|
|
|
|
2023-06-04 07:07:24 +02:00
|
|
|
flexmock(os.path).should_receive('dirname').and_return('/var/borgmatic/config')
|
2023-06-04 06:50:35 +02:00
|
|
|
builtins = flexmock(sys.modules['builtins'])
|
|
|
|
builtins.should_receive('open').with_args(schema_path).and_raise(FileNotFoundError)
|
|
|
|
|
|
|
|
with pytest.raises(FileNotFoundError):
|
|
|
|
module.schema_filename()
|
2023-04-15 06:21:25 +02:00
|
|
|
|
|
|
|
|
2022-01-20 05:39:03 +01:00
|
|
|
def test_format_json_error_path_element_formats_array_index():
|
|
|
|
module.format_json_error_path_element(3) == '[3]'
|
2021-06-22 22:27:59 +02:00
|
|
|
|
|
|
|
|
2022-01-20 05:39:03 +01:00
|
|
|
def test_format_json_error_path_element_formats_property():
|
|
|
|
module.format_json_error_path_element('foo') == '.foo'
|
2021-06-22 22:27:59 +02:00
|
|
|
|
|
|
|
|
2022-01-20 05:39:03 +01:00
|
|
|
def test_format_json_error_formats_error_including_path():
|
2023-03-24 07:11:14 +01:00
|
|
|
flexmock(module).format_json_error_path_element = lambda element: f'.{element}'
|
2021-06-22 22:27:59 +02:00
|
|
|
error = flexmock(message='oops', path=['foo', 'bar'])
|
|
|
|
|
2022-01-20 05:39:03 +01:00
|
|
|
assert module.format_json_error(error) == "At 'foo.bar': oops"
|
2021-06-22 22:27:59 +02:00
|
|
|
|
|
|
|
|
2022-01-20 05:39:03 +01:00
|
|
|
def test_format_json_error_formats_error_without_path():
|
|
|
|
flexmock(module).should_receive('format_json_error_path_element').never()
|
2021-06-22 22:27:59 +02:00
|
|
|
error = flexmock(message='oops', path=[])
|
|
|
|
|
2022-01-20 05:39:03 +01:00
|
|
|
assert module.format_json_error(error) == 'At the top level: oops'
|
2021-06-22 22:27:59 +02:00
|
|
|
|
|
|
|
|
2022-01-20 05:39:03 +01:00
|
|
|
def test_validation_error_string_contains_errors():
|
|
|
|
flexmock(module).format_json_error = lambda error: error.message
|
|
|
|
error = module.Validation_error('config.yaml', ('oops', 'uh oh'))
|
2017-10-30 03:36:26 +01:00
|
|
|
|
|
|
|
result = str(error)
|
|
|
|
|
|
|
|
assert 'config.yaml' in result
|
|
|
|
assert 'oops' in result
|
|
|
|
assert 'uh oh' in result
|
|
|
|
|
|
|
|
|
2023-03-28 20:45:55 +02:00
|
|
|
def test_apply_logical_validation_raises_if_unknown_repository_in_check_repositories():
|
2022-01-20 05:39:03 +01:00
|
|
|
flexmock(module).format_json_error = lambda error: error.message
|
2021-06-22 22:27:59 +02:00
|
|
|
|
2018-10-14 05:34:51 +02:00
|
|
|
with pytest.raises(module.Validation_error):
|
|
|
|
module.apply_logical_validation(
|
|
|
|
'config.yaml',
|
|
|
|
{
|
2023-07-09 08:14:30 +02:00
|
|
|
'repositories': ['repo.borg', 'other.borg'],
|
|
|
|
'keep_secondly': 1000,
|
|
|
|
'check_repositories': ['repo.borg', 'unknown.borg'],
|
2018-10-14 05:34:51 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-28 20:45:55 +02:00
|
|
|
def test_apply_logical_validation_does_not_raise_if_known_repository_path_in_check_repositories():
|
2018-10-14 05:34:51 +02:00
|
|
|
module.apply_logical_validation(
|
|
|
|
'config.yaml',
|
|
|
|
{
|
2023-07-09 08:14:30 +02:00
|
|
|
'repositories': [{'path': 'repo.borg'}, {'path': 'other.borg'}],
|
|
|
|
'keep_secondly': 1000,
|
|
|
|
'check_repositories': ['repo.borg'],
|
2018-10-14 05:34:51 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-28 20:45:55 +02:00
|
|
|
def test_apply_logical_validation_does_not_raise_if_known_repository_label_in_check_repositories():
|
2023-03-28 19:21:35 +02:00
|
|
|
module.apply_logical_validation(
|
|
|
|
'config.yaml',
|
|
|
|
{
|
2023-07-09 08:14:30 +02:00
|
|
|
'repositories': [
|
|
|
|
{'path': 'repo.borg', 'label': 'my_repo'},
|
|
|
|
{'path': 'other.borg', 'label': 'other_repo'},
|
|
|
|
],
|
|
|
|
'keep_secondly': 1000,
|
|
|
|
'check_repositories': ['my_repo'],
|
2023-03-28 19:21:35 +02:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-06-20 05:48:54 +02:00
|
|
|
def test_apply_logical_validation_does_not_raise_if_archive_name_format_and_prefix_present():
|
2017-10-30 03:36:26 +01:00
|
|
|
module.apply_logical_validation(
|
|
|
|
'config.yaml',
|
|
|
|
{
|
2023-07-09 08:14:30 +02:00
|
|
|
'archive_name_format': '{hostname}-{now}', # noqa: FS003
|
|
|
|
'prefix': '{hostname}-', # noqa: FS003
|
|
|
|
'prefix': '{hostname}-', # noqa: FS003
|
2017-10-30 03:36:26 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_apply_logical_validation_does_not_raise_otherwise():
|
2023-07-09 08:14:30 +02:00
|
|
|
module.apply_logical_validation('config.yaml', {'keep_secondly': 1000})
|
2019-02-18 21:58:39 +01:00
|
|
|
|
|
|
|
|
2019-12-07 01:29:41 +01:00
|
|
|
def test_normalize_repository_path_passes_through_remote_repository():
|
|
|
|
repository = 'example.org:test.borg'
|
|
|
|
|
|
|
|
module.normalize_repository_path(repository) == repository
|
|
|
|
|
|
|
|
|
2023-03-21 12:36:03 +01:00
|
|
|
def test_normalize_repository_path_passes_through_file_repository():
|
|
|
|
repository = 'file:///foo/bar/test.borg'
|
|
|
|
flexmock(module.os.path).should_receive('abspath').and_return('/foo/bar/test.borg')
|
|
|
|
|
|
|
|
module.normalize_repository_path(repository) == '/foo/bar/test.borg'
|
|
|
|
|
|
|
|
|
2019-12-07 01:29:41 +01:00
|
|
|
def test_normalize_repository_path_passes_through_absolute_repository():
|
|
|
|
repository = '/foo/bar/test.borg'
|
|
|
|
flexmock(module.os.path).should_receive('abspath').and_return(repository)
|
|
|
|
|
|
|
|
module.normalize_repository_path(repository) == repository
|
|
|
|
|
|
|
|
|
|
|
|
def test_normalize_repository_path_resolves_relative_repository():
|
|
|
|
repository = 'test.borg'
|
|
|
|
absolute = '/foo/bar/test.borg'
|
|
|
|
flexmock(module.os.path).should_receive('abspath').and_return(absolute)
|
|
|
|
|
|
|
|
module.normalize_repository_path(repository) == absolute
|
|
|
|
|
|
|
|
|
|
|
|
def test_repositories_match_does_not_raise():
|
|
|
|
flexmock(module).should_receive('normalize_repository_path')
|
|
|
|
|
|
|
|
module.repositories_match('foo', 'bar')
|
|
|
|
|
|
|
|
|
2019-02-18 21:58:39 +01:00
|
|
|
def test_guard_configuration_contains_repository_does_not_raise_when_repository_in_config():
|
2019-12-07 01:29:41 +01:00
|
|
|
flexmock(module).should_receive('repositories_match').replace_with(
|
|
|
|
lambda first, second: first == second
|
|
|
|
)
|
|
|
|
|
2019-02-18 21:58:39 +01:00
|
|
|
module.guard_configuration_contains_repository(
|
2023-07-09 08:14:30 +02:00
|
|
|
repository='repo', configurations={'config.yaml': {'repositories': ['repo']}}
|
2019-02-18 21:58:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-26 20:49:23 +02:00
|
|
|
def test_guard_configuration_contains_repository_does_not_raise_when_repository_label_in_config():
|
|
|
|
module.guard_configuration_contains_repository(
|
|
|
|
repository='repo',
|
2023-07-09 08:14:30 +02:00
|
|
|
configurations={'config.yaml': {'repositories': [{'path': 'foo/bar', 'label': 'repo'}]}},
|
2023-03-26 20:49:23 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-02-18 21:58:39 +01:00
|
|
|
def test_guard_configuration_contains_repository_does_not_raise_when_repository_not_given():
|
|
|
|
module.guard_configuration_contains_repository(
|
2023-07-09 08:14:30 +02:00
|
|
|
repository=None, configurations={'config.yaml': {'repositories': ['repo']}}
|
2019-02-18 21:58:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guard_configuration_contains_repository_errors_when_repository_missing_from_config():
|
2019-12-07 01:29:41 +01:00
|
|
|
flexmock(module).should_receive('repositories_match').replace_with(
|
|
|
|
lambda first, second: first == second
|
|
|
|
)
|
|
|
|
|
2019-02-18 21:58:39 +01:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.guard_configuration_contains_repository(
|
2019-02-18 22:27:35 +01:00
|
|
|
repository='nope',
|
2023-07-09 08:14:30 +02:00
|
|
|
configurations={'config.yaml': {'repositories': ['repo', 'repo2']}},
|
2019-02-18 21:58:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-07-25 20:30:02 +02:00
|
|
|
def test_guard_single_repository_selected_raises_when_multiple_repositories_configured_and_none_selected():
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.guard_single_repository_selected(
|
|
|
|
repository=None,
|
2023-07-09 08:14:30 +02:00
|
|
|
configurations={'config.yaml': {'repositories': ['repo', 'repo2']}},
|
2022-07-25 20:30:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guard_single_repository_selected_does_not_raise_when_single_repository_configured_and_none_selected():
|
|
|
|
module.guard_single_repository_selected(
|
2023-04-15 04:35:24 +02:00
|
|
|
repository=None,
|
2023-07-09 08:14:30 +02:00
|
|
|
configurations={'config.yaml': {'repositories': ['repo']}},
|
2022-07-25 20:30:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guard_single_repository_selected_does_not_raise_when_no_repositories_configured_and_one_selected():
|
|
|
|
module.guard_single_repository_selected(
|
2023-04-15 04:35:24 +02:00
|
|
|
repository='repo',
|
2023-07-09 08:14:30 +02:00
|
|
|
configurations={'config.yaml': {'repositories': []}},
|
2022-07-25 20:30:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guard_single_repository_selected_does_not_raise_when_repositories_configured_and_one_selected():
|
|
|
|
module.guard_single_repository_selected(
|
|
|
|
repository='repo',
|
2023-07-09 08:14:30 +02:00
|
|
|
configurations={'config.yaml': {'repositories': ['repo', 'repo2']}},
|
2022-07-25 20:30:02 +02:00
|
|
|
)
|