test_convert.py 5.3 KB

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