test_borgmatic.py 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180
  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_recreate():
  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.recreate).should_receive('run_recreate').once()
  928. tuple(
  929. module.run_actions(
  930. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'recreate': 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_recreate():
  941. flexmock(module).should_receive('add_custom_log_levels')
  942. flexmock(module).should_receive('get_skip_actions').and_return(['recreate'])
  943. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  944. flexmock(borgmatic.actions.recreate).should_receive('run_recreate').never()
  945. tuple(
  946. module.run_actions(
  947. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'recreate': flexmock()},
  948. config_filename=flexmock(),
  949. config={'repositories': [], 'skip_actions': ['recreate']},
  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_prune():
  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.prune).should_receive('run_prune').once()
  962. tuple(
  963. module.run_actions(
  964. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': 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_prune():
  975. flexmock(module).should_receive('add_custom_log_levels')
  976. flexmock(module).should_receive('get_skip_actions').and_return(['prune'])
  977. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  978. flexmock(borgmatic.actions.prune).should_receive('run_prune').never()
  979. tuple(
  980. module.run_actions(
  981. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  982. config_filename=flexmock(),
  983. config={'repositories': [], 'skip_actions': ['prune']},
  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_compact():
  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(borgmatic.actions.compact).should_receive('run_compact').once()
  996. tuple(
  997. module.run_actions(
  998. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  999. config_filename=flexmock(),
  1000. config={'repositories': []},
  1001. config_paths=[],
  1002. local_path=flexmock(),
  1003. remote_path=flexmock(),
  1004. local_borg_version=flexmock(),
  1005. repository={'path': 'repo'},
  1006. )
  1007. )
  1008. def test_run_actions_with_skip_actions_skips_compact():
  1009. flexmock(module).should_receive('add_custom_log_levels')
  1010. flexmock(module).should_receive('get_skip_actions').and_return(['compact'])
  1011. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1012. flexmock(borgmatic.actions.compact).should_receive('run_compact').never()
  1013. tuple(
  1014. module.run_actions(
  1015. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  1016. config_filename=flexmock(),
  1017. config={'repositories': [], 'skip_actions': ['compact']},
  1018. config_paths=[],
  1019. local_path=flexmock(),
  1020. remote_path=flexmock(),
  1021. local_borg_version=flexmock(),
  1022. repository={'path': 'repo'},
  1023. )
  1024. )
  1025. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  1026. flexmock(module).should_receive('add_custom_log_levels')
  1027. flexmock(module).should_receive('get_skip_actions').and_return([])
  1028. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1029. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  1030. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  1031. tuple(
  1032. module.run_actions(
  1033. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  1034. config_filename=flexmock(),
  1035. config={'repositories': []},
  1036. config_paths=[],
  1037. local_path=flexmock(),
  1038. remote_path=flexmock(),
  1039. local_borg_version=flexmock(),
  1040. repository={'path': 'repo'},
  1041. )
  1042. )
  1043. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  1044. flexmock(module).should_receive('add_custom_log_levels')
  1045. flexmock(module).should_receive('get_skip_actions').and_return([])
  1046. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1047. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  1048. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  1049. tuple(
  1050. module.run_actions(
  1051. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  1052. config_filename=flexmock(),
  1053. config={'repositories': []},
  1054. config_paths=[],
  1055. local_path=flexmock(),
  1056. remote_path=flexmock(),
  1057. local_borg_version=flexmock(),
  1058. repository={'path': 'repo'},
  1059. )
  1060. )
  1061. def test_run_actions_with_skip_actions_skips_check():
  1062. flexmock(module).should_receive('add_custom_log_levels')
  1063. flexmock(module).should_receive('get_skip_actions').and_return(['check'])
  1064. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1065. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  1066. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  1067. tuple(
  1068. module.run_actions(
  1069. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  1070. config_filename=flexmock(),
  1071. config={'repositories': [], 'skip_actions': ['check']},
  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_extract():
  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.extract).should_receive('run_extract').once()
  1084. tuple(
  1085. module.run_actions(
  1086. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'extract': 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_export_tar():
  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.export_tar).should_receive('run_export_tar').once()
  1101. tuple(
  1102. module.run_actions(
  1103. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export-tar': 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_mount():
  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. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  1118. tuple(
  1119. module.run_actions(
  1120. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'mount': flexmock()},
  1121. config_filename=flexmock(),
  1122. config={'repositories': []},
  1123. config_paths=[],
  1124. local_path=flexmock(),
  1125. remote_path=flexmock(),
  1126. local_borg_version=flexmock(),
  1127. repository={'path': 'repo'},
  1128. )
  1129. )
  1130. def test_run_actions_runs_restore():
  1131. flexmock(module).should_receive('add_custom_log_levels')
  1132. flexmock(module).should_receive('get_skip_actions').and_return([])
  1133. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1134. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  1135. tuple(
  1136. module.run_actions(
  1137. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'restore': flexmock()},
  1138. config_filename=flexmock(),
  1139. config={'repositories': []},
  1140. config_paths=[],
  1141. local_path=flexmock(),
  1142. remote_path=flexmock(),
  1143. local_borg_version=flexmock(),
  1144. repository={'path': 'repo'},
  1145. )
  1146. )
  1147. def test_run_actions_runs_repo_list():
  1148. flexmock(module).should_receive('add_custom_log_levels')
  1149. flexmock(module).should_receive('get_skip_actions').and_return([])
  1150. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1151. expected = flexmock()
  1152. flexmock(borgmatic.actions.repo_list).should_receive('run_repo_list').and_yield(expected).once()
  1153. result = tuple(
  1154. module.run_actions(
  1155. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'repo-list': flexmock()},
  1156. config_filename=flexmock(),
  1157. config={'repositories': []},
  1158. config_paths=[],
  1159. local_path=flexmock(),
  1160. remote_path=flexmock(),
  1161. local_borg_version=flexmock(),
  1162. repository={'path': 'repo'},
  1163. )
  1164. )
  1165. assert result == (expected,)
  1166. def test_run_actions_runs_list():
  1167. flexmock(module).should_receive('add_custom_log_levels')
  1168. flexmock(module).should_receive('get_skip_actions').and_return([])
  1169. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1170. expected = flexmock()
  1171. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  1172. result = tuple(
  1173. module.run_actions(
  1174. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'list': flexmock()},
  1175. config_filename=flexmock(),
  1176. config={'repositories': []},
  1177. config_paths=[],
  1178. local_path=flexmock(),
  1179. remote_path=flexmock(),
  1180. local_borg_version=flexmock(),
  1181. repository={'path': 'repo'},
  1182. )
  1183. )
  1184. assert result == (expected,)
  1185. def test_run_actions_runs_repo_info():
  1186. flexmock(module).should_receive('add_custom_log_levels')
  1187. flexmock(module).should_receive('get_skip_actions').and_return([])
  1188. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1189. expected = flexmock()
  1190. flexmock(borgmatic.actions.repo_info).should_receive('run_repo_info').and_yield(expected).once()
  1191. result = tuple(
  1192. module.run_actions(
  1193. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'repo-info': flexmock()},
  1194. config_filename=flexmock(),
  1195. config={'repositories': []},
  1196. config_paths=[],
  1197. local_path=flexmock(),
  1198. remote_path=flexmock(),
  1199. local_borg_version=flexmock(),
  1200. repository={'path': 'repo'},
  1201. )
  1202. )
  1203. assert result == (expected,)
  1204. def test_run_actions_runs_info():
  1205. flexmock(module).should_receive('add_custom_log_levels')
  1206. flexmock(module).should_receive('get_skip_actions').and_return([])
  1207. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1208. expected = flexmock()
  1209. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  1210. result = tuple(
  1211. module.run_actions(
  1212. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'info': flexmock()},
  1213. config_filename=flexmock(),
  1214. config={'repositories': []},
  1215. config_paths=[],
  1216. local_path=flexmock(),
  1217. remote_path=flexmock(),
  1218. local_borg_version=flexmock(),
  1219. repository={'path': 'repo'},
  1220. )
  1221. )
  1222. assert result == (expected,)
  1223. def test_run_actions_runs_break_lock():
  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.break_lock).should_receive('run_break_lock').once()
  1228. tuple(
  1229. module.run_actions(
  1230. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'break-lock': 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_export_key():
  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.export_key).should_receive('run_export_key').once()
  1245. tuple(
  1246. module.run_actions(
  1247. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export': flexmock()},
  1248. config_filename=flexmock(),
  1249. config={'repositories': []},
  1250. config_paths=[],
  1251. local_path=flexmock(),
  1252. remote_path=flexmock(),
  1253. local_borg_version=flexmock(),
  1254. repository={'path': 'repo'},
  1255. )
  1256. )
  1257. def test_run_actions_runs_import_key():
  1258. flexmock(module).should_receive('add_custom_log_levels')
  1259. flexmock(module).should_receive('get_skip_actions').and_return([])
  1260. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1261. flexmock(borgmatic.actions.import_key).should_receive('run_import_key').once()
  1262. tuple(
  1263. module.run_actions(
  1264. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'import': flexmock()},
  1265. config_filename=flexmock(),
  1266. config={'repositories': []},
  1267. config_paths=[],
  1268. local_path=flexmock(),
  1269. remote_path=flexmock(),
  1270. local_borg_version=flexmock(),
  1271. repository={'path': 'repo'},
  1272. )
  1273. )
  1274. def test_run_actions_runs_change_passphrase():
  1275. flexmock(module).should_receive('add_custom_log_levels')
  1276. flexmock(module).should_receive('get_skip_actions').and_return([])
  1277. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1278. flexmock(borgmatic.actions.change_passphrase).should_receive('run_change_passphrase').once()
  1279. tuple(
  1280. module.run_actions(
  1281. arguments={
  1282. 'global': flexmock(dry_run=False, log_file='foo'),
  1283. 'change-passphrase': flexmock(),
  1284. },
  1285. config_filename=flexmock(),
  1286. config={'repositories': []},
  1287. config_paths=[],
  1288. local_path=flexmock(),
  1289. remote_path=flexmock(),
  1290. local_borg_version=flexmock(),
  1291. repository={'path': 'repo'},
  1292. )
  1293. )
  1294. def test_run_actions_runs_delete():
  1295. flexmock(module).should_receive('add_custom_log_levels')
  1296. flexmock(module).should_receive('get_skip_actions').and_return([])
  1297. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1298. flexmock(borgmatic.actions.delete).should_receive('run_delete').once()
  1299. tuple(
  1300. module.run_actions(
  1301. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'delete': flexmock()},
  1302. config_filename=flexmock(),
  1303. config={'repositories': []},
  1304. config_paths=[],
  1305. local_path=flexmock(),
  1306. remote_path=flexmock(),
  1307. local_borg_version=flexmock(),
  1308. repository={'path': 'repo'},
  1309. )
  1310. )
  1311. def test_run_actions_runs_repo_delete():
  1312. flexmock(module).should_receive('add_custom_log_levels')
  1313. flexmock(module).should_receive('get_skip_actions').and_return([])
  1314. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1315. flexmock(borgmatic.actions.repo_delete).should_receive('run_repo_delete').once()
  1316. tuple(
  1317. module.run_actions(
  1318. arguments={
  1319. 'global': flexmock(dry_run=False, log_file='foo'),
  1320. 'repo-delete': flexmock(),
  1321. },
  1322. config_filename=flexmock(),
  1323. config={'repositories': []},
  1324. config_paths=[],
  1325. local_path=flexmock(),
  1326. remote_path=flexmock(),
  1327. local_borg_version=flexmock(),
  1328. repository={'path': 'repo'},
  1329. )
  1330. )
  1331. def test_run_actions_runs_borg():
  1332. flexmock(module).should_receive('add_custom_log_levels')
  1333. flexmock(module).should_receive('get_skip_actions').and_return([])
  1334. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1335. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  1336. tuple(
  1337. module.run_actions(
  1338. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'borg': flexmock()},
  1339. config_filename=flexmock(),
  1340. config={'repositories': []},
  1341. config_paths=[],
  1342. local_path=flexmock(),
  1343. remote_path=flexmock(),
  1344. local_borg_version=flexmock(),
  1345. repository={'path': 'repo'},
  1346. )
  1347. )
  1348. def test_run_actions_runs_multiple_actions_in_argument_order():
  1349. flexmock(module).should_receive('add_custom_log_levels')
  1350. flexmock(module).should_receive('get_skip_actions').and_return([])
  1351. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1352. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  1353. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  1354. tuple(
  1355. module.run_actions(
  1356. arguments={
  1357. 'global': flexmock(dry_run=False, log_file='foo'),
  1358. 'borg': flexmock(),
  1359. 'restore': flexmock(),
  1360. },
  1361. config_filename=flexmock(),
  1362. config={'repositories': []},
  1363. config_paths=[],
  1364. local_path=flexmock(),
  1365. remote_path=flexmock(),
  1366. local_borg_version=flexmock(),
  1367. repository={'path': 'repo'},
  1368. )
  1369. )
  1370. @pytest.mark.parametrize(
  1371. 'resolve_env',
  1372. ((True, False),),
  1373. )
  1374. def test_load_configurations_collects_parsed_configurations_and_logs(resolve_env):
  1375. configuration = flexmock()
  1376. other_configuration = flexmock()
  1377. test_expected_logs = [flexmock(), flexmock()]
  1378. other_expected_logs = [flexmock(), flexmock()]
  1379. flexmock(module.validate).should_receive('parse_configuration').and_return(
  1380. configuration, ['/tmp/test.yaml'], test_expected_logs
  1381. ).and_return(other_configuration, ['/tmp/other.yaml'], other_expected_logs)
  1382. configs, config_paths, logs = tuple(
  1383. module.load_configurations(
  1384. ('test.yaml', 'other.yaml'),
  1385. global_arguments=flexmock(),
  1386. resolve_env=resolve_env,
  1387. )
  1388. )
  1389. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  1390. assert config_paths == ['/tmp/other.yaml', '/tmp/test.yaml']
  1391. assert set(logs) >= set(test_expected_logs + other_expected_logs)
  1392. def test_load_configurations_logs_warning_for_permission_error():
  1393. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  1394. configs, config_paths, logs = tuple(
  1395. module.load_configurations(('test.yaml',), global_arguments=flexmock())
  1396. )
  1397. assert configs == {}
  1398. assert config_paths == []
  1399. assert max(log.levelno for log in logs) == logging.WARNING
  1400. def test_load_configurations_logs_critical_for_parse_error():
  1401. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  1402. configs, config_paths, logs = tuple(
  1403. module.load_configurations(('test.yaml',), global_arguments=flexmock())
  1404. )
  1405. assert configs == {}
  1406. assert config_paths == []
  1407. assert max(log.levelno for log in logs) == logging.CRITICAL
  1408. def test_log_record_does_not_raise():
  1409. module.log_record(levelno=1, foo='bar', baz='quux')
  1410. def test_log_record_with_suppress_does_not_raise():
  1411. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  1412. def test_log_error_records_generates_output_logs_for_message_only():
  1413. flexmock(module).should_receive('log_record').replace_with(dict).once()
  1414. logs = tuple(module.log_error_records('Error'))
  1415. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1416. def test_log_error_records_generates_output_logs_for_called_process_error_with_bytes_ouput():
  1417. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  1418. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1419. logs = tuple(
  1420. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', b'error output'))
  1421. )
  1422. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1423. assert any(log for log in logs if 'error output' in str(log))
  1424. def test_log_error_records_generates_output_logs_for_called_process_error_with_string_ouput():
  1425. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  1426. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1427. logs = tuple(
  1428. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  1429. )
  1430. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1431. assert any(log for log in logs if 'error output' in str(log))
  1432. def test_log_error_records_generates_work_around_output_logs_for_called_process_error_with_repository_access_aborted_exit_code():
  1433. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  1434. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1435. logs = tuple(
  1436. module.log_error_records(
  1437. 'Error',
  1438. subprocess.CalledProcessError(
  1439. module.BORG_REPOSITORY_ACCESS_ABORTED_EXIT_CODE, 'ls', 'error output'
  1440. ),
  1441. )
  1442. )
  1443. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1444. assert any(log for log in logs if 'error output' in str(log))
  1445. assert any(log for log in logs if 'To work around this' in str(log))
  1446. def test_log_error_records_splits_called_process_error_with_multiline_ouput_into_multiple_logs():
  1447. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  1448. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1449. logs = tuple(
  1450. module.log_error_records(
  1451. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output\nanother line')
  1452. )
  1453. )
  1454. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1455. assert any(log for log in logs if 'error output' in str(log))
  1456. def test_log_error_records_generates_logs_for_value_error():
  1457. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  1458. logs = tuple(module.log_error_records('Error', ValueError()))
  1459. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1460. def test_log_error_records_generates_logs_for_os_error():
  1461. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  1462. logs = tuple(module.log_error_records('Error', OSError()))
  1463. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1464. def test_log_error_records_generates_nothing_for_other_error():
  1465. flexmock(module).should_receive('log_record').never()
  1466. logs = tuple(module.log_error_records('Error', KeyError()))
  1467. assert logs == ()
  1468. def test_get_local_path_uses_configuration_value():
  1469. assert module.get_local_path({'test.yaml': {'local_path': 'borg1'}}) == 'borg1'
  1470. def test_get_local_path_without_local_path_defaults_to_borg():
  1471. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  1472. def test_collect_highlander_action_summary_logs_info_for_success_with_bootstrap():
  1473. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1474. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  1475. arguments = {
  1476. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1477. 'global': flexmock(dry_run=False),
  1478. }
  1479. logs = tuple(
  1480. module.collect_highlander_action_summary_logs(
  1481. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1482. )
  1483. )
  1484. assert {log.levelno for log in logs} == {logging.ANSWER}
  1485. def test_collect_highlander_action_summary_logs_error_on_bootstrap_failure():
  1486. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1487. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  1488. ValueError
  1489. )
  1490. arguments = {
  1491. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  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_error_on_bootstrap_local_borg_version_failure():
  1501. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  1502. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').never()
  1503. arguments = {
  1504. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1505. 'global': flexmock(dry_run=False),
  1506. }
  1507. logs = tuple(
  1508. module.collect_highlander_action_summary_logs(
  1509. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1510. )
  1511. )
  1512. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1513. def test_collect_highlander_action_summary_logs_info_for_success_with_generate():
  1514. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate')
  1515. arguments = {
  1516. 'generate': flexmock(destination='test.yaml'),
  1517. 'global': flexmock(dry_run=False),
  1518. }
  1519. logs = tuple(
  1520. module.collect_highlander_action_summary_logs(
  1521. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1522. )
  1523. )
  1524. assert {log.levelno for log in logs} == {logging.ANSWER}
  1525. def test_collect_highlander_action_summary_logs_error_on_generate_failure():
  1526. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate').and_raise(
  1527. ValueError
  1528. )
  1529. arguments = {
  1530. 'generate': flexmock(destination='test.yaml'),
  1531. 'global': flexmock(dry_run=False),
  1532. }
  1533. logs = tuple(
  1534. module.collect_highlander_action_summary_logs(
  1535. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1536. )
  1537. )
  1538. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1539. def test_collect_highlander_action_summary_logs_info_for_success_with_validate():
  1540. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1541. arguments = {
  1542. 'validate': flexmock(),
  1543. 'global': flexmock(dry_run=False),
  1544. }
  1545. logs = tuple(
  1546. module.collect_highlander_action_summary_logs(
  1547. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1548. )
  1549. )
  1550. assert {log.levelno for log in logs} == {logging.ANSWER}
  1551. def test_collect_highlander_action_summary_logs_error_on_validate_parse_failure():
  1552. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1553. arguments = {
  1554. 'validate': flexmock(),
  1555. 'global': flexmock(dry_run=False),
  1556. }
  1557. logs = tuple(
  1558. module.collect_highlander_action_summary_logs(
  1559. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=True
  1560. )
  1561. )
  1562. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1563. def test_collect_highlander_action_summary_logs_error_on_run_validate_failure():
  1564. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate').and_raise(
  1565. ValueError
  1566. )
  1567. arguments = {
  1568. 'validate': flexmock(),
  1569. 'global': flexmock(dry_run=False),
  1570. }
  1571. logs = tuple(
  1572. module.collect_highlander_action_summary_logs(
  1573. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1574. )
  1575. )
  1576. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1577. def test_collect_configuration_run_summary_logs_info_for_success():
  1578. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1579. flexmock(module.command).should_receive('filter_hooks')
  1580. flexmock(module.command).should_receive('execute_hooks')
  1581. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1582. flexmock(module).should_receive('run_configuration').and_return([])
  1583. arguments = {'global': flexmock(dry_run=False, log_file=flexmock())}
  1584. logs = tuple(
  1585. module.collect_configuration_run_summary_logs(
  1586. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1587. )
  1588. )
  1589. assert {log.levelno for log in logs} == {logging.INFO}
  1590. def test_collect_configuration_run_summary_executes_hooks_for_create():
  1591. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1592. flexmock(module.command).should_receive('filter_hooks')
  1593. flexmock(module.command).should_receive('execute_hooks')
  1594. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1595. flexmock(module).should_receive('run_configuration').and_return([])
  1596. arguments = {
  1597. 'create': flexmock(),
  1598. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  1599. }
  1600. logs = tuple(
  1601. module.collect_configuration_run_summary_logs(
  1602. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1603. )
  1604. )
  1605. assert {log.levelno for log in logs} == {logging.INFO}
  1606. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  1607. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1608. flexmock(module.command).should_receive('filter_hooks')
  1609. flexmock(module.command).should_receive('execute_hooks')
  1610. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1611. flexmock(module).should_receive('run_configuration').and_return([])
  1612. arguments = {
  1613. 'extract': flexmock(repository='repo'),
  1614. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1615. }
  1616. logs = tuple(
  1617. module.collect_configuration_run_summary_logs(
  1618. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1619. )
  1620. )
  1621. assert {log.levelno for log in logs} == {logging.INFO}
  1622. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  1623. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1624. ValueError
  1625. )
  1626. expected_logs = (flexmock(),)
  1627. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1628. arguments = {'extract': flexmock(repository='repo', log_file=flexmock())}
  1629. logs = tuple(
  1630. module.collect_configuration_run_summary_logs(
  1631. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1632. )
  1633. )
  1634. assert logs == expected_logs
  1635. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  1636. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1637. flexmock(module.command).should_receive('filter_hooks')
  1638. flexmock(module.command).should_receive('execute_hooks')
  1639. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1640. flexmock(module).should_receive('run_configuration').and_return([])
  1641. arguments = {
  1642. 'mount': flexmock(repository='repo'),
  1643. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1644. }
  1645. logs = tuple(
  1646. module.collect_configuration_run_summary_logs(
  1647. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1648. )
  1649. )
  1650. assert {log.levelno for log in logs} == {logging.INFO}
  1651. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  1652. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1653. ValueError
  1654. )
  1655. expected_logs = (flexmock(),)
  1656. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1657. arguments = {
  1658. 'mount': flexmock(repository='repo'),
  1659. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1660. }
  1661. logs = tuple(
  1662. module.collect_configuration_run_summary_logs(
  1663. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1664. )
  1665. )
  1666. assert logs == expected_logs
  1667. def test_collect_configuration_run_summary_logs_missing_configs_error():
  1668. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1669. flexmock(module.command).should_receive('filter_hooks')
  1670. flexmock(module.command).should_receive('execute_hooks')
  1671. arguments = {'global': flexmock(config_paths=[], log_file=flexmock())}
  1672. expected_logs = (flexmock(),)
  1673. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1674. logs = tuple(
  1675. module.collect_configuration_run_summary_logs({}, config_paths=[], arguments=arguments)
  1676. )
  1677. assert logs == expected_logs
  1678. def test_collect_configuration_run_summary_logs_pre_hook_error():
  1679. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1680. flexmock(module.command).should_receive('filter_hooks')
  1681. flexmock(module.command).should_receive('execute_hooks').and_raise(ValueError)
  1682. expected_logs = (flexmock(),)
  1683. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1684. arguments = {
  1685. 'create': flexmock(),
  1686. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  1687. }
  1688. logs = tuple(
  1689. module.collect_configuration_run_summary_logs(
  1690. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1691. )
  1692. )
  1693. assert logs == expected_logs
  1694. def test_collect_configuration_run_summary_logs_post_hook_error():
  1695. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1696. flexmock(module.command).should_receive('filter_hooks')
  1697. flexmock(module.command).should_receive('execute_hooks').and_return(None).and_raise(ValueError)
  1698. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1699. flexmock(module).should_receive('run_configuration').and_return([])
  1700. expected_logs = (flexmock(),)
  1701. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1702. arguments = {
  1703. 'create': flexmock(),
  1704. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  1705. }
  1706. logs = tuple(
  1707. module.collect_configuration_run_summary_logs(
  1708. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1709. )
  1710. )
  1711. assert expected_logs[0] in logs
  1712. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  1713. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1714. ValueError
  1715. )
  1716. expected_logs = (flexmock(),)
  1717. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1718. arguments = {
  1719. 'list': flexmock(repository='repo', archive='test'),
  1720. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1721. }
  1722. logs = tuple(
  1723. module.collect_configuration_run_summary_logs(
  1724. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1725. )
  1726. )
  1727. assert logs == expected_logs
  1728. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  1729. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1730. flexmock(module.command).should_receive('filter_hooks')
  1731. flexmock(module.command).should_receive('execute_hooks')
  1732. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1733. flexmock(module).should_receive('run_configuration').and_return([])
  1734. arguments = {
  1735. 'list': flexmock(repository='repo', archive=None),
  1736. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1737. }
  1738. logs = tuple(
  1739. module.collect_configuration_run_summary_logs(
  1740. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1741. )
  1742. )
  1743. assert {log.levelno for log in logs} == {logging.INFO}
  1744. def test_collect_configuration_run_summary_logs_run_configuration_error_logs():
  1745. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1746. flexmock(module.command).should_receive('filter_hooks')
  1747. flexmock(module.command).should_receive('execute_hooks')
  1748. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1749. flexmock(module).should_receive('run_configuration').and_return(
  1750. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1751. )
  1752. flexmock(module).should_receive('log_error_records').and_return([])
  1753. arguments = {'global': flexmock(dry_run=False, log_file=flexmock())}
  1754. logs = tuple(
  1755. module.collect_configuration_run_summary_logs(
  1756. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1757. )
  1758. )
  1759. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1760. def test_collect_configuration_run_summary_logs_run_umount_error():
  1761. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1762. flexmock(module.command).should_receive('filter_hooks')
  1763. flexmock(module.command).should_receive('execute_hooks')
  1764. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1765. flexmock(module).should_receive('run_configuration').and_return([])
  1766. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  1767. flexmock(module).should_receive('log_error_records').and_return(
  1768. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1769. )
  1770. arguments = {
  1771. 'umount': flexmock(mount_point='/mnt'),
  1772. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1773. }
  1774. logs = tuple(
  1775. module.collect_configuration_run_summary_logs(
  1776. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1777. )
  1778. )
  1779. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  1780. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  1781. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1782. flexmock(module.command).should_receive('filter_hooks')
  1783. flexmock(module.command).should_receive('execute_hooks')
  1784. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1785. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  1786. ['baz']
  1787. )
  1788. stdout = flexmock()
  1789. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  1790. flexmock(module.sys).stdout = stdout
  1791. arguments = {'global': flexmock(dry_run=False, log_file=flexmock())}
  1792. tuple(
  1793. module.collect_configuration_run_summary_logs(
  1794. {'test.yaml': {}, 'test2.yaml': {}},
  1795. config_paths=['/tmp/test.yaml', '/tmp/test2.yaml'],
  1796. arguments=arguments,
  1797. )
  1798. )
  1799. def test_check_and_show_help_on_no_args_shows_help_when_no_args_and_default_actions_false():
  1800. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1801. flexmock(module).should_receive('parse_arguments').with_args('--help').once()
  1802. flexmock(module.sys).should_receive('exit').with_args(0).once()
  1803. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}})
  1804. def test_check_and_show_help_on_no_args_does_not_show_help_when_no_args_and_default_actions_true():
  1805. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1806. flexmock(module).should_receive('parse_arguments').never()
  1807. flexmock(module.sys).should_receive('exit').never()
  1808. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': True}})
  1809. def test_check_and_show_help_on_no_args_does_not_show_help_when_args_provided():
  1810. flexmock(module.sys).should_receive('argv').and_return(['borgmatic', '--create'])
  1811. flexmock(module).should_receive('parse_arguments').never()
  1812. flexmock(module.sys).should_receive('exit').never()
  1813. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}})
  1814. def test_check_and_show_help_on_no_args_with_no_default_actions_in_all_configs():
  1815. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1816. # Both configs have default_actions set to False, so help should be shown
  1817. configs = {
  1818. 'config1.yaml': {'default_actions': False},
  1819. 'config2.yaml': {'default_actions': False},
  1820. }
  1821. # Expect help to be shown
  1822. flexmock(module).should_receive('parse_arguments').with_args('--help').once()
  1823. flexmock(module.sys).should_receive('exit').with_args(0).once()
  1824. module.check_and_show_help_on_no_args(configs)
  1825. def test_check_and_show_help_on_no_args_with_conflicting_configs():
  1826. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1827. # Simulate two config files with conflicting 'default_actions' values
  1828. configs = {
  1829. 'config1.yaml': {'default_actions': True},
  1830. 'config2.yaml': {'default_actions': False},
  1831. }
  1832. # Expect help not to be shown because at least one config enables default actions
  1833. flexmock(module).should_receive('parse_arguments').never()
  1834. flexmock(module.sys).should_receive('exit').never()
  1835. module.check_and_show_help_on_no_args(configs)