test_borgmatic.py 11 KB

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