test_validate.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import io
  2. import string
  3. import sys
  4. import pytest
  5. from flexmock import flexmock
  6. from borgmatic.config import validate as module
  7. def test_schema_filename_returns_plausable_path():
  8. schema_path = module.schema_filename()
  9. assert schema_path.endswith('/schema.yaml')
  10. def mock_config_and_schema(config_yaml, schema_yaml=None):
  11. '''
  12. Set up mocks for the given config config YAML string and the schema YAML string, or the default
  13. schema if no schema is provided. The idea is that that the code under test consumes these mocks
  14. when parsing the configuration.
  15. '''
  16. config_stream = io.StringIO(config_yaml)
  17. if schema_yaml is None:
  18. schema_stream = open(module.schema_filename())
  19. else:
  20. schema_stream = io.StringIO(schema_yaml)
  21. builtins = flexmock(sys.modules['builtins'])
  22. builtins.should_receive('open').with_args('config.yaml').and_return(config_stream)
  23. builtins.should_receive('open').with_args('schema.yaml').and_return(schema_stream)
  24. def test_parse_configuration_transforms_file_into_mapping():
  25. mock_config_and_schema(
  26. '''
  27. location:
  28. source_directories:
  29. - /home
  30. - /etc
  31. repositories:
  32. - hostname.borg
  33. retention:
  34. keep_minutely: 60
  35. keep_hourly: 24
  36. keep_daily: 7
  37. consistency:
  38. checks:
  39. - repository
  40. - archives
  41. '''
  42. )
  43. result = module.parse_configuration('config.yaml', 'schema.yaml')
  44. assert result == {
  45. 'location': {'source_directories': ['/home', '/etc'], 'repositories': ['hostname.borg']},
  46. 'retention': {'keep_daily': 7, 'keep_hourly': 24, 'keep_minutely': 60},
  47. 'consistency': {'checks': ['repository', 'archives']},
  48. }
  49. def test_parse_configuration_passes_through_quoted_punctuation():
  50. escaped_punctuation = string.punctuation.replace('\\', r'\\').replace('"', r'\"')
  51. mock_config_and_schema(
  52. '''
  53. location:
  54. source_directories:
  55. - /home
  56. repositories:
  57. - "{}.borg"
  58. '''.format(
  59. escaped_punctuation
  60. )
  61. )
  62. result = module.parse_configuration('config.yaml', 'schema.yaml')
  63. assert result == {
  64. 'location': {
  65. 'source_directories': ['/home'],
  66. 'repositories': ['{}.borg'.format(string.punctuation)],
  67. }
  68. }
  69. def test_parse_configuration_with_schema_lacking_examples_does_not_raise():
  70. mock_config_and_schema(
  71. '''
  72. location:
  73. source_directories:
  74. - /home
  75. repositories:
  76. - hostname.borg
  77. ''',
  78. '''
  79. map:
  80. location:
  81. required: true
  82. map:
  83. source_directories:
  84. required: true
  85. seq:
  86. - type: scalar
  87. repositories:
  88. required: true
  89. seq:
  90. - type: scalar
  91. ''',
  92. )
  93. module.parse_configuration('config.yaml', 'schema.yaml')
  94. def test_parse_configuration_inlines_include():
  95. mock_config_and_schema(
  96. '''
  97. location:
  98. source_directories:
  99. - /home
  100. repositories:
  101. - hostname.borg
  102. retention:
  103. !include include.yaml
  104. '''
  105. )
  106. builtins = flexmock(sys.modules['builtins'])
  107. builtins.should_receive('open').with_args('include.yaml').and_return(
  108. '''
  109. keep_daily: 7
  110. keep_hourly: 24
  111. '''
  112. )
  113. result = module.parse_configuration('config.yaml', 'schema.yaml')
  114. assert result == {
  115. 'location': {'source_directories': ['/home'], 'repositories': ['hostname.borg']},
  116. 'retention': {'keep_daily': 7, 'keep_hourly': 24},
  117. }
  118. def test_parse_configuration_merges_include():
  119. mock_config_and_schema(
  120. '''
  121. location:
  122. source_directories:
  123. - /home
  124. repositories:
  125. - hostname.borg
  126. retention:
  127. keep_daily: 1
  128. <<: !include include.yaml
  129. '''
  130. )
  131. builtins = flexmock(sys.modules['builtins'])
  132. builtins.should_receive('open').with_args('include.yaml').and_return(
  133. '''
  134. keep_daily: 7
  135. keep_hourly: 24
  136. '''
  137. )
  138. result = module.parse_configuration('config.yaml', 'schema.yaml')
  139. assert result == {
  140. 'location': {'source_directories': ['/home'], 'repositories': ['hostname.borg']},
  141. 'retention': {'keep_daily': 1, 'keep_hourly': 24},
  142. }
  143. def test_parse_configuration_raises_for_missing_config_file():
  144. with pytest.raises(FileNotFoundError):
  145. module.parse_configuration('config.yaml', 'schema.yaml')
  146. def test_parse_configuration_raises_for_missing_schema_file():
  147. mock_config_and_schema('')
  148. builtins = flexmock(sys.modules['builtins'])
  149. builtins.should_receive('open').with_args('schema.yaml').and_raise(FileNotFoundError)
  150. with pytest.raises(FileNotFoundError):
  151. module.parse_configuration('config.yaml', 'schema.yaml')
  152. def test_parse_configuration_raises_for_syntax_error():
  153. mock_config_and_schema('foo:\nbar')
  154. with pytest.raises(ValueError):
  155. module.parse_configuration('config.yaml', 'schema.yaml')
  156. def test_parse_configuration_raises_for_validation_error():
  157. mock_config_and_schema(
  158. '''
  159. location:
  160. source_directories: yes
  161. repositories:
  162. - hostname.borg
  163. '''
  164. )
  165. with pytest.raises(module.Validation_error):
  166. module.parse_configuration('config.yaml', 'schema.yaml')
  167. def test_parse_configuration_applies_overrides():
  168. mock_config_and_schema(
  169. '''
  170. location:
  171. source_directories:
  172. - /home
  173. repositories:
  174. - hostname.borg
  175. local_path: borg1
  176. '''
  177. )
  178. result = module.parse_configuration(
  179. 'config.yaml', 'schema.yaml', overrides=['location.local_path=borg2']
  180. )
  181. assert result == {
  182. 'location': {
  183. 'source_directories': ['/home'],
  184. 'repositories': ['hostname.borg'],
  185. 'local_path': 'borg2',
  186. }
  187. }
  188. def test_parse_configuration_applies_normalization():
  189. mock_config_and_schema(
  190. '''
  191. location:
  192. source_directories:
  193. - /home
  194. repositories:
  195. - hostname.borg
  196. exclude_if_present: .nobackup
  197. '''
  198. )
  199. result = module.parse_configuration('config.yaml', 'schema.yaml')
  200. assert result == {
  201. 'location': {
  202. 'source_directories': ['/home'],
  203. 'repositories': ['hostname.borg'],
  204. 'exclude_if_present': ['.nobackup'],
  205. }
  206. }