test_borgmatic.py 11 KB

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