test_borgmatic.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. import logging
  2. import subprocess
  3. import time
  4. from flexmock import flexmock
  5. import borgmatic.hooks.command
  6. from borgmatic.commands import borgmatic as module
  7. def test_run_configuration_runs_actions_for_each_repository():
  8. flexmock(module.borg_environment).should_receive('initialize')
  9. expected_results = [flexmock(), flexmock()]
  10. flexmock(module).should_receive('run_actions').and_return(expected_results[:1]).and_return(
  11. expected_results[1:]
  12. )
  13. config = {'location': {'repositories': ['foo', 'bar']}}
  14. arguments = {'global': flexmock(monitoring_verbosity=1)}
  15. results = list(module.run_configuration('test.yaml', config, arguments))
  16. assert results == expected_results
  17. def test_run_configuration_calls_hooks_for_prune_action():
  18. flexmock(module.borg_environment).should_receive('initialize')
  19. flexmock(module.command).should_receive('execute_hook').twice()
  20. flexmock(module.dispatch).should_receive('call_hooks').at_least().twice()
  21. flexmock(module).should_receive('run_actions').and_return([])
  22. config = {'location': {'repositories': ['foo']}}
  23. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'prune': flexmock()}
  24. list(module.run_configuration('test.yaml', config, arguments))
  25. def test_run_configuration_calls_hooks_for_compact_action():
  26. flexmock(module.borg_environment).should_receive('initialize')
  27. flexmock(module.command).should_receive('execute_hook').twice()
  28. flexmock(module).should_receive('run_actions').and_return([])
  29. config = {'location': {'repositories': ['foo']}}
  30. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'compact': flexmock()}
  31. list(module.run_configuration('test.yaml', config, arguments))
  32. def test_run_configuration_executes_and_calls_hooks_for_create_action():
  33. flexmock(module.borg_environment).should_receive('initialize')
  34. flexmock(module.command).should_receive('execute_hook').twice()
  35. flexmock(module.dispatch).should_receive('call_hooks').at_least().twice()
  36. flexmock(module).should_receive('run_actions').and_return([])
  37. config = {'location': {'repositories': ['foo']}}
  38. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  39. list(module.run_configuration('test.yaml', config, arguments))
  40. def test_run_configuration_calls_hooks_for_check_action():
  41. flexmock(module.borg_environment).should_receive('initialize')
  42. flexmock(module.command).should_receive('execute_hook').twice()
  43. flexmock(module.dispatch).should_receive('call_hooks').at_least().twice()
  44. flexmock(module).should_receive('run_actions').and_return([])
  45. config = {'location': {'repositories': ['foo']}}
  46. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'check': flexmock()}
  47. list(module.run_configuration('test.yaml', config, arguments))
  48. def test_run_configuration_calls_hooks_for_extract_action():
  49. flexmock(module.borg_environment).should_receive('initialize')
  50. flexmock(module.command).should_receive('execute_hook').twice()
  51. flexmock(module.dispatch).should_receive('call_hooks').never()
  52. flexmock(module).should_receive('run_actions').and_return([])
  53. config = {'location': {'repositories': ['foo']}}
  54. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'extract': flexmock()}
  55. list(module.run_configuration('test.yaml', config, arguments))
  56. def test_run_configuration_does_not_trigger_hooks_for_list_action():
  57. flexmock(module.borg_environment).should_receive('initialize')
  58. flexmock(module.command).should_receive('execute_hook').never()
  59. flexmock(module.dispatch).should_receive('call_hooks').never()
  60. flexmock(module).should_receive('run_actions').and_return([])
  61. config = {'location': {'repositories': ['foo']}}
  62. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'list': flexmock()}
  63. list(module.run_configuration('test.yaml', config, arguments))
  64. def test_run_configuration_logs_actions_error():
  65. flexmock(module.borg_environment).should_receive('initialize')
  66. flexmock(module.command).should_receive('execute_hook')
  67. flexmock(module.dispatch).should_receive('call_hooks')
  68. expected_results = [flexmock()]
  69. flexmock(module).should_receive('make_error_log_records').and_return(expected_results)
  70. flexmock(module).should_receive('run_actions').and_raise(OSError)
  71. config = {'location': {'repositories': ['foo']}}
  72. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  73. results = list(module.run_configuration('test.yaml', config, arguments))
  74. assert results == expected_results
  75. def test_run_configuration_logs_pre_hook_error():
  76. flexmock(module.borg_environment).should_receive('initialize')
  77. flexmock(module.command).should_receive('execute_hook').and_raise(OSError).and_return(None)
  78. expected_results = [flexmock()]
  79. flexmock(module).should_receive('make_error_log_records').and_return(expected_results)
  80. flexmock(module).should_receive('run_actions').never()
  81. config = {'location': {'repositories': ['foo']}}
  82. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  83. results = list(module.run_configuration('test.yaml', config, arguments))
  84. assert results == expected_results
  85. def test_run_configuration_bails_for_pre_hook_soft_failure():
  86. flexmock(module.borg_environment).should_receive('initialize')
  87. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  88. flexmock(module.command).should_receive('execute_hook').and_raise(error).and_return(None)
  89. flexmock(module).should_receive('make_error_log_records').never()
  90. flexmock(module).should_receive('run_actions').never()
  91. config = {'location': {'repositories': ['foo']}}
  92. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  93. results = list(module.run_configuration('test.yaml', config, arguments))
  94. assert results == []
  95. def test_run_configuration_logs_post_hook_error():
  96. flexmock(module.borg_environment).should_receive('initialize')
  97. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(
  98. OSError
  99. ).and_return(None)
  100. flexmock(module.dispatch).should_receive('call_hooks')
  101. expected_results = [flexmock()]
  102. flexmock(module).should_receive('make_error_log_records').and_return(expected_results)
  103. flexmock(module).should_receive('run_actions').and_return([])
  104. config = {'location': {'repositories': ['foo']}}
  105. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  106. results = list(module.run_configuration('test.yaml', config, arguments))
  107. assert results == expected_results
  108. def test_run_configuration_bails_for_post_hook_soft_failure():
  109. flexmock(module.borg_environment).should_receive('initialize')
  110. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  111. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(
  112. error
  113. ).and_return(None)
  114. flexmock(module.dispatch).should_receive('call_hooks')
  115. flexmock(module).should_receive('make_error_log_records').never()
  116. flexmock(module).should_receive('run_actions').and_return([])
  117. config = {'location': {'repositories': ['foo']}}
  118. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  119. results = list(module.run_configuration('test.yaml', config, arguments))
  120. assert results == []
  121. def test_run_configuration_logs_on_error_hook_error():
  122. flexmock(module.borg_environment).should_receive('initialize')
  123. flexmock(module.command).should_receive('execute_hook').and_raise(OSError)
  124. expected_results = [flexmock(), flexmock()]
  125. flexmock(module).should_receive('make_error_log_records').and_return(
  126. expected_results[:1]
  127. ).and_return(expected_results[1:])
  128. flexmock(module).should_receive('run_actions').and_raise(OSError)
  129. config = {'location': {'repositories': ['foo']}}
  130. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  131. results = list(module.run_configuration('test.yaml', config, arguments))
  132. assert results == expected_results
  133. def test_run_configuration_bails_for_on_error_hook_soft_failure():
  134. flexmock(module.borg_environment).should_receive('initialize')
  135. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  136. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(error)
  137. expected_results = [flexmock()]
  138. flexmock(module).should_receive('make_error_log_records').and_return(expected_results)
  139. flexmock(module).should_receive('run_actions').and_raise(OSError)
  140. config = {'location': {'repositories': ['foo']}}
  141. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  142. results = list(module.run_configuration('test.yaml', config, arguments))
  143. assert results == expected_results
  144. def test_run_configuration_retries_soft_error():
  145. # Run action first fails, second passes
  146. flexmock(module.borg_environment).should_receive('initialize')
  147. flexmock(module.command).should_receive('execute_hook')
  148. flexmock(module).should_receive('run_actions').and_raise(OSError).and_return([])
  149. expected_results = [flexmock()]
  150. flexmock(module).should_receive('make_error_log_records').and_return(expected_results).once()
  151. config = {'location': {'repositories': ['foo']}, 'storage': {'retries': 1}}
  152. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  153. results = list(module.run_configuration('test.yaml', config, arguments))
  154. assert results == expected_results
  155. def test_run_configuration_retries_hard_error():
  156. # Run action fails twice
  157. flexmock(module.borg_environment).should_receive('initialize')
  158. flexmock(module.command).should_receive('execute_hook')
  159. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  160. expected_results = [flexmock(), flexmock()]
  161. flexmock(module).should_receive('make_error_log_records').with_args(
  162. 'foo: Error running actions for repository', OSError
  163. ).and_return(expected_results[:1]).with_args(
  164. 'foo: Error running actions for repository', OSError
  165. ).and_return(
  166. expected_results[1:]
  167. ).twice()
  168. config = {'location': {'repositories': ['foo']}, 'storage': {'retries': 1}}
  169. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  170. results = list(module.run_configuration('test.yaml', config, arguments))
  171. assert results == expected_results
  172. def test_run_repos_ordered():
  173. flexmock(module.borg_environment).should_receive('initialize')
  174. flexmock(module.command).should_receive('execute_hook')
  175. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  176. expected_results = [flexmock(), flexmock()]
  177. flexmock(module).should_receive('make_error_log_records').with_args(
  178. 'foo: Error running actions for repository', OSError
  179. ).and_return(expected_results[:1]).ordered()
  180. flexmock(module).should_receive('make_error_log_records').with_args(
  181. 'bar: Error running actions for repository', OSError
  182. ).and_return(expected_results[1:]).ordered()
  183. config = {'location': {'repositories': ['foo', 'bar']}}
  184. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  185. results = list(module.run_configuration('test.yaml', config, arguments))
  186. assert results == expected_results
  187. def test_run_configuration_retries_round_robbin():
  188. flexmock(module.borg_environment).should_receive('initialize')
  189. flexmock(module.command).should_receive('execute_hook')
  190. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  191. expected_results = [flexmock(), flexmock(), flexmock(), flexmock()]
  192. flexmock(module).should_receive('make_error_log_records').with_args(
  193. 'foo: Error running actions for repository', OSError
  194. ).and_return(expected_results[0:1]).ordered()
  195. flexmock(module).should_receive('make_error_log_records').with_args(
  196. 'bar: Error running actions for repository', OSError
  197. ).and_return(expected_results[1:2]).ordered()
  198. flexmock(module).should_receive('make_error_log_records').with_args(
  199. 'foo: Error running actions for repository', OSError
  200. ).and_return(expected_results[2:3]).ordered()
  201. flexmock(module).should_receive('make_error_log_records').with_args(
  202. 'bar: Error running actions for repository', OSError
  203. ).and_return(expected_results[3:4]).ordered()
  204. config = {'location': {'repositories': ['foo', 'bar']}, 'storage': {'retries': 1}}
  205. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  206. results = list(module.run_configuration('test.yaml', config, arguments))
  207. assert results == expected_results
  208. def test_run_configuration_retries_one_passes():
  209. flexmock(module.borg_environment).should_receive('initialize')
  210. flexmock(module.command).should_receive('execute_hook')
  211. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  212. []
  213. ).and_raise(OSError).times(4)
  214. expected_results = [flexmock(), flexmock(), flexmock()]
  215. flexmock(module).should_receive('make_error_log_records').with_args(
  216. 'foo: Error running actions for repository', OSError
  217. ).and_return(expected_results[0:1]).ordered()
  218. flexmock(module).should_receive('make_error_log_records').with_args(
  219. 'bar: Error running actions for repository', OSError
  220. ).and_return(expected_results[1:2]).ordered()
  221. flexmock(module).should_receive('make_error_log_records').with_args(
  222. 'bar: Error running actions for repository', OSError
  223. ).and_return(expected_results[2:3]).ordered()
  224. config = {'location': {'repositories': ['foo', 'bar']}, 'storage': {'retries': 1}}
  225. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  226. results = list(module.run_configuration('test.yaml', config, arguments))
  227. assert results == expected_results
  228. def test_run_configuration_retry_wait():
  229. flexmock(module.borg_environment).should_receive('initialize')
  230. flexmock(module.command).should_receive('execute_hook')
  231. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  232. expected_results = [flexmock(), flexmock(), flexmock(), flexmock()]
  233. flexmock(module).should_receive('make_error_log_records').with_args(
  234. 'foo: Error running actions for repository', OSError
  235. ).and_return(expected_results[0:1]).ordered()
  236. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  237. flexmock(module).should_receive('make_error_log_records').with_args(
  238. 'foo: Error running actions for repository', OSError
  239. ).and_return(expected_results[1:2]).ordered()
  240. flexmock(time).should_receive('sleep').with_args(20).and_return().ordered()
  241. flexmock(module).should_receive('make_error_log_records').with_args(
  242. 'foo: Error running actions for repository', OSError
  243. ).and_return(expected_results[2:3]).ordered()
  244. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  245. flexmock(module).should_receive('make_error_log_records').with_args(
  246. 'foo: Error running actions for repository', OSError
  247. ).and_return(expected_results[3:4]).ordered()
  248. config = {'location': {'repositories': ['foo']}, 'storage': {'retries': 3, 'retry_wait': 10}}
  249. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  250. results = list(module.run_configuration('test.yaml', config, arguments))
  251. assert results == expected_results
  252. def test_run_configuration_retries_timeout_multiple_repos():
  253. flexmock(module.borg_environment).should_receive('initialize')
  254. flexmock(module.command).should_receive('execute_hook')
  255. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  256. []
  257. ).and_raise(OSError).times(4)
  258. expected_results = [flexmock(), flexmock(), flexmock()]
  259. flexmock(module).should_receive('make_error_log_records').with_args(
  260. 'foo: Error running actions for repository', OSError
  261. ).and_return(expected_results[0:1]).ordered()
  262. flexmock(module).should_receive('make_error_log_records').with_args(
  263. 'bar: Error running actions for repository', OSError
  264. ).and_return(expected_results[1:2]).ordered()
  265. # Sleep before retrying foo (and passing)
  266. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  267. # Sleep before retrying bar (and failing)
  268. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  269. flexmock(module).should_receive('make_error_log_records').with_args(
  270. 'bar: Error running actions for repository', OSError
  271. ).and_return(expected_results[2:3]).ordered()
  272. config = {
  273. 'location': {'repositories': ['foo', 'bar']},
  274. 'storage': {'retries': 1, 'retry_wait': 10},
  275. }
  276. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  277. results = list(module.run_configuration('test.yaml', config, arguments))
  278. assert results == expected_results
  279. def test_load_configurations_collects_parsed_configurations():
  280. configuration = flexmock()
  281. other_configuration = flexmock()
  282. flexmock(module.validate).should_receive('parse_configuration').and_return(
  283. configuration
  284. ).and_return(other_configuration)
  285. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  286. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  287. assert logs == []
  288. def test_load_configurations_logs_critical_for_parse_error():
  289. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  290. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  291. assert configs == {}
  292. assert {log.levelno for log in logs} == {logging.CRITICAL}
  293. def test_log_record_does_not_raise():
  294. module.log_record(levelno=1, foo='bar', baz='quux')
  295. def test_log_record_with_suppress_does_not_raise():
  296. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  297. def test_make_error_log_records_generates_output_logs_for_message_only():
  298. flexmock(module).should_receive('log_record').replace_with(dict)
  299. logs = tuple(module.make_error_log_records('Error'))
  300. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  301. def test_make_error_log_records_generates_output_logs_for_called_process_error():
  302. flexmock(module).should_receive('log_record').replace_with(dict)
  303. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  304. logs = tuple(
  305. module.make_error_log_records(
  306. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output')
  307. )
  308. )
  309. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  310. assert any(log for log in logs if 'error output' in str(log))
  311. def test_make_error_log_records_generates_logs_for_value_error():
  312. flexmock(module).should_receive('log_record').replace_with(dict)
  313. logs = tuple(module.make_error_log_records('Error', ValueError()))
  314. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  315. def test_make_error_log_records_generates_logs_for_os_error():
  316. flexmock(module).should_receive('log_record').replace_with(dict)
  317. logs = tuple(module.make_error_log_records('Error', OSError()))
  318. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  319. def test_make_error_log_records_generates_nothing_for_other_error():
  320. flexmock(module).should_receive('log_record').replace_with(dict)
  321. logs = tuple(module.make_error_log_records('Error', KeyError()))
  322. assert logs == ()
  323. def test_get_local_path_uses_configuration_value():
  324. assert module.get_local_path({'test.yaml': {'location': {'local_path': 'borg1'}}}) == 'borg1'
  325. def test_get_local_path_without_location_defaults_to_borg():
  326. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  327. def test_get_local_path_without_local_path_defaults_to_borg():
  328. assert module.get_local_path({'test.yaml': {'location': {}}}) == 'borg'
  329. def test_collect_configuration_run_summary_logs_info_for_success():
  330. flexmock(module.command).should_receive('execute_hook').never()
  331. flexmock(module).should_receive('run_configuration').and_return([])
  332. arguments = {}
  333. logs = tuple(
  334. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  335. )
  336. assert {log.levelno for log in logs} == {logging.INFO}
  337. def test_collect_configuration_run_summary_executes_hooks_for_create():
  338. flexmock(module).should_receive('run_configuration').and_return([])
  339. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  340. logs = tuple(
  341. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  342. )
  343. assert {log.levelno for log in logs} == {logging.INFO}
  344. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  345. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  346. flexmock(module).should_receive('run_configuration').and_return([])
  347. arguments = {'extract': flexmock(repository='repo')}
  348. logs = tuple(
  349. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  350. )
  351. assert {log.levelno for log in logs} == {logging.INFO}
  352. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  353. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  354. ValueError
  355. )
  356. expected_logs = (flexmock(),)
  357. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  358. arguments = {'extract': flexmock(repository='repo')}
  359. logs = tuple(
  360. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  361. )
  362. assert logs == expected_logs
  363. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  364. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  365. flexmock(module).should_receive('run_configuration').and_return([])
  366. arguments = {'mount': flexmock(repository='repo')}
  367. logs = tuple(
  368. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  369. )
  370. assert {log.levelno for log in logs} == {logging.INFO}
  371. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  372. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  373. ValueError
  374. )
  375. expected_logs = (flexmock(),)
  376. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  377. arguments = {'mount': flexmock(repository='repo')}
  378. logs = tuple(
  379. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  380. )
  381. assert logs == expected_logs
  382. def test_collect_configuration_run_summary_logs_missing_configs_error():
  383. arguments = {'global': flexmock(config_paths=[])}
  384. expected_logs = (flexmock(),)
  385. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  386. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  387. assert logs == expected_logs
  388. def test_collect_configuration_run_summary_logs_pre_hook_error():
  389. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  390. expected_logs = (flexmock(),)
  391. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  392. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  393. logs = tuple(
  394. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  395. )
  396. assert logs == expected_logs
  397. def test_collect_configuration_run_summary_logs_post_hook_error():
  398. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  399. flexmock(module).should_receive('run_configuration').and_return([])
  400. expected_logs = (flexmock(),)
  401. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  402. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  403. logs = tuple(
  404. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  405. )
  406. assert expected_logs[0] in logs
  407. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  408. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  409. ValueError
  410. )
  411. expected_logs = (flexmock(),)
  412. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  413. arguments = {'list': flexmock(repository='repo', archive='test')}
  414. logs = tuple(
  415. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  416. )
  417. assert logs == expected_logs
  418. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  419. flexmock(module).should_receive('run_configuration').and_return([])
  420. arguments = {'list': flexmock(repository='repo', archive=None)}
  421. logs = tuple(
  422. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  423. )
  424. assert {log.levelno for log in logs} == {logging.INFO}
  425. def test_collect_configuration_run_summary_logs_run_configuration_error():
  426. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  427. flexmock(module).should_receive('run_configuration').and_return(
  428. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  429. )
  430. flexmock(module).should_receive('make_error_log_records').and_return([])
  431. arguments = {}
  432. logs = tuple(
  433. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  434. )
  435. assert {log.levelno for log in logs} == {logging.CRITICAL}
  436. def test_collect_configuration_run_summary_logs_run_umount_error():
  437. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  438. flexmock(module).should_receive('run_configuration').and_return([])
  439. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  440. flexmock(module).should_receive('make_error_log_records').and_return(
  441. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  442. )
  443. arguments = {'umount': flexmock(mount_point='/mnt')}
  444. logs = tuple(
  445. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  446. )
  447. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  448. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  449. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  450. ['baz']
  451. )
  452. stdout = flexmock()
  453. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  454. flexmock(module.sys).stdout = stdout
  455. arguments = {}
  456. tuple(
  457. module.collect_configuration_run_summary_logs(
  458. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  459. )
  460. )