test_borgmatic.py 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654
  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('get_verbosity').and_return(module.logging.INFO)
  26. flexmock(module).should_receive('verbosity_to_log_level').and_return(module.DISABLED)
  27. flexmock(module.dispatch).should_receive('call_hooks').never()
  28. with module.Monitoring_hooks(
  29. config_filename='test.yaml',
  30. config={},
  31. arguments={'create': flexmock()},
  32. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  33. ):
  34. pass
  35. def test_monitoring_hooks_with_non_primary_action_bails():
  36. flexmock(module).should_receive('get_verbosity').and_return(module.logging.INFO)
  37. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  38. flexmock(module.dispatch).should_receive('call_hooks').never()
  39. with module.Monitoring_hooks(
  40. config_filename='test.yaml',
  41. config={},
  42. arguments={'extract': flexmock()},
  43. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  44. ):
  45. pass
  46. def test_monitoring_hooks_pings_monitors():
  47. flexmock(module).should_receive('get_verbosity').and_return(module.logging.INFO)
  48. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  49. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  50. 'initialize_monitor',
  51. object,
  52. object,
  53. object,
  54. object,
  55. object,
  56. ).once()
  57. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  58. 'ping_monitor',
  59. object,
  60. object,
  61. object,
  62. module.monitor.State.START,
  63. object,
  64. object,
  65. ).once()
  66. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  67. 'ping_monitor',
  68. object,
  69. object,
  70. object,
  71. module.monitor.State.LOG,
  72. object,
  73. object,
  74. ).once()
  75. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  76. 'ping_monitor',
  77. object,
  78. object,
  79. object,
  80. module.monitor.State.FINISH,
  81. object,
  82. object,
  83. ).once()
  84. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  85. 'ping_monitor',
  86. object,
  87. object,
  88. object,
  89. module.monitor.State.FAIL,
  90. object,
  91. object,
  92. ).never()
  93. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  94. 'destroy_monitor',
  95. object,
  96. object,
  97. object,
  98. object,
  99. ).once()
  100. with module.Monitoring_hooks(
  101. config_filename='test.yaml',
  102. config={},
  103. arguments={'create': flexmock()},
  104. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  105. ):
  106. pass
  107. def test_monitoring_hooks_with_start_ping_error_raises():
  108. flexmock(module).should_receive('get_verbosity').and_return(module.logging.INFO)
  109. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  110. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  111. 'initialize_monitor',
  112. object,
  113. object,
  114. object,
  115. object,
  116. object,
  117. ).once()
  118. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  119. 'ping_monitor',
  120. object,
  121. object,
  122. object,
  123. module.monitor.State.START,
  124. object,
  125. object,
  126. ).and_raise(OSError).once()
  127. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  128. 'ping_monitor',
  129. object,
  130. object,
  131. object,
  132. module.monitor.State.LOG,
  133. object,
  134. object,
  135. ).never()
  136. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  137. 'ping_monitor',
  138. object,
  139. object,
  140. object,
  141. module.monitor.State.FINISH,
  142. object,
  143. object,
  144. ).never()
  145. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  146. 'destroy_monitor',
  147. object,
  148. object,
  149. object,
  150. object,
  151. ).never()
  152. with (
  153. pytest.raises(ValueError),
  154. module.Monitoring_hooks(
  155. config_filename='test.yaml',
  156. config={},
  157. arguments={'create': flexmock()},
  158. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  159. ),
  160. ):
  161. raise AssertionError() # This should never get called.
  162. def test_monitoring_hooks_with_log_ping_error_raises():
  163. flexmock(module).should_receive('get_verbosity').and_return(module.logging.INFO)
  164. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  165. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  166. 'initialize_monitor',
  167. object,
  168. object,
  169. object,
  170. object,
  171. object,
  172. ).once()
  173. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  174. 'ping_monitor',
  175. object,
  176. object,
  177. object,
  178. module.monitor.State.START,
  179. object,
  180. object,
  181. ).once()
  182. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  183. 'ping_monitor',
  184. object,
  185. object,
  186. object,
  187. module.monitor.State.LOG,
  188. object,
  189. object,
  190. ).and_raise(OSError).once()
  191. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  192. 'ping_monitor',
  193. object,
  194. object,
  195. object,
  196. module.monitor.State.FINISH,
  197. object,
  198. object,
  199. ).never()
  200. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  201. 'destroy_monitor',
  202. object,
  203. object,
  204. object,
  205. object,
  206. ).never()
  207. with (
  208. pytest.raises(ValueError),
  209. module.Monitoring_hooks(
  210. config_filename='test.yaml',
  211. config={},
  212. arguments={'create': flexmock()},
  213. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  214. ),
  215. ):
  216. pass
  217. def test_monitoring_hooks_with_finish_ping_error_raises():
  218. flexmock(module).should_receive('get_verbosity').and_return(module.logging.INFO)
  219. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  220. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  221. 'initialize_monitor',
  222. object,
  223. object,
  224. object,
  225. object,
  226. object,
  227. ).once()
  228. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  229. 'ping_monitor',
  230. object,
  231. object,
  232. object,
  233. module.monitor.State.START,
  234. object,
  235. object,
  236. ).once()
  237. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  238. 'ping_monitor',
  239. object,
  240. object,
  241. object,
  242. module.monitor.State.LOG,
  243. object,
  244. object,
  245. ).once()
  246. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  247. 'ping_monitor',
  248. object,
  249. object,
  250. object,
  251. module.monitor.State.FINISH,
  252. object,
  253. object,
  254. ).and_raise(OSError).once()
  255. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  256. 'destroy_monitor',
  257. object,
  258. object,
  259. object,
  260. object,
  261. ).never()
  262. with (
  263. pytest.raises(ValueError),
  264. module.Monitoring_hooks(
  265. config_filename='test.yaml',
  266. config={},
  267. arguments={'create': flexmock()},
  268. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  269. ),
  270. ):
  271. pass
  272. def test_monitoring_hooks_with_wrapped_code_error_pings_fail():
  273. flexmock(module).should_receive('get_verbosity').and_return(module.logging.INFO)
  274. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  275. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  276. 'initialize_monitor',
  277. object,
  278. object,
  279. object,
  280. object,
  281. object,
  282. ).once()
  283. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  284. 'ping_monitor',
  285. object,
  286. object,
  287. object,
  288. module.monitor.State.START,
  289. object,
  290. object,
  291. ).once()
  292. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  293. 'ping_monitor',
  294. object,
  295. object,
  296. object,
  297. module.monitor.State.LOG,
  298. object,
  299. object,
  300. ).once()
  301. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  302. 'ping_monitor',
  303. object,
  304. object,
  305. object,
  306. module.monitor.State.FINISH,
  307. object,
  308. object,
  309. ).never()
  310. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  311. 'ping_monitor',
  312. object,
  313. object,
  314. object,
  315. module.monitor.State.FAIL,
  316. object,
  317. object,
  318. ).once()
  319. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  320. 'destroy_monitor',
  321. object,
  322. object,
  323. object,
  324. object,
  325. ).once()
  326. with (
  327. pytest.raises(OSError),
  328. module.Monitoring_hooks(
  329. config_filename='test.yaml',
  330. config={},
  331. arguments={'create': flexmock()},
  332. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  333. ),
  334. ):
  335. raise OSError()
  336. def test_monitoring_hooks_with_fail_ping_error_raise_original_error():
  337. flexmock(module).should_receive('get_verbosity').and_return(module.logging.INFO)
  338. flexmock(module).should_receive('verbosity_to_log_level').and_return(flexmock())
  339. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  340. 'initialize_monitor',
  341. object,
  342. object,
  343. object,
  344. object,
  345. object,
  346. ).once()
  347. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  348. 'ping_monitor',
  349. object,
  350. object,
  351. object,
  352. module.monitor.State.START,
  353. object,
  354. object,
  355. ).once()
  356. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  357. 'ping_monitor',
  358. object,
  359. object,
  360. object,
  361. module.monitor.State.LOG,
  362. object,
  363. object,
  364. ).once()
  365. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  366. 'ping_monitor',
  367. object,
  368. object,
  369. object,
  370. module.monitor.State.FINISH,
  371. object,
  372. object,
  373. ).never()
  374. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  375. 'ping_monitor',
  376. object,
  377. object,
  378. object,
  379. module.monitor.State.FAIL,
  380. object,
  381. object,
  382. ).and_raise(OSError).once()
  383. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  384. 'destroy_monitor',
  385. object,
  386. object,
  387. object,
  388. object,
  389. ).never()
  390. with (
  391. pytest.raises(OSError),
  392. module.Monitoring_hooks(
  393. config_filename='test.yaml',
  394. config={},
  395. arguments={'create': flexmock()},
  396. global_arguments=flexmock(monitoring_verbosity=99, dry_run=False),
  397. ),
  398. ):
  399. raise OSError()
  400. def test_run_configuration_runs_actions_for_each_repository():
  401. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  402. flexmock(module).should_receive('get_skip_actions').and_return([])
  403. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  404. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  405. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  406. expected_results = [flexmock(), flexmock()]
  407. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  408. flexmock(module).should_receive('run_actions').and_return(expected_results[:1]).and_return(
  409. expected_results[1:],
  410. )
  411. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  412. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  413. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  414. assert results == expected_results
  415. def test_run_configuration_with_skip_actions_does_not_raise():
  416. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  417. flexmock(module).should_receive('get_skip_actions').and_return(['compact'])
  418. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  419. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  420. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  421. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  422. flexmock(module).should_receive('run_actions').and_return(flexmock()).and_return(flexmock())
  423. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}], 'skip_actions': ['compact']}
  424. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  425. list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  426. def test_run_configuration_with_invalid_borg_version_errors():
  427. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  428. flexmock(module).should_receive('get_skip_actions').and_return([])
  429. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  430. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  431. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  432. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  433. flexmock(module).should_receive('run_actions').never()
  434. config = {'repositories': [{'path': 'foo'}]}
  435. arguments = {
  436. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  437. 'prune': flexmock(),
  438. }
  439. list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  440. def test_run_configuration_logs_actions_error():
  441. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  442. flexmock(module).should_receive('get_skip_actions').and_return([])
  443. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  444. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  445. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  446. expected_results = [flexmock(), flexmock()]
  447. flexmock(module).should_receive('log_error_records').and_return(
  448. expected_results[:1],
  449. ).and_return(expected_results[1:])
  450. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  451. flexmock(module).should_receive('run_actions').and_raise(OSError)
  452. flexmock(module.command).should_receive('filter_hooks')
  453. flexmock(module.command).should_receive('execute_hooks')
  454. config = {'repositories': [{'path': 'foo'}]}
  455. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  456. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  457. assert results == expected_results
  458. def test_run_configuration_skips_remaining_actions_for_actions_soft_failure_but_still_runs_next_repository_actions():
  459. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  460. flexmock(module).should_receive('get_skip_actions').and_return([])
  461. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  462. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  463. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  464. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  465. log = flexmock()
  466. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  467. flexmock(module).should_receive('run_actions').twice().and_raise(error).and_yield(log)
  468. flexmock(module).should_receive('log_error_records').never()
  469. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  470. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  471. arguments = {
  472. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  473. 'create': flexmock(),
  474. }
  475. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  476. assert results == [log]
  477. def test_run_configuration_does_not_call_monitoring_hooks_if_monitoring_hooks_are_disabled():
  478. flexmock(module).should_receive('verbosity_to_log_level').and_return(module.DISABLED)
  479. flexmock(module).should_receive('get_skip_actions').and_return([])
  480. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  481. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  482. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  483. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  484. flexmock(module).should_receive('run_actions').and_return([])
  485. config = {'repositories': [{'path': 'foo'}]}
  486. arguments = {
  487. 'global': flexmock(monitoring_verbosity=-2, dry_run=False),
  488. 'create': flexmock(),
  489. }
  490. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  491. assert results == []
  492. def test_run_configuration_logs_on_error_hook_error():
  493. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  494. flexmock(module).should_receive('get_skip_actions').and_return([])
  495. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  496. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  497. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  498. flexmock(module.command).should_receive('filter_hooks')
  499. flexmock(module.command).should_receive('execute_hooks').and_raise(OSError)
  500. expected_results = [flexmock(), flexmock()]
  501. flexmock(module).should_receive('log_error_records').and_return(
  502. expected_results[:1],
  503. ).and_return(expected_results[1:2]).and_return(expected_results[2:])
  504. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  505. flexmock(module).should_receive('run_actions').and_raise(OSError)
  506. config = {'repositories': [{'path': 'foo'}]}
  507. arguments = {
  508. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  509. 'create': flexmock(),
  510. }
  511. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  512. assert results == expected_results
  513. def test_run_configuration_logs_on_before_command_hook_error():
  514. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  515. flexmock(module).should_receive('get_skip_actions').and_return([])
  516. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  517. flexmock(module.command).should_receive('Before_after_hooks').and_raise(OSError)
  518. flexmock(module.borg_version).should_receive('local_borg_version').never()
  519. flexmock(module.command).should_receive('filter_hooks')
  520. flexmock(module.command).should_receive('execute_hooks')
  521. expected_results = [flexmock()]
  522. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  523. flexmock(module).should_receive('Log_prefix').never()
  524. flexmock(module).should_receive('run_actions').never()
  525. config = {'repositories': [{'path': 'foo'}]}
  526. arguments = {
  527. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  528. 'create': flexmock(),
  529. }
  530. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  531. assert results == expected_results
  532. def test_run_configuration_logs_on_monitoring_hook_error():
  533. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  534. flexmock(module).should_receive('get_skip_actions').and_return([])
  535. flexmock(module).should_receive('Monitoring_hooks').and_raise(OSError)
  536. flexmock(module.command).should_receive('Before_after_hooks').never()
  537. flexmock(module.borg_version).should_receive('local_borg_version').never()
  538. flexmock(module.command).should_receive('filter_hooks')
  539. flexmock(module.command).should_receive('execute_hooks')
  540. expected_results = [flexmock()]
  541. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  542. flexmock(module).should_receive('Log_prefix').never()
  543. flexmock(module).should_receive('run_actions').never()
  544. config = {'repositories': [{'path': 'foo'}]}
  545. arguments = {
  546. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  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_bails_for_on_error_hook_soft_failure():
  552. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  553. flexmock(module).should_receive('get_skip_actions').and_return([])
  554. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  555. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  556. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  557. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  558. flexmock(module.command).should_receive('filter_hooks')
  559. flexmock(module.command).should_receive('execute_hooks').and_raise(error)
  560. expected_results = [flexmock(), flexmock()]
  561. flexmock(module).should_receive('log_error_records').and_return(
  562. expected_results[:1],
  563. ).and_return(expected_results[1:])
  564. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  565. flexmock(module).should_receive('run_actions').and_raise(OSError)
  566. config = {'repositories': [{'path': 'foo'}]}
  567. arguments = {
  568. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  569. 'create': flexmock(),
  570. }
  571. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  572. assert results == expected_results
  573. def test_run_configuration_retries_soft_error():
  574. # Run action first fails, second passes.
  575. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  576. flexmock(module).should_receive('get_skip_actions').and_return([])
  577. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  578. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  579. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  580. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  581. flexmock(module).should_receive('run_actions').and_raise(OSError).and_return([])
  582. flexmock(module).should_receive('log_error_records').and_return([flexmock()]).once()
  583. flexmock(module.command).should_receive('filter_hooks').never()
  584. flexmock(module.command).should_receive('execute_hooks').never()
  585. config = {'repositories': [{'path': 'foo'}], 'retries': 1}
  586. arguments = {
  587. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  588. 'create': flexmock(),
  589. }
  590. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  591. assert results == []
  592. def test_run_configuration_retries_hard_error():
  593. # Run action fails twice.
  594. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  595. flexmock(module).should_receive('get_skip_actions').and_return([])
  596. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  597. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  598. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  599. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  600. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  601. flexmock(module).should_receive('log_error_records').with_args(
  602. 'Error running actions for repository',
  603. OSError,
  604. levelno=logging.WARNING,
  605. log_command_error_output=True,
  606. ).and_return([flexmock()])
  607. error_logs = [flexmock(), flexmock()]
  608. flexmock(module).should_receive('log_error_records').with_args(
  609. 'Error running actions for repository',
  610. OSError,
  611. ).and_return(error_logs[:1]).ordered()
  612. flexmock(module).should_receive('log_error_records').with_args(
  613. 'Error running configuration',
  614. ).and_return(error_logs[1:]).ordered()
  615. flexmock(module.command).should_receive('filter_hooks')
  616. flexmock(module.command).should_receive('execute_hooks')
  617. config = {'repositories': [{'path': 'foo'}], 'retries': 1}
  618. arguments = {
  619. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  620. 'create': flexmock(),
  621. }
  622. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  623. assert results == error_logs
  624. def test_run_configuration_retries_repositories_in_order():
  625. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  626. flexmock(module).should_receive('get_skip_actions').and_return([])
  627. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  628. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  629. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  630. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  631. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  632. expected_results = [flexmock(), flexmock(), flexmock()]
  633. flexmock(module).should_receive('log_error_records').with_args(
  634. 'Error running actions for repository',
  635. OSError,
  636. ).and_return(expected_results[:1]).ordered()
  637. flexmock(module).should_receive('log_error_records').with_args(
  638. 'Error running actions for repository',
  639. OSError,
  640. ).and_return(expected_results[1:2]).ordered()
  641. flexmock(module).should_receive('log_error_records').with_args(
  642. 'Error running configuration',
  643. ).and_return(expected_results[2:]).ordered()
  644. flexmock(module.command).should_receive('filter_hooks')
  645. flexmock(module.command).should_receive('execute_hooks')
  646. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  647. arguments = {
  648. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  649. 'create': flexmock(),
  650. }
  651. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  652. assert results == expected_results
  653. def test_run_configuration_retries_round_robin():
  654. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  655. flexmock(module).should_receive('get_skip_actions').and_return([])
  656. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  657. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  658. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  659. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  660. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  661. flexmock(module).should_receive('log_error_records').with_args(
  662. 'Error running actions for repository',
  663. OSError,
  664. levelno=logging.WARNING,
  665. log_command_error_output=True,
  666. ).and_return([flexmock()]).ordered()
  667. flexmock(module).should_receive('log_error_records').with_args(
  668. 'Error running actions for repository',
  669. OSError,
  670. levelno=logging.WARNING,
  671. log_command_error_output=True,
  672. ).and_return([flexmock()]).ordered()
  673. foo_error_logs = [flexmock()]
  674. flexmock(module).should_receive('log_error_records').with_args(
  675. 'Error running actions for repository',
  676. OSError,
  677. ).and_return(foo_error_logs).ordered()
  678. bar_error_logs = [flexmock()]
  679. flexmock(module).should_receive('log_error_records').with_args(
  680. 'Error running actions for repository',
  681. OSError,
  682. ).and_return(bar_error_logs).ordered()
  683. config_error_logs = [flexmock()]
  684. flexmock(module).should_receive('log_error_records').with_args(
  685. 'Error running configuration',
  686. ).and_return(config_error_logs).ordered()
  687. flexmock(module.command).should_receive('filter_hooks')
  688. flexmock(module.command).should_receive('execute_hooks')
  689. config = {
  690. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  691. 'retries': 1,
  692. }
  693. arguments = {
  694. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  695. 'create': flexmock(),
  696. }
  697. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  698. assert results == foo_error_logs + bar_error_logs + config_error_logs
  699. def test_run_configuration_with_one_retry():
  700. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  701. flexmock(module).should_receive('get_skip_actions').and_return([])
  702. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  703. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  704. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  705. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  706. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  707. [],
  708. ).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(module).should_receive('log_error_records').with_args(
  716. 'Error running actions for repository',
  717. OSError,
  718. levelno=logging.WARNING,
  719. log_command_error_output=True,
  720. ).and_return(flexmock()).ordered()
  721. error_logs = [flexmock(), flexmock()]
  722. flexmock(module).should_receive('log_error_records').with_args(
  723. 'Error running actions for repository',
  724. OSError,
  725. ).and_return(error_logs[:1]).ordered()
  726. flexmock(module).should_receive('log_error_records').with_args(
  727. 'Error running configuration',
  728. ).and_return(error_logs[1:]).ordered()
  729. flexmock(module.command).should_receive('filter_hooks')
  730. flexmock(module.command).should_receive('execute_hooks')
  731. config = {
  732. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  733. 'retries': 1,
  734. }
  735. arguments = {
  736. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  737. 'create': flexmock(),
  738. }
  739. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  740. assert results == error_logs
  741. def test_run_configuration_with_retry_wait_does_backoff_after_each_retry():
  742. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  743. flexmock(module).should_receive('get_skip_actions').and_return([])
  744. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  745. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  746. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  747. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  748. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  749. flexmock(module).should_receive('log_error_records').with_args(
  750. 'Error running actions for repository',
  751. OSError,
  752. levelno=logging.WARNING,
  753. log_command_error_output=True,
  754. ).and_return([flexmock()]).ordered()
  755. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  756. flexmock(module).should_receive('log_error_records').with_args(
  757. 'Error running actions for repository',
  758. OSError,
  759. levelno=logging.WARNING,
  760. log_command_error_output=True,
  761. ).and_return([flexmock()]).ordered()
  762. flexmock(time).should_receive('sleep').with_args(20).and_return().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. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  770. error_logs = [flexmock(), flexmock()]
  771. flexmock(module).should_receive('log_error_records').with_args(
  772. 'Error running actions for repository',
  773. OSError,
  774. ).and_return(error_logs[:1]).ordered()
  775. flexmock(module).should_receive('log_error_records').with_args(
  776. 'Error running configuration',
  777. ).and_return(error_logs[1:]).ordered()
  778. flexmock(module.command).should_receive('filter_hooks')
  779. flexmock(module.command).should_receive('execute_hooks')
  780. config = {
  781. 'repositories': [{'path': 'foo'}],
  782. 'retries': 3,
  783. 'retry_wait': 10,
  784. }
  785. arguments = {
  786. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  787. 'create': flexmock(),
  788. }
  789. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  790. assert results == error_logs
  791. def test_run_configuration_with_multiple_repositories_retries_with_timeout():
  792. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  793. flexmock(module).should_receive('get_skip_actions').and_return([])
  794. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  795. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  796. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  797. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  798. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  799. [],
  800. ).and_raise(OSError).times(4)
  801. flexmock(module).should_receive('log_error_records').with_args(
  802. 'Error running actions for repository',
  803. OSError,
  804. levelno=logging.WARNING,
  805. log_command_error_output=True,
  806. ).and_return([flexmock()]).ordered()
  807. flexmock(module).should_receive('log_error_records').with_args(
  808. 'Error running actions for repository',
  809. OSError,
  810. levelno=logging.WARNING,
  811. log_command_error_output=True,
  812. ).and_return([flexmock()]).ordered()
  813. # Sleep before retrying foo (and passing)
  814. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  815. # Sleep before retrying bar (and failing)
  816. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  817. error_logs = [flexmock(), flexmock()]
  818. flexmock(module).should_receive('log_error_records').with_args(
  819. 'Error running actions for repository',
  820. OSError,
  821. ).and_return(error_logs[:1]).ordered()
  822. flexmock(module).should_receive('log_error_records').with_args(
  823. 'Error running configuration',
  824. ).and_return(error_logs[1:]).ordered()
  825. flexmock(module.command).should_receive('filter_hooks')
  826. flexmock(module.command).should_receive('execute_hooks')
  827. config = {
  828. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  829. 'retries': 1,
  830. 'retry_wait': 10,
  831. }
  832. arguments = {
  833. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  834. 'create': flexmock(),
  835. }
  836. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  837. assert results == error_logs
  838. def test_run_actions_runs_repo_create():
  839. flexmock(module).should_receive('add_custom_log_levels')
  840. flexmock(module).should_receive('get_skip_actions').and_return([])
  841. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  842. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  843. flexmock(),
  844. )
  845. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  846. flexmock(borgmatic.actions.repo_create).should_receive('run_repo_create').once()
  847. tuple(
  848. module.run_actions(
  849. arguments={
  850. 'global': flexmock(dry_run=False),
  851. 'repo-create': flexmock(),
  852. },
  853. config_filename=flexmock(),
  854. config={'repositories': []},
  855. config_paths=[],
  856. local_path=flexmock(),
  857. remote_path=flexmock(),
  858. local_borg_version=flexmock(),
  859. repository={'path': 'repo'},
  860. ),
  861. )
  862. def test_run_actions_with_matching_repository_flag_runs_repo_create():
  863. flexmock(module).should_receive('add_custom_log_levels')
  864. flexmock(module).should_receive('get_skip_actions').and_return([])
  865. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(True)
  866. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  867. flexmock(),
  868. )
  869. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  870. flexmock(borgmatic.actions.repo_create).should_receive('run_repo_create').once()
  871. tuple(
  872. module.run_actions(
  873. arguments={
  874. 'global': flexmock(dry_run=False),
  875. 'repo-create': flexmock(repository='repo'),
  876. },
  877. config_filename=flexmock(),
  878. config={'repositories': []},
  879. config_paths=[],
  880. local_path=flexmock(),
  881. remote_path=flexmock(),
  882. local_borg_version=flexmock(),
  883. repository={'path': 'repo'},
  884. ),
  885. )
  886. def test_run_actions_with_non_matching_repository_flag_bails():
  887. flexmock(module).should_receive('add_custom_log_levels')
  888. flexmock(module).should_receive('get_skip_actions').and_return([])
  889. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').and_return(
  890. False
  891. )
  892. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').never()
  893. flexmock(module.command).should_receive('Before_after_hooks').never()
  894. flexmock(borgmatic.actions.repo_create).should_receive('run_repo_create').never()
  895. tuple(
  896. module.run_actions(
  897. arguments={
  898. 'global': flexmock(dry_run=False),
  899. 'repo-create': flexmock(repository='other-repo'),
  900. },
  901. config_filename=flexmock(),
  902. config={'repositories': []},
  903. config_paths=[],
  904. local_path=flexmock(),
  905. remote_path=flexmock(),
  906. local_borg_version=flexmock(),
  907. repository={'path': 'repo'},
  908. ),
  909. )
  910. def test_run_actions_adds_label_file_to_hook_context():
  911. flexmock(module).should_receive('add_custom_log_levels')
  912. flexmock(module).should_receive('get_skip_actions').and_return([])
  913. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  914. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  915. flexmock(),
  916. )
  917. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  918. expected = flexmock()
  919. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  920. config_filename=object,
  921. repository={'path': 'repo', 'label': 'my repo'},
  922. config={'repositories': []},
  923. config_paths=[],
  924. local_borg_version=object,
  925. create_arguments=object,
  926. global_arguments=object,
  927. dry_run_label='',
  928. local_path=object,
  929. remote_path=object,
  930. ).once().and_return(expected)
  931. result = tuple(
  932. module.run_actions(
  933. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  934. config_filename=flexmock(),
  935. config={'repositories': []},
  936. config_paths=[],
  937. local_path=flexmock(),
  938. remote_path=flexmock(),
  939. local_borg_version=flexmock(),
  940. repository={'path': 'repo', 'label': 'my repo'},
  941. ),
  942. )
  943. assert result == (expected,)
  944. def test_run_actions_adds_log_file_to_hook_context():
  945. flexmock(module).should_receive('add_custom_log_levels')
  946. flexmock(module).should_receive('get_skip_actions').and_return([])
  947. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  948. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  949. flexmock(),
  950. )
  951. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  952. expected = flexmock()
  953. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  954. config_filename=object,
  955. repository={'path': 'repo'},
  956. config={'repositories': [], 'log_file': 'foo'},
  957. config_paths=[],
  958. local_borg_version=object,
  959. create_arguments=object,
  960. global_arguments=object,
  961. dry_run_label='',
  962. local_path=object,
  963. remote_path=object,
  964. ).once().and_return(expected)
  965. result = tuple(
  966. module.run_actions(
  967. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  968. config_filename=flexmock(),
  969. config={'repositories': [], 'log_file': 'foo'},
  970. config_paths=[],
  971. local_path=flexmock(),
  972. remote_path=flexmock(),
  973. local_borg_version=flexmock(),
  974. repository={'path': 'repo'},
  975. ),
  976. )
  977. assert result == (expected,)
  978. def test_run_actions_runs_transfer():
  979. flexmock(module).should_receive('add_custom_log_levels')
  980. flexmock(module).should_receive('get_skip_actions').and_return([])
  981. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  982. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  983. flexmock(),
  984. )
  985. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  986. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  987. tuple(
  988. module.run_actions(
  989. arguments={'global': flexmock(dry_run=False), 'transfer': flexmock()},
  990. config_filename=flexmock(),
  991. config={'repositories': []},
  992. config_paths=[],
  993. local_path=flexmock(),
  994. remote_path=flexmock(),
  995. local_borg_version=flexmock(),
  996. repository={'path': 'repo'},
  997. ),
  998. )
  999. def test_run_actions_runs_create():
  1000. flexmock(module).should_receive('add_custom_log_levels')
  1001. flexmock(module).should_receive('get_skip_actions').and_return([])
  1002. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1003. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1004. flexmock(),
  1005. )
  1006. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1007. expected = flexmock()
  1008. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  1009. result = tuple(
  1010. module.run_actions(
  1011. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  1012. config_filename=flexmock(),
  1013. config={'repositories': []},
  1014. config_paths=[],
  1015. local_path=flexmock(),
  1016. remote_path=flexmock(),
  1017. local_borg_version=flexmock(),
  1018. repository={'path': 'repo'},
  1019. ),
  1020. )
  1021. assert result == (expected,)
  1022. def test_run_actions_with_skip_actions_does_not_run_action_or_action_command_hooks():
  1023. flexmock(module).should_receive('add_custom_log_levels')
  1024. flexmock(module).should_receive('get_skip_actions').and_return(['create'])
  1025. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1026. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1027. flexmock(),
  1028. )
  1029. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1030. flexmock(module.command).should_receive('Before_after_hooks').with_args(
  1031. command_hooks=object,
  1032. before_after='action',
  1033. umask=object,
  1034. working_directory=object,
  1035. dry_run=object,
  1036. action_names=object,
  1037. configuration_filename=object,
  1038. repository_label=object,
  1039. log_file=object,
  1040. repositories=object,
  1041. repository=object,
  1042. ).never()
  1043. flexmock(borgmatic.actions.create).should_receive('run_create').never()
  1044. tuple(
  1045. module.run_actions(
  1046. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  1047. config_filename=flexmock(),
  1048. config={'repositories': [], 'skip_actions': ['create']},
  1049. config_paths=[],
  1050. local_path=flexmock(),
  1051. remote_path=flexmock(),
  1052. local_borg_version=flexmock(),
  1053. repository={'path': 'repo'},
  1054. ),
  1055. )
  1056. def test_run_actions_runs_recreate():
  1057. flexmock(module).should_receive('add_custom_log_levels')
  1058. flexmock(module).should_receive('get_skip_actions').and_return([])
  1059. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1060. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1061. flexmock(),
  1062. )
  1063. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1064. flexmock(borgmatic.actions.recreate).should_receive('run_recreate').once()
  1065. tuple(
  1066. module.run_actions(
  1067. arguments={'global': flexmock(dry_run=False), 'recreate': flexmock()},
  1068. config_filename=flexmock(),
  1069. config={'repositories': []},
  1070. config_paths=[],
  1071. local_path=flexmock(),
  1072. remote_path=flexmock(),
  1073. local_borg_version=flexmock(),
  1074. repository={'path': 'repo'},
  1075. ),
  1076. )
  1077. def test_run_actions_runs_prune():
  1078. flexmock(module).should_receive('add_custom_log_levels')
  1079. flexmock(module).should_receive('get_skip_actions').and_return([])
  1080. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1081. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1082. flexmock(),
  1083. )
  1084. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1085. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  1086. tuple(
  1087. module.run_actions(
  1088. arguments={'global': flexmock(dry_run=False), 'prune': flexmock()},
  1089. config_filename=flexmock(),
  1090. config={'repositories': []},
  1091. config_paths=[],
  1092. local_path=flexmock(),
  1093. remote_path=flexmock(),
  1094. local_borg_version=flexmock(),
  1095. repository={'path': 'repo'},
  1096. ),
  1097. )
  1098. def test_run_actions_runs_compact():
  1099. flexmock(module).should_receive('add_custom_log_levels')
  1100. flexmock(module).should_receive('get_skip_actions').and_return([])
  1101. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1102. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1103. flexmock(),
  1104. )
  1105. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1106. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  1107. tuple(
  1108. module.run_actions(
  1109. arguments={'global': flexmock(dry_run=False), 'compact': flexmock()},
  1110. config_filename=flexmock(),
  1111. config={'repositories': []},
  1112. config_paths=[],
  1113. local_path=flexmock(),
  1114. remote_path=flexmock(),
  1115. local_borg_version=flexmock(),
  1116. repository={'path': 'repo'},
  1117. ),
  1118. )
  1119. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  1120. flexmock(module).should_receive('add_custom_log_levels')
  1121. flexmock(module).should_receive('get_skip_actions').and_return([])
  1122. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1123. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1124. flexmock(),
  1125. )
  1126. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1127. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  1128. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  1129. tuple(
  1130. module.run_actions(
  1131. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  1132. config_filename=flexmock(),
  1133. config={'repositories': []},
  1134. config_paths=[],
  1135. local_path=flexmock(),
  1136. remote_path=flexmock(),
  1137. local_borg_version=flexmock(),
  1138. repository={'path': 'repo'},
  1139. ),
  1140. )
  1141. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  1142. flexmock(module).should_receive('add_custom_log_levels')
  1143. flexmock(module).should_receive('get_skip_actions').and_return([])
  1144. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1145. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1146. flexmock(),
  1147. )
  1148. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1149. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  1150. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  1151. tuple(
  1152. module.run_actions(
  1153. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  1154. config_filename=flexmock(),
  1155. config={'repositories': []},
  1156. config_paths=[],
  1157. local_path=flexmock(),
  1158. remote_path=flexmock(),
  1159. local_borg_version=flexmock(),
  1160. repository={'path': 'repo'},
  1161. ),
  1162. )
  1163. def test_run_actions_runs_extract():
  1164. flexmock(module).should_receive('add_custom_log_levels')
  1165. flexmock(module).should_receive('get_skip_actions').and_return([])
  1166. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1167. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1168. flexmock(),
  1169. )
  1170. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1171. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  1172. tuple(
  1173. module.run_actions(
  1174. arguments={'global': flexmock(dry_run=False), 'extract': 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. def test_run_actions_runs_export_tar():
  1185. flexmock(module).should_receive('add_custom_log_levels')
  1186. flexmock(module).should_receive('get_skip_actions').and_return([])
  1187. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1188. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1189. flexmock(),
  1190. )
  1191. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1192. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  1193. tuple(
  1194. module.run_actions(
  1195. arguments={'global': flexmock(dry_run=False), 'export-tar': flexmock()},
  1196. config_filename=flexmock(),
  1197. config={'repositories': []},
  1198. config_paths=[],
  1199. local_path=flexmock(),
  1200. remote_path=flexmock(),
  1201. local_borg_version=flexmock(),
  1202. repository={'path': 'repo'},
  1203. ),
  1204. )
  1205. def test_run_actions_runs_mount():
  1206. flexmock(module).should_receive('add_custom_log_levels')
  1207. flexmock(module).should_receive('get_skip_actions').and_return([])
  1208. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1209. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1210. flexmock(),
  1211. )
  1212. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1213. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  1214. tuple(
  1215. module.run_actions(
  1216. arguments={'global': flexmock(dry_run=False), 'mount': flexmock()},
  1217. config_filename=flexmock(),
  1218. config={'repositories': []},
  1219. config_paths=[],
  1220. local_path=flexmock(),
  1221. remote_path=flexmock(),
  1222. local_borg_version=flexmock(),
  1223. repository={'path': 'repo'},
  1224. ),
  1225. )
  1226. def test_run_actions_runs_restore():
  1227. flexmock(module).should_receive('add_custom_log_levels')
  1228. flexmock(module).should_receive('get_skip_actions').and_return([])
  1229. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1230. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1231. flexmock(),
  1232. )
  1233. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1234. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  1235. tuple(
  1236. module.run_actions(
  1237. arguments={'global': flexmock(dry_run=False), 'restore': flexmock()},
  1238. config_filename=flexmock(),
  1239. config={'repositories': []},
  1240. config_paths=[],
  1241. local_path=flexmock(),
  1242. remote_path=flexmock(),
  1243. local_borg_version=flexmock(),
  1244. repository={'path': 'repo'},
  1245. ),
  1246. )
  1247. def test_run_actions_runs_repo_list():
  1248. flexmock(module).should_receive('add_custom_log_levels')
  1249. flexmock(module).should_receive('get_skip_actions').and_return([])
  1250. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1251. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1252. flexmock(),
  1253. )
  1254. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1255. expected = flexmock()
  1256. flexmock(borgmatic.actions.repo_list).should_receive('run_repo_list').and_yield(expected).once()
  1257. result = tuple(
  1258. module.run_actions(
  1259. arguments={'global': flexmock(dry_run=False), 'repo-list': flexmock()},
  1260. config_filename=flexmock(),
  1261. config={'repositories': []},
  1262. config_paths=[],
  1263. local_path=flexmock(),
  1264. remote_path=flexmock(),
  1265. local_borg_version=flexmock(),
  1266. repository={'path': 'repo'},
  1267. ),
  1268. )
  1269. assert result == (expected,)
  1270. def test_run_actions_runs_list():
  1271. flexmock(module).should_receive('add_custom_log_levels')
  1272. flexmock(module).should_receive('get_skip_actions').and_return([])
  1273. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1274. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1275. flexmock(),
  1276. )
  1277. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1278. expected = flexmock()
  1279. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  1280. result = tuple(
  1281. module.run_actions(
  1282. arguments={'global': flexmock(dry_run=False), 'list': flexmock()},
  1283. config_filename=flexmock(),
  1284. config={'repositories': []},
  1285. config_paths=[],
  1286. local_path=flexmock(),
  1287. remote_path=flexmock(),
  1288. local_borg_version=flexmock(),
  1289. repository={'path': 'repo'},
  1290. ),
  1291. )
  1292. assert result == (expected,)
  1293. def test_run_actions_runs_repo_info():
  1294. flexmock(module).should_receive('add_custom_log_levels')
  1295. flexmock(module).should_receive('get_skip_actions').and_return([])
  1296. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1297. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1298. flexmock(),
  1299. )
  1300. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1301. expected = flexmock()
  1302. flexmock(borgmatic.actions.repo_info).should_receive('run_repo_info').and_yield(expected).once()
  1303. result = tuple(
  1304. module.run_actions(
  1305. arguments={'global': flexmock(dry_run=False), 'repo-info': flexmock()},
  1306. config_filename=flexmock(),
  1307. config={'repositories': []},
  1308. config_paths=[],
  1309. local_path=flexmock(),
  1310. remote_path=flexmock(),
  1311. local_borg_version=flexmock(),
  1312. repository={'path': 'repo'},
  1313. ),
  1314. )
  1315. assert result == (expected,)
  1316. def test_run_actions_runs_info():
  1317. flexmock(module).should_receive('add_custom_log_levels')
  1318. flexmock(module).should_receive('get_skip_actions').and_return([])
  1319. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1320. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1321. flexmock(),
  1322. )
  1323. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1324. expected = flexmock()
  1325. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  1326. result = tuple(
  1327. module.run_actions(
  1328. arguments={'global': flexmock(dry_run=False), 'info': flexmock()},
  1329. config_filename=flexmock(),
  1330. config={'repositories': []},
  1331. config_paths=[],
  1332. local_path=flexmock(),
  1333. remote_path=flexmock(),
  1334. local_borg_version=flexmock(),
  1335. repository={'path': 'repo'},
  1336. ),
  1337. )
  1338. assert result == (expected,)
  1339. def test_run_actions_runs_break_lock():
  1340. flexmock(module).should_receive('add_custom_log_levels')
  1341. flexmock(module).should_receive('get_skip_actions').and_return([])
  1342. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1343. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1344. flexmock(),
  1345. )
  1346. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1347. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  1348. tuple(
  1349. module.run_actions(
  1350. arguments={'global': flexmock(dry_run=False), 'break-lock': flexmock()},
  1351. config_filename=flexmock(),
  1352. config={'repositories': []},
  1353. config_paths=[],
  1354. local_path=flexmock(),
  1355. remote_path=flexmock(),
  1356. local_borg_version=flexmock(),
  1357. repository={'path': 'repo'},
  1358. ),
  1359. )
  1360. def test_run_actions_runs_export_key():
  1361. flexmock(module).should_receive('add_custom_log_levels')
  1362. flexmock(module).should_receive('get_skip_actions').and_return([])
  1363. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1364. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1365. flexmock(),
  1366. )
  1367. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1368. flexmock(borgmatic.actions.export_key).should_receive('run_export_key').once()
  1369. tuple(
  1370. module.run_actions(
  1371. arguments={'global': flexmock(dry_run=False), 'export': flexmock()},
  1372. config_filename=flexmock(),
  1373. config={'repositories': []},
  1374. config_paths=[],
  1375. local_path=flexmock(),
  1376. remote_path=flexmock(),
  1377. local_borg_version=flexmock(),
  1378. repository={'path': 'repo'},
  1379. ),
  1380. )
  1381. def test_run_actions_runs_import_key():
  1382. flexmock(module).should_receive('add_custom_log_levels')
  1383. flexmock(module).should_receive('get_skip_actions').and_return([])
  1384. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1385. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1386. flexmock(),
  1387. )
  1388. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1389. flexmock(borgmatic.actions.import_key).should_receive('run_import_key').once()
  1390. tuple(
  1391. module.run_actions(
  1392. arguments={'global': flexmock(dry_run=False), 'import': flexmock()},
  1393. config_filename=flexmock(),
  1394. config={'repositories': []},
  1395. config_paths=[],
  1396. local_path=flexmock(),
  1397. remote_path=flexmock(),
  1398. local_borg_version=flexmock(),
  1399. repository={'path': 'repo'},
  1400. ),
  1401. )
  1402. def test_run_actions_runs_change_passphrase():
  1403. flexmock(module).should_receive('add_custom_log_levels')
  1404. flexmock(module).should_receive('get_skip_actions').and_return([])
  1405. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1406. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1407. flexmock(),
  1408. )
  1409. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1410. flexmock(borgmatic.actions.change_passphrase).should_receive('run_change_passphrase').once()
  1411. tuple(
  1412. module.run_actions(
  1413. arguments={
  1414. 'global': flexmock(dry_run=False),
  1415. 'change-passphrase': flexmock(),
  1416. },
  1417. config_filename=flexmock(),
  1418. config={'repositories': []},
  1419. config_paths=[],
  1420. local_path=flexmock(),
  1421. remote_path=flexmock(),
  1422. local_borg_version=flexmock(),
  1423. repository={'path': 'repo'},
  1424. ),
  1425. )
  1426. def test_run_actions_runs_delete():
  1427. flexmock(module).should_receive('add_custom_log_levels')
  1428. flexmock(module).should_receive('get_skip_actions').and_return([])
  1429. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1430. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1431. flexmock(),
  1432. )
  1433. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1434. flexmock(borgmatic.actions.delete).should_receive('run_delete').once()
  1435. tuple(
  1436. module.run_actions(
  1437. arguments={'global': flexmock(dry_run=False), 'delete': flexmock()},
  1438. config_filename=flexmock(),
  1439. config={'repositories': []},
  1440. config_paths=[],
  1441. local_path=flexmock(),
  1442. remote_path=flexmock(),
  1443. local_borg_version=flexmock(),
  1444. repository={'path': 'repo'},
  1445. ),
  1446. )
  1447. def test_run_actions_runs_repo_delete():
  1448. flexmock(module).should_receive('add_custom_log_levels')
  1449. flexmock(module).should_receive('get_skip_actions').and_return([])
  1450. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1451. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1452. flexmock(),
  1453. )
  1454. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1455. flexmock(borgmatic.actions.repo_delete).should_receive('run_repo_delete').once()
  1456. tuple(
  1457. module.run_actions(
  1458. arguments={
  1459. 'global': flexmock(dry_run=False),
  1460. 'repo-delete': flexmock(),
  1461. },
  1462. config_filename=flexmock(),
  1463. config={'repositories': []},
  1464. config_paths=[],
  1465. local_path=flexmock(),
  1466. remote_path=flexmock(),
  1467. local_borg_version=flexmock(),
  1468. repository={'path': 'repo'},
  1469. ),
  1470. )
  1471. def test_run_actions_runs_borg():
  1472. flexmock(module).should_receive('add_custom_log_levels')
  1473. flexmock(module).should_receive('get_skip_actions').and_return([])
  1474. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1475. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1476. flexmock(),
  1477. )
  1478. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1479. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  1480. tuple(
  1481. module.run_actions(
  1482. arguments={'global': flexmock(dry_run=False), 'borg': flexmock()},
  1483. config_filename=flexmock(),
  1484. config={'repositories': []},
  1485. config_paths=[],
  1486. local_path=flexmock(),
  1487. remote_path=flexmock(),
  1488. local_borg_version=flexmock(),
  1489. repository={'path': 'repo'},
  1490. ),
  1491. )
  1492. def test_run_actions_runs_multiple_actions_in_argument_order():
  1493. flexmock(module).should_receive('add_custom_log_levels')
  1494. flexmock(module).should_receive('get_skip_actions').and_return([])
  1495. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1496. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1497. flexmock(),
  1498. )
  1499. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1500. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  1501. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  1502. tuple(
  1503. module.run_actions(
  1504. arguments={
  1505. 'global': flexmock(dry_run=False),
  1506. 'borg': flexmock(),
  1507. 'restore': flexmock(),
  1508. },
  1509. config_filename=flexmock(),
  1510. config={'repositories': []},
  1511. config_paths=[],
  1512. local_path=flexmock(),
  1513. remote_path=flexmock(),
  1514. local_borg_version=flexmock(),
  1515. repository={'path': 'repo'},
  1516. ),
  1517. )
  1518. def test_run_actions_runs_action_hooks_for_one_action_at_a_time():
  1519. flexmock(module).should_receive('add_custom_log_levels')
  1520. flexmock(module).should_receive('get_skip_actions').and_return([])
  1521. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1522. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1523. flexmock(),
  1524. )
  1525. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1526. for action_name in ('borg', 'restore'):
  1527. flexmock(module.command).should_receive('Before_after_hooks').with_args(
  1528. command_hooks=object,
  1529. before_after='action',
  1530. umask=object,
  1531. working_directory=object,
  1532. dry_run=object,
  1533. action_names=(action_name,),
  1534. configuration_filename=object,
  1535. repository_label=object,
  1536. log_file=object,
  1537. repositories=object,
  1538. repository=object,
  1539. ).and_return(flexmock()).once()
  1540. flexmock(borgmatic.actions.borg).should_receive('run_borg')
  1541. flexmock(borgmatic.actions.restore).should_receive('run_restore')
  1542. tuple(
  1543. module.run_actions(
  1544. arguments={
  1545. 'global': flexmock(dry_run=False),
  1546. 'borg': flexmock(),
  1547. 'restore': flexmock(),
  1548. },
  1549. config_filename=flexmock(),
  1550. config={'repositories': []},
  1551. config_paths=[],
  1552. local_path=flexmock(),
  1553. remote_path=flexmock(),
  1554. local_borg_version=flexmock(),
  1555. repository={'path': 'repo'},
  1556. ),
  1557. )
  1558. @pytest.mark.parametrize(
  1559. 'resolve_env',
  1560. ((True, False),),
  1561. )
  1562. def test_load_configurations_collects_parsed_configurations_and_logs(resolve_env):
  1563. configuration = flexmock()
  1564. other_configuration = flexmock()
  1565. test_expected_logs = [flexmock(), flexmock()]
  1566. other_expected_logs = [flexmock(), flexmock()]
  1567. flexmock(module.validate).should_receive('parse_configuration').and_return(
  1568. configuration,
  1569. ['/tmp/test.yaml'],
  1570. test_expected_logs,
  1571. ).and_return(other_configuration, ['/tmp/other.yaml'], other_expected_logs)
  1572. configs, config_paths, logs = tuple(
  1573. module.load_configurations(
  1574. ('test.yaml', 'other.yaml'),
  1575. arguments=flexmock(),
  1576. resolve_env=resolve_env,
  1577. ),
  1578. )
  1579. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  1580. assert config_paths == ['/tmp/other.yaml', '/tmp/test.yaml']
  1581. assert set(logs) >= set(test_expected_logs + other_expected_logs)
  1582. def test_load_configurations_logs_error_for_permission_error():
  1583. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  1584. configs, config_paths, logs = tuple(
  1585. module.load_configurations(('test.yaml',), arguments=flexmock()),
  1586. )
  1587. assert configs == {}
  1588. assert config_paths == []
  1589. assert max(log.levelno for log in logs) == logging.CRITICAL
  1590. def test_load_configurations_logs_critical_for_parse_error():
  1591. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  1592. configs, config_paths, logs = tuple(
  1593. module.load_configurations(('test.yaml',), arguments=flexmock()),
  1594. )
  1595. assert configs == {}
  1596. assert config_paths == []
  1597. assert max(log.levelno for log in logs) == logging.CRITICAL
  1598. def test_log_record_does_not_raise():
  1599. module.log_record(levelno=1, foo='bar', baz='quux')
  1600. def test_log_record_with_suppress_does_not_raise():
  1601. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  1602. def test_log_error_records_generates_output_logs_for_message_only():
  1603. flexmock(module).should_receive('log_record').replace_with(dict).once()
  1604. logs = tuple(module.log_error_records('Error'))
  1605. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1606. def test_log_error_records_generates_output_logs_for_called_process_error_with_bytes_ouput():
  1607. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  1608. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1609. logs = tuple(
  1610. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', b'error output')),
  1611. )
  1612. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1613. assert any(log for log in logs if 'error output' in str(log))
  1614. def test_log_error_records_generates_output_logs_for_called_process_error_with_string_ouput():
  1615. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  1616. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1617. logs = tuple(
  1618. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output')),
  1619. )
  1620. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1621. assert any(log for log in logs if 'error output' in str(log))
  1622. def test_log_error_records_generates_work_around_output_logs_for_called_process_error_with_repository_access_aborted_exit_code():
  1623. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  1624. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1625. logs = tuple(
  1626. module.log_error_records(
  1627. 'Error',
  1628. subprocess.CalledProcessError(
  1629. module.BORG_REPOSITORY_ACCESS_ABORTED_EXIT_CODE,
  1630. 'ls',
  1631. 'error output',
  1632. ),
  1633. ),
  1634. )
  1635. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1636. assert any(log for log in logs if 'error output' in str(log))
  1637. assert any(log for log in logs if 'To work around this' in str(log))
  1638. def test_log_error_records_splits_called_process_error_with_multiline_ouput_into_multiple_logs():
  1639. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  1640. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1641. logs = tuple(
  1642. module.log_error_records(
  1643. 'Error',
  1644. subprocess.CalledProcessError(1, 'ls', 'error output\nanother line'),
  1645. ),
  1646. )
  1647. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1648. assert any(log for log in logs if 'error output' in str(log))
  1649. def test_log_error_records_generates_logs_for_value_error():
  1650. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  1651. logs = tuple(module.log_error_records('Error', ValueError()))
  1652. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1653. def test_log_error_records_generates_logs_for_os_error():
  1654. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  1655. logs = tuple(module.log_error_records('Error', OSError()))
  1656. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1657. def test_log_error_records_generates_nothing_for_other_error():
  1658. flexmock(module).should_receive('log_record').never()
  1659. logs = tuple(module.log_error_records('Error', KeyError()))
  1660. assert logs == ()
  1661. def test_get_local_path_uses_configuration_value():
  1662. assert module.get_local_path({'test.yaml': {'local_path': 'borg1'}}) == 'borg1'
  1663. def test_get_local_path_without_local_path_defaults_to_borg():
  1664. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  1665. def test_collect_highlander_action_summary_logs_info_for_success_with_bootstrap():
  1666. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1667. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  1668. arguments = {
  1669. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1670. 'global': flexmock(dry_run=False),
  1671. }
  1672. logs = tuple(
  1673. module.collect_highlander_action_summary_logs(
  1674. {'test.yaml': {}},
  1675. arguments=arguments,
  1676. configuration_parse_errors=False,
  1677. ),
  1678. )
  1679. assert {log.levelno for log in logs} == {logging.ANSWER}
  1680. def test_collect_highlander_action_summary_logs_error_on_bootstrap_failure():
  1681. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1682. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  1683. ValueError,
  1684. )
  1685. arguments = {
  1686. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1687. 'global': flexmock(dry_run=False),
  1688. }
  1689. logs = tuple(
  1690. module.collect_highlander_action_summary_logs(
  1691. {'test.yaml': {}},
  1692. arguments=arguments,
  1693. configuration_parse_errors=False,
  1694. ),
  1695. )
  1696. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1697. def test_collect_highlander_action_summary_logs_error_on_bootstrap_local_borg_version_failure():
  1698. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  1699. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').never()
  1700. arguments = {
  1701. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1702. 'global': flexmock(dry_run=False),
  1703. }
  1704. logs = tuple(
  1705. module.collect_highlander_action_summary_logs(
  1706. {'test.yaml': {}},
  1707. arguments=arguments,
  1708. configuration_parse_errors=False,
  1709. ),
  1710. )
  1711. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1712. def test_collect_highlander_action_summary_logs_info_for_success_with_generate():
  1713. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate')
  1714. arguments = {
  1715. 'generate': flexmock(destination='test.yaml'),
  1716. 'global': flexmock(dry_run=False),
  1717. }
  1718. logs = tuple(
  1719. module.collect_highlander_action_summary_logs(
  1720. {'test.yaml': {}},
  1721. arguments=arguments,
  1722. configuration_parse_errors=False,
  1723. ),
  1724. )
  1725. assert {log.levelno for log in logs} == {logging.ANSWER}
  1726. def test_collect_highlander_action_summary_logs_error_on_generate_failure():
  1727. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate').and_raise(
  1728. ValueError,
  1729. )
  1730. arguments = {
  1731. 'generate': flexmock(destination='test.yaml'),
  1732. 'global': flexmock(dry_run=False),
  1733. }
  1734. logs = tuple(
  1735. module.collect_highlander_action_summary_logs(
  1736. {'test.yaml': {}},
  1737. arguments=arguments,
  1738. configuration_parse_errors=False,
  1739. ),
  1740. )
  1741. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1742. def test_collect_highlander_action_summary_logs_info_for_success_with_validate():
  1743. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1744. arguments = {
  1745. 'validate': flexmock(),
  1746. 'global': flexmock(dry_run=False),
  1747. }
  1748. logs = tuple(
  1749. module.collect_highlander_action_summary_logs(
  1750. {'test.yaml': {}},
  1751. arguments=arguments,
  1752. configuration_parse_errors=False,
  1753. ),
  1754. )
  1755. assert {log.levelno for log in logs} == {logging.ANSWER}
  1756. def test_collect_highlander_action_summary_logs_error_on_validate_parse_failure():
  1757. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1758. arguments = {
  1759. 'validate': flexmock(),
  1760. 'global': flexmock(dry_run=False),
  1761. }
  1762. logs = tuple(
  1763. module.collect_highlander_action_summary_logs(
  1764. {'test.yaml': {}},
  1765. arguments=arguments,
  1766. configuration_parse_errors=True,
  1767. ),
  1768. )
  1769. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1770. def test_collect_highlander_action_summary_logs_error_on_run_validate_failure():
  1771. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate').and_raise(
  1772. ValueError,
  1773. )
  1774. arguments = {
  1775. 'validate': flexmock(),
  1776. 'global': flexmock(dry_run=False),
  1777. }
  1778. logs = tuple(
  1779. module.collect_highlander_action_summary_logs(
  1780. {'test.yaml': {}},
  1781. arguments=arguments,
  1782. configuration_parse_errors=False,
  1783. ),
  1784. )
  1785. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1786. def test_collect_configuration_run_summary_logs_info_for_success():
  1787. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1788. flexmock(module.command).should_receive('filter_hooks').with_args(
  1789. object,
  1790. before='everything',
  1791. action_names=object,
  1792. )
  1793. flexmock(module.command).should_receive('filter_hooks').with_args(
  1794. object,
  1795. after='everything',
  1796. action_names=object,
  1797. state_names=['finish'],
  1798. )
  1799. flexmock(module.command).should_receive('execute_hooks')
  1800. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1801. flexmock(module).should_receive('run_configuration').and_return([])
  1802. arguments = {'global': flexmock(dry_run=False)}
  1803. logs = tuple(
  1804. module.collect_configuration_run_summary_logs(
  1805. {'test.yaml': {}},
  1806. config_paths=['/tmp/test.yaml'],
  1807. arguments=arguments,
  1808. log_file_path=None,
  1809. ),
  1810. )
  1811. assert {log.levelno for log in logs} == {logging.INFO}
  1812. def test_collect_configuration_run_summary_executes_hooks_for_create():
  1813. before_everything_hook = {'before': 'everything', 'run': ['echo hi']}
  1814. after_everything_hook = {'after': 'everything', 'run': ['echo hi']}
  1815. command_hooks = (before_everything_hook, after_everything_hook)
  1816. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1817. flexmock(module.command).should_receive('filter_hooks').with_args(
  1818. command_hooks,
  1819. before='everything',
  1820. action_names=object,
  1821. ).and_return([before_everything_hook])
  1822. flexmock(module.command).should_receive('filter_hooks').with_args(
  1823. command_hooks,
  1824. after='everything',
  1825. action_names=object,
  1826. state_names=['finish'],
  1827. ).and_return([after_everything_hook])
  1828. flexmock(module.command).should_receive('execute_hooks').twice()
  1829. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1830. flexmock(module).should_receive('run_configuration').and_return([])
  1831. arguments = {
  1832. 'create': flexmock(),
  1833. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  1834. }
  1835. logs = tuple(
  1836. module.collect_configuration_run_summary_logs(
  1837. {'test.yaml': {'commands': command_hooks}},
  1838. config_paths=['/tmp/test.yaml'],
  1839. arguments=arguments,
  1840. log_file_path=None,
  1841. ),
  1842. )
  1843. assert {log.levelno for log in logs} == {logging.INFO}
  1844. def test_collect_configuration_run_summary_deduplicates_everything_hooks_across_config_files():
  1845. before_everything_hook = {'before': 'everything', 'run': ['echo hi']}
  1846. after_everything_hook = {'after': 'everything', 'run': ['echo hi']}
  1847. command_hooks = (before_everything_hook, after_everything_hook)
  1848. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1849. flexmock(module.command).should_receive('filter_hooks').with_args(
  1850. command_hooks,
  1851. before='everything',
  1852. action_names=object,
  1853. ).and_return([before_everything_hook]).once()
  1854. flexmock(module.command).should_receive('filter_hooks').with_args(
  1855. (after_everything_hook,),
  1856. before='everything',
  1857. action_names=object,
  1858. ).and_return([]).once()
  1859. flexmock(module.command).should_receive('filter_hooks').with_args(
  1860. command_hooks,
  1861. after='everything',
  1862. action_names=object,
  1863. state_names=['finish'],
  1864. ).and_return([after_everything_hook]).once()
  1865. flexmock(module.command).should_receive('filter_hooks').with_args(
  1866. (before_everything_hook,),
  1867. after='everything',
  1868. action_names=object,
  1869. state_names=['finish'],
  1870. ).and_return([]).once()
  1871. flexmock(module.command).should_receive('execute_hooks').twice()
  1872. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1873. flexmock(module).should_receive('run_configuration').and_return([])
  1874. arguments = {
  1875. 'create': flexmock(),
  1876. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  1877. }
  1878. logs = tuple(
  1879. module.collect_configuration_run_summary_logs(
  1880. {'test.yaml': {'commands': command_hooks}, 'other.yaml': {'commands': command_hooks}},
  1881. config_paths=['/tmp/test.yaml', '/tmp/other.yaml'],
  1882. arguments=arguments,
  1883. log_file_path=None,
  1884. ),
  1885. )
  1886. assert {log.levelno for log in logs} == {logging.INFO}
  1887. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  1888. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1889. flexmock(module.command).should_receive('filter_hooks').with_args(
  1890. object,
  1891. before='everything',
  1892. action_names=object,
  1893. )
  1894. flexmock(module.command).should_receive('filter_hooks').with_args(
  1895. object,
  1896. after='everything',
  1897. action_names=object,
  1898. state_names=['finish'],
  1899. )
  1900. flexmock(module.command).should_receive('execute_hooks')
  1901. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1902. flexmock(module).should_receive('run_configuration').and_return([])
  1903. arguments = {
  1904. 'extract': flexmock(repository='repo'),
  1905. 'global': flexmock(dry_run=False),
  1906. }
  1907. logs = tuple(
  1908. module.collect_configuration_run_summary_logs(
  1909. {'test.yaml': {}},
  1910. config_paths=['/tmp/test.yaml'],
  1911. arguments=arguments,
  1912. log_file_path=None,
  1913. ),
  1914. )
  1915. assert {log.levelno for log in logs} == {logging.INFO}
  1916. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  1917. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1918. ValueError,
  1919. )
  1920. expected_logs = (flexmock(),)
  1921. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1922. arguments = {'extract': flexmock(repository='repo')}
  1923. logs = tuple(
  1924. module.collect_configuration_run_summary_logs(
  1925. {'test.yaml': {}},
  1926. config_paths=['/tmp/test.yaml'],
  1927. arguments=arguments,
  1928. log_file_path=None,
  1929. ),
  1930. )
  1931. assert logs == expected_logs
  1932. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  1933. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1934. flexmock(module.command).should_receive('filter_hooks').with_args(
  1935. object,
  1936. before='everything',
  1937. action_names=object,
  1938. )
  1939. flexmock(module.command).should_receive('filter_hooks').with_args(
  1940. object,
  1941. after='everything',
  1942. action_names=object,
  1943. state_names=['finish'],
  1944. )
  1945. flexmock(module.command).should_receive('execute_hooks')
  1946. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1947. flexmock(module).should_receive('run_configuration').and_return([])
  1948. arguments = {
  1949. 'mount': flexmock(repository='repo'),
  1950. 'global': flexmock(dry_run=False),
  1951. }
  1952. logs = tuple(
  1953. module.collect_configuration_run_summary_logs(
  1954. {'test.yaml': {}},
  1955. config_paths=['/tmp/test.yaml'],
  1956. arguments=arguments,
  1957. log_file_path=None,
  1958. ),
  1959. )
  1960. assert {log.levelno for log in logs} == {logging.INFO}
  1961. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  1962. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1963. ValueError,
  1964. )
  1965. expected_logs = (flexmock(),)
  1966. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1967. arguments = {
  1968. 'mount': flexmock(repository='repo'),
  1969. 'global': flexmock(dry_run=False),
  1970. }
  1971. logs = tuple(
  1972. module.collect_configuration_run_summary_logs(
  1973. {'test.yaml': {}},
  1974. config_paths=['/tmp/test.yaml'],
  1975. arguments=arguments,
  1976. log_file_path=None,
  1977. ),
  1978. )
  1979. assert logs == expected_logs
  1980. def test_collect_configuration_run_summary_logs_missing_configs_error():
  1981. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1982. flexmock(module.command).should_receive('filter_hooks').with_args(
  1983. object,
  1984. before='everything',
  1985. action_names=object,
  1986. )
  1987. flexmock(module.command).should_receive('filter_hooks').with_args(
  1988. object,
  1989. after='everything',
  1990. action_names=object,
  1991. state_names=['fail'],
  1992. )
  1993. flexmock(module.command).should_receive('execute_hooks')
  1994. arguments = {'global': flexmock(config_paths=[])}
  1995. expected_logs = (flexmock(),)
  1996. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1997. logs = tuple(
  1998. module.collect_configuration_run_summary_logs(
  1999. {},
  2000. config_paths=[],
  2001. arguments=arguments,
  2002. log_file_path=None,
  2003. ),
  2004. )
  2005. assert logs == expected_logs
  2006. def test_collect_configuration_run_summary_logs_before_hook_error():
  2007. before_everything_hook = {'before': 'everything', 'run': ['echo hi']}
  2008. after_everything_hook = {'after': 'everything', 'run': ['echo hi']}
  2009. command_hooks = (before_everything_hook, after_everything_hook)
  2010. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  2011. flexmock(module.command).should_receive('filter_hooks').with_args(
  2012. object,
  2013. before='everything',
  2014. action_names=object,
  2015. ).and_return([before_everything_hook])
  2016. flexmock(module.command).should_receive('filter_hooks').with_args(
  2017. object,
  2018. after='everything',
  2019. action_names=object,
  2020. state_names=['fail'],
  2021. ).and_return([after_everything_hook])
  2022. flexmock(module.command).should_receive('execute_hooks').and_raise(ValueError)
  2023. expected_logs = (flexmock(),)
  2024. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  2025. arguments = {
  2026. 'create': flexmock(),
  2027. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  2028. }
  2029. logs = tuple(
  2030. module.collect_configuration_run_summary_logs(
  2031. {'test.yaml': {'commands': command_hooks}},
  2032. config_paths=['/tmp/test.yaml'],
  2033. arguments=arguments,
  2034. log_file_path=None,
  2035. ),
  2036. )
  2037. assert logs == expected_logs
  2038. def test_collect_configuration_run_summary_logs_after_hook_error():
  2039. before_everything_hook = {'before': 'everything', 'run': ['echo hi']}
  2040. after_everything_hook = {'after': 'everything', 'run': ['echo hi']}
  2041. command_hooks = (before_everything_hook, after_everything_hook)
  2042. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  2043. flexmock(module.command).should_receive('filter_hooks').with_args(
  2044. object,
  2045. before='everything',
  2046. action_names=object,
  2047. ).and_return([before_everything_hook])
  2048. flexmock(module.command).should_receive('filter_hooks').with_args(
  2049. object,
  2050. after='everything',
  2051. action_names=object,
  2052. state_names=['finish'],
  2053. ).and_return([after_everything_hook])
  2054. flexmock(module.command).should_receive('execute_hooks').and_return(None).and_raise(ValueError)
  2055. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  2056. flexmock(module).should_receive('run_configuration').and_return([])
  2057. expected_logs = (flexmock(),)
  2058. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  2059. arguments = {
  2060. 'create': flexmock(),
  2061. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  2062. }
  2063. logs = tuple(
  2064. module.collect_configuration_run_summary_logs(
  2065. {'test.yaml': {'commands': command_hooks}},
  2066. config_paths=['/tmp/test.yaml'],
  2067. arguments=arguments,
  2068. log_file_path=None,
  2069. ),
  2070. )
  2071. assert expected_logs[0] in logs
  2072. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  2073. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  2074. ValueError,
  2075. )
  2076. expected_logs = (flexmock(),)
  2077. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  2078. arguments = {
  2079. 'list': flexmock(repository='repo', archive='test'),
  2080. 'global': flexmock(dry_run=False),
  2081. }
  2082. logs = tuple(
  2083. module.collect_configuration_run_summary_logs(
  2084. {'test.yaml': {}},
  2085. config_paths=['/tmp/test.yaml'],
  2086. arguments=arguments,
  2087. log_file_path=None,
  2088. ),
  2089. )
  2090. assert logs == expected_logs
  2091. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  2092. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  2093. flexmock(module.command).should_receive('filter_hooks').with_args(
  2094. object,
  2095. before='everything',
  2096. action_names=object,
  2097. )
  2098. flexmock(module.command).should_receive('filter_hooks').with_args(
  2099. object,
  2100. after='everything',
  2101. action_names=object,
  2102. state_names=['finish'],
  2103. )
  2104. flexmock(module.command).should_receive('execute_hooks')
  2105. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  2106. flexmock(module).should_receive('run_configuration').and_return([])
  2107. arguments = {
  2108. 'list': flexmock(repository='repo', archive=None),
  2109. 'global': flexmock(dry_run=False),
  2110. }
  2111. logs = tuple(
  2112. module.collect_configuration_run_summary_logs(
  2113. {'test.yaml': {}},
  2114. config_paths=['/tmp/test.yaml'],
  2115. arguments=arguments,
  2116. log_file_path=None,
  2117. ),
  2118. )
  2119. assert {log.levelno for log in logs} == {logging.INFO}
  2120. def test_collect_configuration_run_summary_logs_run_configuration_error_logs():
  2121. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  2122. flexmock(module.command).should_receive('filter_hooks').with_args(
  2123. object,
  2124. before='everything',
  2125. action_names=object,
  2126. )
  2127. flexmock(module.command).should_receive('filter_hooks').with_args(
  2128. object,
  2129. after='everything',
  2130. action_names=object,
  2131. state_names=['fail'],
  2132. )
  2133. flexmock(module.command).should_receive('execute_hooks')
  2134. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  2135. flexmock(module).should_receive('run_configuration').and_return(
  2136. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))],
  2137. )
  2138. flexmock(module).should_receive('log_error_records').and_return([])
  2139. arguments = {'global': flexmock(dry_run=False)}
  2140. logs = tuple(
  2141. module.collect_configuration_run_summary_logs(
  2142. {'test.yaml': {}},
  2143. config_paths=['/tmp/test.yaml'],
  2144. arguments=arguments,
  2145. log_file_path=None,
  2146. ),
  2147. )
  2148. assert {log.levelno for log in logs} == {logging.CRITICAL}
  2149. def test_collect_configuration_run_summary_logs_run_umount_error():
  2150. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  2151. flexmock(module.command).should_receive('filter_hooks').with_args(
  2152. object,
  2153. before='everything',
  2154. action_names=object,
  2155. )
  2156. flexmock(module.command).should_receive('filter_hooks').with_args(
  2157. object,
  2158. after='everything',
  2159. action_names=object,
  2160. state_names=['fail'],
  2161. )
  2162. flexmock(module.command).should_receive('execute_hooks')
  2163. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  2164. flexmock(module).should_receive('run_configuration').and_return([])
  2165. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  2166. flexmock(module).should_receive('log_error_records').and_return(
  2167. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))],
  2168. )
  2169. arguments = {
  2170. 'umount': flexmock(mount_point='/mnt'),
  2171. 'global': flexmock(dry_run=False),
  2172. }
  2173. logs = tuple(
  2174. module.collect_configuration_run_summary_logs(
  2175. {'test.yaml': {}},
  2176. config_paths=['/tmp/test.yaml'],
  2177. arguments=arguments,
  2178. log_file_path=None,
  2179. ),
  2180. )
  2181. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  2182. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  2183. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  2184. flexmock(module.command).should_receive('filter_hooks').with_args(
  2185. object,
  2186. before='everything',
  2187. action_names=object,
  2188. )
  2189. flexmock(module.command).should_receive('filter_hooks').with_args(
  2190. object,
  2191. after='everything',
  2192. action_names=object,
  2193. state_names=['finish'],
  2194. )
  2195. flexmock(module.command).should_receive('execute_hooks')
  2196. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  2197. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  2198. ['baz'],
  2199. )
  2200. stdout = flexmock()
  2201. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  2202. flexmock(module.sys).stdout = stdout
  2203. arguments = {'global': flexmock(dry_run=False)}
  2204. tuple(
  2205. module.collect_configuration_run_summary_logs(
  2206. {'test.yaml': {}, 'test2.yaml': {}},
  2207. config_paths=['/tmp/test.yaml', '/tmp/test2.yaml'],
  2208. arguments=arguments,
  2209. log_file_path=None,
  2210. ),
  2211. )
  2212. def test_check_and_show_help_on_no_args_shows_help_when_no_args_and_default_actions_false():
  2213. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  2214. flexmock(module).should_receive('parse_arguments').with_args('--help').once()
  2215. flexmock(module.sys).should_receive('exit').with_args(0).once()
  2216. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}})
  2217. def test_check_and_show_help_on_no_args_does_not_show_help_when_no_args_and_default_actions_true():
  2218. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  2219. flexmock(module).should_receive('parse_arguments').never()
  2220. flexmock(module.sys).should_receive('exit').never()
  2221. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': True}})
  2222. def test_check_and_show_help_on_no_args_does_not_show_help_when_args_provided():
  2223. flexmock(module.sys).should_receive('argv').and_return(['borgmatic', '--create'])
  2224. flexmock(module).should_receive('parse_arguments').never()
  2225. flexmock(module.sys).should_receive('exit').never()
  2226. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}})
  2227. def test_check_and_show_help_on_no_args_with_no_default_actions_in_all_configs():
  2228. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  2229. # Both configs have default_actions set to False, so help should be shown
  2230. configs = {
  2231. 'config1.yaml': {'default_actions': False},
  2232. 'config2.yaml': {'default_actions': False},
  2233. }
  2234. # Expect help to be shown
  2235. flexmock(module).should_receive('parse_arguments').with_args('--help').once()
  2236. flexmock(module.sys).should_receive('exit').with_args(0).once()
  2237. module.check_and_show_help_on_no_args(configs)
  2238. def test_check_and_show_help_on_no_args_with_conflicting_configs():
  2239. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  2240. # Simulate two config files with conflicting 'default_actions' values
  2241. configs = {
  2242. 'config1.yaml': {'default_actions': True},
  2243. 'config2.yaml': {'default_actions': False},
  2244. }
  2245. # Expect help not to be shown because at least one config enables default actions
  2246. flexmock(module).should_receive('parse_arguments').never()
  2247. flexmock(module.sys).should_receive('exit').never()
  2248. module.check_and_show_help_on_no_args(configs)
  2249. def test_get_singular_option_value_with_conflicting_values_exits():
  2250. flexmock(module).should_receive('configure_logging')
  2251. flexmock(module).should_receive('exit_with_help_link').once()
  2252. module.get_singular_option_value(
  2253. configs={
  2254. 'test1.yaml': {'foo': 1, 'bar': 'baz'},
  2255. 'test2.yaml': {'foo': 2, 'bar': 'baz'},
  2256. },
  2257. option_name='foo',
  2258. )
  2259. def test_get_singular_option_value_with_same_value_returns_it():
  2260. flexmock(module).should_receive('configure_logging').never()
  2261. flexmock(module).should_receive('exit_with_help_link').never()
  2262. assert (
  2263. module.get_singular_option_value(
  2264. configs={
  2265. 'test1.yaml': {'foo': 1, 'bar': 'baz'},
  2266. 'test2.yaml': {'foo': 1, 'bar': 'baz'},
  2267. },
  2268. option_name='foo',
  2269. )
  2270. == 1
  2271. )
  2272. def test_get_singular_option_value_with_no_values_returns_none():
  2273. flexmock(module).should_receive('configure_logging').never()
  2274. flexmock(module).should_receive('exit_with_help_link').never()
  2275. assert (
  2276. module.get_singular_option_value(
  2277. configs={
  2278. 'test1.yaml': {'bar': 'baz'},
  2279. 'test2.yaml': {'bar': 'baz'},
  2280. },
  2281. option_name='foo',
  2282. )
  2283. is None
  2284. )
  2285. def test_get_singular_option_value_with_no_config_returns_none():
  2286. flexmock(module).should_receive('configure_logging').never()
  2287. flexmock(module).should_receive('exit_with_help_link').never()
  2288. assert (
  2289. module.get_singular_option_value(
  2290. configs={},
  2291. option_name='foo',
  2292. )
  2293. is None
  2294. )