test_borgmatic.py 93 KB

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