test_borgmatic.py 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  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': [{'path': 'foo'}, {'path': '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).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  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_logs_monitor_start_error():
  28. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  29. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  30. flexmock(module.dispatch).should_receive('call_hooks').and_raise(OSError).and_return(
  31. None
  32. ).and_return(None).and_return(None)
  33. expected_results = [flexmock()]
  34. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  35. flexmock(module).should_receive('run_actions').never()
  36. config = {'location': {'repositories': ['foo']}}
  37. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  38. results = list(module.run_configuration('test.yaml', config, arguments))
  39. assert results == expected_results
  40. def test_run_configuration_bails_for_monitor_start_soft_failure():
  41. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  42. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  43. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  44. flexmock(module.dispatch).should_receive('call_hooks').and_raise(error)
  45. flexmock(module).should_receive('log_error_records').never()
  46. flexmock(module).should_receive('run_actions').never()
  47. config = {'location': {'repositories': ['foo']}}
  48. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  49. results = list(module.run_configuration('test.yaml', config, arguments))
  50. assert results == []
  51. def test_run_configuration_logs_actions_error():
  52. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  53. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  54. flexmock(module.command).should_receive('execute_hook')
  55. flexmock(module.dispatch).should_receive('call_hooks')
  56. expected_results = [flexmock()]
  57. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  58. flexmock(module).should_receive('run_actions').and_raise(OSError)
  59. config = {'location': {'repositories': [{'path': 'foo'}]}}
  60. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  61. results = list(module.run_configuration('test.yaml', config, arguments))
  62. assert results == expected_results
  63. def test_run_configuration_bails_for_actions_soft_failure():
  64. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  65. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  66. flexmock(module.dispatch).should_receive('call_hooks')
  67. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  68. flexmock(module).should_receive('run_actions').and_raise(error)
  69. flexmock(module).should_receive('log_error_records').never()
  70. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  71. config = {'location': {'repositories': [{'path': 'foo'}]}}
  72. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  73. results = list(module.run_configuration('test.yaml', config, arguments))
  74. assert results == []
  75. def test_run_configuration_logs_monitor_log_error():
  76. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  77. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  78. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  79. None
  80. ).and_raise(OSError)
  81. expected_results = [flexmock()]
  82. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  83. flexmock(module).should_receive('run_actions').and_return([])
  84. config = {'location': {'repositories': [{'path': '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 == expected_results
  88. def test_run_configuration_bails_for_monitor_log_soft_failure():
  89. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  90. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  91. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  92. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  93. None
  94. ).and_raise(error)
  95. flexmock(module).should_receive('log_error_records').never()
  96. flexmock(module).should_receive('run_actions').and_return([])
  97. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  98. config = {'location': {'repositories': [{'path': 'foo'}]}}
  99. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  100. results = list(module.run_configuration('test.yaml', config, arguments))
  101. assert results == []
  102. def test_run_configuration_logs_monitor_finish_error():
  103. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  104. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  105. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  106. None
  107. ).and_return(None).and_raise(OSError)
  108. expected_results = [flexmock()]
  109. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  110. flexmock(module).should_receive('run_actions').and_return([])
  111. config = {'location': {'repositories': [{'path': 'foo'}]}}
  112. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  113. results = list(module.run_configuration('test.yaml', config, arguments))
  114. assert results == expected_results
  115. def test_run_configuration_bails_for_monitor_finish_soft_failure():
  116. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  117. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  118. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  119. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  120. None
  121. ).and_raise(None).and_raise(error)
  122. flexmock(module).should_receive('log_error_records').never()
  123. flexmock(module).should_receive('run_actions').and_return([])
  124. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  125. config = {'location': {'repositories': [{'path': 'foo'}]}}
  126. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  127. results = list(module.run_configuration('test.yaml', config, arguments))
  128. assert results == []
  129. def test_run_configuration_does_not_call_monitoring_hooks_if_monitoring_hooks_are_disabled():
  130. flexmock(module).should_receive('verbosity_to_log_level').and_return(module.DISABLED)
  131. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  132. flexmock(module.dispatch).should_receive('call_hooks').never()
  133. flexmock(module).should_receive('run_actions').and_return([])
  134. config = {'location': {'repositories': [{'path': 'foo'}]}}
  135. arguments = {'global': flexmock(monitoring_verbosity=-2, dry_run=False), 'create': flexmock()}
  136. results = list(module.run_configuration('test.yaml', config, arguments))
  137. assert results == []
  138. def test_run_configuration_logs_on_error_hook_error():
  139. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  140. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  141. flexmock(module.command).should_receive('execute_hook').and_raise(OSError)
  142. expected_results = [flexmock(), flexmock()]
  143. flexmock(module).should_receive('log_error_records').and_return(
  144. expected_results[:1]
  145. ).and_return(expected_results[1:])
  146. flexmock(module).should_receive('run_actions').and_raise(OSError)
  147. config = {'location': {'repositories': [{'path': 'foo'}]}}
  148. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  149. results = list(module.run_configuration('test.yaml', config, arguments))
  150. assert results == expected_results
  151. def test_run_configuration_bails_for_on_error_hook_soft_failure():
  152. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  153. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  154. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  155. flexmock(module.command).should_receive('execute_hook').and_raise(error)
  156. expected_results = [flexmock()]
  157. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  158. flexmock(module).should_receive('run_actions').and_raise(OSError)
  159. config = {'location': {'repositories': [{'path': 'foo'}]}}
  160. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  161. results = list(module.run_configuration('test.yaml', config, arguments))
  162. assert results == expected_results
  163. def test_run_configuration_retries_soft_error():
  164. # Run action first fails, second passes
  165. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  166. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  167. flexmock(module.command).should_receive('execute_hook')
  168. flexmock(module).should_receive('run_actions').and_raise(OSError).and_return([])
  169. flexmock(module).should_receive('log_error_records').and_return([flexmock()]).once()
  170. config = {'location': {'repositories': [{'path': 'foo'}]}, 'storage': {'retries': 1}}
  171. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  172. results = list(module.run_configuration('test.yaml', config, arguments))
  173. assert results == []
  174. def test_run_configuration_retries_hard_error():
  175. # Run action fails twice
  176. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  177. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  178. flexmock(module.command).should_receive('execute_hook')
  179. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  180. flexmock(module).should_receive('log_error_records').with_args(
  181. 'foo: Error running actions for repository',
  182. OSError,
  183. levelno=logging.WARNING,
  184. log_command_error_output=True,
  185. ).and_return([flexmock()])
  186. error_logs = [flexmock()]
  187. flexmock(module).should_receive('log_error_records').with_args(
  188. 'foo: Error running actions for repository',
  189. OSError,
  190. ).and_return(error_logs)
  191. config = {'location': {'repositories': [{'path': 'foo'}]}, 'storage': {'retries': 1}}
  192. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  193. results = list(module.run_configuration('test.yaml', config, arguments))
  194. assert results == error_logs
  195. def test_run_configuration_repos_ordered():
  196. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  197. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  198. flexmock(module.command).should_receive('execute_hook')
  199. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  200. expected_results = [flexmock(), flexmock()]
  201. flexmock(module).should_receive('log_error_records').with_args(
  202. 'foo: Error running actions for repository', OSError
  203. ).and_return(expected_results[:1]).ordered()
  204. flexmock(module).should_receive('log_error_records').with_args(
  205. 'bar: Error running actions for repository', OSError
  206. ).and_return(expected_results[1:]).ordered()
  207. config = {'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}}
  208. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  209. results = list(module.run_configuration('test.yaml', config, arguments))
  210. assert results == expected_results
  211. def test_run_configuration_retries_round_robin():
  212. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  213. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  214. flexmock(module.command).should_receive('execute_hook')
  215. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  216. flexmock(module).should_receive('log_error_records').with_args(
  217. 'foo: Error running actions for repository',
  218. OSError,
  219. levelno=logging.WARNING,
  220. log_command_error_output=True,
  221. ).and_return([flexmock()]).ordered()
  222. flexmock(module).should_receive('log_error_records').with_args(
  223. 'bar: Error running actions for repository',
  224. OSError,
  225. levelno=logging.WARNING,
  226. log_command_error_output=True,
  227. ).and_return([flexmock()]).ordered()
  228. foo_error_logs = [flexmock()]
  229. flexmock(module).should_receive('log_error_records').with_args(
  230. 'foo: Error running actions for repository', OSError
  231. ).and_return(foo_error_logs).ordered()
  232. bar_error_logs = [flexmock()]
  233. flexmock(module).should_receive('log_error_records').with_args(
  234. 'bar: Error running actions for repository', OSError
  235. ).and_return(bar_error_logs).ordered()
  236. config = {
  237. 'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]},
  238. 'storage': {'retries': 1},
  239. }
  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).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  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 = {
  267. 'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]},
  268. 'storage': {'retries': 1},
  269. }
  270. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  271. results = list(module.run_configuration('test.yaml', config, arguments))
  272. assert results == error_logs
  273. def test_run_configuration_retry_wait():
  274. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  275. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  276. flexmock(module.command).should_receive('execute_hook')
  277. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  278. flexmock(module).should_receive('log_error_records').with_args(
  279. 'foo: Error running actions for repository',
  280. OSError,
  281. levelno=logging.WARNING,
  282. log_command_error_output=True,
  283. ).and_return([flexmock()]).ordered()
  284. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  285. flexmock(module).should_receive('log_error_records').with_args(
  286. 'foo: Error running actions for repository',
  287. OSError,
  288. levelno=logging.WARNING,
  289. log_command_error_output=True,
  290. ).and_return([flexmock()]).ordered()
  291. flexmock(time).should_receive('sleep').with_args(20).and_return().ordered()
  292. flexmock(module).should_receive('log_error_records').with_args(
  293. 'foo: Error running actions for repository',
  294. OSError,
  295. levelno=logging.WARNING,
  296. log_command_error_output=True,
  297. ).and_return([flexmock()]).ordered()
  298. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  299. error_logs = [flexmock()]
  300. flexmock(module).should_receive('log_error_records').with_args(
  301. 'foo: Error running actions for repository', OSError
  302. ).and_return(error_logs).ordered()
  303. config = {
  304. 'location': {'repositories': [{'path': 'foo'}]},
  305. 'storage': {'retries': 3, 'retry_wait': 10},
  306. }
  307. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  308. results = list(module.run_configuration('test.yaml', config, arguments))
  309. assert results == error_logs
  310. def test_run_configuration_retries_timeout_multiple_repos():
  311. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  312. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  313. flexmock(module.command).should_receive('execute_hook')
  314. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  315. []
  316. ).and_raise(OSError).times(4)
  317. flexmock(module).should_receive('log_error_records').with_args(
  318. 'foo: Error running actions for repository',
  319. OSError,
  320. levelno=logging.WARNING,
  321. log_command_error_output=True,
  322. ).and_return([flexmock()]).ordered()
  323. flexmock(module).should_receive('log_error_records').with_args(
  324. 'bar: Error running actions for repository',
  325. OSError,
  326. levelno=logging.WARNING,
  327. log_command_error_output=True,
  328. ).and_return([flexmock()]).ordered()
  329. # Sleep before retrying foo (and passing)
  330. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  331. # Sleep before retrying bar (and failing)
  332. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  333. error_logs = [flexmock()]
  334. flexmock(module).should_receive('log_error_records').with_args(
  335. 'bar: Error running actions for repository', OSError
  336. ).and_return(error_logs).ordered()
  337. config = {
  338. 'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]},
  339. 'storage': {'retries': 1, 'retry_wait': 10},
  340. }
  341. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  342. results = list(module.run_configuration('test.yaml', config, arguments))
  343. assert results == error_logs
  344. def test_run_actions_runs_rcreate():
  345. flexmock(module).should_receive('add_custom_log_levels')
  346. flexmock(module.command).should_receive('execute_hook')
  347. flexmock(borgmatic.actions.rcreate).should_receive('run_rcreate').once()
  348. tuple(
  349. module.run_actions(
  350. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rcreate': flexmock()},
  351. config_filename=flexmock(),
  352. location={'repositories': []},
  353. storage=flexmock(),
  354. retention=flexmock(),
  355. consistency=flexmock(),
  356. hooks={},
  357. local_path=flexmock(),
  358. remote_path=flexmock(),
  359. local_borg_version=flexmock(),
  360. repository={'path': 'repo'},
  361. )
  362. )
  363. def test_run_actions_adds_log_file_to_hook_context():
  364. flexmock(module).should_receive('add_custom_log_levels')
  365. flexmock(module.command).should_receive('execute_hook')
  366. expected = flexmock()
  367. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  368. config_filename=object,
  369. repository={'path': 'repo'},
  370. location={'repositories': []},
  371. storage=object,
  372. hooks={},
  373. hook_context={'repository': 'repo', 'repositories': '', 'log_file': 'foo'},
  374. local_borg_version=object,
  375. create_arguments=object,
  376. global_arguments=object,
  377. dry_run_label='',
  378. local_path=object,
  379. remote_path=object,
  380. ).once().and_return(expected)
  381. result = tuple(
  382. module.run_actions(
  383. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  384. config_filename=flexmock(),
  385. location={'repositories': []},
  386. storage=flexmock(),
  387. retention=flexmock(),
  388. consistency=flexmock(),
  389. hooks={},
  390. local_path=flexmock(),
  391. remote_path=flexmock(),
  392. local_borg_version=flexmock(),
  393. repository={'path': 'repo'},
  394. )
  395. )
  396. assert result == (expected,)
  397. def test_run_actions_runs_transfer():
  398. flexmock(module).should_receive('add_custom_log_levels')
  399. flexmock(module.command).should_receive('execute_hook')
  400. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  401. tuple(
  402. module.run_actions(
  403. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'transfer': flexmock()},
  404. config_filename=flexmock(),
  405. location={'repositories': []},
  406. storage=flexmock(),
  407. retention=flexmock(),
  408. consistency=flexmock(),
  409. hooks={},
  410. local_path=flexmock(),
  411. remote_path=flexmock(),
  412. local_borg_version=flexmock(),
  413. repository={'path': 'repo'},
  414. )
  415. )
  416. def test_run_actions_runs_create():
  417. flexmock(module).should_receive('add_custom_log_levels')
  418. flexmock(module.command).should_receive('execute_hook')
  419. expected = flexmock()
  420. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  421. result = tuple(
  422. module.run_actions(
  423. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  424. config_filename=flexmock(),
  425. location={'repositories': []},
  426. storage=flexmock(),
  427. retention=flexmock(),
  428. consistency=flexmock(),
  429. hooks={},
  430. local_path=flexmock(),
  431. remote_path=flexmock(),
  432. local_borg_version=flexmock(),
  433. repository={'path': 'repo'},
  434. )
  435. )
  436. assert result == (expected,)
  437. def test_run_actions_runs_prune():
  438. flexmock(module).should_receive('add_custom_log_levels')
  439. flexmock(module.command).should_receive('execute_hook')
  440. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  441. tuple(
  442. module.run_actions(
  443. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  444. config_filename=flexmock(),
  445. location={'repositories': []},
  446. storage=flexmock(),
  447. retention=flexmock(),
  448. consistency=flexmock(),
  449. hooks={},
  450. local_path=flexmock(),
  451. remote_path=flexmock(),
  452. local_borg_version=flexmock(),
  453. repository={'path': 'repo'},
  454. )
  455. )
  456. def test_run_actions_runs_compact():
  457. flexmock(module).should_receive('add_custom_log_levels')
  458. flexmock(module.command).should_receive('execute_hook')
  459. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  460. tuple(
  461. module.run_actions(
  462. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  463. config_filename=flexmock(),
  464. location={'repositories': []},
  465. storage=flexmock(),
  466. retention=flexmock(),
  467. consistency=flexmock(),
  468. hooks={},
  469. local_path=flexmock(),
  470. remote_path=flexmock(),
  471. local_borg_version=flexmock(),
  472. repository={'path': 'repo'},
  473. )
  474. )
  475. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  476. flexmock(module).should_receive('add_custom_log_levels')
  477. flexmock(module.command).should_receive('execute_hook')
  478. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  479. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  480. tuple(
  481. module.run_actions(
  482. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  483. config_filename=flexmock(),
  484. location={'repositories': []},
  485. storage=flexmock(),
  486. retention=flexmock(),
  487. consistency=flexmock(),
  488. hooks={},
  489. local_path=flexmock(),
  490. remote_path=flexmock(),
  491. local_borg_version=flexmock(),
  492. repository={'path': 'repo'},
  493. )
  494. )
  495. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  496. flexmock(module).should_receive('add_custom_log_levels')
  497. flexmock(module.command).should_receive('execute_hook')
  498. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  499. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  500. tuple(
  501. module.run_actions(
  502. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  503. config_filename=flexmock(),
  504. location={'repositories': []},
  505. storage=flexmock(),
  506. retention=flexmock(),
  507. consistency=flexmock(),
  508. hooks={},
  509. local_path=flexmock(),
  510. remote_path=flexmock(),
  511. local_borg_version=flexmock(),
  512. repository={'path': 'repo'},
  513. )
  514. )
  515. def test_run_actions_runs_extract():
  516. flexmock(module).should_receive('add_custom_log_levels')
  517. flexmock(module.command).should_receive('execute_hook')
  518. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  519. tuple(
  520. module.run_actions(
  521. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'extract': flexmock()},
  522. config_filename=flexmock(),
  523. location={'repositories': []},
  524. storage=flexmock(),
  525. retention=flexmock(),
  526. consistency=flexmock(),
  527. hooks={},
  528. local_path=flexmock(),
  529. remote_path=flexmock(),
  530. local_borg_version=flexmock(),
  531. repository={'path': 'repo'},
  532. )
  533. )
  534. def test_run_actions_runs_export_tar():
  535. flexmock(module).should_receive('add_custom_log_levels')
  536. flexmock(module.command).should_receive('execute_hook')
  537. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  538. tuple(
  539. module.run_actions(
  540. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export-tar': flexmock()},
  541. config_filename=flexmock(),
  542. location={'repositories': []},
  543. storage=flexmock(),
  544. retention=flexmock(),
  545. consistency=flexmock(),
  546. hooks={},
  547. local_path=flexmock(),
  548. remote_path=flexmock(),
  549. local_borg_version=flexmock(),
  550. repository={'path': 'repo'},
  551. )
  552. )
  553. def test_run_actions_runs_mount():
  554. flexmock(module).should_receive('add_custom_log_levels')
  555. flexmock(module.command).should_receive('execute_hook')
  556. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  557. tuple(
  558. module.run_actions(
  559. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'mount': flexmock()},
  560. config_filename=flexmock(),
  561. location={'repositories': []},
  562. storage=flexmock(),
  563. retention=flexmock(),
  564. consistency=flexmock(),
  565. hooks={},
  566. local_path=flexmock(),
  567. remote_path=flexmock(),
  568. local_borg_version=flexmock(),
  569. repository={'path': 'repo'},
  570. )
  571. )
  572. def test_run_actions_runs_restore():
  573. flexmock(module).should_receive('add_custom_log_levels')
  574. flexmock(module.command).should_receive('execute_hook')
  575. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  576. tuple(
  577. module.run_actions(
  578. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'restore': flexmock()},
  579. config_filename=flexmock(),
  580. location={'repositories': []},
  581. storage=flexmock(),
  582. retention=flexmock(),
  583. consistency=flexmock(),
  584. hooks={},
  585. local_path=flexmock(),
  586. remote_path=flexmock(),
  587. local_borg_version=flexmock(),
  588. repository={'path': 'repo'},
  589. )
  590. )
  591. def test_run_actions_runs_rlist():
  592. flexmock(module).should_receive('add_custom_log_levels')
  593. flexmock(module.command).should_receive('execute_hook')
  594. expected = flexmock()
  595. flexmock(borgmatic.actions.rlist).should_receive('run_rlist').and_yield(expected).once()
  596. result = tuple(
  597. module.run_actions(
  598. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rlist': flexmock()},
  599. config_filename=flexmock(),
  600. location={'repositories': []},
  601. storage=flexmock(),
  602. retention=flexmock(),
  603. consistency=flexmock(),
  604. hooks={},
  605. local_path=flexmock(),
  606. remote_path=flexmock(),
  607. local_borg_version=flexmock(),
  608. repository={'path': 'repo'},
  609. )
  610. )
  611. assert result == (expected,)
  612. def test_run_actions_runs_list():
  613. flexmock(module).should_receive('add_custom_log_levels')
  614. flexmock(module.command).should_receive('execute_hook')
  615. expected = flexmock()
  616. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  617. result = tuple(
  618. module.run_actions(
  619. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'list': flexmock()},
  620. config_filename=flexmock(),
  621. location={'repositories': []},
  622. storage=flexmock(),
  623. retention=flexmock(),
  624. consistency=flexmock(),
  625. hooks={},
  626. local_path=flexmock(),
  627. remote_path=flexmock(),
  628. local_borg_version=flexmock(),
  629. repository={'path': 'repo'},
  630. )
  631. )
  632. assert result == (expected,)
  633. def test_run_actions_runs_rinfo():
  634. flexmock(module).should_receive('add_custom_log_levels')
  635. flexmock(module.command).should_receive('execute_hook')
  636. expected = flexmock()
  637. flexmock(borgmatic.actions.rinfo).should_receive('run_rinfo').and_yield(expected).once()
  638. result = tuple(
  639. module.run_actions(
  640. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rinfo': flexmock()},
  641. config_filename=flexmock(),
  642. location={'repositories': []},
  643. storage=flexmock(),
  644. retention=flexmock(),
  645. consistency=flexmock(),
  646. hooks={},
  647. local_path=flexmock(),
  648. remote_path=flexmock(),
  649. local_borg_version=flexmock(),
  650. repository={'path': 'repo'},
  651. )
  652. )
  653. assert result == (expected,)
  654. def test_run_actions_runs_info():
  655. flexmock(module).should_receive('add_custom_log_levels')
  656. flexmock(module.command).should_receive('execute_hook')
  657. expected = flexmock()
  658. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  659. result = tuple(
  660. module.run_actions(
  661. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'info': flexmock()},
  662. config_filename=flexmock(),
  663. location={'repositories': []},
  664. storage=flexmock(),
  665. retention=flexmock(),
  666. consistency=flexmock(),
  667. hooks={},
  668. local_path=flexmock(),
  669. remote_path=flexmock(),
  670. local_borg_version=flexmock(),
  671. repository={'path': 'repo'},
  672. )
  673. )
  674. assert result == (expected,)
  675. def test_run_actions_runs_break_lock():
  676. flexmock(module).should_receive('add_custom_log_levels')
  677. flexmock(module.command).should_receive('execute_hook')
  678. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  679. tuple(
  680. module.run_actions(
  681. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'break-lock': flexmock()},
  682. config_filename=flexmock(),
  683. location={'repositories': []},
  684. storage=flexmock(),
  685. retention=flexmock(),
  686. consistency=flexmock(),
  687. hooks={},
  688. local_path=flexmock(),
  689. remote_path=flexmock(),
  690. local_borg_version=flexmock(),
  691. repository={'path': 'repo'},
  692. )
  693. )
  694. def test_run_actions_runs_borg():
  695. flexmock(module).should_receive('add_custom_log_levels')
  696. flexmock(module.command).should_receive('execute_hook')
  697. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  698. tuple(
  699. module.run_actions(
  700. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'borg': flexmock()},
  701. config_filename=flexmock(),
  702. location={'repositories': []},
  703. storage=flexmock(),
  704. retention=flexmock(),
  705. consistency=flexmock(),
  706. hooks={},
  707. local_path=flexmock(),
  708. remote_path=flexmock(),
  709. local_borg_version=flexmock(),
  710. repository={'path': 'repo'},
  711. )
  712. )
  713. def test_run_actions_runs_multiple_actions_in_argument_order():
  714. flexmock(module).should_receive('add_custom_log_levels')
  715. flexmock(module.command).should_receive('execute_hook')
  716. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  717. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  718. tuple(
  719. module.run_actions(
  720. arguments={
  721. 'global': flexmock(dry_run=False, log_file='foo'),
  722. 'borg': flexmock(),
  723. 'restore': flexmock(),
  724. },
  725. config_filename=flexmock(),
  726. location={'repositories': []},
  727. storage=flexmock(),
  728. retention=flexmock(),
  729. consistency=flexmock(),
  730. hooks={},
  731. local_path=flexmock(),
  732. remote_path=flexmock(),
  733. local_borg_version=flexmock(),
  734. repository={'path': 'repo'},
  735. )
  736. )
  737. def test_load_configurations_collects_parsed_configurations_and_logs():
  738. configuration = flexmock()
  739. other_configuration = flexmock()
  740. test_expected_logs = [flexmock(), flexmock()]
  741. other_expected_logs = [flexmock(), flexmock()]
  742. flexmock(module.validate).should_receive('parse_configuration').and_return(
  743. configuration, test_expected_logs
  744. ).and_return(other_configuration, other_expected_logs)
  745. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  746. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  747. assert logs == test_expected_logs + other_expected_logs
  748. def test_load_configurations_logs_warning_for_permission_error():
  749. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  750. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  751. assert configs == {}
  752. assert {log.levelno for log in logs} == {logging.WARNING}
  753. def test_load_configurations_logs_critical_for_parse_error():
  754. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  755. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  756. assert configs == {}
  757. assert {log.levelno for log in logs} == {logging.CRITICAL}
  758. def test_log_record_does_not_raise():
  759. module.log_record(levelno=1, foo='bar', baz='quux')
  760. def test_log_record_with_suppress_does_not_raise():
  761. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  762. def test_log_error_records_generates_output_logs_for_message_only():
  763. flexmock(module).should_receive('log_record').replace_with(dict)
  764. logs = tuple(module.log_error_records('Error'))
  765. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  766. def test_log_error_records_generates_output_logs_for_called_process_error():
  767. flexmock(module).should_receive('log_record').replace_with(dict)
  768. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  769. logs = tuple(
  770. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  771. )
  772. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  773. assert any(log for log in logs if 'error output' in str(log))
  774. def test_log_error_records_generates_logs_for_value_error():
  775. flexmock(module).should_receive('log_record').replace_with(dict)
  776. logs = tuple(module.log_error_records('Error', ValueError()))
  777. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  778. def test_log_error_records_generates_logs_for_os_error():
  779. flexmock(module).should_receive('log_record').replace_with(dict)
  780. logs = tuple(module.log_error_records('Error', OSError()))
  781. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  782. def test_log_error_records_generates_nothing_for_other_error():
  783. flexmock(module).should_receive('log_record').replace_with(dict)
  784. logs = tuple(module.log_error_records('Error', KeyError()))
  785. assert logs == ()
  786. def test_get_local_path_uses_configuration_value():
  787. assert module.get_local_path({'test.yaml': {'location': {'local_path': 'borg1'}}}) == 'borg1'
  788. def test_get_local_path_without_location_defaults_to_borg():
  789. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  790. def test_get_local_path_without_local_path_defaults_to_borg():
  791. assert module.get_local_path({'test.yaml': {'location': {}}}) == 'borg'
  792. def test_collect_configuration_run_summary_logs_info_for_success():
  793. flexmock(module.command).should_receive('execute_hook').never()
  794. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  795. flexmock(module).should_receive('run_configuration').and_return([])
  796. arguments = {}
  797. logs = tuple(
  798. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  799. )
  800. assert {log.levelno for log in logs} == {logging.INFO}
  801. def test_collect_configuration_run_summary_executes_hooks_for_create():
  802. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  803. flexmock(module).should_receive('run_configuration').and_return([])
  804. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  805. logs = tuple(
  806. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  807. )
  808. assert {log.levelno for log in logs} == {logging.INFO}
  809. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  810. flexmock(module.validate).should_receive('guard_single_repository_selected')
  811. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  812. flexmock(module).should_receive('run_configuration').and_return([])
  813. arguments = {'extract': flexmock(repository='repo')}
  814. logs = tuple(
  815. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  816. )
  817. assert {log.levelno for log in logs} == {logging.INFO}
  818. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  819. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  820. ValueError
  821. )
  822. expected_logs = (flexmock(),)
  823. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  824. arguments = {'extract': flexmock(repository='repo')}
  825. logs = tuple(
  826. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  827. )
  828. assert logs == expected_logs
  829. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  830. flexmock(module.validate).should_receive('guard_single_repository_selected')
  831. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  832. flexmock(module).should_receive('run_configuration').and_return([])
  833. arguments = {'mount': flexmock(repository='repo')}
  834. logs = tuple(
  835. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  836. )
  837. assert {log.levelno for log in logs} == {logging.INFO}
  838. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  839. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  840. ValueError
  841. )
  842. expected_logs = (flexmock(),)
  843. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  844. arguments = {'mount': flexmock(repository='repo')}
  845. logs = tuple(
  846. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  847. )
  848. assert logs == expected_logs
  849. def test_collect_configuration_run_summary_logs_missing_configs_error():
  850. arguments = {'global': flexmock(config_paths=[])}
  851. expected_logs = (flexmock(),)
  852. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  853. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  854. assert logs == expected_logs
  855. def test_collect_configuration_run_summary_logs_pre_hook_error():
  856. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  857. expected_logs = (flexmock(),)
  858. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  859. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  860. logs = tuple(
  861. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  862. )
  863. assert logs == expected_logs
  864. def test_collect_configuration_run_summary_logs_post_hook_error():
  865. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  866. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  867. flexmock(module).should_receive('run_configuration').and_return([])
  868. expected_logs = (flexmock(),)
  869. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  870. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  871. logs = tuple(
  872. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  873. )
  874. assert expected_logs[0] in logs
  875. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  876. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  877. ValueError
  878. )
  879. expected_logs = (flexmock(),)
  880. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  881. arguments = {'list': flexmock(repository='repo', archive='test')}
  882. logs = tuple(
  883. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  884. )
  885. assert logs == expected_logs
  886. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  887. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  888. flexmock(module).should_receive('run_configuration').and_return([])
  889. arguments = {'list': flexmock(repository='repo', archive=None)}
  890. logs = tuple(
  891. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  892. )
  893. assert {log.levelno for log in logs} == {logging.INFO}
  894. def test_collect_configuration_run_summary_logs_run_configuration_error():
  895. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  896. flexmock(module).should_receive('run_configuration').and_return(
  897. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  898. )
  899. flexmock(module).should_receive('log_error_records').and_return([])
  900. arguments = {}
  901. logs = tuple(
  902. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  903. )
  904. assert {log.levelno for log in logs} == {logging.CRITICAL}
  905. def test_collect_configuration_run_summary_logs_run_umount_error():
  906. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  907. flexmock(module).should_receive('run_configuration').and_return([])
  908. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  909. flexmock(module).should_receive('log_error_records').and_return(
  910. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  911. )
  912. arguments = {'umount': flexmock(mount_point='/mnt')}
  913. logs = tuple(
  914. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  915. )
  916. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  917. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  918. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  919. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  920. ['baz']
  921. )
  922. stdout = flexmock()
  923. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  924. flexmock(module.sys).stdout = stdout
  925. arguments = {}
  926. tuple(
  927. module.collect_configuration_run_summary_logs(
  928. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  929. )
  930. )