test_borgmatic.py 11 KB

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