test_borgmatic.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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_make_error_log_records_generates_output_logs_for_message_only():
  86. flexmock(module).should_receive('log_record').replace_with(dict)
  87. logs = tuple(module.make_error_log_records('Error'))
  88. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  89. def test_make_error_log_records_generates_output_logs_for_called_process_error():
  90. flexmock(module).should_receive('log_record').replace_with(dict)
  91. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  92. logs = tuple(
  93. module.make_error_log_records(
  94. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output')
  95. )
  96. )
  97. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  98. assert any(log for log in logs if 'error output' in str(log))
  99. def test_make_error_log_records_generates_logs_for_value_error():
  100. flexmock(module).should_receive('log_record').replace_with(dict)
  101. logs = tuple(module.make_error_log_records('Error', ValueError()))
  102. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  103. def test_make_error_log_records_generates_logs_for_os_error():
  104. flexmock(module).should_receive('log_record').replace_with(dict)
  105. logs = tuple(module.make_error_log_records('Error', OSError()))
  106. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  107. def test_make_error_log_records_generates_nothing_for_other_error():
  108. flexmock(module).should_receive('log_record').replace_with(dict)
  109. logs = tuple(module.make_error_log_records('Error', KeyError()))
  110. assert logs == ()
  111. def test_collect_configuration_run_summary_logs_info_for_success():
  112. flexmock(module.command).should_receive('execute_hook').never()
  113. flexmock(module).should_receive('run_configuration').and_return([])
  114. arguments = {}
  115. logs = tuple(
  116. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  117. )
  118. assert {log.levelno for log in logs} == {logging.INFO}
  119. def test_collect_configuration_run_summary_executes_hooks_for_create():
  120. flexmock(module).should_receive('run_configuration').and_return([])
  121. arguments = {'create': flexmock(), 'global': flexmock(dry_run=False)}
  122. logs = tuple(
  123. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  124. )
  125. assert {log.levelno for log in logs} == {logging.INFO}
  126. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  127. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  128. flexmock(module).should_receive('run_configuration').and_return([])
  129. arguments = {'extract': flexmock(repository='repo')}
  130. logs = tuple(
  131. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  132. )
  133. assert {log.levelno for log in logs} == {logging.INFO}
  134. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  135. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  136. ValueError
  137. )
  138. expected_logs = (flexmock(),)
  139. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  140. arguments = {'extract': flexmock(repository='repo')}
  141. logs = tuple(
  142. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  143. )
  144. assert logs == expected_logs
  145. def test_collect_configuration_run_summary_logs_missing_configs_error():
  146. arguments = {'global': flexmock(config_paths=[])}
  147. expected_logs = (flexmock(),)
  148. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  149. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  150. assert logs == expected_logs
  151. def test_collect_configuration_run_summary_logs_pre_hook_error():
  152. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  153. expected_logs = (flexmock(),)
  154. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  155. arguments = {'create': flexmock(), 'global': flexmock(dry_run=False)}
  156. logs = tuple(
  157. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  158. )
  159. assert logs == expected_logs
  160. def test_collect_configuration_run_summary_logs_post_hook_error():
  161. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  162. flexmock(module).should_receive('run_configuration').and_return([])
  163. expected_logs = (flexmock(),)
  164. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  165. arguments = {'create': flexmock(), 'global': flexmock(dry_run=False)}
  166. logs = tuple(
  167. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  168. )
  169. assert expected_logs[0] in logs
  170. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  171. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  172. ValueError
  173. )
  174. expected_logs = (flexmock(),)
  175. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  176. arguments = {'list': flexmock(repository='repo', archive='test')}
  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_info_for_success_with_list():
  182. flexmock(module).should_receive('run_configuration').and_return([])
  183. arguments = {'list': flexmock(repository='repo', archive=None)}
  184. logs = tuple(
  185. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  186. )
  187. assert {log.levelno for log in logs} == {logging.INFO}
  188. def test_collect_configuration_run_summary_logs_run_configuration_error():
  189. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  190. flexmock(module).should_receive('run_configuration').and_return(
  191. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  192. )
  193. flexmock(module).should_receive('make_error_log_records').and_return([])
  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. )