123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- import pytest
- from flexmock import flexmock
- from borgmatic.config import normalize as module
- @pytest.mark.parametrize(
- 'config,expected_config,produces_logs',
- (
- (
- {'location': {'foo': 'bar', 'baz': 'quux'}},
- {'foo': 'bar', 'baz': 'quux'},
- True,
- ),
- (
- {'retention': {'foo': 'bar', 'baz': 'quux'}},
- {'foo': 'bar', 'baz': 'quux'},
- True,
- ),
- (
- {'consistency': {'foo': 'bar', 'baz': 'quux'}},
- {'foo': 'bar', 'baz': 'quux'},
- True,
- ),
- (
- {'output': {'foo': 'bar', 'baz': 'quux'}},
- {'foo': 'bar', 'baz': 'quux'},
- True,
- ),
- (
- {'hooks': {'foo': 'bar', 'baz': 'quux'}},
- {'foo': 'bar', 'baz': 'quux'},
- True,
- ),
- (
- {'location': {'foo': 'bar'}, 'storage': {'baz': 'quux'}},
- {'foo': 'bar', 'baz': 'quux'},
- True,
- ),
- (
- {'foo': 'bar', 'baz': 'quux'},
- {'foo': 'bar', 'baz': 'quux'},
- False,
- ),
- (
- {'location': {'prefix': 'foo'}, 'consistency': {'prefix': 'foo'}},
- {'prefix': 'foo'},
- True,
- ),
- (
- {'location': {'prefix': 'foo'}, 'consistency': {'prefix': 'foo'}},
- {'prefix': 'foo'},
- True,
- ),
- (
- {'location': {'prefix': 'foo'}, 'consistency': {'bar': 'baz'}},
- {'prefix': 'foo', 'bar': 'baz'},
- True,
- ),
- (
- {'storage': {'umask': 'foo'}, 'hooks': {'umask': 'foo'}},
- {'umask': 'foo'},
- True,
- ),
- (
- {'storage': {'umask': 'foo'}, 'hooks': {'umask': 'foo'}},
- {'umask': 'foo'},
- True,
- ),
- (
- {'storage': {'umask': 'foo'}, 'hooks': {'bar': 'baz'}},
- {'umask': 'foo', 'bar': 'baz'},
- True,
- ),
- (
- {'location': {'bar': 'baz'}, 'consistency': {'prefix': 'foo'}},
- {'bar': 'baz', 'prefix': 'foo'},
- True,
- ),
- (
- {'location': {}, 'consistency': {'prefix': 'foo'}},
- {'prefix': 'foo'},
- True,
- ),
- (
- {},
- {},
- False,
- ),
- ),
- )
- def test_normalize_sections_moves_section_options_to_global_scope(
- config, expected_config, produces_logs
- ):
- logs = module.normalize_sections('test.yaml', config)
- assert config == expected_config
- if produces_logs:
- assert logs
- else:
- assert logs == []
- def test_normalize_sections_with_different_prefix_values_raises():
- config = {'location': {'prefix': 'foo'}, 'consistency': {'prefix': 'bar'}}
- with pytest.raises(ValueError):
- module.normalize_sections('test.yaml', config)
- def test_normalize_sections_with_different_umask_values_raises():
- config = {'storage': {'umask': 'foo'}, 'hooks': {'umask': 'bar'}}
- with pytest.raises(ValueError):
- module.normalize_sections('test.yaml', config)
- def test_normalize_sections_with_only_scalar_raises():
- config = 33
- with pytest.raises(ValueError):
- module.normalize_sections('test.yaml', config)
- @pytest.mark.parametrize(
- 'config,expected_config,produces_logs',
- (
- (
- {'before_actions': ['foo', 'bar'], 'after_actions': ['baz']},
- {
- 'commands': [
- {'before': 'repository', 'run': ['foo', 'bar']},
- {'after': 'repository', 'run': ['baz']},
- ]
- },
- True,
- ),
- (
- {'before_backup': ['foo', 'bar'], 'after_backup': ['baz']},
- {
- 'commands': [
- {'before': 'action', 'when': ['create'], 'run': ['foo', 'bar']},
- {'after': 'action', 'when': ['create'], 'run': ['baz']},
- ]
- },
- True,
- ),
- (
- {'before_prune': ['foo', 'bar'], 'after_prune': ['baz']},
- {
- 'commands': [
- {'before': 'action', 'when': ['prune'], 'run': ['foo', 'bar']},
- {'after': 'action', 'when': ['prune'], 'run': ['baz']},
- ]
- },
- True,
- ),
- (
- {'before_compact': ['foo', 'bar'], 'after_compact': ['baz']},
- {
- 'commands': [
- {'before': 'action', 'when': ['compact'], 'run': ['foo', 'bar']},
- {'after': 'action', 'when': ['compact'], 'run': ['baz']},
- ]
- },
- True,
- ),
- (
- {'before_check': ['foo', 'bar'], 'after_check': ['baz']},
- {
- 'commands': [
- {'before': 'action', 'when': ['check'], 'run': ['foo', 'bar']},
- {'after': 'action', 'when': ['check'], 'run': ['baz']},
- ]
- },
- True,
- ),
- (
- {'before_extract': ['foo', 'bar'], 'after_extract': ['baz']},
- {
- 'commands': [
- {'before': 'action', 'when': ['extract'], 'run': ['foo', 'bar']},
- {'after': 'action', 'when': ['extract'], 'run': ['baz']},
- ]
- },
- True,
- ),
- (
- {'on_error': ['foo', 'bar']},
- {
- 'commands': [
- {
- 'after': 'error',
- 'when': ['create', 'prune', 'compact', 'check'],
- 'run': ['foo', 'bar'],
- },
- ]
- },
- True,
- ),
- (
- {'before_everything': ['foo', 'bar'], 'after_everything': ['baz']},
- {
- 'commands': [
- {'before': 'everything', 'when': ['create'], 'run': ['foo', 'bar']},
- {'after': 'everything', 'when': ['create'], 'run': ['baz']},
- ]
- },
- True,
- ),
- (
- {'other': 'options', 'unrelated_to': 'commands'},
- {'other': 'options', 'unrelated_to': 'commands'},
- False,
- ),
- ),
- )
- def test_normalize_commands_moves_individual_command_hooks_to_unified_commands(
- config, expected_config, produces_logs
- ):
- flexmock(module).should_receive('make_command_hook_deprecation_log').and_return(flexmock())
- logs = module.normalize_commands('test.yaml', config)
- assert config == expected_config
- if produces_logs:
- assert logs
- else:
- assert logs == []
- @pytest.mark.parametrize(
- 'config,expected_config,produces_logs',
- (
- (
- {'exclude_if_present': '.nobackup'},
- {'exclude_if_present': ['.nobackup']},
- True,
- ),
- (
- {'exclude_if_present': ['.nobackup']},
- {'exclude_if_present': ['.nobackup']},
- False,
- ),
- (
- {'store_config_files': False},
- {'bootstrap': {'store_config_files': False}},
- True,
- ),
- (
- {'source_directories': ['foo', 'bar']},
- {'source_directories': ['foo', 'bar']},
- False,
- ),
- (
- {'compression': 'yes_please'},
- {'compression': 'yes_please'},
- False,
- ),
- (
- {'healthchecks': 'https://example.com'},
- {'healthchecks': {'ping_url': 'https://example.com'}},
- True,
- ),
- (
- {'cronitor': 'https://example.com'},
- {'cronitor': {'ping_url': 'https://example.com'}},
- True,
- ),
- (
- {'pagerduty': 'https://example.com'},
- {'pagerduty': {'integration_key': 'https://example.com'}},
- True,
- ),
- (
- {'cronhub': 'https://example.com'},
- {'cronhub': {'ping_url': 'https://example.com'}},
- True,
- ),
- (
- {'checks': ['archives']},
- {'checks': [{'name': 'archives'}]},
- True,
- ),
- (
- {'checks': ['archives']},
- {'checks': [{'name': 'archives'}]},
- True,
- ),
- (
- {'numeric_owner': False},
- {'numeric_ids': False},
- True,
- ),
- (
- {'bsd_flags': False},
- {'flags': False},
- True,
- ),
- (
- {'remote_rate_limit': False},
- {'upload_rate_limit': False},
- True,
- ),
- (
- {'repositories': ['foo@bar:/repo']},
- {'repositories': [{'path': 'ssh://foo@bar/repo'}]},
- True,
- ),
- (
- {'repositories': ['foo@bar:repo']},
- {'repositories': [{'path': 'ssh://foo@bar/./repo'}]},
- True,
- ),
- (
- {'repositories': ['foo@bar:~/repo']},
- {'repositories': [{'path': 'ssh://foo@bar/~/repo'}]},
- True,
- ),
- (
- {'repositories': ['ssh://foo@bar:1234/repo']},
- {'repositories': [{'path': 'ssh://foo@bar:1234/repo'}]},
- True,
- ),
- (
- {'repositories': ['sftp://foo@bar:1234/repo']},
- {'repositories': [{'path': 'sftp://foo@bar:1234/repo'}]},
- True,
- ),
- (
- {'repositories': ['rclone:host:repo']},
- {'repositories': [{'path': 'rclone:host:repo'}]},
- True,
- ),
- (
- {'repositories': ['s3:stuff']},
- {'repositories': [{'path': 's3:stuff'}]},
- True,
- ),
- (
- {'repositories': ['b2:stuff']},
- {'repositories': [{'path': 'b2:stuff'}]},
- True,
- ),
- (
- {'repositories': ['file:///repo']},
- {'repositories': [{'path': '/repo'}]},
- True,
- ),
- (
- {'repositories': [{'path': 'first'}, 'file:///repo']},
- {'repositories': [{'path': 'first'}, {'path': '/repo'}]},
- True,
- ),
- (
- {'repositories': [{'path': 'foo@bar:/repo', 'label': 'foo'}]},
- {'repositories': [{'path': 'ssh://foo@bar/repo', 'label': 'foo'}]},
- True,
- ),
- (
- {'repositories': [{'path': 'file:///repo', 'label': 'foo'}]},
- {'repositories': [{'path': '/repo', 'label': 'foo'}]},
- False,
- ),
- (
- {'repositories': [{'path': '/repo', 'label': 'foo'}]},
- {'repositories': [{'path': '/repo', 'label': 'foo'}]},
- False,
- ),
- (
- {'repositories': [{'path': None, 'label': 'foo'}]},
- {'repositories': []},
- False,
- ),
- (
- {'prefix': 'foo'},
- {'prefix': 'foo'},
- True,
- ),
- ),
- )
- def test_normalize_applies_hard_coded_normalization_to_config(
- config, expected_config, produces_logs
- ):
- flexmock(module).should_receive('normalize_sections').and_return([])
- flexmock(module).should_receive('normalize_commands').and_return([])
- logs = module.normalize('test.yaml', config)
- expected_config.setdefault('bootstrap', {})
- assert config == expected_config
- if produces_logs:
- assert logs
- else:
- assert logs == []
- def test_normalize_config_with_borgmatic_source_directory_warns():
- flexmock(module).should_receive('normalize_sections').and_return([])
- flexmock(module).should_receive('normalize_commands').and_return([])
- logs = module.normalize('test.yaml', {'borgmatic_source_directory': '~/.borgmatic'})
- assert len(logs) == 1
- assert logs[0].levelno == module.logging.WARNING
- assert 'borgmatic_source_directory' in logs[0].msg
|