test_convert.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from collections import defaultdict, OrderedDict, namedtuple
  2. import os
  3. from flexmock import flexmock
  4. import pytest
  5. from borgmatic.config import convert as module
  6. Parsed_config = namedtuple('Parsed_config', ('location', 'storage', 'retention', 'consistency'))
  7. def test_convert_section_generates_integer_value_for_integer_type_in_schema():
  8. flexmock(module.yaml.comments).should_receive('CommentedMap').replace_with(OrderedDict)
  9. source_section_config = OrderedDict([('check_last', '3')])
  10. section_schema = {'map': {'check_last': {'type': 'int'}}}
  11. destination_config = module._convert_section(source_section_config, section_schema)
  12. assert destination_config == OrderedDict([('check_last', 3)])
  13. def test_convert_legacy_parsed_config_transforms_source_config_to_mapping():
  14. flexmock(module.yaml.comments).should_receive('CommentedMap').replace_with(OrderedDict)
  15. source_config = Parsed_config(
  16. location=OrderedDict([('source_directories', '/home'), ('repository', 'hostname.borg')]),
  17. storage=OrderedDict([('encryption_passphrase', 'supersecret')]),
  18. retention=OrderedDict([('keep_daily', 7)]),
  19. consistency=OrderedDict([('checks', 'repository')]),
  20. )
  21. source_excludes = ['/var']
  22. schema = {'map': defaultdict(lambda: {'map': {}})}
  23. destination_config = module.convert_legacy_parsed_config(source_config, source_excludes, schema)
  24. assert destination_config == OrderedDict(
  25. [
  26. (
  27. 'location',
  28. OrderedDict(
  29. [
  30. ('source_directories', ['/home']),
  31. ('repositories', ['hostname.borg']),
  32. ('exclude_patterns', ['/var']),
  33. ]
  34. ),
  35. ),
  36. ('storage', OrderedDict([('encryption_passphrase', 'supersecret')])),
  37. ('retention', OrderedDict([('keep_daily', 7)])),
  38. ('consistency', OrderedDict([('checks', ['repository'])])),
  39. ]
  40. )
  41. def test_convert_legacy_parsed_config_splits_space_separated_values():
  42. flexmock(module.yaml.comments).should_receive('CommentedMap').replace_with(OrderedDict)
  43. source_config = Parsed_config(
  44. location=OrderedDict(
  45. [('source_directories', '/home /etc'), ('repository', 'hostname.borg')]
  46. ),
  47. storage=OrderedDict(),
  48. retention=OrderedDict(),
  49. consistency=OrderedDict([('checks', 'repository archives')]),
  50. )
  51. source_excludes = ['/var']
  52. schema = {'map': defaultdict(lambda: {'map': {}})}
  53. destination_config = module.convert_legacy_parsed_config(source_config, source_excludes, schema)
  54. assert destination_config == OrderedDict(
  55. [
  56. (
  57. 'location',
  58. OrderedDict(
  59. [
  60. ('source_directories', ['/home', '/etc']),
  61. ('repositories', ['hostname.borg']),
  62. ('exclude_patterns', ['/var']),
  63. ]
  64. ),
  65. ),
  66. ('storage', OrderedDict()),
  67. ('retention', OrderedDict()),
  68. ('consistency', OrderedDict([('checks', ['repository', 'archives'])])),
  69. ]
  70. )
  71. def test_guard_configuration_upgraded_raises_when_only_source_config_present():
  72. flexmock(os.path).should_receive('exists').with_args('config').and_return(True)
  73. flexmock(os.path).should_receive('exists').with_args('config.yaml').and_return(False)
  74. flexmock(os.path).should_receive('exists').with_args('other.yaml').and_return(False)
  75. with pytest.raises(module.LegacyConfigurationNotUpgraded):
  76. module.guard_configuration_upgraded('config', ('config.yaml', 'other.yaml'))
  77. def test_guard_configuration_upgraded_does_not_raise_when_only_destination_config_present():
  78. flexmock(os.path).should_receive('exists').with_args('config').and_return(False)
  79. flexmock(os.path).should_receive('exists').with_args('config.yaml').and_return(False)
  80. flexmock(os.path).should_receive('exists').with_args('other.yaml').and_return(True)
  81. module.guard_configuration_upgraded('config', ('config.yaml', 'other.yaml'))
  82. def test_guard_configuration_upgraded_does_not_raise_when_both_configs_present():
  83. flexmock(os.path).should_receive('exists').with_args('config').and_return(True)
  84. flexmock(os.path).should_receive('exists').with_args('config.yaml').and_return(False)
  85. flexmock(os.path).should_receive('exists').with_args('other.yaml').and_return(True)
  86. module.guard_configuration_upgraded('config', ('config.yaml', 'other.yaml'))
  87. def test_guard_configuration_upgraded_does_not_raise_when_neither_config_present():
  88. flexmock(os.path).should_receive('exists').with_args('config').and_return(False)
  89. flexmock(os.path).should_receive('exists').with_args('config.yaml').and_return(False)
  90. flexmock(os.path).should_receive('exists').with_args('other.yaml').and_return(False)
  91. module.guard_configuration_upgraded('config', ('config.yaml', 'other.yaml'))
  92. def test_guard_excludes_filename_omitted_raises_when_filename_provided():
  93. with pytest.raises(module.LegacyExcludesFilenamePresent):
  94. module.guard_excludes_filename_omitted(excludes_filename='/etc/borgmatic/excludes')
  95. def test_guard_excludes_filename_omitted_does_not_raise_when_filename_not_provided():
  96. module.guard_excludes_filename_omitted(excludes_filename=None)