test_validate.py 7.5 KB

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