test_borgmatic.py 15 KB

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