test_validate.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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_plausible_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. config_stream.name = 'config.yaml'
  18. if schema_yaml is None:
  19. schema_stream = open(module.schema_filename())
  20. else:
  21. schema_stream = io.StringIO(schema_yaml)
  22. schema_stream.name = 'schema.yaml'
  23. builtins = flexmock(sys.modules['builtins'])
  24. flexmock(module.os).should_receive('getcwd').and_return('/tmp')
  25. flexmock(module.os.path).should_receive('isabs').and_return(False)
  26. flexmock(module.os.path).should_receive('exists').and_return(True)
  27. builtins.should_receive('open').with_args('/tmp/config.yaml').and_return(config_stream)
  28. builtins.should_receive('open').with_args('/tmp/schema.yaml').and_return(schema_stream)
  29. def test_parse_configuration_transforms_file_into_mapping():
  30. mock_config_and_schema(
  31. '''
  32. location:
  33. source_directories:
  34. - /home
  35. - /etc
  36. repositories:
  37. - hostname.borg
  38. retention:
  39. keep_minutely: 60
  40. keep_hourly: 24
  41. keep_daily: 7
  42. consistency:
  43. checks:
  44. - name: repository
  45. - name: archives
  46. '''
  47. )
  48. config, logs = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  49. assert config == {
  50. 'location': {
  51. 'source_directories': ['/home', '/etc'],
  52. 'repositories': [{'path': 'hostname.borg'}],
  53. },
  54. 'retention': {'keep_daily': 7, 'keep_hourly': 24, 'keep_minutely': 60},
  55. 'consistency': {'checks': [{'name': 'repository'}, {'name': 'archives'}]},
  56. }
  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. location:
  63. source_directories:
  64. - "/home/{escaped_punctuation}"
  65. repositories:
  66. - test.borg
  67. '''
  68. )
  69. config, logs = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  70. assert config == {
  71. 'location': {
  72. 'source_directories': [f'/home/{string.punctuation}'],
  73. 'repositories': [{'path': 'test.borg'}],
  74. }
  75. }
  76. assert logs == []
  77. def test_parse_configuration_with_schema_lacking_examples_does_not_raise():
  78. mock_config_and_schema(
  79. '''
  80. location:
  81. source_directories:
  82. - /home
  83. repositories:
  84. - hostname.borg
  85. ''',
  86. '''
  87. map:
  88. location:
  89. required: true
  90. map:
  91. source_directories:
  92. required: true
  93. seq:
  94. - type: scalar
  95. repositories:
  96. required: true
  97. seq:
  98. - type: scalar
  99. ''',
  100. )
  101. module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  102. def test_parse_configuration_inlines_include():
  103. mock_config_and_schema(
  104. '''
  105. location:
  106. source_directories:
  107. - /home
  108. repositories:
  109. - 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, logs = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  124. assert config == {
  125. 'location': {'source_directories': ['/home'], 'repositories': [{'path': 'hostname.borg'}]},
  126. 'retention': {'keep_daily': 7, 'keep_hourly': 24},
  127. }
  128. assert logs == []
  129. def test_parse_configuration_merges_include():
  130. mock_config_and_schema(
  131. '''
  132. location:
  133. source_directories:
  134. - /home
  135. repositories:
  136. - hostname.borg
  137. retention:
  138. keep_daily: 1
  139. <<: !include include.yaml
  140. '''
  141. )
  142. builtins = flexmock(sys.modules['builtins'])
  143. include_file = io.StringIO(
  144. '''
  145. keep_daily: 7
  146. keep_hourly: 24
  147. '''
  148. )
  149. include_file.name = 'include.yaml'
  150. builtins.should_receive('open').with_args('/tmp/include.yaml').and_return(include_file)
  151. config, logs = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  152. assert config == {
  153. 'location': {'source_directories': ['/home'], 'repositories': [{'path': 'hostname.borg'}]},
  154. 'retention': {'keep_daily': 1, 'keep_hourly': 24},
  155. }
  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/schema.yaml').and_raise(FileNotFoundError)
  164. with pytest.raises(FileNotFoundError):
  165. module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  166. def test_parse_configuration_raises_for_syntax_error():
  167. mock_config_and_schema('foo:\nbar')
  168. with pytest.raises(ValueError):
  169. module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  170. def test_parse_configuration_raises_for_validation_error():
  171. mock_config_and_schema(
  172. '''
  173. location:
  174. source_directories: yes
  175. repositories:
  176. - hostname.borg
  177. '''
  178. )
  179. with pytest.raises(module.Validation_error):
  180. module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  181. def test_parse_configuration_applies_overrides():
  182. mock_config_and_schema(
  183. '''
  184. location:
  185. source_directories:
  186. - /home
  187. repositories:
  188. - hostname.borg
  189. local_path: borg1
  190. '''
  191. )
  192. config, logs = module.parse_configuration(
  193. '/tmp/config.yaml', '/tmp/schema.yaml', overrides=['location.local_path=borg2']
  194. )
  195. assert config == {
  196. 'location': {
  197. 'source_directories': ['/home'],
  198. 'repositories': [{'path': 'hostname.borg'}],
  199. 'local_path': 'borg2',
  200. }
  201. }
  202. assert logs == []
  203. def test_parse_configuration_applies_normalization():
  204. mock_config_and_schema(
  205. '''
  206. location:
  207. source_directories:
  208. - /home
  209. repositories:
  210. - hostname.borg
  211. exclude_if_present: .nobackup
  212. '''
  213. )
  214. config, logs = module.parse_configuration('/tmp/config.yaml', '/tmp/schema.yaml')
  215. assert config == {
  216. 'location': {
  217. 'source_directories': ['/home'],
  218. 'repositories': [{'path': 'hostname.borg'}],
  219. 'exclude_if_present': ['.nobackup'],
  220. }
  221. }
  222. assert logs == []