test_borgmatic.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import logging
  2. import subprocess
  3. from flexmock import flexmock
  4. from borgmatic.commands import borgmatic as module
  5. def test_run_configuration_runs_actions_for_each_repository():
  6. flexmock(module.borg_environment).should_receive('initialize')
  7. expected_results = [flexmock(), flexmock()]
  8. flexmock(module).should_receive('run_actions').and_return(expected_results[:1]).and_return(
  9. expected_results[1:]
  10. )
  11. config = {'location': {'repositories': ['foo', 'bar']}}
  12. arguments = {'global': flexmock()}
  13. results = list(module.run_configuration('test.yaml', config, arguments))
  14. assert results == expected_results
  15. def test_run_configuration_executes_hooks_for_create_action():
  16. flexmock(module.borg_environment).should_receive('initialize')
  17. flexmock(module.command).should_receive('execute_hook').twice()
  18. flexmock(module.dispatch).should_receive('call_hooks').twice()
  19. flexmock(module.healthchecks).should_receive('ping_healthchecks').twice()
  20. flexmock(module.cronitor).should_receive('ping_cronitor').twice()
  21. flexmock(module.cronhub).should_receive('ping_cronhub').twice()
  22. flexmock(module).should_receive('run_actions').and_return([])
  23. config = {'location': {'repositories': ['foo']}}
  24. arguments = {'global': flexmock(dry_run=False), 'create': flexmock()}
  25. list(module.run_configuration('test.yaml', config, arguments))
  26. def test_run_configuration_logs_actions_error():
  27. flexmock(module.borg_environment).should_receive('initialize')
  28. flexmock(module.command).should_receive('execute_hook')
  29. flexmock(module.dispatch).should_receive('call_hooks')
  30. flexmock(module.healthchecks).should_receive('ping_healthchecks')
  31. flexmock(module.cronitor).should_receive('ping_cronitor')
  32. flexmock(module.cronhub).should_receive('ping_cronhub')
  33. expected_results = [flexmock()]
  34. flexmock(module).should_receive('make_error_log_records').and_return(expected_results)
  35. flexmock(module).should_receive('run_actions').and_raise(OSError)
  36. config = {'location': {'repositories': ['foo']}}
  37. arguments = {'global': flexmock(dry_run=False)}
  38. results = list(module.run_configuration('test.yaml', config, arguments))
  39. assert results == expected_results
  40. def test_run_configuration_logs_pre_hook_error():
  41. flexmock(module.borg_environment).should_receive('initialize')
  42. flexmock(module.command).should_receive('execute_hook').and_raise(OSError).and_return(None)
  43. expected_results = [flexmock()]
  44. flexmock(module).should_receive('make_error_log_records').and_return(expected_results)
  45. flexmock(module).should_receive('run_actions').never()
  46. config = {'location': {'repositories': ['foo']}}
  47. arguments = {'global': flexmock(dry_run=False), 'create': flexmock()}
  48. results = list(module.run_configuration('test.yaml', config, arguments))
  49. assert results == expected_results
  50. def test_run_configuration_logs_post_hook_error():
  51. flexmock(module.borg_environment).should_receive('initialize')
  52. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(
  53. OSError
  54. ).and_return(None)
  55. flexmock(module.dispatch).should_receive('call_hooks')
  56. flexmock(module.healthchecks).should_receive('ping_healthchecks')
  57. flexmock(module.cronitor).should_receive('ping_cronitor')
  58. flexmock(module.cronhub).should_receive('ping_cronhub')
  59. expected_results = [flexmock()]
  60. flexmock(module).should_receive('make_error_log_records').and_return(expected_results)
  61. flexmock(module).should_receive('run_actions').and_return([])
  62. config = {'location': {'repositories': ['foo']}}
  63. arguments = {'global': flexmock(dry_run=False), 'create': flexmock()}
  64. results = list(module.run_configuration('test.yaml', config, arguments))
  65. assert results == expected_results
  66. def test_run_configuration_logs_on_error_hook_error():
  67. flexmock(module.borg_environment).should_receive('initialize')
  68. flexmock(module.command).should_receive('execute_hook').and_raise(OSError)
  69. expected_results = [flexmock(), flexmock()]
  70. flexmock(module).should_receive('make_error_log_records').and_return(
  71. expected_results[:1]
  72. ).and_return(expected_results[1:])
  73. flexmock(module).should_receive('run_actions').and_raise(OSError)
  74. config = {'location': {'repositories': ['foo']}}
  75. arguments = {'global': flexmock(dry_run=False)}
  76. results = list(module.run_configuration('test.yaml', config, arguments))
  77. assert results == expected_results
  78. def test_load_configurations_collects_parsed_configurations():
  79. configuration = flexmock()
  80. other_configuration = flexmock()
  81. flexmock(module.validate).should_receive('parse_configuration').and_return(
  82. configuration
  83. ).and_return(other_configuration)
  84. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  85. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  86. assert logs == []
  87. def test_load_configurations_logs_critical_for_parse_error():
  88. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  89. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  90. assert configs == {}
  91. assert {log.levelno for log in logs} == {logging.CRITICAL}
  92. def test_make_error_log_records_generates_output_logs_for_message_only():
  93. logs = tuple(module.make_error_log_records('Error'))
  94. assert {log.levelno for log in logs} == {logging.CRITICAL}
  95. def test_make_error_log_records_generates_output_logs_for_called_process_error():
  96. logs = tuple(
  97. module.make_error_log_records(
  98. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output')
  99. )
  100. )
  101. assert {log.levelno for log in logs} == {logging.CRITICAL}
  102. assert any(log for log in logs if 'error output' in str(log))
  103. def test_make_error_log_records_generates_logs_for_value_error():
  104. logs = tuple(module.make_error_log_records('Error', ValueError()))
  105. assert {log.levelno for log in logs} == {logging.CRITICAL}
  106. def test_make_error_log_records_generates_logs_for_os_error():
  107. logs = tuple(module.make_error_log_records('Error', OSError()))
  108. assert {log.levelno for log in logs} == {logging.CRITICAL}
  109. def test_make_error_log_records_generates_nothing_for_other_error():
  110. logs = tuple(module.make_error_log_records('Error', KeyError()))
  111. assert logs == ()
  112. def test_collect_configuration_run_summary_logs_info_for_success():
  113. flexmock(module.command).should_receive('execute_hook').never()
  114. flexmock(module).should_receive('run_configuration').and_return([])
  115. arguments = {}
  116. logs = tuple(
  117. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  118. )
  119. assert {log.levelno for log in logs} == {logging.INFO}
  120. def test_collect_configuration_run_summary_executes_hooks_for_create():
  121. flexmock(module).should_receive('run_configuration').and_return([])
  122. arguments = {'create': flexmock(), 'global': flexmock(dry_run=False)}
  123. logs = tuple(
  124. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  125. )
  126. assert {log.levelno for log in logs} == {logging.INFO}
  127. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  128. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  129. flexmock(module).should_receive('run_configuration').and_return([])
  130. arguments = {'extract': flexmock(repository='repo')}
  131. logs = tuple(
  132. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  133. )
  134. assert {log.levelno for log in logs} == {logging.INFO}
  135. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  136. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  137. ValueError
  138. )
  139. expected_logs = (flexmock(),)
  140. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  141. arguments = {'extract': flexmock(repository='repo')}
  142. logs = tuple(
  143. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  144. )
  145. assert logs == expected_logs
  146. def test_collect_configuration_run_summary_logs_missing_configs_error():
  147. arguments = {'global': flexmock(config_paths=[])}
  148. expected_logs = (flexmock(),)
  149. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  150. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  151. assert logs == expected_logs
  152. def test_collect_configuration_run_summary_logs_pre_hook_error():
  153. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  154. expected_logs = (flexmock(),)
  155. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  156. arguments = {'create': flexmock(), 'global': flexmock(dry_run=False)}
  157. logs = tuple(
  158. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  159. )
  160. assert logs == expected_logs
  161. def test_collect_configuration_run_summary_logs_post_hook_error():
  162. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  163. flexmock(module).should_receive('run_configuration').and_return([])
  164. expected_logs = (flexmock(),)
  165. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  166. arguments = {'create': flexmock(), 'global': flexmock(dry_run=False)}
  167. logs = tuple(
  168. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  169. )
  170. assert expected_logs[0] in logs
  171. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  172. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  173. ValueError
  174. )
  175. expected_logs = (flexmock(),)
  176. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  177. arguments = {'list': flexmock(repository='repo', archive='test')}
  178. logs = tuple(
  179. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  180. )
  181. assert logs == expected_logs
  182. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  183. flexmock(module).should_receive('run_configuration').and_return([])
  184. arguments = {'list': flexmock(repository='repo', archive=None)}
  185. logs = tuple(
  186. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  187. )
  188. assert {log.levelno for log in logs} == {logging.INFO}
  189. def test_collect_configuration_run_summary_logs_run_configuration_error():
  190. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  191. flexmock(module).should_receive('run_configuration').and_return(
  192. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  193. )
  194. arguments = {}
  195. logs = tuple(
  196. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  197. )
  198. assert {log.levelno for log in logs} == {logging.CRITICAL}
  199. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  200. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  201. ['baz']
  202. )
  203. flexmock(module.sys.stdout).should_receive('write').with_args('["foo", "bar", "baz"]').once()
  204. arguments = {}
  205. tuple(
  206. module.collect_configuration_run_summary_logs(
  207. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  208. )
  209. )