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
|
|
|
|
|
|
|
|
|
|
|
|
def test_validation_error_str_contains_error_messages_and_config_filename():
|
|
|
|
error = module.Validation_error('config.yaml', ('oops', 'uh oh'))
|
|
|
|
|
|
|
|
result = str(error)
|
|
|
|
|
|
|
|
assert 'config.yaml' in result
|
|
|
|
assert 'oops' in result
|
|
|
|
assert 'uh oh' in result
|
|
|
|
|
|
|
|
|
|
|
|
def test_apply_logical_validation_raises_if_archive_name_format_present_without_prefix():
|
|
|
|
with pytest.raises(module.Validation_error):
|
|
|
|
module.apply_logical_validation(
|
|
|
|
'config.yaml',
|
|
|
|
{
|
|
|
|
'storage': {'archive_name_format': '{hostname}-{now}'},
|
|
|
|
'retention': {'keep_daily': 7},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2018-09-30 07:45:00 +02:00
|
|
|
|
2018-03-04 07:17:39 +01:00
|
|
|
def test_apply_logical_validation_raises_if_archive_name_format_present_without_retention_prefix():
|
|
|
|
with pytest.raises(module.Validation_error):
|
|
|
|
module.apply_logical_validation(
|
|
|
|
'config.yaml',
|
|
|
|
{
|
|
|
|
'storage': {'archive_name_format': '{hostname}-{now}'},
|
|
|
|
'retention': {'keep_daily': 7},
|
2018-09-30 07:45:00 +02:00
|
|
|
'consistency': {'prefix': '{hostname}-'},
|
2018-03-04 07:17:39 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-10-14 05:34:51 +02:00
|
|
|
def test_apply_locical_validation_raises_if_unknown_repository_in_check_repositories():
|
|
|
|
with pytest.raises(module.Validation_error):
|
|
|
|
module.apply_logical_validation(
|
|
|
|
'config.yaml',
|
|
|
|
{
|
|
|
|
'location': {'repositories': ['repo.borg', 'other.borg']},
|
|
|
|
'retention': {'keep_secondly': 1000},
|
|
|
|
'consistency': {'check_repositories': ['repo.borg', 'unknown.borg']},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_apply_locical_validation_does_not_raise_if_known_repository_in_check_repositories():
|
|
|
|
module.apply_logical_validation(
|
|
|
|
'config.yaml',
|
|
|
|
{
|
|
|
|
'location': {'repositories': ['repo.borg', 'other.borg']},
|
|
|
|
'retention': {'keep_secondly': 1000},
|
|
|
|
'consistency': {'check_repositories': ['repo.borg']},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
{
|
|
|
|
'storage': {'archive_name_format': '{hostname}-{now}'},
|
|
|
|
'retention': {'prefix': '{hostname}-'},
|
2018-09-30 07:45:00 +02:00
|
|
|
'consistency': {'prefix': '{hostname}-'},
|
2017-10-30 03:36:26 +01:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_apply_logical_validation_does_not_raise_otherwise():
|
2018-09-30 07:45:00 +02:00
|
|
|
module.apply_logical_validation('config.yaml', {'retention': {'keep_secondly': 1000}})
|
2019-02-18 21:58:39 +01:00
|
|
|
|
|
|
|
|
2019-10-23 01:28:42 +02:00
|
|
|
def test_remove_examples_strips_examples_from_map():
|
|
|
|
schema = {
|
|
|
|
'map': {
|
|
|
|
'foo': {'desc': 'thing1', 'example': 'bar'},
|
|
|
|
'baz': {'desc': 'thing2', 'example': 'quux'},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.remove_examples(schema)
|
|
|
|
|
|
|
|
assert schema == {'map': {'foo': {'desc': 'thing1'}, 'baz': {'desc': 'thing2'}}}
|
|
|
|
|
|
|
|
|
|
|
|
def test_remove_examples_strips_examples_from_sequence_of_maps():
|
|
|
|
schema = {'seq': [{'map': {'foo': {'desc': 'thing', 'example': 'bar'}}, 'example': 'stuff'}]}
|
|
|
|
|
|
|
|
module.remove_examples(schema)
|
|
|
|
|
|
|
|
assert schema == {'seq': [{'map': {'foo': {'desc': 'thing'}}}]}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
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(
|
2019-02-18 22:27:35 +01:00
|
|
|
repository='repo', configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
|
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(
|
2019-02-18 22:27:35 +01:00
|
|
|
repository=None, configurations={'config.yaml': {'location': {'repositories': ['repo']}}}
|
2019-02-18 21:58:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-02-18 22:22:14 +01:00
|
|
|
def test_guard_configuration_contains_repository_errors_when_repository_assumed_to_match_config_twice():
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
module.guard_configuration_contains_repository(
|
2019-02-18 22:27:35 +01:00
|
|
|
repository=None,
|
|
|
|
configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
|
2019-02-18 22:22:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
configurations={'config.yaml': {'location': {'repositories': ['repo', 'repo2']}}},
|
2019-02-18 21:58:39 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_guard_configuration_contains_repository_errors_when_repository_matches_config_twice():
|
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(
|
|
|
|
repository='repo',
|
|
|
|
configurations={
|
2019-02-18 22:27:35 +01:00
|
|
|
'config.yaml': {'location': {'repositories': ['repo', 'repo2']}},
|
|
|
|
'other.yaml': {'location': {'repositories': ['repo']}},
|
2019-02-18 21:58:39 +01:00
|
|
|
},
|
|
|
|
)
|