test_borgmatic.py 13 KB

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