test_validate.py 8.4 KB

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