test_borgmatic.py 30 KB

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