test_borgmatic.py 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101
  1. import logging
  2. import subprocess
  3. import time
  4. import pytest
  5. from flexmock import flexmock
  6. import borgmatic.hooks.command
  7. from borgmatic.commands import borgmatic as module
  8. @pytest.mark.parametrize(
  9. 'config,arguments,expected_actions',
  10. (
  11. ({}, {}, []),
  12. ({'skip_actions': []}, {}, []),
  13. ({'skip_actions': ['prune', 'check']}, {}, ['prune', 'check']),
  14. (
  15. {'skip_actions': ['prune', 'check']},
  16. {'check': flexmock(force=False)},
  17. ['prune', 'check'],
  18. ),
  19. ({'skip_actions': ['prune', 'check']}, {'check': flexmock(force=True)}, ['prune']),
  20. ),
  21. )
  22. def test_get_skip_actions_uses_config_and_arguments(config, arguments, expected_actions):
  23. assert module.get_skip_actions(config, arguments) == expected_actions
  24. def test_monitoring_hooks_with_monioring_disabled_bails():
  25. flexmock(module).should_receive('verbosity_to_log_level').and_return(module.DISABLED)
  26. flexmock(module.dispatch).should_receive('call_hooks').never()
  27. with module.Monitoring_hooks(
  28. config_filename='test.yaml',
  29. config={},
  30. arguments={'create': flexmock()},
  31. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  32. ):
  33. pass
  34. def test_monitoring_hooks_with_non_primary_action_bails():
  35. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  36. flexmock(module.dispatch).should_receive('call_hooks').never()
  37. with module.Monitoring_hooks(
  38. config_filename='test.yaml',
  39. config={},
  40. arguments={'extract': flexmock()},
  41. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  42. ):
  43. pass
  44. def test_monitoring_hooks_pings_monitors():
  45. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  46. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  47. 'initialize_monitor',
  48. object,
  49. object,
  50. object,
  51. object,
  52. object,
  53. ).once()
  54. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  55. 'ping_monitor',
  56. object,
  57. object,
  58. object,
  59. module.monitor.State.START,
  60. object,
  61. object,
  62. ).once()
  63. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  64. 'ping_monitor',
  65. object,
  66. object,
  67. object,
  68. module.monitor.State.LOG,
  69. object,
  70. object,
  71. ).once()
  72. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  73. 'ping_monitor',
  74. object,
  75. object,
  76. object,
  77. module.monitor.State.FINISH,
  78. object,
  79. object,
  80. ).once()
  81. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  82. 'ping_monitor',
  83. object,
  84. object,
  85. object,
  86. module.monitor.State.FAIL,
  87. object,
  88. object,
  89. ).never()
  90. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  91. 'destroy_monitor',
  92. object,
  93. object,
  94. object,
  95. object,
  96. ).once()
  97. with module.Monitoring_hooks(
  98. config_filename='test.yaml',
  99. config={},
  100. arguments={'create': flexmock()},
  101. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  102. ):
  103. pass
  104. def test_monitoring_hooks_with_start_ping_error_raises():
  105. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  106. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  107. 'initialize_monitor',
  108. object,
  109. object,
  110. object,
  111. object,
  112. object,
  113. ).once()
  114. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  115. 'ping_monitor',
  116. object,
  117. object,
  118. object,
  119. module.monitor.State.START,
  120. object,
  121. object,
  122. ).and_raise(OSError).once()
  123. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  124. 'ping_monitor',
  125. object,
  126. object,
  127. object,
  128. module.monitor.State.LOG,
  129. object,
  130. object,
  131. ).never()
  132. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  133. 'ping_monitor',
  134. object,
  135. object,
  136. object,
  137. module.monitor.State.FINISH,
  138. object,
  139. object,
  140. ).never()
  141. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  142. 'destroy_monitor',
  143. object,
  144. object,
  145. object,
  146. object,
  147. ).never()
  148. with pytest.raises(ValueError):
  149. with module.Monitoring_hooks(
  150. config_filename='test.yaml',
  151. config={},
  152. arguments={'create': flexmock()},
  153. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  154. ):
  155. assert False # This should never get called.
  156. def test_monitoring_hooks_with_log_ping_error_raises():
  157. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  158. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  159. 'initialize_monitor',
  160. object,
  161. object,
  162. object,
  163. object,
  164. object,
  165. ).once()
  166. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  167. 'ping_monitor',
  168. object,
  169. object,
  170. object,
  171. module.monitor.State.START,
  172. object,
  173. object,
  174. ).once()
  175. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  176. 'ping_monitor',
  177. object,
  178. object,
  179. object,
  180. module.monitor.State.LOG,
  181. object,
  182. object,
  183. ).and_raise(OSError).once()
  184. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  185. 'ping_monitor',
  186. object,
  187. object,
  188. object,
  189. module.monitor.State.FINISH,
  190. object,
  191. object,
  192. ).never()
  193. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  194. 'destroy_monitor',
  195. object,
  196. object,
  197. object,
  198. object,
  199. ).never()
  200. with pytest.raises(ValueError):
  201. with module.Monitoring_hooks(
  202. config_filename='test.yaml',
  203. config={},
  204. arguments={'create': flexmock()},
  205. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  206. ):
  207. pass
  208. def test_monitoring_hooks_with_finish_ping_error_raises():
  209. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  210. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  211. 'initialize_monitor',
  212. object,
  213. object,
  214. object,
  215. object,
  216. object,
  217. ).once()
  218. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  219. 'ping_monitor',
  220. object,
  221. object,
  222. object,
  223. module.monitor.State.START,
  224. object,
  225. object,
  226. ).once()
  227. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  228. 'ping_monitor',
  229. object,
  230. object,
  231. object,
  232. module.monitor.State.LOG,
  233. object,
  234. object,
  235. ).once()
  236. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  237. 'ping_monitor',
  238. object,
  239. object,
  240. object,
  241. module.monitor.State.FINISH,
  242. object,
  243. object,
  244. ).and_raise(OSError).once()
  245. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  246. 'destroy_monitor',
  247. object,
  248. object,
  249. object,
  250. object,
  251. ).never()
  252. with pytest.raises(ValueError):
  253. with module.Monitoring_hooks(
  254. config_filename='test.yaml',
  255. config={},
  256. arguments={'create': flexmock()},
  257. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  258. ):
  259. pass
  260. def test_monitoring_with_wrapped_code_error_pings_fail():
  261. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  262. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  263. 'initialize_monitor',
  264. object,
  265. object,
  266. object,
  267. object,
  268. object,
  269. ).once()
  270. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  271. 'ping_monitor',
  272. object,
  273. object,
  274. object,
  275. module.monitor.State.START,
  276. object,
  277. object,
  278. ).once()
  279. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  280. 'ping_monitor',
  281. object,
  282. object,
  283. object,
  284. module.monitor.State.LOG,
  285. object,
  286. object,
  287. ).once()
  288. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  289. 'ping_monitor',
  290. object,
  291. object,
  292. object,
  293. module.monitor.State.FINISH,
  294. object,
  295. object,
  296. ).never()
  297. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  298. 'ping_monitor',
  299. object,
  300. object,
  301. object,
  302. module.monitor.State.FAIL,
  303. object,
  304. object,
  305. ).once()
  306. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  307. 'destroy_monitor',
  308. object,
  309. object,
  310. object,
  311. object,
  312. ).once()
  313. with pytest.raises(OSError):
  314. with module.Monitoring_hooks(
  315. config_filename='test.yaml',
  316. config={},
  317. arguments={'create': flexmock()},
  318. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  319. ):
  320. raise OSError()
  321. def test_monitoring_with_fail_ping_error_raise_original_error():
  322. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  323. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  324. 'initialize_monitor',
  325. object,
  326. object,
  327. object,
  328. object,
  329. object,
  330. ).once()
  331. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  332. 'ping_monitor',
  333. object,
  334. object,
  335. object,
  336. module.monitor.State.START,
  337. object,
  338. object,
  339. ).once()
  340. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  341. 'ping_monitor',
  342. object,
  343. object,
  344. object,
  345. module.monitor.State.LOG,
  346. object,
  347. object,
  348. ).once()
  349. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  350. 'ping_monitor',
  351. object,
  352. object,
  353. object,
  354. module.monitor.State.FINISH,
  355. object,
  356. object,
  357. ).never()
  358. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  359. 'ping_monitor',
  360. object,
  361. object,
  362. object,
  363. module.monitor.State.FAIL,
  364. object,
  365. object,
  366. ).and_raise(OSError).once()
  367. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  368. 'destroy_monitor',
  369. object,
  370. object,
  371. object,
  372. object,
  373. ).never()
  374. with pytest.raises(OSError):
  375. with module.Monitoring_hooks(
  376. config_filename='test.yaml',
  377. config={},
  378. arguments={'create': flexmock()},
  379. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  380. ):
  381. raise OSError()
  382. def test_run_configuration_runs_actions_for_each_repository():
  383. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  384. flexmock(module).should_receive('get_skip_actions').and_return([])
  385. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  386. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  387. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  388. expected_results = [flexmock(), flexmock()]
  389. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  390. flexmock(module).should_receive('run_actions').and_return(expected_results[:1]).and_return(
  391. expected_results[1:]
  392. )
  393. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  394. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock())}
  395. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  396. assert results == expected_results
  397. def test_run_configuration_with_skip_actions_does_not_raise():
  398. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  399. flexmock(module).should_receive('get_skip_actions').and_return(['compact'])
  400. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  401. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  402. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  403. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  404. flexmock(module).should_receive('run_actions').and_return(flexmock()).and_return(flexmock())
  405. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}], 'skip_actions': ['compact']}
  406. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock())}
  407. list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  408. def test_run_configuration_with_invalid_borg_version_errors():
  409. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  410. flexmock(module).should_receive('get_skip_actions').and_return([])
  411. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  412. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  413. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  414. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  415. flexmock(module).should_receive('run_actions').never()
  416. config = {'repositories': [{'path': 'foo'}]}
  417. arguments = {
  418. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  419. 'prune': flexmock(),
  420. }
  421. list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  422. def test_run_configuration_logs_actions_error():
  423. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  424. flexmock(module).should_receive('get_skip_actions').and_return([])
  425. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  426. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  427. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  428. expected_results = [flexmock()]
  429. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  430. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  431. flexmock(module).should_receive('run_actions').and_raise(OSError)
  432. flexmock(module.command).should_receive('filter_hooks')
  433. flexmock(module.command).should_receive('execute_hooks')
  434. config = {'repositories': [{'path': 'foo'}]}
  435. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock())}
  436. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  437. assert results == expected_results
  438. def test_run_configuration_skips_remaining_actions_for_actions_soft_failure_but_still_runs_next_repository_actions():
  439. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  440. flexmock(module).should_receive('get_skip_actions').and_return([])
  441. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  442. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  443. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  444. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  445. log = flexmock()
  446. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  447. flexmock(module).should_receive('run_actions').twice().and_raise(error).and_yield(log)
  448. flexmock(module).should_receive('log_error_records').never()
  449. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  450. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  451. arguments = {
  452. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  453. 'create': flexmock(),
  454. }
  455. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  456. assert results == [log]
  457. def test_run_configuration_does_not_call_monitoring_hooks_if_monitoring_hooks_are_disabled():
  458. flexmock(module).should_receive('verbosity_to_log_level').and_return(module.DISABLED)
  459. flexmock(module).should_receive('get_skip_actions').and_return([])
  460. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  461. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  462. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  463. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  464. flexmock(module).should_receive('run_actions').and_return([])
  465. config = {'repositories': [{'path': 'foo'}]}
  466. arguments = {
  467. 'global': flexmock(monitoring_verbosity=-2, dry_run=False, log_file=flexmock()),
  468. 'create': flexmock(),
  469. }
  470. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  471. assert results == []
  472. def test_run_configuration_logs_on_error_hook_error():
  473. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  474. flexmock(module).should_receive('get_skip_actions').and_return([])
  475. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  476. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  477. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  478. flexmock(module.command).should_receive('filter_hooks')
  479. flexmock(module.command).should_receive('execute_hooks').and_raise(OSError)
  480. expected_results = [flexmock(), flexmock()]
  481. flexmock(module).should_receive('log_error_records').and_return(
  482. expected_results[:1]
  483. ).and_return(expected_results[1:])
  484. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  485. flexmock(module).should_receive('run_actions').and_raise(OSError)
  486. config = {'repositories': [{'path': 'foo'}]}
  487. arguments = {
  488. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  489. 'create': flexmock(),
  490. }
  491. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  492. assert results == expected_results
  493. def test_run_configuration_logs_on_before_command_hook_error():
  494. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  495. flexmock(module).should_receive('get_skip_actions').and_return([])
  496. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  497. flexmock(module.command).should_receive('Before_after_hooks').and_raise(OSError)
  498. flexmock(module.borg_version).should_receive('local_borg_version').never()
  499. flexmock(module.command).should_receive('filter_hooks')
  500. flexmock(module.command).should_receive('execute_hooks')
  501. expected_results = [flexmock()]
  502. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  503. flexmock(module).should_receive('Log_prefix').never()
  504. flexmock(module).should_receive('run_actions').never()
  505. config = {'repositories': [{'path': 'foo'}]}
  506. arguments = {
  507. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  508. 'create': flexmock(),
  509. }
  510. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  511. assert results == expected_results
  512. def test_run_configuration_logs_on_monitoring_hook_error():
  513. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  514. flexmock(module).should_receive('get_skip_actions').and_return([])
  515. flexmock(module).should_receive('Monitoring_hooks').and_raise(OSError)
  516. flexmock(module.command).should_receive('Before_after_hooks').never()
  517. flexmock(module.borg_version).should_receive('local_borg_version').never()
  518. flexmock(module.command).should_receive('filter_hooks')
  519. flexmock(module.command).should_receive('execute_hooks')
  520. expected_results = [flexmock()]
  521. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  522. flexmock(module).should_receive('Log_prefix').never()
  523. flexmock(module).should_receive('run_actions').never()
  524. config = {'repositories': [{'path': 'foo'}]}
  525. arguments = {
  526. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  527. 'create': flexmock(),
  528. }
  529. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  530. assert results == expected_results
  531. def test_run_configuration_bails_for_on_error_hook_soft_failure():
  532. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  533. flexmock(module).should_receive('get_skip_actions').and_return([])
  534. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  535. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  536. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  537. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  538. flexmock(module.command).should_receive('filter_hooks')
  539. flexmock(module.command).should_receive('execute_hooks').and_raise(error)
  540. expected_results = [flexmock()]
  541. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  542. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  543. flexmock(module).should_receive('run_actions').and_raise(OSError)
  544. config = {'repositories': [{'path': 'foo'}]}
  545. arguments = {
  546. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  547. 'create': flexmock(),
  548. }
  549. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  550. assert results == expected_results
  551. def test_run_configuration_retries_soft_error():
  552. # Run action first fails, second passes.
  553. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  554. flexmock(module).should_receive('get_skip_actions').and_return([])
  555. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  556. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  557. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  558. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  559. flexmock(module).should_receive('run_actions').and_raise(OSError).and_return([])
  560. flexmock(module).should_receive('log_error_records').and_return([flexmock()]).once()
  561. flexmock(module.command).should_receive('filter_hooks').never()
  562. flexmock(module.command).should_receive('execute_hooks').never()
  563. config = {'repositories': [{'path': 'foo'}], 'retries': 1}
  564. arguments = {
  565. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  566. 'create': flexmock(),
  567. }
  568. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  569. assert results == []
  570. def test_run_configuration_retries_hard_error():
  571. # Run action fails twice.
  572. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  573. flexmock(module).should_receive('get_skip_actions').and_return([])
  574. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  575. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  576. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  577. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  578. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  579. flexmock(module).should_receive('log_error_records').with_args(
  580. 'Error running actions for repository',
  581. OSError,
  582. levelno=logging.WARNING,
  583. log_command_error_output=True,
  584. ).and_return([flexmock()])
  585. error_logs = [flexmock()]
  586. flexmock(module).should_receive('log_error_records').with_args(
  587. 'Error running actions for repository',
  588. OSError,
  589. ).and_return(error_logs)
  590. flexmock(module.command).should_receive('filter_hooks')
  591. flexmock(module.command).should_receive('execute_hooks')
  592. config = {'repositories': [{'path': 'foo'}], 'retries': 1}
  593. arguments = {
  594. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  595. 'create': flexmock(),
  596. }
  597. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  598. assert results == error_logs
  599. def test_run_configuration_retries_repositories_in_order():
  600. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  601. flexmock(module).should_receive('get_skip_actions').and_return([])
  602. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  603. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  604. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  605. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  606. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  607. expected_results = [flexmock(), flexmock()]
  608. flexmock(module).should_receive('log_error_records').with_args(
  609. 'Error running actions for repository', OSError
  610. ).and_return(expected_results[:1]).ordered()
  611. flexmock(module).should_receive('log_error_records').with_args(
  612. 'Error running actions for repository', OSError
  613. ).and_return(expected_results[1:]).ordered()
  614. flexmock(module.command).should_receive('filter_hooks')
  615. flexmock(module.command).should_receive('execute_hooks')
  616. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  617. arguments = {
  618. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  619. 'create': flexmock(),
  620. }
  621. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  622. assert results == expected_results
  623. def test_run_configuration_retries_round_robin():
  624. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  625. flexmock(module).should_receive('get_skip_actions').and_return([])
  626. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  627. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  628. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  629. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  630. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  631. flexmock(module).should_receive('log_error_records').with_args(
  632. 'Error running actions for repository',
  633. OSError,
  634. levelno=logging.WARNING,
  635. log_command_error_output=True,
  636. ).and_return([flexmock()]).ordered()
  637. flexmock(module).should_receive('log_error_records').with_args(
  638. 'Error running actions for repository',
  639. OSError,
  640. levelno=logging.WARNING,
  641. log_command_error_output=True,
  642. ).and_return([flexmock()]).ordered()
  643. foo_error_logs = [flexmock()]
  644. flexmock(module).should_receive('log_error_records').with_args(
  645. 'Error running actions for repository', OSError
  646. ).and_return(foo_error_logs).ordered()
  647. bar_error_logs = [flexmock()]
  648. flexmock(module).should_receive('log_error_records').with_args(
  649. 'Error running actions for repository', OSError
  650. ).and_return(bar_error_logs).ordered()
  651. flexmock(module.command).should_receive('filter_hooks')
  652. flexmock(module.command).should_receive('execute_hooks')
  653. config = {
  654. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  655. 'retries': 1,
  656. }
  657. arguments = {
  658. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  659. 'create': flexmock(),
  660. }
  661. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  662. assert results == foo_error_logs + bar_error_logs
  663. def test_run_configuration_with_one_retry():
  664. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  665. flexmock(module).should_receive('get_skip_actions').and_return([])
  666. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  667. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  668. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  669. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  670. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  671. []
  672. ).and_raise(OSError).times(4)
  673. flexmock(module).should_receive('log_error_records').with_args(
  674. 'Error running actions for repository',
  675. OSError,
  676. levelno=logging.WARNING,
  677. log_command_error_output=True,
  678. ).and_return([flexmock()]).ordered()
  679. flexmock(module).should_receive('log_error_records').with_args(
  680. 'Error running actions for repository',
  681. OSError,
  682. levelno=logging.WARNING,
  683. log_command_error_output=True,
  684. ).and_return(flexmock()).ordered()
  685. error_logs = [flexmock()]
  686. flexmock(module).should_receive('log_error_records').with_args(
  687. 'Error running actions for repository', OSError
  688. ).and_return(error_logs).ordered()
  689. flexmock(module.command).should_receive('filter_hooks')
  690. flexmock(module.command).should_receive('execute_hooks')
  691. config = {
  692. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  693. 'retries': 1,
  694. }
  695. arguments = {
  696. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  697. 'create': flexmock(),
  698. }
  699. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  700. assert results == error_logs
  701. def test_run_configuration_with_retry_wait_does_backoff_after_each_retry():
  702. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  703. flexmock(module).should_receive('get_skip_actions').and_return([])
  704. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  705. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  706. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  707. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  708. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  709. flexmock(module).should_receive('log_error_records').with_args(
  710. 'Error running actions for repository',
  711. OSError,
  712. levelno=logging.WARNING,
  713. log_command_error_output=True,
  714. ).and_return([flexmock()]).ordered()
  715. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  716. flexmock(module).should_receive('log_error_records').with_args(
  717. 'Error running actions for repository',
  718. OSError,
  719. levelno=logging.WARNING,
  720. log_command_error_output=True,
  721. ).and_return([flexmock()]).ordered()
  722. flexmock(time).should_receive('sleep').with_args(20).and_return().ordered()
  723. flexmock(module).should_receive('log_error_records').with_args(
  724. 'Error running actions for repository',
  725. OSError,
  726. levelno=logging.WARNING,
  727. log_command_error_output=True,
  728. ).and_return([flexmock()]).ordered()
  729. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  730. error_logs = [flexmock()]
  731. flexmock(module).should_receive('log_error_records').with_args(
  732. 'Error running actions for repository', OSError
  733. ).and_return(error_logs).ordered()
  734. flexmock(module.command).should_receive('filter_hooks')
  735. flexmock(module.command).should_receive('execute_hooks')
  736. config = {
  737. 'repositories': [{'path': 'foo'}],
  738. 'retries': 3,
  739. 'retry_wait': 10,
  740. }
  741. arguments = {
  742. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  743. 'create': flexmock(),
  744. }
  745. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  746. assert results == error_logs
  747. def test_run_configuration_with_multiple_repositories_retries_with_timeout():
  748. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  749. flexmock(module).should_receive('get_skip_actions').and_return([])
  750. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  751. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  752. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  753. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  754. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  755. []
  756. ).and_raise(OSError).times(4)
  757. flexmock(module).should_receive('log_error_records').with_args(
  758. 'Error running actions for repository',
  759. OSError,
  760. levelno=logging.WARNING,
  761. log_command_error_output=True,
  762. ).and_return([flexmock()]).ordered()
  763. flexmock(module).should_receive('log_error_records').with_args(
  764. 'Error running actions for repository',
  765. OSError,
  766. levelno=logging.WARNING,
  767. log_command_error_output=True,
  768. ).and_return([flexmock()]).ordered()
  769. # Sleep before retrying foo (and passing)
  770. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  771. # Sleep before retrying bar (and failing)
  772. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  773. error_logs = [flexmock()]
  774. flexmock(module).should_receive('log_error_records').with_args(
  775. 'Error running actions for repository', OSError
  776. ).and_return(error_logs).ordered()
  777. flexmock(module.command).should_receive('filter_hooks')
  778. flexmock(module.command).should_receive('execute_hooks')
  779. config = {
  780. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  781. 'retries': 1,
  782. 'retry_wait': 10,
  783. }
  784. arguments = {
  785. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  786. 'create': flexmock(),
  787. }
  788. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  789. assert results == error_logs
  790. def test_run_actions_runs_repo_create():
  791. flexmock(module).should_receive('add_custom_log_levels')
  792. flexmock(module).should_receive('get_skip_actions').and_return([])
  793. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  794. flexmock(borgmatic.actions.repo_create).should_receive('run_repo_create').once()
  795. tuple(
  796. module.run_actions(
  797. arguments={
  798. 'global': flexmock(dry_run=False, log_file='foo'),
  799. 'repo-create': flexmock(),
  800. },
  801. config_filename=flexmock(),
  802. config={'repositories': []},
  803. config_paths=[],
  804. local_path=flexmock(),
  805. remote_path=flexmock(),
  806. local_borg_version=flexmock(),
  807. repository={'path': 'repo'},
  808. )
  809. )
  810. def test_run_actions_adds_label_file_to_hook_context():
  811. flexmock(module).should_receive('add_custom_log_levels')
  812. flexmock(module).should_receive('get_skip_actions').and_return([])
  813. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  814. expected = flexmock()
  815. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  816. config_filename=object,
  817. repository={'path': 'repo', 'label': 'my repo'},
  818. config={'repositories': []},
  819. config_paths=[],
  820. local_borg_version=object,
  821. create_arguments=object,
  822. global_arguments=object,
  823. dry_run_label='',
  824. local_path=object,
  825. remote_path=object,
  826. ).once().and_return(expected)
  827. result = tuple(
  828. module.run_actions(
  829. arguments={'global': flexmock(dry_run=False, log_file=None), 'create': flexmock()},
  830. config_filename=flexmock(),
  831. config={'repositories': []},
  832. config_paths=[],
  833. local_path=flexmock(),
  834. remote_path=flexmock(),
  835. local_borg_version=flexmock(),
  836. repository={'path': 'repo', 'label': 'my repo'},
  837. )
  838. )
  839. assert result == (expected,)
  840. def test_run_actions_adds_log_file_to_hook_context():
  841. flexmock(module).should_receive('add_custom_log_levels')
  842. flexmock(module).should_receive('get_skip_actions').and_return([])
  843. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  844. expected = flexmock()
  845. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  846. config_filename=object,
  847. repository={'path': 'repo'},
  848. config={'repositories': []},
  849. config_paths=[],
  850. local_borg_version=object,
  851. create_arguments=object,
  852. global_arguments=object,
  853. dry_run_label='',
  854. local_path=object,
  855. remote_path=object,
  856. ).once().and_return(expected)
  857. result = tuple(
  858. module.run_actions(
  859. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  860. config_filename=flexmock(),
  861. config={'repositories': []},
  862. config_paths=[],
  863. local_path=flexmock(),
  864. remote_path=flexmock(),
  865. local_borg_version=flexmock(),
  866. repository={'path': 'repo'},
  867. )
  868. )
  869. assert result == (expected,)
  870. def test_run_actions_runs_transfer():
  871. flexmock(module).should_receive('add_custom_log_levels')
  872. flexmock(module).should_receive('get_skip_actions').and_return([])
  873. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  874. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  875. tuple(
  876. module.run_actions(
  877. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'transfer': flexmock()},
  878. config_filename=flexmock(),
  879. config={'repositories': []},
  880. config_paths=[],
  881. local_path=flexmock(),
  882. remote_path=flexmock(),
  883. local_borg_version=flexmock(),
  884. repository={'path': 'repo'},
  885. )
  886. )
  887. def test_run_actions_runs_create():
  888. flexmock(module).should_receive('add_custom_log_levels')
  889. flexmock(module).should_receive('get_skip_actions').and_return([])
  890. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  891. expected = flexmock()
  892. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  893. result = tuple(
  894. module.run_actions(
  895. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  896. config_filename=flexmock(),
  897. config={'repositories': []},
  898. config_paths=[],
  899. local_path=flexmock(),
  900. remote_path=flexmock(),
  901. local_borg_version=flexmock(),
  902. repository={'path': 'repo'},
  903. )
  904. )
  905. assert result == (expected,)
  906. def test_run_actions_with_skip_actions_skips_create():
  907. flexmock(module).should_receive('add_custom_log_levels')
  908. flexmock(module).should_receive('get_skip_actions').and_return(['create'])
  909. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  910. flexmock(borgmatic.actions.create).should_receive('run_create').never()
  911. tuple(
  912. module.run_actions(
  913. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  914. config_filename=flexmock(),
  915. config={'repositories': [], 'skip_actions': ['create']},
  916. config_paths=[],
  917. local_path=flexmock(),
  918. remote_path=flexmock(),
  919. local_borg_version=flexmock(),
  920. repository={'path': 'repo'},
  921. )
  922. )
  923. def test_run_actions_runs_prune():
  924. flexmock(module).should_receive('add_custom_log_levels')
  925. flexmock(module).should_receive('get_skip_actions').and_return([])
  926. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  927. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  928. tuple(
  929. module.run_actions(
  930. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  931. config_filename=flexmock(),
  932. config={'repositories': []},
  933. config_paths=[],
  934. local_path=flexmock(),
  935. remote_path=flexmock(),
  936. local_borg_version=flexmock(),
  937. repository={'path': 'repo'},
  938. )
  939. )
  940. def test_run_actions_with_skip_actions_skips_prune():
  941. flexmock(module).should_receive('add_custom_log_levels')
  942. flexmock(module).should_receive('get_skip_actions').and_return(['prune'])
  943. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  944. flexmock(borgmatic.actions.prune).should_receive('run_prune').never()
  945. tuple(
  946. module.run_actions(
  947. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  948. config_filename=flexmock(),
  949. config={'repositories': [], 'skip_actions': ['prune']},
  950. config_paths=[],
  951. local_path=flexmock(),
  952. remote_path=flexmock(),
  953. local_borg_version=flexmock(),
  954. repository={'path': 'repo'},
  955. )
  956. )
  957. def test_run_actions_runs_compact():
  958. flexmock(module).should_receive('add_custom_log_levels')
  959. flexmock(module).should_receive('get_skip_actions').and_return([])
  960. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  961. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  962. tuple(
  963. module.run_actions(
  964. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  965. config_filename=flexmock(),
  966. config={'repositories': []},
  967. config_paths=[],
  968. local_path=flexmock(),
  969. remote_path=flexmock(),
  970. local_borg_version=flexmock(),
  971. repository={'path': 'repo'},
  972. )
  973. )
  974. def test_run_actions_with_skip_actions_skips_compact():
  975. flexmock(module).should_receive('add_custom_log_levels')
  976. flexmock(module).should_receive('get_skip_actions').and_return(['compact'])
  977. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  978. flexmock(borgmatic.actions.compact).should_receive('run_compact').never()
  979. tuple(
  980. module.run_actions(
  981. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  982. config_filename=flexmock(),
  983. config={'repositories': [], 'skip_actions': ['compact']},
  984. config_paths=[],
  985. local_path=flexmock(),
  986. remote_path=flexmock(),
  987. local_borg_version=flexmock(),
  988. repository={'path': 'repo'},
  989. )
  990. )
  991. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  992. flexmock(module).should_receive('add_custom_log_levels')
  993. flexmock(module).should_receive('get_skip_actions').and_return([])
  994. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  995. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  996. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  997. tuple(
  998. module.run_actions(
  999. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  1000. config_filename=flexmock(),
  1001. config={'repositories': []},
  1002. config_paths=[],
  1003. local_path=flexmock(),
  1004. remote_path=flexmock(),
  1005. local_borg_version=flexmock(),
  1006. repository={'path': 'repo'},
  1007. )
  1008. )
  1009. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  1010. flexmock(module).should_receive('add_custom_log_levels')
  1011. flexmock(module).should_receive('get_skip_actions').and_return([])
  1012. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1013. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  1014. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  1015. tuple(
  1016. module.run_actions(
  1017. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  1018. config_filename=flexmock(),
  1019. config={'repositories': []},
  1020. config_paths=[],
  1021. local_path=flexmock(),
  1022. remote_path=flexmock(),
  1023. local_borg_version=flexmock(),
  1024. repository={'path': 'repo'},
  1025. )
  1026. )
  1027. def test_run_actions_with_skip_actions_skips_check():
  1028. flexmock(module).should_receive('add_custom_log_levels')
  1029. flexmock(module).should_receive('get_skip_actions').and_return(['check'])
  1030. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1031. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  1032. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  1033. tuple(
  1034. module.run_actions(
  1035. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  1036. config_filename=flexmock(),
  1037. config={'repositories': [], 'skip_actions': ['check']},
  1038. config_paths=[],
  1039. local_path=flexmock(),
  1040. remote_path=flexmock(),
  1041. local_borg_version=flexmock(),
  1042. repository={'path': 'repo'},
  1043. )
  1044. )
  1045. def test_run_actions_runs_extract():
  1046. flexmock(module).should_receive('add_custom_log_levels')
  1047. flexmock(module).should_receive('get_skip_actions').and_return([])
  1048. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1049. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  1050. tuple(
  1051. module.run_actions(
  1052. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'extract': flexmock()},
  1053. config_filename=flexmock(),
  1054. config={'repositories': []},
  1055. config_paths=[],
  1056. local_path=flexmock(),
  1057. remote_path=flexmock(),
  1058. local_borg_version=flexmock(),
  1059. repository={'path': 'repo'},
  1060. )
  1061. )
  1062. def test_run_actions_runs_export_tar():
  1063. flexmock(module).should_receive('add_custom_log_levels')
  1064. flexmock(module).should_receive('get_skip_actions').and_return([])
  1065. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1066. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  1067. tuple(
  1068. module.run_actions(
  1069. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export-tar': flexmock()},
  1070. config_filename=flexmock(),
  1071. config={'repositories': []},
  1072. config_paths=[],
  1073. local_path=flexmock(),
  1074. remote_path=flexmock(),
  1075. local_borg_version=flexmock(),
  1076. repository={'path': 'repo'},
  1077. )
  1078. )
  1079. def test_run_actions_runs_mount():
  1080. flexmock(module).should_receive('add_custom_log_levels')
  1081. flexmock(module).should_receive('get_skip_actions').and_return([])
  1082. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1083. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  1084. tuple(
  1085. module.run_actions(
  1086. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'mount': flexmock()},
  1087. config_filename=flexmock(),
  1088. config={'repositories': []},
  1089. config_paths=[],
  1090. local_path=flexmock(),
  1091. remote_path=flexmock(),
  1092. local_borg_version=flexmock(),
  1093. repository={'path': 'repo'},
  1094. )
  1095. )
  1096. def test_run_actions_runs_restore():
  1097. flexmock(module).should_receive('add_custom_log_levels')
  1098. flexmock(module).should_receive('get_skip_actions').and_return([])
  1099. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1100. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  1101. tuple(
  1102. module.run_actions(
  1103. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'restore': flexmock()},
  1104. config_filename=flexmock(),
  1105. config={'repositories': []},
  1106. config_paths=[],
  1107. local_path=flexmock(),
  1108. remote_path=flexmock(),
  1109. local_borg_version=flexmock(),
  1110. repository={'path': 'repo'},
  1111. )
  1112. )
  1113. def test_run_actions_runs_repo_list():
  1114. flexmock(module).should_receive('add_custom_log_levels')
  1115. flexmock(module).should_receive('get_skip_actions').and_return([])
  1116. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1117. expected = flexmock()
  1118. flexmock(borgmatic.actions.repo_list).should_receive('run_repo_list').and_yield(expected).once()
  1119. result = tuple(
  1120. module.run_actions(
  1121. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'repo-list': flexmock()},
  1122. config_filename=flexmock(),
  1123. config={'repositories': []},
  1124. config_paths=[],
  1125. local_path=flexmock(),
  1126. remote_path=flexmock(),
  1127. local_borg_version=flexmock(),
  1128. repository={'path': 'repo'},
  1129. )
  1130. )
  1131. assert result == (expected,)
  1132. def test_run_actions_runs_list():
  1133. flexmock(module).should_receive('add_custom_log_levels')
  1134. flexmock(module).should_receive('get_skip_actions').and_return([])
  1135. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1136. expected = flexmock()
  1137. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  1138. result = tuple(
  1139. module.run_actions(
  1140. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'list': flexmock()},
  1141. config_filename=flexmock(),
  1142. config={'repositories': []},
  1143. config_paths=[],
  1144. local_path=flexmock(),
  1145. remote_path=flexmock(),
  1146. local_borg_version=flexmock(),
  1147. repository={'path': 'repo'},
  1148. )
  1149. )
  1150. assert result == (expected,)
  1151. def test_run_actions_runs_repo_info():
  1152. flexmock(module).should_receive('add_custom_log_levels')
  1153. flexmock(module).should_receive('get_skip_actions').and_return([])
  1154. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1155. expected = flexmock()
  1156. flexmock(borgmatic.actions.repo_info).should_receive('run_repo_info').and_yield(expected).once()
  1157. result = tuple(
  1158. module.run_actions(
  1159. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'repo-info': flexmock()},
  1160. config_filename=flexmock(),
  1161. config={'repositories': []},
  1162. config_paths=[],
  1163. local_path=flexmock(),
  1164. remote_path=flexmock(),
  1165. local_borg_version=flexmock(),
  1166. repository={'path': 'repo'},
  1167. )
  1168. )
  1169. assert result == (expected,)
  1170. def test_run_actions_runs_info():
  1171. flexmock(module).should_receive('add_custom_log_levels')
  1172. flexmock(module).should_receive('get_skip_actions').and_return([])
  1173. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1174. expected = flexmock()
  1175. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  1176. result = tuple(
  1177. module.run_actions(
  1178. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'info': flexmock()},
  1179. config_filename=flexmock(),
  1180. config={'repositories': []},
  1181. config_paths=[],
  1182. local_path=flexmock(),
  1183. remote_path=flexmock(),
  1184. local_borg_version=flexmock(),
  1185. repository={'path': 'repo'},
  1186. )
  1187. )
  1188. assert result == (expected,)
  1189. def test_run_actions_runs_break_lock():
  1190. flexmock(module).should_receive('add_custom_log_levels')
  1191. flexmock(module).should_receive('get_skip_actions').and_return([])
  1192. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1193. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  1194. tuple(
  1195. module.run_actions(
  1196. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'break-lock': flexmock()},
  1197. config_filename=flexmock(),
  1198. config={'repositories': []},
  1199. config_paths=[],
  1200. local_path=flexmock(),
  1201. remote_path=flexmock(),
  1202. local_borg_version=flexmock(),
  1203. repository={'path': 'repo'},
  1204. )
  1205. )
  1206. def test_run_actions_runs_export_key():
  1207. flexmock(module).should_receive('add_custom_log_levels')
  1208. flexmock(module).should_receive('get_skip_actions').and_return([])
  1209. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1210. flexmock(borgmatic.actions.export_key).should_receive('run_export_key').once()
  1211. tuple(
  1212. module.run_actions(
  1213. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export': flexmock()},
  1214. config_filename=flexmock(),
  1215. config={'repositories': []},
  1216. config_paths=[],
  1217. local_path=flexmock(),
  1218. remote_path=flexmock(),
  1219. local_borg_version=flexmock(),
  1220. repository={'path': 'repo'},
  1221. )
  1222. )
  1223. def test_run_actions_runs_import_key():
  1224. flexmock(module).should_receive('add_custom_log_levels')
  1225. flexmock(module).should_receive('get_skip_actions').and_return([])
  1226. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1227. flexmock(borgmatic.actions.import_key).should_receive('run_import_key').once()
  1228. tuple(
  1229. module.run_actions(
  1230. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'import': flexmock()},
  1231. config_filename=flexmock(),
  1232. config={'repositories': []},
  1233. config_paths=[],
  1234. local_path=flexmock(),
  1235. remote_path=flexmock(),
  1236. local_borg_version=flexmock(),
  1237. repository={'path': 'repo'},
  1238. )
  1239. )
  1240. def test_run_actions_runs_change_passphrase():
  1241. flexmock(module).should_receive('add_custom_log_levels')
  1242. flexmock(module).should_receive('get_skip_actions').and_return([])
  1243. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1244. flexmock(borgmatic.actions.change_passphrase).should_receive('run_change_passphrase').once()
  1245. tuple(
  1246. module.run_actions(
  1247. arguments={
  1248. 'global': flexmock(dry_run=False, log_file='foo'),
  1249. 'change-passphrase': flexmock(),
  1250. },
  1251. config_filename=flexmock(),
  1252. config={'repositories': []},
  1253. config_paths=[],
  1254. local_path=flexmock(),
  1255. remote_path=flexmock(),
  1256. local_borg_version=flexmock(),
  1257. repository={'path': 'repo'},
  1258. )
  1259. )
  1260. def test_run_actions_runs_delete():
  1261. flexmock(module).should_receive('add_custom_log_levels')
  1262. flexmock(module).should_receive('get_skip_actions').and_return([])
  1263. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1264. flexmock(borgmatic.actions.delete).should_receive('run_delete').once()
  1265. tuple(
  1266. module.run_actions(
  1267. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'delete': flexmock()},
  1268. config_filename=flexmock(),
  1269. config={'repositories': []},
  1270. config_paths=[],
  1271. local_path=flexmock(),
  1272. remote_path=flexmock(),
  1273. local_borg_version=flexmock(),
  1274. repository={'path': 'repo'},
  1275. )
  1276. )
  1277. def test_run_actions_runs_repo_delete():
  1278. flexmock(module).should_receive('add_custom_log_levels')
  1279. flexmock(module).should_receive('get_skip_actions').and_return([])
  1280. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1281. flexmock(borgmatic.actions.repo_delete).should_receive('run_repo_delete').once()
  1282. tuple(
  1283. module.run_actions(
  1284. arguments={
  1285. 'global': flexmock(dry_run=False, log_file='foo'),
  1286. 'repo-delete': flexmock(),
  1287. },
  1288. config_filename=flexmock(),
  1289. config={'repositories': []},
  1290. config_paths=[],
  1291. local_path=flexmock(),
  1292. remote_path=flexmock(),
  1293. local_borg_version=flexmock(),
  1294. repository={'path': 'repo'},
  1295. )
  1296. )
  1297. def test_run_actions_runs_borg():
  1298. flexmock(module).should_receive('add_custom_log_levels')
  1299. flexmock(module).should_receive('get_skip_actions').and_return([])
  1300. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1301. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  1302. tuple(
  1303. module.run_actions(
  1304. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'borg': flexmock()},
  1305. config_filename=flexmock(),
  1306. config={'repositories': []},
  1307. config_paths=[],
  1308. local_path=flexmock(),
  1309. remote_path=flexmock(),
  1310. local_borg_version=flexmock(),
  1311. repository={'path': 'repo'},
  1312. )
  1313. )
  1314. def test_run_actions_runs_multiple_actions_in_argument_order():
  1315. flexmock(module).should_receive('add_custom_log_levels')
  1316. flexmock(module).should_receive('get_skip_actions').and_return([])
  1317. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1318. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  1319. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  1320. tuple(
  1321. module.run_actions(
  1322. arguments={
  1323. 'global': flexmock(dry_run=False, log_file='foo'),
  1324. 'borg': flexmock(),
  1325. 'restore': flexmock(),
  1326. },
  1327. config_filename=flexmock(),
  1328. config={'repositories': []},
  1329. config_paths=[],
  1330. local_path=flexmock(),
  1331. remote_path=flexmock(),
  1332. local_borg_version=flexmock(),
  1333. repository={'path': 'repo'},
  1334. )
  1335. )
  1336. @pytest.mark.parametrize(
  1337. 'resolve_env',
  1338. ((True, False),),
  1339. )
  1340. def test_load_configurations_collects_parsed_configurations_and_logs(resolve_env):
  1341. configuration = flexmock()
  1342. other_configuration = flexmock()
  1343. test_expected_logs = [flexmock(), flexmock()]
  1344. other_expected_logs = [flexmock(), flexmock()]
  1345. flexmock(module.validate).should_receive('parse_configuration').and_return(
  1346. configuration, ['/tmp/test.yaml'], test_expected_logs
  1347. ).and_return(other_configuration, ['/tmp/other.yaml'], other_expected_logs)
  1348. configs, config_paths, logs = tuple(
  1349. module.load_configurations(
  1350. ('test.yaml', 'other.yaml'),
  1351. resolve_env=resolve_env,
  1352. )
  1353. )
  1354. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  1355. assert config_paths == ['/tmp/other.yaml', '/tmp/test.yaml']
  1356. assert set(logs) >= set(test_expected_logs + other_expected_logs)
  1357. def test_load_configurations_logs_warning_for_permission_error():
  1358. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  1359. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml',)))
  1360. assert configs == {}
  1361. assert config_paths == []
  1362. assert max(log.levelno for log in logs) == logging.WARNING
  1363. def test_load_configurations_logs_critical_for_parse_error():
  1364. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  1365. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml',)))
  1366. assert configs == {}
  1367. assert config_paths == []
  1368. assert max(log.levelno for log in logs) == logging.CRITICAL
  1369. def test_log_record_does_not_raise():
  1370. module.log_record(levelno=1, foo='bar', baz='quux')
  1371. def test_log_record_with_suppress_does_not_raise():
  1372. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  1373. def test_log_error_records_generates_output_logs_for_message_only():
  1374. flexmock(module).should_receive('log_record').replace_with(dict).once()
  1375. logs = tuple(module.log_error_records('Error'))
  1376. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1377. def test_log_error_records_generates_output_logs_for_called_process_error_with_bytes_ouput():
  1378. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  1379. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1380. logs = tuple(
  1381. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', b'error output'))
  1382. )
  1383. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1384. assert any(log for log in logs if 'error output' in str(log))
  1385. def test_log_error_records_generates_output_logs_for_called_process_error_with_string_ouput():
  1386. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  1387. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1388. logs = tuple(
  1389. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  1390. )
  1391. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1392. assert any(log for log in logs if 'error output' in str(log))
  1393. def test_log_error_records_generates_work_around_output_logs_for_called_process_error_with_repository_access_aborted_exit_code():
  1394. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  1395. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1396. logs = tuple(
  1397. module.log_error_records(
  1398. 'Error',
  1399. subprocess.CalledProcessError(
  1400. module.BORG_REPOSITORY_ACCESS_ABORTED_EXIT_CODE, 'ls', 'error output'
  1401. ),
  1402. )
  1403. )
  1404. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1405. assert any(log for log in logs if 'error output' in str(log))
  1406. assert any(log for log in logs if 'To work around this' in str(log))
  1407. def test_log_error_records_splits_called_process_error_with_multiline_ouput_into_multiple_logs():
  1408. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  1409. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1410. logs = tuple(
  1411. module.log_error_records(
  1412. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output\nanother line')
  1413. )
  1414. )
  1415. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1416. assert any(log for log in logs if 'error output' in str(log))
  1417. def test_log_error_records_generates_logs_for_value_error():
  1418. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  1419. logs = tuple(module.log_error_records('Error', ValueError()))
  1420. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1421. def test_log_error_records_generates_logs_for_os_error():
  1422. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  1423. logs = tuple(module.log_error_records('Error', OSError()))
  1424. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1425. def test_log_error_records_generates_nothing_for_other_error():
  1426. flexmock(module).should_receive('log_record').never()
  1427. logs = tuple(module.log_error_records('Error', KeyError()))
  1428. assert logs == ()
  1429. def test_get_local_path_uses_configuration_value():
  1430. assert module.get_local_path({'test.yaml': {'local_path': 'borg1'}}) == 'borg1'
  1431. def test_get_local_path_without_local_path_defaults_to_borg():
  1432. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  1433. def test_collect_highlander_action_summary_logs_info_for_success_with_bootstrap():
  1434. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1435. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  1436. arguments = {
  1437. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1438. 'global': flexmock(dry_run=False),
  1439. }
  1440. logs = tuple(
  1441. module.collect_highlander_action_summary_logs(
  1442. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1443. )
  1444. )
  1445. assert {log.levelno for log in logs} == {logging.ANSWER}
  1446. def test_collect_highlander_action_summary_logs_error_on_bootstrap_failure():
  1447. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1448. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  1449. ValueError
  1450. )
  1451. arguments = {
  1452. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1453. 'global': flexmock(dry_run=False),
  1454. }
  1455. logs = tuple(
  1456. module.collect_highlander_action_summary_logs(
  1457. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1458. )
  1459. )
  1460. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1461. def test_collect_highlander_action_summary_logs_error_on_bootstrap_local_borg_version_failure():
  1462. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  1463. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').never()
  1464. arguments = {
  1465. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1466. 'global': flexmock(dry_run=False),
  1467. }
  1468. logs = tuple(
  1469. module.collect_highlander_action_summary_logs(
  1470. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1471. )
  1472. )
  1473. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1474. def test_collect_highlander_action_summary_logs_info_for_success_with_generate():
  1475. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate')
  1476. arguments = {
  1477. 'generate': flexmock(destination='test.yaml'),
  1478. 'global': flexmock(dry_run=False),
  1479. }
  1480. logs = tuple(
  1481. module.collect_highlander_action_summary_logs(
  1482. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1483. )
  1484. )
  1485. assert {log.levelno for log in logs} == {logging.ANSWER}
  1486. def test_collect_highlander_action_summary_logs_error_on_generate_failure():
  1487. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate').and_raise(
  1488. ValueError
  1489. )
  1490. arguments = {
  1491. 'generate': flexmock(destination='test.yaml'),
  1492. 'global': flexmock(dry_run=False),
  1493. }
  1494. logs = tuple(
  1495. module.collect_highlander_action_summary_logs(
  1496. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1497. )
  1498. )
  1499. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1500. def test_collect_highlander_action_summary_logs_info_for_success_with_validate():
  1501. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1502. arguments = {
  1503. 'validate': flexmock(),
  1504. 'global': flexmock(dry_run=False),
  1505. }
  1506. logs = tuple(
  1507. module.collect_highlander_action_summary_logs(
  1508. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1509. )
  1510. )
  1511. assert {log.levelno for log in logs} == {logging.ANSWER}
  1512. def test_collect_highlander_action_summary_logs_error_on_validate_parse_failure():
  1513. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1514. arguments = {
  1515. 'validate': flexmock(),
  1516. 'global': flexmock(dry_run=False),
  1517. }
  1518. logs = tuple(
  1519. module.collect_highlander_action_summary_logs(
  1520. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=True
  1521. )
  1522. )
  1523. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1524. def test_collect_highlander_action_summary_logs_error_on_run_validate_failure():
  1525. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate').and_raise(
  1526. ValueError
  1527. )
  1528. arguments = {
  1529. 'validate': flexmock(),
  1530. 'global': flexmock(dry_run=False),
  1531. }
  1532. logs = tuple(
  1533. module.collect_highlander_action_summary_logs(
  1534. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1535. )
  1536. )
  1537. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1538. def test_collect_configuration_run_summary_logs_info_for_success():
  1539. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1540. flexmock(module.command).should_receive('filter_hooks')
  1541. flexmock(module.command).should_receive('execute_hooks')
  1542. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1543. flexmock(module).should_receive('run_configuration').and_return([])
  1544. arguments = {'global': flexmock(dry_run=False, log_file=flexmock())}
  1545. logs = tuple(
  1546. module.collect_configuration_run_summary_logs(
  1547. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1548. )
  1549. )
  1550. assert {log.levelno for log in logs} == {logging.INFO}
  1551. def test_collect_configuration_run_summary_executes_hooks_for_create():
  1552. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1553. flexmock(module.command).should_receive('filter_hooks')
  1554. flexmock(module.command).should_receive('execute_hooks')
  1555. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1556. flexmock(module).should_receive('run_configuration').and_return([])
  1557. arguments = {
  1558. 'create': flexmock(),
  1559. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  1560. }
  1561. logs = tuple(
  1562. module.collect_configuration_run_summary_logs(
  1563. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1564. )
  1565. )
  1566. assert {log.levelno for log in logs} == {logging.INFO}
  1567. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  1568. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1569. flexmock(module.command).should_receive('filter_hooks')
  1570. flexmock(module.command).should_receive('execute_hooks')
  1571. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1572. flexmock(module).should_receive('run_configuration').and_return([])
  1573. arguments = {
  1574. 'extract': flexmock(repository='repo'),
  1575. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1576. }
  1577. logs = tuple(
  1578. module.collect_configuration_run_summary_logs(
  1579. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1580. )
  1581. )
  1582. assert {log.levelno for log in logs} == {logging.INFO}
  1583. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  1584. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1585. ValueError
  1586. )
  1587. expected_logs = (flexmock(),)
  1588. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1589. arguments = {'extract': flexmock(repository='repo', log_file=flexmock())}
  1590. logs = tuple(
  1591. module.collect_configuration_run_summary_logs(
  1592. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1593. )
  1594. )
  1595. assert logs == expected_logs
  1596. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  1597. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1598. flexmock(module.command).should_receive('filter_hooks')
  1599. flexmock(module.command).should_receive('execute_hooks')
  1600. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1601. flexmock(module).should_receive('run_configuration').and_return([])
  1602. arguments = {
  1603. 'mount': flexmock(repository='repo'),
  1604. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1605. }
  1606. logs = tuple(
  1607. module.collect_configuration_run_summary_logs(
  1608. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1609. )
  1610. )
  1611. assert {log.levelno for log in logs} == {logging.INFO}
  1612. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  1613. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1614. ValueError
  1615. )
  1616. expected_logs = (flexmock(),)
  1617. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1618. arguments = {
  1619. 'mount': flexmock(repository='repo'),
  1620. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1621. }
  1622. logs = tuple(
  1623. module.collect_configuration_run_summary_logs(
  1624. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1625. )
  1626. )
  1627. assert logs == expected_logs
  1628. def test_collect_configuration_run_summary_logs_missing_configs_error():
  1629. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1630. flexmock(module.command).should_receive('filter_hooks')
  1631. flexmock(module.command).should_receive('execute_hooks')
  1632. arguments = {'global': flexmock(config_paths=[], log_file=flexmock())}
  1633. expected_logs = (flexmock(),)
  1634. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1635. logs = tuple(
  1636. module.collect_configuration_run_summary_logs({}, config_paths=[], arguments=arguments)
  1637. )
  1638. assert logs == expected_logs
  1639. def test_collect_configuration_run_summary_logs_pre_hook_error():
  1640. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1641. flexmock(module.command).should_receive('filter_hooks')
  1642. flexmock(module.command).should_receive('execute_hooks').and_raise(ValueError)
  1643. expected_logs = (flexmock(),)
  1644. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1645. arguments = {
  1646. 'create': flexmock(),
  1647. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  1648. }
  1649. logs = tuple(
  1650. module.collect_configuration_run_summary_logs(
  1651. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1652. )
  1653. )
  1654. assert logs == expected_logs
  1655. def test_collect_configuration_run_summary_logs_post_hook_error():
  1656. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1657. flexmock(module.command).should_receive('filter_hooks')
  1658. flexmock(module.command).should_receive('execute_hooks').and_return(None).and_raise(ValueError)
  1659. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1660. flexmock(module).should_receive('run_configuration').and_return([])
  1661. expected_logs = (flexmock(),)
  1662. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1663. arguments = {
  1664. 'create': flexmock(),
  1665. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  1666. }
  1667. logs = tuple(
  1668. module.collect_configuration_run_summary_logs(
  1669. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1670. )
  1671. )
  1672. assert expected_logs[0] in logs
  1673. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  1674. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1675. ValueError
  1676. )
  1677. expected_logs = (flexmock(),)
  1678. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1679. arguments = {
  1680. 'list': flexmock(repository='repo', archive='test'),
  1681. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1682. }
  1683. logs = tuple(
  1684. module.collect_configuration_run_summary_logs(
  1685. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1686. )
  1687. )
  1688. assert logs == expected_logs
  1689. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  1690. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1691. flexmock(module.command).should_receive('filter_hooks')
  1692. flexmock(module.command).should_receive('execute_hooks')
  1693. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1694. flexmock(module).should_receive('run_configuration').and_return([])
  1695. arguments = {
  1696. 'list': flexmock(repository='repo', archive=None),
  1697. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1698. }
  1699. logs = tuple(
  1700. module.collect_configuration_run_summary_logs(
  1701. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1702. )
  1703. )
  1704. assert {log.levelno for log in logs} == {logging.INFO}
  1705. def test_collect_configuration_run_summary_logs_run_configuration_error_logs():
  1706. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1707. flexmock(module.command).should_receive('filter_hooks')
  1708. flexmock(module.command).should_receive('execute_hooks')
  1709. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1710. flexmock(module).should_receive('run_configuration').and_return(
  1711. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1712. )
  1713. flexmock(module).should_receive('log_error_records').and_return([])
  1714. arguments = {'global': flexmock(dry_run=False, log_file=flexmock())}
  1715. logs = tuple(
  1716. module.collect_configuration_run_summary_logs(
  1717. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1718. )
  1719. )
  1720. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1721. def test_collect_configuration_run_summary_logs_run_umount_error():
  1722. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1723. flexmock(module.command).should_receive('filter_hooks')
  1724. flexmock(module.command).should_receive('execute_hooks')
  1725. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1726. flexmock(module).should_receive('run_configuration').and_return([])
  1727. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  1728. flexmock(module).should_receive('log_error_records').and_return(
  1729. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1730. )
  1731. arguments = {
  1732. 'umount': flexmock(mount_point='/mnt'),
  1733. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1734. }
  1735. logs = tuple(
  1736. module.collect_configuration_run_summary_logs(
  1737. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1738. )
  1739. )
  1740. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  1741. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  1742. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1743. flexmock(module.command).should_receive('filter_hooks')
  1744. flexmock(module.command).should_receive('execute_hooks')
  1745. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1746. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  1747. ['baz']
  1748. )
  1749. stdout = flexmock()
  1750. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  1751. flexmock(module.sys).stdout = stdout
  1752. arguments = {'global': flexmock(dry_run=False, log_file=flexmock())}
  1753. tuple(
  1754. module.collect_configuration_run_summary_logs(
  1755. {'test.yaml': {}, 'test2.yaml': {}},
  1756. config_paths=['/tmp/test.yaml', '/tmp/test2.yaml'],
  1757. arguments=arguments,
  1758. )
  1759. )
  1760. def test_check_and_show_help_on_no_args_shows_help_when_no_args_and_default_actions_false():
  1761. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1762. flexmock(module).should_receive('parse_arguments').with_args('--help').once()
  1763. flexmock(module.sys).should_receive('exit').with_args(0).once()
  1764. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}})
  1765. def test_check_and_show_help_on_no_args_does_not_show_help_when_no_args_and_default_actions_true():
  1766. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1767. flexmock(module).should_receive('parse_arguments').never()
  1768. flexmock(module.sys).should_receive('exit').never()
  1769. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': True}})
  1770. def test_check_and_show_help_on_no_args_does_not_show_help_when_args_provided():
  1771. flexmock(module.sys).should_receive('argv').and_return(['borgmatic', '--create'])
  1772. flexmock(module).should_receive('parse_arguments').never()
  1773. flexmock(module.sys).should_receive('exit').never()
  1774. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}})