/tests/unit/config/test_convert.py

https://github.com/witten/borgmatic · Python · 120 lines · 93 code · 27 blank · 0 comment · 1 complexity · 9700b91966389cb133c0d85858ea9f7b MD5 · raw file

  1. import os
  2. from collections import OrderedDict, defaultdict, namedtuple
  3. import pytest
  4. from flexmock import flexmock
  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. flexmock(module.generate).should_receive('add_comments_to_configuration_map')
  16. source_config = Parsed_config(
  17. location=OrderedDict([('source_directories', '/home'), ('repository', 'hostname.borg')]),
  18. storage=OrderedDict([('encryption_passphrase', 'supersecret')]),
  19. retention=OrderedDict([('keep_daily', 7)]),
  20. consistency=OrderedDict([('checks', 'repository')]),
  21. )
  22. source_excludes = ['/var']
  23. schema = {'map': defaultdict(lambda: {'map': {}})}
  24. destination_config = module.convert_legacy_parsed_config(source_config, source_excludes, schema)
  25. assert destination_config == OrderedDict(
  26. [
  27. (
  28. 'location',
  29. OrderedDict(
  30. [
  31. ('source_directories', ['/home']),
  32. ('repositories', ['hostname.borg']),
  33. ('exclude_patterns', ['/var']),
  34. ]
  35. ),
  36. ),
  37. ('storage', OrderedDict([('encryption_passphrase', 'supersecret')])),
  38. ('retention', OrderedDict([('keep_daily', 7)])),
  39. ('consistency', OrderedDict([('checks', ['repository'])])),
  40. ]
  41. )
  42. def test_convert_legacy_parsed_config_splits_space_separated_values():
  43. flexmock(module.yaml.comments).should_receive('CommentedMap').replace_with(OrderedDict)
  44. flexmock(module.generate).should_receive('add_comments_to_configuration_map')
  45. source_config = Parsed_config(
  46. location=OrderedDict(
  47. [('source_directories', '/home /etc'), ('repository', 'hostname.borg')]
  48. ),
  49. storage=OrderedDict(),
  50. retention=OrderedDict(),
  51. consistency=OrderedDict([('checks', 'repository archives')]),
  52. )
  53. source_excludes = ['/var']
  54. schema = {'map': defaultdict(lambda: {'map': {}})}
  55. destination_config = module.convert_legacy_parsed_config(source_config, source_excludes, schema)
  56. assert destination_config == OrderedDict(
  57. [
  58. (
  59. 'location',
  60. OrderedDict(
  61. [
  62. ('source_directories', ['/home', '/etc']),
  63. ('repositories', ['hostname.borg']),
  64. ('exclude_patterns', ['/var']),
  65. ]
  66. ),
  67. ),
  68. ('storage', OrderedDict()),
  69. ('retention', OrderedDict()),
  70. ('consistency', OrderedDict([('checks', ['repository', 'archives'])])),
  71. ]
  72. )
  73. def test_guard_configuration_upgraded_raises_when_only_source_config_present():
  74. flexmock(os.path).should_receive('exists').with_args('config').and_return(True)
  75. flexmock(os.path).should_receive('exists').with_args('config.yaml').and_return(False)
  76. flexmock(os.path).should_receive('exists').with_args('other.yaml').and_return(False)
  77. with pytest.raises(module.Legacy_configuration_not_upgraded):
  78. module.guard_configuration_upgraded('config', ('config.yaml', 'other.yaml'))
  79. def test_guard_configuration_upgraded_does_not_raise_when_only_destination_config_present():
  80. flexmock(os.path).should_receive('exists').with_args('config').and_return(False)
  81. flexmock(os.path).should_receive('exists').with_args('config.yaml').and_return(False)
  82. flexmock(os.path).should_receive('exists').with_args('other.yaml').and_return(True)
  83. module.guard_configuration_upgraded('config', ('config.yaml', 'other.yaml'))
  84. def test_guard_configuration_upgraded_does_not_raise_when_both_configs_present():
  85. flexmock(os.path).should_receive('exists').with_args('config').and_return(True)
  86. flexmock(os.path).should_receive('exists').with_args('config.yaml').and_return(False)
  87. flexmock(os.path).should_receive('exists').with_args('other.yaml').and_return(True)
  88. module.guard_configuration_upgraded('config', ('config.yaml', 'other.yaml'))
  89. def test_guard_configuration_upgraded_does_not_raise_when_neither_config_present():
  90. flexmock(os.path).should_receive('exists').with_args('config').and_return(False)
  91. flexmock(os.path).should_receive('exists').with_args('config.yaml').and_return(False)
  92. flexmock(os.path).should_receive('exists').with_args('other.yaml').and_return(False)
  93. module.guard_configuration_upgraded('config', ('config.yaml', 'other.yaml'))