test_borgmatic.py 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'repositories': [{'path': 'foo'}], '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 = {'repositories': [{'path': 'foo'}], '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 = {'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. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  238. '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. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  268. '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. 'repositories': [{'path': 'foo'}],
  305. 'retries': 3,
  306. 'retry_wait': 10,
  307. }
  308. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  309. results = list(module.run_configuration('test.yaml', config, arguments))
  310. assert results == error_logs
  311. def test_run_configuration_retries_timeout_multiple_repos():
  312. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  313. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  314. flexmock(module.command).should_receive('execute_hook')
  315. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  316. []
  317. ).and_raise(OSError).times(4)
  318. flexmock(module).should_receive('log_error_records').with_args(
  319. 'foo: Error running actions for repository',
  320. OSError,
  321. levelno=logging.WARNING,
  322. log_command_error_output=True,
  323. ).and_return([flexmock()]).ordered()
  324. flexmock(module).should_receive('log_error_records').with_args(
  325. 'bar: Error running actions for repository',
  326. OSError,
  327. levelno=logging.WARNING,
  328. log_command_error_output=True,
  329. ).and_return([flexmock()]).ordered()
  330. # Sleep before retrying foo (and passing)
  331. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  332. # Sleep before retrying bar (and failing)
  333. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  334. error_logs = [flexmock()]
  335. flexmock(module).should_receive('log_error_records').with_args(
  336. 'bar: Error running actions for repository', OSError
  337. ).and_return(error_logs).ordered()
  338. config = {
  339. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  340. 'retries': 1,
  341. 'retry_wait': 10,
  342. }
  343. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  344. results = list(module.run_configuration('test.yaml', config, arguments))
  345. assert results == error_logs
  346. def test_run_actions_runs_rcreate():
  347. flexmock(module).should_receive('add_custom_log_levels')
  348. flexmock(module.command).should_receive('execute_hook')
  349. flexmock(borgmatic.actions.rcreate).should_receive('run_rcreate').once()
  350. tuple(
  351. module.run_actions(
  352. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rcreate': flexmock()},
  353. config_filename=flexmock(),
  354. config={'repositories': []},
  355. local_path=flexmock(),
  356. remote_path=flexmock(),
  357. local_borg_version=flexmock(),
  358. repository={'path': 'repo'},
  359. )
  360. )
  361. def test_run_actions_adds_log_file_to_hook_context():
  362. flexmock(module).should_receive('add_custom_log_levels')
  363. flexmock(module.command).should_receive('execute_hook')
  364. expected = flexmock()
  365. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  366. config_filename=object,
  367. repository={'path': 'repo'},
  368. config={'repositories': []},
  369. hook_context={'repository': 'repo', 'repositories': '', 'log_file': 'foo'},
  370. local_borg_version=object,
  371. create_arguments=object,
  372. global_arguments=object,
  373. dry_run_label='',
  374. local_path=object,
  375. remote_path=object,
  376. ).once().and_return(expected)
  377. result = tuple(
  378. module.run_actions(
  379. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  380. config_filename=flexmock(),
  381. config={'repositories': []},
  382. local_path=flexmock(),
  383. remote_path=flexmock(),
  384. local_borg_version=flexmock(),
  385. repository={'path': 'repo'},
  386. )
  387. )
  388. assert result == (expected,)
  389. def test_run_actions_runs_transfer():
  390. flexmock(module).should_receive('add_custom_log_levels')
  391. flexmock(module.command).should_receive('execute_hook')
  392. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  393. tuple(
  394. module.run_actions(
  395. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'transfer': flexmock()},
  396. config_filename=flexmock(),
  397. config={'repositories': []},
  398. local_path=flexmock(),
  399. remote_path=flexmock(),
  400. local_borg_version=flexmock(),
  401. repository={'path': 'repo'},
  402. )
  403. )
  404. def test_run_actions_runs_create():
  405. flexmock(module).should_receive('add_custom_log_levels')
  406. flexmock(module.command).should_receive('execute_hook')
  407. expected = flexmock()
  408. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  409. result = tuple(
  410. module.run_actions(
  411. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  412. config_filename=flexmock(),
  413. config={'repositories': []},
  414. local_path=flexmock(),
  415. remote_path=flexmock(),
  416. local_borg_version=flexmock(),
  417. repository={'path': 'repo'},
  418. )
  419. )
  420. assert result == (expected,)
  421. def test_run_actions_runs_prune():
  422. flexmock(module).should_receive('add_custom_log_levels')
  423. flexmock(module.command).should_receive('execute_hook')
  424. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  425. tuple(
  426. module.run_actions(
  427. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  428. config_filename=flexmock(),
  429. config={'repositories': []},
  430. local_path=flexmock(),
  431. remote_path=flexmock(),
  432. local_borg_version=flexmock(),
  433. repository={'path': 'repo'},
  434. )
  435. )
  436. def test_run_actions_runs_compact():
  437. flexmock(module).should_receive('add_custom_log_levels')
  438. flexmock(module.command).should_receive('execute_hook')
  439. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  440. tuple(
  441. module.run_actions(
  442. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  443. config_filename=flexmock(),
  444. config={'repositories': []},
  445. local_path=flexmock(),
  446. remote_path=flexmock(),
  447. local_borg_version=flexmock(),
  448. repository={'path': 'repo'},
  449. )
  450. )
  451. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  452. flexmock(module).should_receive('add_custom_log_levels')
  453. flexmock(module.command).should_receive('execute_hook')
  454. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  455. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  456. tuple(
  457. module.run_actions(
  458. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  459. config_filename=flexmock(),
  460. config={'repositories': []},
  461. local_path=flexmock(),
  462. remote_path=flexmock(),
  463. local_borg_version=flexmock(),
  464. repository={'path': 'repo'},
  465. )
  466. )
  467. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  468. flexmock(module).should_receive('add_custom_log_levels')
  469. flexmock(module.command).should_receive('execute_hook')
  470. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  471. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  472. tuple(
  473. module.run_actions(
  474. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  475. config_filename=flexmock(),
  476. config={'repositories': []},
  477. local_path=flexmock(),
  478. remote_path=flexmock(),
  479. local_borg_version=flexmock(),
  480. repository={'path': 'repo'},
  481. )
  482. )
  483. def test_run_actions_runs_extract():
  484. flexmock(module).should_receive('add_custom_log_levels')
  485. flexmock(module.command).should_receive('execute_hook')
  486. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  487. tuple(
  488. module.run_actions(
  489. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'extract': flexmock()},
  490. config_filename=flexmock(),
  491. config={'repositories': []},
  492. local_path=flexmock(),
  493. remote_path=flexmock(),
  494. local_borg_version=flexmock(),
  495. repository={'path': 'repo'},
  496. )
  497. )
  498. def test_run_actions_runs_export_tar():
  499. flexmock(module).should_receive('add_custom_log_levels')
  500. flexmock(module.command).should_receive('execute_hook')
  501. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  502. tuple(
  503. module.run_actions(
  504. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export-tar': flexmock()},
  505. config_filename=flexmock(),
  506. config={'repositories': []},
  507. local_path=flexmock(),
  508. remote_path=flexmock(),
  509. local_borg_version=flexmock(),
  510. repository={'path': 'repo'},
  511. )
  512. )
  513. def test_run_actions_runs_mount():
  514. flexmock(module).should_receive('add_custom_log_levels')
  515. flexmock(module.command).should_receive('execute_hook')
  516. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  517. tuple(
  518. module.run_actions(
  519. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'mount': flexmock()},
  520. config_filename=flexmock(),
  521. config={'repositories': []},
  522. local_path=flexmock(),
  523. remote_path=flexmock(),
  524. local_borg_version=flexmock(),
  525. repository={'path': 'repo'},
  526. )
  527. )
  528. def test_run_actions_runs_restore():
  529. flexmock(module).should_receive('add_custom_log_levels')
  530. flexmock(module.command).should_receive('execute_hook')
  531. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  532. tuple(
  533. module.run_actions(
  534. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'restore': flexmock()},
  535. config_filename=flexmock(),
  536. config={'repositories': []},
  537. local_path=flexmock(),
  538. remote_path=flexmock(),
  539. local_borg_version=flexmock(),
  540. repository={'path': 'repo'},
  541. )
  542. )
  543. def test_run_actions_runs_rlist():
  544. flexmock(module).should_receive('add_custom_log_levels')
  545. flexmock(module.command).should_receive('execute_hook')
  546. expected = flexmock()
  547. flexmock(borgmatic.actions.rlist).should_receive('run_rlist').and_yield(expected).once()
  548. result = tuple(
  549. module.run_actions(
  550. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rlist': flexmock()},
  551. config_filename=flexmock(),
  552. config={'repositories': []},
  553. local_path=flexmock(),
  554. remote_path=flexmock(),
  555. local_borg_version=flexmock(),
  556. repository={'path': 'repo'},
  557. )
  558. )
  559. assert result == (expected,)
  560. def test_run_actions_runs_list():
  561. flexmock(module).should_receive('add_custom_log_levels')
  562. flexmock(module.command).should_receive('execute_hook')
  563. expected = flexmock()
  564. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  565. result = tuple(
  566. module.run_actions(
  567. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'list': flexmock()},
  568. config_filename=flexmock(),
  569. config={'repositories': []},
  570. local_path=flexmock(),
  571. remote_path=flexmock(),
  572. local_borg_version=flexmock(),
  573. repository={'path': 'repo'},
  574. )
  575. )
  576. assert result == (expected,)
  577. def test_run_actions_runs_rinfo():
  578. flexmock(module).should_receive('add_custom_log_levels')
  579. flexmock(module.command).should_receive('execute_hook')
  580. expected = flexmock()
  581. flexmock(borgmatic.actions.rinfo).should_receive('run_rinfo').and_yield(expected).once()
  582. result = tuple(
  583. module.run_actions(
  584. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rinfo': flexmock()},
  585. config_filename=flexmock(),
  586. config={'repositories': []},
  587. local_path=flexmock(),
  588. remote_path=flexmock(),
  589. local_borg_version=flexmock(),
  590. repository={'path': 'repo'},
  591. )
  592. )
  593. assert result == (expected,)
  594. def test_run_actions_runs_info():
  595. flexmock(module).should_receive('add_custom_log_levels')
  596. flexmock(module.command).should_receive('execute_hook')
  597. expected = flexmock()
  598. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  599. result = tuple(
  600. module.run_actions(
  601. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'info': flexmock()},
  602. config_filename=flexmock(),
  603. config={'repositories': []},
  604. local_path=flexmock(),
  605. remote_path=flexmock(),
  606. local_borg_version=flexmock(),
  607. repository={'path': 'repo'},
  608. )
  609. )
  610. assert result == (expected,)
  611. def test_run_actions_runs_break_lock():
  612. flexmock(module).should_receive('add_custom_log_levels')
  613. flexmock(module.command).should_receive('execute_hook')
  614. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  615. tuple(
  616. module.run_actions(
  617. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'break-lock': flexmock()},
  618. config_filename=flexmock(),
  619. config={'repositories': []},
  620. local_path=flexmock(),
  621. remote_path=flexmock(),
  622. local_borg_version=flexmock(),
  623. repository={'path': 'repo'},
  624. )
  625. )
  626. def test_run_actions_runs_borg():
  627. flexmock(module).should_receive('add_custom_log_levels')
  628. flexmock(module.command).should_receive('execute_hook')
  629. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  630. tuple(
  631. module.run_actions(
  632. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'borg': flexmock()},
  633. config_filename=flexmock(),
  634. config={'repositories': []},
  635. local_path=flexmock(),
  636. remote_path=flexmock(),
  637. local_borg_version=flexmock(),
  638. repository={'path': 'repo'},
  639. )
  640. )
  641. def test_run_actions_runs_multiple_actions_in_argument_order():
  642. flexmock(module).should_receive('add_custom_log_levels')
  643. flexmock(module.command).should_receive('execute_hook')
  644. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  645. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  646. tuple(
  647. module.run_actions(
  648. arguments={
  649. 'global': flexmock(dry_run=False, log_file='foo'),
  650. 'borg': flexmock(),
  651. 'restore': flexmock(),
  652. },
  653. config_filename=flexmock(),
  654. config={'repositories': []},
  655. local_path=flexmock(),
  656. remote_path=flexmock(),
  657. local_borg_version=flexmock(),
  658. repository={'path': 'repo'},
  659. )
  660. )
  661. def test_load_configurations_collects_parsed_configurations_and_logs():
  662. configuration = flexmock()
  663. other_configuration = flexmock()
  664. test_expected_logs = [flexmock(), flexmock()]
  665. other_expected_logs = [flexmock(), flexmock()]
  666. flexmock(module.validate).should_receive('parse_configuration').and_return(
  667. configuration, test_expected_logs
  668. ).and_return(other_configuration, other_expected_logs)
  669. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  670. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  671. assert set(logs) >= set(test_expected_logs + other_expected_logs)
  672. def test_load_configurations_logs_warning_for_permission_error():
  673. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  674. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  675. assert configs == {}
  676. assert max(log.levelno for log in logs) == logging.WARNING
  677. def test_load_configurations_logs_critical_for_parse_error():
  678. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  679. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  680. assert configs == {}
  681. assert max(log.levelno for log in logs) == logging.CRITICAL
  682. def test_log_record_does_not_raise():
  683. module.log_record(levelno=1, foo='bar', baz='quux')
  684. def test_log_record_with_suppress_does_not_raise():
  685. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  686. def test_log_error_records_generates_output_logs_for_message_only():
  687. flexmock(module).should_receive('log_record').replace_with(dict)
  688. logs = tuple(module.log_error_records('Error'))
  689. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  690. def test_log_error_records_generates_output_logs_for_called_process_error_with_bytes_ouput():
  691. flexmock(module).should_receive('log_record').replace_with(dict)
  692. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  693. logs = tuple(
  694. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', b'error output'))
  695. )
  696. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  697. assert any(log for log in logs if 'error output' in str(log))
  698. def test_log_error_records_generates_output_logs_for_called_process_error_with_string_ouput():
  699. flexmock(module).should_receive('log_record').replace_with(dict)
  700. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  701. logs = tuple(
  702. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  703. )
  704. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  705. assert any(log for log in logs if 'error output' in str(log))
  706. def test_log_error_records_generates_logs_for_value_error():
  707. flexmock(module).should_receive('log_record').replace_with(dict)
  708. logs = tuple(module.log_error_records('Error', ValueError()))
  709. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  710. def test_log_error_records_generates_logs_for_os_error():
  711. flexmock(module).should_receive('log_record').replace_with(dict)
  712. logs = tuple(module.log_error_records('Error', OSError()))
  713. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  714. def test_log_error_records_generates_nothing_for_other_error():
  715. flexmock(module).should_receive('log_record').replace_with(dict)
  716. logs = tuple(module.log_error_records('Error', KeyError()))
  717. assert logs == ()
  718. def test_get_local_path_uses_configuration_value():
  719. assert module.get_local_path({'test.yaml': {'local_path': 'borg1'}}) == 'borg1'
  720. def test_get_local_path_without_local_path_defaults_to_borg():
  721. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  722. def test_collect_highlander_action_summary_logs_info_for_success_with_bootstrap():
  723. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  724. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  725. arguments = {
  726. 'bootstrap': flexmock(repository='repo'),
  727. 'global': flexmock(dry_run=False),
  728. }
  729. logs = tuple(
  730. module.collect_highlander_action_summary_logs(
  731. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  732. )
  733. )
  734. assert {log.levelno for log in logs} == {logging.ANSWER}
  735. def test_collect_highlander_action_summary_logs_error_on_bootstrap_failure():
  736. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  737. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  738. ValueError
  739. )
  740. arguments = {
  741. 'bootstrap': flexmock(repository='repo'),
  742. 'global': flexmock(dry_run=False),
  743. }
  744. logs = tuple(
  745. module.collect_highlander_action_summary_logs(
  746. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  747. )
  748. )
  749. assert {log.levelno for log in logs} == {logging.CRITICAL}
  750. def test_collect_highlander_action_summary_logs_error_on_bootstrap_local_borg_version_failure():
  751. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  752. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').never()
  753. arguments = {
  754. 'bootstrap': flexmock(repository='repo'),
  755. 'global': flexmock(dry_run=False),
  756. }
  757. logs = tuple(
  758. module.collect_highlander_action_summary_logs(
  759. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  760. )
  761. )
  762. assert {log.levelno for log in logs} == {logging.CRITICAL}
  763. def test_collect_highlander_action_summary_logs_info_for_success_with_generate():
  764. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate')
  765. arguments = {
  766. 'generate': flexmock(destination='test.yaml'),
  767. 'global': flexmock(dry_run=False),
  768. }
  769. logs = tuple(
  770. module.collect_highlander_action_summary_logs(
  771. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  772. )
  773. )
  774. assert {log.levelno for log in logs} == {logging.ANSWER}
  775. def test_collect_highlander_action_summary_logs_error_on_generate_failure():
  776. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate').and_raise(
  777. ValueError
  778. )
  779. arguments = {
  780. 'generate': flexmock(destination='test.yaml'),
  781. 'global': flexmock(dry_run=False),
  782. }
  783. logs = tuple(
  784. module.collect_highlander_action_summary_logs(
  785. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  786. )
  787. )
  788. assert {log.levelno for log in logs} == {logging.CRITICAL}
  789. def test_collect_highlander_action_summary_logs_info_for_success_with_validate():
  790. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  791. arguments = {
  792. 'validate': flexmock(),
  793. 'global': flexmock(dry_run=False),
  794. }
  795. logs = tuple(
  796. module.collect_highlander_action_summary_logs(
  797. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  798. )
  799. )
  800. assert {log.levelno for log in logs} == {logging.ANSWER}
  801. def test_collect_highlander_action_summary_logs_error_on_validate_parse_failure():
  802. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  803. arguments = {
  804. 'validate': flexmock(),
  805. 'global': flexmock(dry_run=False),
  806. }
  807. logs = tuple(
  808. module.collect_highlander_action_summary_logs(
  809. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=True
  810. )
  811. )
  812. assert {log.levelno for log in logs} == {logging.CRITICAL}
  813. def test_collect_highlander_action_summary_logs_error_on_run_validate_failure():
  814. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate').and_raise(
  815. ValueError
  816. )
  817. arguments = {
  818. 'validate': flexmock(),
  819. 'global': flexmock(dry_run=False),
  820. }
  821. logs = tuple(
  822. module.collect_highlander_action_summary_logs(
  823. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  824. )
  825. )
  826. assert {log.levelno for log in logs} == {logging.CRITICAL}
  827. def test_collect_configuration_run_summary_logs_info_for_success():
  828. flexmock(module.command).should_receive('execute_hook').never()
  829. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  830. flexmock(module).should_receive('run_configuration').and_return([])
  831. arguments = {}
  832. logs = tuple(
  833. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  834. )
  835. assert {log.levelno for log in logs} == {logging.INFO}
  836. def test_collect_configuration_run_summary_executes_hooks_for_create():
  837. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  838. flexmock(module).should_receive('run_configuration').and_return([])
  839. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  840. logs = tuple(
  841. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  842. )
  843. assert {log.levelno for log in logs} == {logging.INFO}
  844. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  845. flexmock(module.validate).should_receive('guard_single_repository_selected')
  846. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  847. flexmock(module).should_receive('run_configuration').and_return([])
  848. arguments = {'extract': flexmock(repository='repo')}
  849. logs = tuple(
  850. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  851. )
  852. assert {log.levelno for log in logs} == {logging.INFO}
  853. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  854. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  855. ValueError
  856. )
  857. expected_logs = (flexmock(),)
  858. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  859. arguments = {'extract': flexmock(repository='repo')}
  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_info_for_success_with_mount():
  865. flexmock(module.validate).should_receive('guard_single_repository_selected')
  866. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  867. flexmock(module).should_receive('run_configuration').and_return([])
  868. arguments = {'mount': flexmock(repository='repo')}
  869. logs = tuple(
  870. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  871. )
  872. assert {log.levelno for log in logs} == {logging.INFO}
  873. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  874. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  875. ValueError
  876. )
  877. expected_logs = (flexmock(),)
  878. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  879. arguments = {'mount': flexmock(repository='repo')}
  880. logs = tuple(
  881. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  882. )
  883. assert logs == expected_logs
  884. def test_collect_configuration_run_summary_logs_missing_configs_error():
  885. arguments = {'global': flexmock(config_paths=[])}
  886. expected_logs = (flexmock(),)
  887. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  888. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  889. assert logs == expected_logs
  890. def test_collect_configuration_run_summary_logs_pre_hook_error():
  891. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  892. expected_logs = (flexmock(),)
  893. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  894. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  895. logs = tuple(
  896. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  897. )
  898. assert logs == expected_logs
  899. def test_collect_configuration_run_summary_logs_post_hook_error():
  900. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  901. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  902. flexmock(module).should_receive('run_configuration').and_return([])
  903. expected_logs = (flexmock(),)
  904. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  905. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  906. logs = tuple(
  907. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  908. )
  909. assert expected_logs[0] in logs
  910. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  911. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  912. ValueError
  913. )
  914. expected_logs = (flexmock(),)
  915. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  916. arguments = {'list': flexmock(repository='repo', archive='test')}
  917. logs = tuple(
  918. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  919. )
  920. assert logs == expected_logs
  921. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  922. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  923. flexmock(module).should_receive('run_configuration').and_return([])
  924. arguments = {'list': flexmock(repository='repo', archive=None)}
  925. logs = tuple(
  926. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  927. )
  928. assert {log.levelno for log in logs} == {logging.INFO}
  929. def test_collect_configuration_run_summary_logs_run_configuration_error():
  930. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  931. flexmock(module).should_receive('run_configuration').and_return(
  932. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  933. )
  934. flexmock(module).should_receive('log_error_records').and_return([])
  935. arguments = {}
  936. logs = tuple(
  937. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  938. )
  939. assert {log.levelno for log in logs} == {logging.CRITICAL}
  940. def test_collect_configuration_run_summary_logs_run_umount_error():
  941. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  942. flexmock(module).should_receive('run_configuration').and_return([])
  943. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  944. flexmock(module).should_receive('log_error_records').and_return(
  945. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  946. )
  947. arguments = {'umount': flexmock(mount_point='/mnt')}
  948. logs = tuple(
  949. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  950. )
  951. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  952. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  953. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  954. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  955. ['baz']
  956. )
  957. stdout = flexmock()
  958. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  959. flexmock(module.sys).stdout = stdout
  960. arguments = {}
  961. tuple(
  962. module.collect_configuration_run_summary_logs(
  963. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  964. )
  965. )