test_borgmatic.py 90 KB

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