test_borgmatic.py 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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_export_key():
  627. flexmock(module).should_receive('add_custom_log_levels')
  628. flexmock(module.command).should_receive('execute_hook')
  629. flexmock(borgmatic.actions.export_key).should_receive('run_export_key').once()
  630. tuple(
  631. module.run_actions(
  632. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export': 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_borg():
  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()
  645. tuple(
  646. module.run_actions(
  647. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'borg': flexmock()},
  648. config_filename=flexmock(),
  649. config={'repositories': []},
  650. local_path=flexmock(),
  651. remote_path=flexmock(),
  652. local_borg_version=flexmock(),
  653. repository={'path': 'repo'},
  654. )
  655. )
  656. def test_run_actions_runs_multiple_actions_in_argument_order():
  657. flexmock(module).should_receive('add_custom_log_levels')
  658. flexmock(module.command).should_receive('execute_hook')
  659. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  660. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  661. tuple(
  662. module.run_actions(
  663. arguments={
  664. 'global': flexmock(dry_run=False, log_file='foo'),
  665. 'borg': flexmock(),
  666. 'restore': flexmock(),
  667. },
  668. config_filename=flexmock(),
  669. config={'repositories': []},
  670. local_path=flexmock(),
  671. remote_path=flexmock(),
  672. local_borg_version=flexmock(),
  673. repository={'path': 'repo'},
  674. )
  675. )
  676. def test_load_configurations_collects_parsed_configurations_and_logs():
  677. configuration = flexmock()
  678. other_configuration = flexmock()
  679. test_expected_logs = [flexmock(), flexmock()]
  680. other_expected_logs = [flexmock(), flexmock()]
  681. flexmock(module.validate).should_receive('parse_configuration').and_return(
  682. configuration, test_expected_logs
  683. ).and_return(other_configuration, other_expected_logs)
  684. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  685. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  686. assert set(logs) >= set(test_expected_logs + other_expected_logs)
  687. def test_load_configurations_logs_warning_for_permission_error():
  688. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  689. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  690. assert configs == {}
  691. assert max(log.levelno for log in logs) == logging.WARNING
  692. def test_load_configurations_logs_critical_for_parse_error():
  693. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  694. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  695. assert configs == {}
  696. assert max(log.levelno for log in logs) == logging.CRITICAL
  697. def test_log_record_does_not_raise():
  698. module.log_record(levelno=1, foo='bar', baz='quux')
  699. def test_log_record_with_suppress_does_not_raise():
  700. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  701. def test_log_error_records_generates_output_logs_for_message_only():
  702. flexmock(module).should_receive('log_record').replace_with(dict)
  703. logs = tuple(module.log_error_records('Error'))
  704. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  705. def test_log_error_records_generates_output_logs_for_called_process_error_with_bytes_ouput():
  706. flexmock(module).should_receive('log_record').replace_with(dict)
  707. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  708. logs = tuple(
  709. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', b'error output'))
  710. )
  711. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  712. assert any(log for log in logs if 'error output' in str(log))
  713. def test_log_error_records_generates_output_logs_for_called_process_error_with_string_ouput():
  714. flexmock(module).should_receive('log_record').replace_with(dict)
  715. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  716. logs = tuple(
  717. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  718. )
  719. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  720. assert any(log for log in logs if 'error output' in str(log))
  721. def test_log_error_records_generates_logs_for_value_error():
  722. flexmock(module).should_receive('log_record').replace_with(dict)
  723. logs = tuple(module.log_error_records('Error', ValueError()))
  724. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  725. def test_log_error_records_generates_logs_for_os_error():
  726. flexmock(module).should_receive('log_record').replace_with(dict)
  727. logs = tuple(module.log_error_records('Error', OSError()))
  728. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  729. def test_log_error_records_generates_nothing_for_other_error():
  730. flexmock(module).should_receive('log_record').replace_with(dict)
  731. logs = tuple(module.log_error_records('Error', KeyError()))
  732. assert logs == ()
  733. def test_get_local_path_uses_configuration_value():
  734. assert module.get_local_path({'test.yaml': {'local_path': 'borg1'}}) == 'borg1'
  735. def test_get_local_path_without_local_path_defaults_to_borg():
  736. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  737. def test_collect_highlander_action_summary_logs_info_for_success_with_bootstrap():
  738. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  739. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  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.ANSWER}
  750. def test_collect_highlander_action_summary_logs_error_on_bootstrap_failure():
  751. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  752. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  753. ValueError
  754. )
  755. arguments = {
  756. 'bootstrap': flexmock(repository='repo'),
  757. 'global': flexmock(dry_run=False),
  758. }
  759. logs = tuple(
  760. module.collect_highlander_action_summary_logs(
  761. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  762. )
  763. )
  764. assert {log.levelno for log in logs} == {logging.CRITICAL}
  765. def test_collect_highlander_action_summary_logs_error_on_bootstrap_local_borg_version_failure():
  766. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  767. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').never()
  768. arguments = {
  769. 'bootstrap': flexmock(repository='repo'),
  770. 'global': flexmock(dry_run=False),
  771. }
  772. logs = tuple(
  773. module.collect_highlander_action_summary_logs(
  774. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  775. )
  776. )
  777. assert {log.levelno for log in logs} == {logging.CRITICAL}
  778. def test_collect_highlander_action_summary_logs_info_for_success_with_generate():
  779. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate')
  780. arguments = {
  781. 'generate': flexmock(destination='test.yaml'),
  782. 'global': flexmock(dry_run=False),
  783. }
  784. logs = tuple(
  785. module.collect_highlander_action_summary_logs(
  786. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  787. )
  788. )
  789. assert {log.levelno for log in logs} == {logging.ANSWER}
  790. def test_collect_highlander_action_summary_logs_error_on_generate_failure():
  791. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate').and_raise(
  792. ValueError
  793. )
  794. arguments = {
  795. 'generate': flexmock(destination='test.yaml'),
  796. 'global': flexmock(dry_run=False),
  797. }
  798. logs = tuple(
  799. module.collect_highlander_action_summary_logs(
  800. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  801. )
  802. )
  803. assert {log.levelno for log in logs} == {logging.CRITICAL}
  804. def test_collect_highlander_action_summary_logs_info_for_success_with_validate():
  805. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  806. arguments = {
  807. 'validate': flexmock(),
  808. 'global': flexmock(dry_run=False),
  809. }
  810. logs = tuple(
  811. module.collect_highlander_action_summary_logs(
  812. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  813. )
  814. )
  815. assert {log.levelno for log in logs} == {logging.ANSWER}
  816. def test_collect_highlander_action_summary_logs_error_on_validate_parse_failure():
  817. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  818. arguments = {
  819. 'validate': flexmock(),
  820. 'global': flexmock(dry_run=False),
  821. }
  822. logs = tuple(
  823. module.collect_highlander_action_summary_logs(
  824. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=True
  825. )
  826. )
  827. assert {log.levelno for log in logs} == {logging.CRITICAL}
  828. def test_collect_highlander_action_summary_logs_error_on_run_validate_failure():
  829. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate').and_raise(
  830. ValueError
  831. )
  832. arguments = {
  833. 'validate': flexmock(),
  834. 'global': flexmock(dry_run=False),
  835. }
  836. logs = tuple(
  837. module.collect_highlander_action_summary_logs(
  838. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  839. )
  840. )
  841. assert {log.levelno for log in logs} == {logging.CRITICAL}
  842. def test_collect_configuration_run_summary_logs_info_for_success():
  843. flexmock(module.command).should_receive('execute_hook').never()
  844. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  845. flexmock(module).should_receive('run_configuration').and_return([])
  846. arguments = {}
  847. logs = tuple(
  848. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  849. )
  850. assert {log.levelno for log in logs} == {logging.INFO}
  851. def test_collect_configuration_run_summary_executes_hooks_for_create():
  852. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  853. flexmock(module).should_receive('run_configuration').and_return([])
  854. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  855. logs = tuple(
  856. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  857. )
  858. assert {log.levelno for log in logs} == {logging.INFO}
  859. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  860. flexmock(module.validate).should_receive('guard_single_repository_selected')
  861. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  862. flexmock(module).should_receive('run_configuration').and_return([])
  863. arguments = {'extract': flexmock(repository='repo')}
  864. logs = tuple(
  865. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  866. )
  867. assert {log.levelno for log in logs} == {logging.INFO}
  868. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  869. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  870. ValueError
  871. )
  872. expected_logs = (flexmock(),)
  873. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  874. arguments = {'extract': flexmock(repository='repo')}
  875. logs = tuple(
  876. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  877. )
  878. assert logs == expected_logs
  879. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  880. flexmock(module.validate).should_receive('guard_single_repository_selected')
  881. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  882. flexmock(module).should_receive('run_configuration').and_return([])
  883. arguments = {'mount': flexmock(repository='repo')}
  884. logs = tuple(
  885. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  886. )
  887. assert {log.levelno for log in logs} == {logging.INFO}
  888. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  889. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  890. ValueError
  891. )
  892. expected_logs = (flexmock(),)
  893. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  894. arguments = {'mount': flexmock(repository='repo')}
  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_missing_configs_error():
  900. arguments = {'global': flexmock(config_paths=[])}
  901. expected_logs = (flexmock(),)
  902. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  903. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  904. assert logs == expected_logs
  905. def test_collect_configuration_run_summary_logs_pre_hook_error():
  906. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  907. expected_logs = (flexmock(),)
  908. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  909. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  910. logs = tuple(
  911. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  912. )
  913. assert logs == expected_logs
  914. def test_collect_configuration_run_summary_logs_post_hook_error():
  915. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  916. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  917. flexmock(module).should_receive('run_configuration').and_return([])
  918. expected_logs = (flexmock(),)
  919. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  920. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  921. logs = tuple(
  922. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  923. )
  924. assert expected_logs[0] in logs
  925. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  926. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  927. ValueError
  928. )
  929. expected_logs = (flexmock(),)
  930. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  931. arguments = {'list': flexmock(repository='repo', archive='test')}
  932. logs = tuple(
  933. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  934. )
  935. assert logs == expected_logs
  936. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  937. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  938. flexmock(module).should_receive('run_configuration').and_return([])
  939. arguments = {'list': flexmock(repository='repo', archive=None)}
  940. logs = tuple(
  941. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  942. )
  943. assert {log.levelno for log in logs} == {logging.INFO}
  944. def test_collect_configuration_run_summary_logs_run_configuration_error():
  945. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  946. flexmock(module).should_receive('run_configuration').and_return(
  947. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  948. )
  949. flexmock(module).should_receive('log_error_records').and_return([])
  950. arguments = {}
  951. logs = tuple(
  952. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  953. )
  954. assert {log.levelno for log in logs} == {logging.CRITICAL}
  955. def test_collect_configuration_run_summary_logs_run_umount_error():
  956. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  957. flexmock(module).should_receive('run_configuration').and_return([])
  958. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  959. flexmock(module).should_receive('log_error_records').and_return(
  960. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  961. )
  962. arguments = {'umount': flexmock(mount_point='/mnt')}
  963. logs = tuple(
  964. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  965. )
  966. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  967. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  968. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  969. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  970. ['baz']
  971. )
  972. stdout = flexmock()
  973. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  974. flexmock(module.sys).stdout = stdout
  975. arguments = {}
  976. tuple(
  977. module.collect_configuration_run_summary_logs(
  978. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  979. )
  980. )