test_validate.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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('/tmp/config.yaml', '/tmp/schema.yaml')
  47. assert config == {
  48. 'source_directories': ['/home', '/etc'],
  49. 'repositories': [{'path': 'hostname.borg'}],
  50. 'keep_daily': 7,
  51. 'keep_hourly': 24,
  52. 'keep_minutely': 60,
  53. 'checks': [{'name': 'repository'}, {'name': 'archives'}],
  54. 'bootstrap': {},
  55. }
  56. assert config_paths == {'/tmp/config.yaml'}
  57. assert logs == []
  58. def test_parse_configuration_passes_through_quoted_punctuation():
  59. escaped_punctuation = string.punctuation.replace('\\', r'\\').replace('"', r'\"')
  60. mock_config_and_schema(
  61. f'''
  62. source_directories:
  63. - "/home/{escaped_punctuation}"
  64. repositories:
  65. - path: test.borg
  66. '''
  67. )
  68. config, config_paths, logs = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  69. assert config == {
  70. 'source_directories': [f'/home/{string.punctuation}'],
  71. 'repositories': [{'path': 'test.borg'}],
  72. 'bootstrap': {},
  73. }
  74. assert config_paths == {'/tmp/config.yaml'}
  75. assert logs == []
  76. def test_parse_configuration_with_schema_lacking_examples_does_not_raise():
  77. mock_config_and_schema(
  78. '''
  79. source_directories:
  80. - /home
  81. repositories:
  82. - path: hostname.borg
  83. ''',
  84. '''
  85. map:
  86. source_directories:
  87. required: true
  88. seq:
  89. - type: scalar
  90. repositories:
  91. required: true
  92. seq:
  93. - type: scalar
  94. ''',
  95. )
  96. module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  97. def test_parse_configuration_inlines_include_inside_deprecated_section():
  98. mock_config_and_schema(
  99. '''
  100. source_directories:
  101. - /home
  102. repositories:
  103. - path: hostname.borg
  104. retention:
  105. !include include.yaml
  106. '''
  107. )
  108. builtins = flexmock(sys.modules['builtins'])
  109. include_file = io.StringIO(
  110. '''
  111. keep_daily: 7
  112. keep_hourly: 24
  113. '''
  114. )
  115. include_file.name = 'include.yaml'
  116. builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
  117. config, config_paths, logs = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  118. assert config == {
  119. 'source_directories': ['/home'],
  120. 'repositories': [{'path': 'hostname.borg'}],
  121. 'keep_daily': 7,
  122. 'keep_hourly': 24,
  123. 'bootstrap': {},
  124. }
  125. assert config_paths == {'/tmp/include.yaml', '/tmp/config.yaml'}
  126. assert len(logs) == 1
  127. def test_parse_configuration_merges_include():
  128. mock_config_and_schema(
  129. '''
  130. source_directories:
  131. - /home
  132. repositories:
  133. - path: hostname.borg
  134. keep_daily: 1
  135. <<: !include include.yaml
  136. '''
  137. )
  138. builtins = flexmock(sys.modules['builtins'])
  139. include_file = io.StringIO(
  140. '''
  141. keep_daily: 7
  142. keep_hourly: 24
  143. '''
  144. )
  145. include_file.name = 'include.yaml'
  146. builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
  147. config, config_paths, logs = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  148. assert config == {
  149. 'source_directories': ['/home'],
  150. 'repositories': [{'path': 'hostname.borg'}],
  151. 'keep_daily': 1,
  152. 'keep_hourly': 24,
  153. 'bootstrap': {},
  154. }
  155. assert config_paths == {'/tmp/include.yaml', '/tmp/config.yaml'}
  156. assert logs == []
  157. def test_parse_configuration_raises_for_missing_config_file():
  158. with pytest.raises(FileNotFoundError):
  159. module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  160. def test_parse_configuration_raises_for_missing_schema_file():
  161. mock_config_and_schema('')
  162. builtins = flexmock(sys.modules['builtins'])
  163. builtins.should_receive('open').with_args('/tmp/config.yaml').and_return(
  164. io.StringIO('foo: bar')
  165. )
  166. builtins.should_receive('open').with_args('/tmp/schema.yaml').and_raise(FileNotFoundError)
  167. with pytest.raises(FileNotFoundError):
  168. module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  169. def test_parse_configuration_raises_for_syntax_error():
  170. mock_config_and_schema('foo:\nbar')
  171. with pytest.raises(ValueError):
  172. module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  173. def test_parse_configuration_raises_for_validation_error():
  174. mock_config_and_schema(
  175. '''
  176. source_directories: yes
  177. repositories:
  178. - path: hostname.borg
  179. '''
  180. )
  181. with pytest.raises(module.Validation_error):
  182. module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  183. def test_parse_configuration_applies_overrides():
  184. mock_config_and_schema(
  185. '''
  186. source_directories:
  187. - /home
  188. repositories:
  189. - path: hostname.borg
  190. local_path: borg1
  191. '''
  192. )
  193. config, config_paths, logs = module.parse_configuration(
  194. '/tmp/config.yaml', '/tmp/schema.yaml', overrides=['local_path=borg2']
  195. )
  196. assert config == {
  197. 'source_directories': ['/home'],
  198. 'repositories': [{'path': 'hostname.borg'}],
  199. 'local_path': 'borg2',
  200. 'bootstrap': {},
  201. }
  202. assert config_paths == {'/tmp/config.yaml'}
  203. assert logs == []
  204. def test_parse_configuration_applies_normalization_after_environment_variable_interpolation():
  205. mock_config_and_schema(
  206. '''
  207. location:
  208. source_directories:
  209. - /home
  210. repositories:
  211. - ${NO_EXIST:-user@hostname:repo}
  212. exclude_if_present: .nobackup
  213. '''
  214. )
  215. flexmock(os).should_receive('getenv').replace_with(lambda variable_name, default: default)
  216. config, config_paths, logs = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  217. assert config == {
  218. 'source_directories': ['/home'],
  219. 'repositories': [{'path': 'ssh://user@hostname/./repo'}],
  220. 'exclude_if_present': ['.nobackup'],
  221. 'bootstrap': {},
  222. }
  223. assert config_paths == {'/tmp/config.yaml'}
  224. assert logs