test_borgmatic.py 27 KB

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