test_borgmatic.py 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  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()]
  429. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  430. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  431. flexmock(module).should_receive('run_actions').and_raise(OSError)
  432. flexmock(module.command).should_receive('filter_hooks')
  433. flexmock(module.command).should_receive('execute_hooks')
  434. config = {'repositories': [{'path': 'foo'}]}
  435. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  436. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  437. assert results == expected_results
  438. def test_run_configuration_skips_remaining_actions_for_actions_soft_failure_but_still_runs_next_repository_actions():
  439. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  440. flexmock(module).should_receive('get_skip_actions').and_return([])
  441. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  442. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  443. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  444. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  445. log = flexmock()
  446. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  447. flexmock(module).should_receive('run_actions').twice().and_raise(error).and_yield(log)
  448. flexmock(module).should_receive('log_error_records').never()
  449. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  450. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  451. arguments = {
  452. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  453. 'create': flexmock(),
  454. }
  455. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  456. assert results == [log]
  457. def test_run_configuration_does_not_call_monitoring_hooks_if_monitoring_hooks_are_disabled():
  458. flexmock(module).should_receive('verbosity_to_log_level').and_return(module.DISABLED)
  459. flexmock(module).should_receive('get_skip_actions').and_return([])
  460. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  461. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  462. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  463. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  464. flexmock(module).should_receive('run_actions').and_return([])
  465. config = {'repositories': [{'path': 'foo'}]}
  466. arguments = {
  467. 'global': flexmock(monitoring_verbosity=-2, dry_run=False),
  468. 'create': flexmock(),
  469. }
  470. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  471. assert results == []
  472. def test_run_configuration_logs_on_error_hook_error():
  473. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  474. flexmock(module).should_receive('get_skip_actions').and_return([])
  475. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  476. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  477. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  478. flexmock(module.command).should_receive('filter_hooks')
  479. flexmock(module.command).should_receive('execute_hooks').and_raise(OSError)
  480. expected_results = [flexmock(), flexmock()]
  481. flexmock(module).should_receive('log_error_records').and_return(
  482. expected_results[:1]
  483. ).and_return(expected_results[1:])
  484. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  485. flexmock(module).should_receive('run_actions').and_raise(OSError)
  486. config = {'repositories': [{'path': 'foo'}]}
  487. arguments = {
  488. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  489. 'create': flexmock(),
  490. }
  491. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  492. assert results == expected_results
  493. def test_run_configuration_logs_on_before_command_hook_error():
  494. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  495. flexmock(module).should_receive('get_skip_actions').and_return([])
  496. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  497. flexmock(module.command).should_receive('Before_after_hooks').and_raise(OSError)
  498. flexmock(module.borg_version).should_receive('local_borg_version').never()
  499. flexmock(module.command).should_receive('filter_hooks')
  500. flexmock(module.command).should_receive('execute_hooks')
  501. expected_results = [flexmock()]
  502. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  503. flexmock(module).should_receive('Log_prefix').never()
  504. flexmock(module).should_receive('run_actions').never()
  505. config = {'repositories': [{'path': 'foo'}]}
  506. arguments = {
  507. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  508. 'create': flexmock(),
  509. }
  510. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  511. assert results == expected_results
  512. def test_run_configuration_logs_on_monitoring_hook_error():
  513. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  514. flexmock(module).should_receive('get_skip_actions').and_return([])
  515. flexmock(module).should_receive('Monitoring_hooks').and_raise(OSError)
  516. flexmock(module.command).should_receive('Before_after_hooks').never()
  517. flexmock(module.borg_version).should_receive('local_borg_version').never()
  518. flexmock(module.command).should_receive('filter_hooks')
  519. flexmock(module.command).should_receive('execute_hooks')
  520. expected_results = [flexmock()]
  521. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  522. flexmock(module).should_receive('Log_prefix').never()
  523. flexmock(module).should_receive('run_actions').never()
  524. config = {'repositories': [{'path': 'foo'}]}
  525. arguments = {
  526. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  527. 'create': flexmock(),
  528. }
  529. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  530. assert results == expected_results
  531. def test_run_configuration_bails_for_on_error_hook_soft_failure():
  532. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  533. flexmock(module).should_receive('get_skip_actions').and_return([])
  534. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  535. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  536. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  537. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  538. flexmock(module.command).should_receive('filter_hooks')
  539. flexmock(module.command).should_receive('execute_hooks').and_raise(error)
  540. expected_results = [flexmock()]
  541. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  542. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  543. flexmock(module).should_receive('run_actions').and_raise(OSError)
  544. config = {'repositories': [{'path': 'foo'}]}
  545. arguments = {
  546. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  547. 'create': flexmock(),
  548. }
  549. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  550. assert results == expected_results
  551. def test_run_configuration_retries_soft_error():
  552. # Run action first fails, second passes.
  553. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  554. flexmock(module).should_receive('get_skip_actions').and_return([])
  555. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  556. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  557. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  558. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  559. flexmock(module).should_receive('run_actions').and_raise(OSError).and_return([])
  560. flexmock(module).should_receive('log_error_records').and_return([flexmock()]).once()
  561. flexmock(module.command).should_receive('filter_hooks').never()
  562. flexmock(module.command).should_receive('execute_hooks').never()
  563. config = {'repositories': [{'path': 'foo'}], 'retries': 1}
  564. arguments = {
  565. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  566. 'create': flexmock(),
  567. }
  568. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  569. assert results == []
  570. def test_run_configuration_retries_hard_error():
  571. # Run action fails twice.
  572. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  573. flexmock(module).should_receive('get_skip_actions').and_return([])
  574. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  575. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  576. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  577. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  578. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  579. flexmock(module).should_receive('log_error_records').with_args(
  580. 'Error running actions for repository',
  581. OSError,
  582. levelno=logging.WARNING,
  583. log_command_error_output=True,
  584. ).and_return([flexmock()])
  585. error_logs = [flexmock()]
  586. flexmock(module).should_receive('log_error_records').with_args(
  587. 'Error running actions for repository',
  588. OSError,
  589. ).and_return(error_logs)
  590. flexmock(module.command).should_receive('filter_hooks')
  591. flexmock(module.command).should_receive('execute_hooks')
  592. config = {'repositories': [{'path': 'foo'}], 'retries': 1}
  593. arguments = {
  594. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  595. 'create': flexmock(),
  596. }
  597. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  598. assert results == error_logs
  599. def test_run_configuration_retries_repositories_in_order():
  600. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  601. flexmock(module).should_receive('get_skip_actions').and_return([])
  602. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  603. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  604. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  605. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  606. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  607. expected_results = [flexmock(), flexmock()]
  608. flexmock(module).should_receive('log_error_records').with_args(
  609. 'Error running actions for repository', OSError
  610. ).and_return(expected_results[:1]).ordered()
  611. flexmock(module).should_receive('log_error_records').with_args(
  612. 'Error running actions for repository', OSError
  613. ).and_return(expected_results[1:]).ordered()
  614. flexmock(module.command).should_receive('filter_hooks')
  615. flexmock(module.command).should_receive('execute_hooks')
  616. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  617. arguments = {
  618. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  619. 'create': flexmock(),
  620. }
  621. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  622. assert results == expected_results
  623. def test_run_configuration_retries_round_robin():
  624. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  625. flexmock(module).should_receive('get_skip_actions').and_return([])
  626. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  627. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  628. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  629. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  630. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  631. flexmock(module).should_receive('log_error_records').with_args(
  632. 'Error running actions for repository',
  633. OSError,
  634. levelno=logging.WARNING,
  635. log_command_error_output=True,
  636. ).and_return([flexmock()]).ordered()
  637. flexmock(module).should_receive('log_error_records').with_args(
  638. 'Error running actions for repository',
  639. OSError,
  640. levelno=logging.WARNING,
  641. log_command_error_output=True,
  642. ).and_return([flexmock()]).ordered()
  643. foo_error_logs = [flexmock()]
  644. flexmock(module).should_receive('log_error_records').with_args(
  645. 'Error running actions for repository', OSError
  646. ).and_return(foo_error_logs).ordered()
  647. bar_error_logs = [flexmock()]
  648. flexmock(module).should_receive('log_error_records').with_args(
  649. 'Error running actions for repository', OSError
  650. ).and_return(bar_error_logs).ordered()
  651. flexmock(module.command).should_receive('filter_hooks')
  652. flexmock(module.command).should_receive('execute_hooks')
  653. config = {
  654. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  655. 'retries': 1,
  656. }
  657. arguments = {
  658. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  659. 'create': flexmock(),
  660. }
  661. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  662. assert results == foo_error_logs + bar_error_logs
  663. def test_run_configuration_with_one_retry():
  664. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  665. flexmock(module).should_receive('get_skip_actions').and_return([])
  666. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  667. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  668. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  669. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  670. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  671. []
  672. ).and_raise(OSError).times(4)
  673. flexmock(module).should_receive('log_error_records').with_args(
  674. 'Error running actions for repository',
  675. OSError,
  676. levelno=logging.WARNING,
  677. log_command_error_output=True,
  678. ).and_return([flexmock()]).ordered()
  679. flexmock(module).should_receive('log_error_records').with_args(
  680. 'Error running actions for repository',
  681. OSError,
  682. levelno=logging.WARNING,
  683. log_command_error_output=True,
  684. ).and_return(flexmock()).ordered()
  685. error_logs = [flexmock()]
  686. flexmock(module).should_receive('log_error_records').with_args(
  687. 'Error running actions for repository', OSError
  688. ).and_return(error_logs).ordered()
  689. flexmock(module.command).should_receive('filter_hooks')
  690. flexmock(module.command).should_receive('execute_hooks')
  691. config = {
  692. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  693. 'retries': 1,
  694. }
  695. arguments = {
  696. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  697. 'create': flexmock(),
  698. }
  699. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  700. assert results == error_logs
  701. def test_run_configuration_with_retry_wait_does_backoff_after_each_retry():
  702. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  703. flexmock(module).should_receive('get_skip_actions').and_return([])
  704. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  705. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  706. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  707. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  708. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  709. flexmock(module).should_receive('log_error_records').with_args(
  710. 'Error running actions for repository',
  711. OSError,
  712. levelno=logging.WARNING,
  713. log_command_error_output=True,
  714. ).and_return([flexmock()]).ordered()
  715. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  716. flexmock(module).should_receive('log_error_records').with_args(
  717. 'Error running actions for repository',
  718. OSError,
  719. levelno=logging.WARNING,
  720. log_command_error_output=True,
  721. ).and_return([flexmock()]).ordered()
  722. flexmock(time).should_receive('sleep').with_args(20).and_return().ordered()
  723. flexmock(module).should_receive('log_error_records').with_args(
  724. 'Error running actions for repository',
  725. OSError,
  726. levelno=logging.WARNING,
  727. log_command_error_output=True,
  728. ).and_return([flexmock()]).ordered()
  729. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  730. error_logs = [flexmock()]
  731. flexmock(module).should_receive('log_error_records').with_args(
  732. 'Error running actions for repository', OSError
  733. ).and_return(error_logs).ordered()
  734. flexmock(module.command).should_receive('filter_hooks')
  735. flexmock(module.command).should_receive('execute_hooks')
  736. config = {
  737. 'repositories': [{'path': 'foo'}],
  738. 'retries': 3,
  739. 'retry_wait': 10,
  740. }
  741. arguments = {
  742. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  743. 'create': flexmock(),
  744. }
  745. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  746. assert results == error_logs
  747. def test_run_configuration_with_multiple_repositories_retries_with_timeout():
  748. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  749. flexmock(module).should_receive('get_skip_actions').and_return([])
  750. flexmock(module).should_receive('Monitoring_hooks').and_return(flexmock())
  751. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  752. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  753. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  754. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  755. []
  756. ).and_raise(OSError).times(4)
  757. flexmock(module).should_receive('log_error_records').with_args(
  758. 'Error running actions for repository',
  759. OSError,
  760. levelno=logging.WARNING,
  761. log_command_error_output=True,
  762. ).and_return([flexmock()]).ordered()
  763. flexmock(module).should_receive('log_error_records').with_args(
  764. 'Error running actions for repository',
  765. OSError,
  766. levelno=logging.WARNING,
  767. log_command_error_output=True,
  768. ).and_return([flexmock()]).ordered()
  769. # Sleep before retrying foo (and passing)
  770. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  771. # Sleep before retrying bar (and failing)
  772. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  773. error_logs = [flexmock()]
  774. flexmock(module).should_receive('log_error_records').with_args(
  775. 'Error running actions for repository', OSError
  776. ).and_return(error_logs).ordered()
  777. flexmock(module.command).should_receive('filter_hooks')
  778. flexmock(module.command).should_receive('execute_hooks')
  779. config = {
  780. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  781. 'retries': 1,
  782. 'retry_wait': 10,
  783. }
  784. arguments = {
  785. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  786. 'create': flexmock(),
  787. }
  788. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  789. assert results == error_logs
  790. def test_run_actions_runs_repo_create():
  791. flexmock(module).should_receive('add_custom_log_levels')
  792. flexmock(module).should_receive('get_skip_actions').and_return([])
  793. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  794. flexmock(borgmatic.actions.repo_create).should_receive('run_repo_create').once()
  795. tuple(
  796. module.run_actions(
  797. arguments={
  798. 'global': flexmock(dry_run=False),
  799. 'repo-create': flexmock(),
  800. },
  801. config_filename=flexmock(),
  802. config={'repositories': []},
  803. config_paths=[],
  804. local_path=flexmock(),
  805. remote_path=flexmock(),
  806. local_borg_version=flexmock(),
  807. repository={'path': 'repo'},
  808. )
  809. )
  810. def test_run_actions_adds_label_file_to_hook_context():
  811. flexmock(module).should_receive('add_custom_log_levels')
  812. flexmock(module).should_receive('get_skip_actions').and_return([])
  813. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  814. expected = flexmock()
  815. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  816. config_filename=object,
  817. repository={'path': 'repo', 'label': 'my repo'},
  818. config={'repositories': []},
  819. config_paths=[],
  820. local_borg_version=object,
  821. create_arguments=object,
  822. global_arguments=object,
  823. dry_run_label='',
  824. local_path=object,
  825. remote_path=object,
  826. ).once().and_return(expected)
  827. result = tuple(
  828. module.run_actions(
  829. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  830. config_filename=flexmock(),
  831. config={'repositories': []},
  832. config_paths=[],
  833. local_path=flexmock(),
  834. remote_path=flexmock(),
  835. local_borg_version=flexmock(),
  836. repository={'path': 'repo', 'label': 'my repo'},
  837. )
  838. )
  839. assert result == (expected,)
  840. def test_run_actions_adds_log_file_to_hook_context():
  841. flexmock(module).should_receive('add_custom_log_levels')
  842. flexmock(module).should_receive('get_skip_actions').and_return([])
  843. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  844. expected = flexmock()
  845. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  846. config_filename=object,
  847. repository={'path': 'repo'},
  848. config={'repositories': [], 'log_file': 'foo'},
  849. config_paths=[],
  850. local_borg_version=object,
  851. create_arguments=object,
  852. global_arguments=object,
  853. dry_run_label='',
  854. local_path=object,
  855. remote_path=object,
  856. ).once().and_return(expected)
  857. result = tuple(
  858. module.run_actions(
  859. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  860. config_filename=flexmock(),
  861. config={'repositories': [], 'log_file': 'foo'},
  862. config_paths=[],
  863. local_path=flexmock(),
  864. remote_path=flexmock(),
  865. local_borg_version=flexmock(),
  866. repository={'path': 'repo'},
  867. )
  868. )
  869. assert result == (expected,)
  870. def test_run_actions_runs_transfer():
  871. flexmock(module).should_receive('add_custom_log_levels')
  872. flexmock(module).should_receive('get_skip_actions').and_return([])
  873. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  874. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  875. tuple(
  876. module.run_actions(
  877. arguments={'global': flexmock(dry_run=False), 'transfer': flexmock()},
  878. config_filename=flexmock(),
  879. config={'repositories': []},
  880. config_paths=[],
  881. local_path=flexmock(),
  882. remote_path=flexmock(),
  883. local_borg_version=flexmock(),
  884. repository={'path': 'repo'},
  885. )
  886. )
  887. def test_run_actions_runs_create():
  888. flexmock(module).should_receive('add_custom_log_levels')
  889. flexmock(module).should_receive('get_skip_actions').and_return([])
  890. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  891. expected = flexmock()
  892. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  893. result = tuple(
  894. module.run_actions(
  895. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  896. config_filename=flexmock(),
  897. config={'repositories': []},
  898. config_paths=[],
  899. local_path=flexmock(),
  900. remote_path=flexmock(),
  901. local_borg_version=flexmock(),
  902. repository={'path': 'repo'},
  903. )
  904. )
  905. assert result == (expected,)
  906. def test_run_actions_with_skip_actions_skips_create():
  907. flexmock(module).should_receive('add_custom_log_levels')
  908. flexmock(module).should_receive('get_skip_actions').and_return(['create'])
  909. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  910. flexmock(borgmatic.actions.create).should_receive('run_create').never()
  911. tuple(
  912. module.run_actions(
  913. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  914. config_filename=flexmock(),
  915. config={'repositories': [], 'skip_actions': ['create']},
  916. config_paths=[],
  917. local_path=flexmock(),
  918. remote_path=flexmock(),
  919. local_borg_version=flexmock(),
  920. repository={'path': 'repo'},
  921. )
  922. )
  923. def test_run_actions_runs_recreate():
  924. flexmock(module).should_receive('add_custom_log_levels')
  925. flexmock(module).should_receive('get_skip_actions').and_return([])
  926. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  927. flexmock(borgmatic.actions.recreate).should_receive('run_recreate').once()
  928. tuple(
  929. module.run_actions(
  930. arguments={'global': flexmock(dry_run=False), 'recreate': flexmock()},
  931. config_filename=flexmock(),
  932. config={'repositories': []},
  933. config_paths=[],
  934. local_path=flexmock(),
  935. remote_path=flexmock(),
  936. local_borg_version=flexmock(),
  937. repository={'path': 'repo'},
  938. )
  939. )
  940. def test_run_actions_with_skip_actions_skips_recreate():
  941. flexmock(module).should_receive('add_custom_log_levels')
  942. flexmock(module).should_receive('get_skip_actions').and_return(['recreate'])
  943. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  944. flexmock(borgmatic.actions.recreate).should_receive('run_recreate').never()
  945. tuple(
  946. module.run_actions(
  947. arguments={'global': flexmock(dry_run=False), 'recreate': flexmock()},
  948. config_filename=flexmock(),
  949. config={'repositories': [], 'skip_actions': ['recreate']},
  950. config_paths=[],
  951. local_path=flexmock(),
  952. remote_path=flexmock(),
  953. local_borg_version=flexmock(),
  954. repository={'path': 'repo'},
  955. )
  956. )
  957. def test_run_actions_runs_prune():
  958. flexmock(module).should_receive('add_custom_log_levels')
  959. flexmock(module).should_receive('get_skip_actions').and_return([])
  960. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  961. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  962. tuple(
  963. module.run_actions(
  964. arguments={'global': flexmock(dry_run=False), 'prune': flexmock()},
  965. config_filename=flexmock(),
  966. config={'repositories': []},
  967. config_paths=[],
  968. local_path=flexmock(),
  969. remote_path=flexmock(),
  970. local_borg_version=flexmock(),
  971. repository={'path': 'repo'},
  972. )
  973. )
  974. def test_run_actions_with_skip_actions_skips_prune():
  975. flexmock(module).should_receive('add_custom_log_levels')
  976. flexmock(module).should_receive('get_skip_actions').and_return(['prune'])
  977. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  978. flexmock(borgmatic.actions.prune).should_receive('run_prune').never()
  979. tuple(
  980. module.run_actions(
  981. arguments={'global': flexmock(dry_run=False), 'prune': flexmock()},
  982. config_filename=flexmock(),
  983. config={'repositories': [], 'skip_actions': ['prune']},
  984. config_paths=[],
  985. local_path=flexmock(),
  986. remote_path=flexmock(),
  987. local_borg_version=flexmock(),
  988. repository={'path': 'repo'},
  989. )
  990. )
  991. def test_run_actions_runs_compact():
  992. flexmock(module).should_receive('add_custom_log_levels')
  993. flexmock(module).should_receive('get_skip_actions').and_return([])
  994. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  995. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  996. tuple(
  997. module.run_actions(
  998. arguments={'global': flexmock(dry_run=False), 'compact': flexmock()},
  999. config_filename=flexmock(),
  1000. config={'repositories': []},
  1001. config_paths=[],
  1002. local_path=flexmock(),
  1003. remote_path=flexmock(),
  1004. local_borg_version=flexmock(),
  1005. repository={'path': 'repo'},
  1006. )
  1007. )
  1008. def test_run_actions_with_skip_actions_skips_compact():
  1009. flexmock(module).should_receive('add_custom_log_levels')
  1010. flexmock(module).should_receive('get_skip_actions').and_return(['compact'])
  1011. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1012. flexmock(borgmatic.actions.compact).should_receive('run_compact').never()
  1013. tuple(
  1014. module.run_actions(
  1015. arguments={'global': flexmock(dry_run=False), 'compact': flexmock()},
  1016. config_filename=flexmock(),
  1017. config={'repositories': [], 'skip_actions': ['compact']},
  1018. config_paths=[],
  1019. local_path=flexmock(),
  1020. remote_path=flexmock(),
  1021. local_borg_version=flexmock(),
  1022. repository={'path': 'repo'},
  1023. )
  1024. )
  1025. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  1026. flexmock(module).should_receive('add_custom_log_levels')
  1027. flexmock(module).should_receive('get_skip_actions').and_return([])
  1028. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1029. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  1030. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  1031. tuple(
  1032. module.run_actions(
  1033. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  1034. config_filename=flexmock(),
  1035. config={'repositories': []},
  1036. config_paths=[],
  1037. local_path=flexmock(),
  1038. remote_path=flexmock(),
  1039. local_borg_version=flexmock(),
  1040. repository={'path': 'repo'},
  1041. )
  1042. )
  1043. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  1044. flexmock(module).should_receive('add_custom_log_levels')
  1045. flexmock(module).should_receive('get_skip_actions').and_return([])
  1046. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1047. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  1048. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  1049. tuple(
  1050. module.run_actions(
  1051. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  1052. config_filename=flexmock(),
  1053. config={'repositories': []},
  1054. config_paths=[],
  1055. local_path=flexmock(),
  1056. remote_path=flexmock(),
  1057. local_borg_version=flexmock(),
  1058. repository={'path': 'repo'},
  1059. )
  1060. )
  1061. def test_run_actions_with_skip_actions_skips_check():
  1062. flexmock(module).should_receive('add_custom_log_levels')
  1063. flexmock(module).should_receive('get_skip_actions').and_return(['check'])
  1064. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1065. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  1066. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  1067. tuple(
  1068. module.run_actions(
  1069. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  1070. config_filename=flexmock(),
  1071. config={'repositories': [], 'skip_actions': ['check']},
  1072. config_paths=[],
  1073. local_path=flexmock(),
  1074. remote_path=flexmock(),
  1075. local_borg_version=flexmock(),
  1076. repository={'path': 'repo'},
  1077. )
  1078. )
  1079. def test_run_actions_runs_extract():
  1080. flexmock(module).should_receive('add_custom_log_levels')
  1081. flexmock(module).should_receive('get_skip_actions').and_return([])
  1082. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1083. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  1084. tuple(
  1085. module.run_actions(
  1086. arguments={'global': flexmock(dry_run=False), 'extract': flexmock()},
  1087. config_filename=flexmock(),
  1088. config={'repositories': []},
  1089. config_paths=[],
  1090. local_path=flexmock(),
  1091. remote_path=flexmock(),
  1092. local_borg_version=flexmock(),
  1093. repository={'path': 'repo'},
  1094. )
  1095. )
  1096. def test_run_actions_runs_export_tar():
  1097. flexmock(module).should_receive('add_custom_log_levels')
  1098. flexmock(module).should_receive('get_skip_actions').and_return([])
  1099. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1100. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  1101. tuple(
  1102. module.run_actions(
  1103. arguments={'global': flexmock(dry_run=False), 'export-tar': flexmock()},
  1104. config_filename=flexmock(),
  1105. config={'repositories': []},
  1106. config_paths=[],
  1107. local_path=flexmock(),
  1108. remote_path=flexmock(),
  1109. local_borg_version=flexmock(),
  1110. repository={'path': 'repo'},
  1111. )
  1112. )
  1113. def test_run_actions_runs_mount():
  1114. flexmock(module).should_receive('add_custom_log_levels')
  1115. flexmock(module).should_receive('get_skip_actions').and_return([])
  1116. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1117. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  1118. tuple(
  1119. module.run_actions(
  1120. arguments={'global': flexmock(dry_run=False), 'mount': flexmock()},
  1121. config_filename=flexmock(),
  1122. config={'repositories': []},
  1123. config_paths=[],
  1124. local_path=flexmock(),
  1125. remote_path=flexmock(),
  1126. local_borg_version=flexmock(),
  1127. repository={'path': 'repo'},
  1128. )
  1129. )
  1130. def test_run_actions_runs_restore():
  1131. flexmock(module).should_receive('add_custom_log_levels')
  1132. flexmock(module).should_receive('get_skip_actions').and_return([])
  1133. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1134. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  1135. tuple(
  1136. module.run_actions(
  1137. arguments={'global': flexmock(dry_run=False), 'restore': flexmock()},
  1138. config_filename=flexmock(),
  1139. config={'repositories': []},
  1140. config_paths=[],
  1141. local_path=flexmock(),
  1142. remote_path=flexmock(),
  1143. local_borg_version=flexmock(),
  1144. repository={'path': 'repo'},
  1145. )
  1146. )
  1147. def test_run_actions_runs_repo_list():
  1148. flexmock(module).should_receive('add_custom_log_levels')
  1149. flexmock(module).should_receive('get_skip_actions').and_return([])
  1150. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1151. expected = flexmock()
  1152. flexmock(borgmatic.actions.repo_list).should_receive('run_repo_list').and_yield(expected).once()
  1153. result = tuple(
  1154. module.run_actions(
  1155. arguments={'global': flexmock(dry_run=False), 'repo-list': flexmock()},
  1156. config_filename=flexmock(),
  1157. config={'repositories': []},
  1158. config_paths=[],
  1159. local_path=flexmock(),
  1160. remote_path=flexmock(),
  1161. local_borg_version=flexmock(),
  1162. repository={'path': 'repo'},
  1163. )
  1164. )
  1165. assert result == (expected,)
  1166. def test_run_actions_runs_list():
  1167. flexmock(module).should_receive('add_custom_log_levels')
  1168. flexmock(module).should_receive('get_skip_actions').and_return([])
  1169. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1170. expected = flexmock()
  1171. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  1172. result = tuple(
  1173. module.run_actions(
  1174. arguments={'global': flexmock(dry_run=False), 'list': flexmock()},
  1175. config_filename=flexmock(),
  1176. config={'repositories': []},
  1177. config_paths=[],
  1178. local_path=flexmock(),
  1179. remote_path=flexmock(),
  1180. local_borg_version=flexmock(),
  1181. repository={'path': 'repo'},
  1182. )
  1183. )
  1184. assert result == (expected,)
  1185. def test_run_actions_runs_repo_info():
  1186. flexmock(module).should_receive('add_custom_log_levels')
  1187. flexmock(module).should_receive('get_skip_actions').and_return([])
  1188. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1189. expected = flexmock()
  1190. flexmock(borgmatic.actions.repo_info).should_receive('run_repo_info').and_yield(expected).once()
  1191. result = tuple(
  1192. module.run_actions(
  1193. arguments={'global': flexmock(dry_run=False), 'repo-info': flexmock()},
  1194. config_filename=flexmock(),
  1195. config={'repositories': []},
  1196. config_paths=[],
  1197. local_path=flexmock(),
  1198. remote_path=flexmock(),
  1199. local_borg_version=flexmock(),
  1200. repository={'path': 'repo'},
  1201. )
  1202. )
  1203. assert result == (expected,)
  1204. def test_run_actions_runs_info():
  1205. flexmock(module).should_receive('add_custom_log_levels')
  1206. flexmock(module).should_receive('get_skip_actions').and_return([])
  1207. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1208. expected = flexmock()
  1209. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  1210. result = tuple(
  1211. module.run_actions(
  1212. arguments={'global': flexmock(dry_run=False), 'info': flexmock()},
  1213. config_filename=flexmock(),
  1214. config={'repositories': []},
  1215. config_paths=[],
  1216. local_path=flexmock(),
  1217. remote_path=flexmock(),
  1218. local_borg_version=flexmock(),
  1219. repository={'path': 'repo'},
  1220. )
  1221. )
  1222. assert result == (expected,)
  1223. def test_run_actions_runs_break_lock():
  1224. flexmock(module).should_receive('add_custom_log_levels')
  1225. flexmock(module).should_receive('get_skip_actions').and_return([])
  1226. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1227. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  1228. tuple(
  1229. module.run_actions(
  1230. arguments={'global': flexmock(dry_run=False), 'break-lock': flexmock()},
  1231. config_filename=flexmock(),
  1232. config={'repositories': []},
  1233. config_paths=[],
  1234. local_path=flexmock(),
  1235. remote_path=flexmock(),
  1236. local_borg_version=flexmock(),
  1237. repository={'path': 'repo'},
  1238. )
  1239. )
  1240. def test_run_actions_runs_export_key():
  1241. flexmock(module).should_receive('add_custom_log_levels')
  1242. flexmock(module).should_receive('get_skip_actions').and_return([])
  1243. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1244. flexmock(borgmatic.actions.export_key).should_receive('run_export_key').once()
  1245. tuple(
  1246. module.run_actions(
  1247. arguments={'global': flexmock(dry_run=False), 'export': flexmock()},
  1248. config_filename=flexmock(),
  1249. config={'repositories': []},
  1250. config_paths=[],
  1251. local_path=flexmock(),
  1252. remote_path=flexmock(),
  1253. local_borg_version=flexmock(),
  1254. repository={'path': 'repo'},
  1255. )
  1256. )
  1257. def test_run_actions_runs_import_key():
  1258. flexmock(module).should_receive('add_custom_log_levels')
  1259. flexmock(module).should_receive('get_skip_actions').and_return([])
  1260. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1261. flexmock(borgmatic.actions.import_key).should_receive('run_import_key').once()
  1262. tuple(
  1263. module.run_actions(
  1264. arguments={'global': flexmock(dry_run=False), 'import': flexmock()},
  1265. config_filename=flexmock(),
  1266. config={'repositories': []},
  1267. config_paths=[],
  1268. local_path=flexmock(),
  1269. remote_path=flexmock(),
  1270. local_borg_version=flexmock(),
  1271. repository={'path': 'repo'},
  1272. )
  1273. )
  1274. def test_run_actions_runs_change_passphrase():
  1275. flexmock(module).should_receive('add_custom_log_levels')
  1276. flexmock(module).should_receive('get_skip_actions').and_return([])
  1277. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1278. flexmock(borgmatic.actions.change_passphrase).should_receive('run_change_passphrase').once()
  1279. tuple(
  1280. module.run_actions(
  1281. arguments={
  1282. 'global': flexmock(dry_run=False),
  1283. 'change-passphrase': flexmock(),
  1284. },
  1285. config_filename=flexmock(),
  1286. config={'repositories': []},
  1287. config_paths=[],
  1288. local_path=flexmock(),
  1289. remote_path=flexmock(),
  1290. local_borg_version=flexmock(),
  1291. repository={'path': 'repo'},
  1292. )
  1293. )
  1294. def test_run_actions_runs_delete():
  1295. flexmock(module).should_receive('add_custom_log_levels')
  1296. flexmock(module).should_receive('get_skip_actions').and_return([])
  1297. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1298. flexmock(borgmatic.actions.delete).should_receive('run_delete').once()
  1299. tuple(
  1300. module.run_actions(
  1301. arguments={'global': flexmock(dry_run=False), 'delete': flexmock()},
  1302. config_filename=flexmock(),
  1303. config={'repositories': []},
  1304. config_paths=[],
  1305. local_path=flexmock(),
  1306. remote_path=flexmock(),
  1307. local_borg_version=flexmock(),
  1308. repository={'path': 'repo'},
  1309. )
  1310. )
  1311. def test_run_actions_runs_repo_delete():
  1312. flexmock(module).should_receive('add_custom_log_levels')
  1313. flexmock(module).should_receive('get_skip_actions').and_return([])
  1314. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1315. flexmock(borgmatic.actions.repo_delete).should_receive('run_repo_delete').once()
  1316. tuple(
  1317. module.run_actions(
  1318. arguments={
  1319. 'global': flexmock(dry_run=False),
  1320. 'repo-delete': flexmock(),
  1321. },
  1322. config_filename=flexmock(),
  1323. config={'repositories': []},
  1324. config_paths=[],
  1325. local_path=flexmock(),
  1326. remote_path=flexmock(),
  1327. local_borg_version=flexmock(),
  1328. repository={'path': 'repo'},
  1329. )
  1330. )
  1331. def test_run_actions_runs_borg():
  1332. flexmock(module).should_receive('add_custom_log_levels')
  1333. flexmock(module).should_receive('get_skip_actions').and_return([])
  1334. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1335. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  1336. tuple(
  1337. module.run_actions(
  1338. arguments={'global': flexmock(dry_run=False), 'borg': flexmock()},
  1339. config_filename=flexmock(),
  1340. config={'repositories': []},
  1341. config_paths=[],
  1342. local_path=flexmock(),
  1343. remote_path=flexmock(),
  1344. local_borg_version=flexmock(),
  1345. repository={'path': 'repo'},
  1346. )
  1347. )
  1348. def test_run_actions_runs_multiple_actions_in_argument_order():
  1349. flexmock(module).should_receive('add_custom_log_levels')
  1350. flexmock(module).should_receive('get_skip_actions').and_return([])
  1351. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1352. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  1353. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  1354. tuple(
  1355. module.run_actions(
  1356. arguments={
  1357. 'global': flexmock(dry_run=False),
  1358. 'borg': flexmock(),
  1359. 'restore': flexmock(),
  1360. },
  1361. config_filename=flexmock(),
  1362. config={'repositories': []},
  1363. config_paths=[],
  1364. local_path=flexmock(),
  1365. remote_path=flexmock(),
  1366. local_borg_version=flexmock(),
  1367. repository={'path': 'repo'},
  1368. )
  1369. )
  1370. @pytest.mark.parametrize(
  1371. 'resolve_env',
  1372. ((True, False),),
  1373. )
  1374. def test_load_configurations_collects_parsed_configurations_and_logs(resolve_env):
  1375. configuration = flexmock()
  1376. other_configuration = flexmock()
  1377. test_expected_logs = [flexmock(), flexmock()]
  1378. other_expected_logs = [flexmock(), flexmock()]
  1379. flexmock(module.validate).should_receive('parse_configuration').and_return(
  1380. configuration, ['/tmp/test.yaml'], test_expected_logs
  1381. ).and_return(other_configuration, ['/tmp/other.yaml'], other_expected_logs)
  1382. configs, config_paths, logs = tuple(
  1383. module.load_configurations(
  1384. ('test.yaml', 'other.yaml'),
  1385. arguments=flexmock(),
  1386. resolve_env=resolve_env,
  1387. )
  1388. )
  1389. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  1390. assert config_paths == ['/tmp/other.yaml', '/tmp/test.yaml']
  1391. assert set(logs) >= set(test_expected_logs + other_expected_logs)
  1392. def test_load_configurations_logs_warning_for_permission_error():
  1393. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  1394. configs, config_paths, logs = tuple(
  1395. module.load_configurations(('test.yaml',), arguments=flexmock())
  1396. )
  1397. assert configs == {}
  1398. assert config_paths == []
  1399. assert max(log.levelno for log in logs) == logging.WARNING
  1400. def test_load_configurations_logs_critical_for_parse_error():
  1401. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  1402. configs, config_paths, logs = tuple(
  1403. module.load_configurations(('test.yaml',), arguments=flexmock())
  1404. )
  1405. assert configs == {}
  1406. assert config_paths == []
  1407. assert max(log.levelno for log in logs) == logging.CRITICAL
  1408. def test_log_record_does_not_raise():
  1409. module.log_record(levelno=1, foo='bar', baz='quux')
  1410. def test_log_record_with_suppress_does_not_raise():
  1411. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  1412. def test_log_error_records_generates_output_logs_for_message_only():
  1413. flexmock(module).should_receive('log_record').replace_with(dict).once()
  1414. logs = tuple(module.log_error_records('Error'))
  1415. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1416. def test_log_error_records_generates_output_logs_for_called_process_error_with_bytes_ouput():
  1417. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  1418. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1419. logs = tuple(
  1420. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', b'error output'))
  1421. )
  1422. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1423. assert any(log for log in logs if 'error output' in str(log))
  1424. def test_log_error_records_generates_output_logs_for_called_process_error_with_string_ouput():
  1425. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  1426. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1427. logs = tuple(
  1428. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  1429. )
  1430. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1431. assert any(log for log in logs if 'error output' in str(log))
  1432. def test_log_error_records_generates_work_around_output_logs_for_called_process_error_with_repository_access_aborted_exit_code():
  1433. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  1434. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1435. logs = tuple(
  1436. module.log_error_records(
  1437. 'Error',
  1438. subprocess.CalledProcessError(
  1439. module.BORG_REPOSITORY_ACCESS_ABORTED_EXIT_CODE, 'ls', 'error output'
  1440. ),
  1441. )
  1442. )
  1443. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1444. assert any(log for log in logs if 'error output' in str(log))
  1445. assert any(log for log in logs if 'To work around this' in str(log))
  1446. def test_log_error_records_splits_called_process_error_with_multiline_ouput_into_multiple_logs():
  1447. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  1448. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1449. logs = tuple(
  1450. module.log_error_records(
  1451. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output\nanother line')
  1452. )
  1453. )
  1454. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1455. assert any(log for log in logs if 'error output' in str(log))
  1456. def test_log_error_records_generates_logs_for_value_error():
  1457. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  1458. logs = tuple(module.log_error_records('Error', ValueError()))
  1459. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1460. def test_log_error_records_generates_logs_for_os_error():
  1461. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  1462. logs = tuple(module.log_error_records('Error', OSError()))
  1463. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1464. def test_log_error_records_generates_nothing_for_other_error():
  1465. flexmock(module).should_receive('log_record').never()
  1466. logs = tuple(module.log_error_records('Error', KeyError()))
  1467. assert logs == ()
  1468. def test_get_local_path_uses_configuration_value():
  1469. assert module.get_local_path({'test.yaml': {'local_path': 'borg1'}}) == 'borg1'
  1470. def test_get_local_path_without_local_path_defaults_to_borg():
  1471. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  1472. def test_collect_highlander_action_summary_logs_info_for_success_with_bootstrap():
  1473. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1474. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  1475. arguments = {
  1476. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1477. 'global': flexmock(dry_run=False),
  1478. }
  1479. logs = tuple(
  1480. module.collect_highlander_action_summary_logs(
  1481. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1482. )
  1483. )
  1484. assert {log.levelno for log in logs} == {logging.ANSWER}
  1485. def test_collect_highlander_action_summary_logs_error_on_bootstrap_failure():
  1486. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1487. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  1488. ValueError
  1489. )
  1490. arguments = {
  1491. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1492. 'global': flexmock(dry_run=False),
  1493. }
  1494. logs = tuple(
  1495. module.collect_highlander_action_summary_logs(
  1496. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1497. )
  1498. )
  1499. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1500. def test_collect_highlander_action_summary_logs_error_on_bootstrap_local_borg_version_failure():
  1501. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  1502. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').never()
  1503. arguments = {
  1504. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1505. 'global': flexmock(dry_run=False),
  1506. }
  1507. logs = tuple(
  1508. module.collect_highlander_action_summary_logs(
  1509. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1510. )
  1511. )
  1512. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1513. def test_collect_highlander_action_summary_logs_info_for_success_with_generate():
  1514. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate')
  1515. arguments = {
  1516. 'generate': flexmock(destination='test.yaml'),
  1517. 'global': flexmock(dry_run=False),
  1518. }
  1519. logs = tuple(
  1520. module.collect_highlander_action_summary_logs(
  1521. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1522. )
  1523. )
  1524. assert {log.levelno for log in logs} == {logging.ANSWER}
  1525. def test_collect_highlander_action_summary_logs_error_on_generate_failure():
  1526. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate').and_raise(
  1527. ValueError
  1528. )
  1529. arguments = {
  1530. 'generate': flexmock(destination='test.yaml'),
  1531. 'global': flexmock(dry_run=False),
  1532. }
  1533. logs = tuple(
  1534. module.collect_highlander_action_summary_logs(
  1535. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1536. )
  1537. )
  1538. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1539. def test_collect_highlander_action_summary_logs_info_for_success_with_validate():
  1540. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1541. arguments = {
  1542. 'validate': flexmock(),
  1543. 'global': flexmock(dry_run=False),
  1544. }
  1545. logs = tuple(
  1546. module.collect_highlander_action_summary_logs(
  1547. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1548. )
  1549. )
  1550. assert {log.levelno for log in logs} == {logging.ANSWER}
  1551. def test_collect_highlander_action_summary_logs_error_on_validate_parse_failure():
  1552. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1553. arguments = {
  1554. 'validate': flexmock(),
  1555. 'global': flexmock(dry_run=False),
  1556. }
  1557. logs = tuple(
  1558. module.collect_highlander_action_summary_logs(
  1559. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=True
  1560. )
  1561. )
  1562. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1563. def test_collect_highlander_action_summary_logs_error_on_run_validate_failure():
  1564. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate').and_raise(
  1565. ValueError
  1566. )
  1567. arguments = {
  1568. 'validate': flexmock(),
  1569. 'global': flexmock(dry_run=False),
  1570. }
  1571. logs = tuple(
  1572. module.collect_highlander_action_summary_logs(
  1573. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1574. )
  1575. )
  1576. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1577. def test_collect_configuration_run_summary_logs_info_for_success():
  1578. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1579. flexmock(module.command).should_receive('filter_hooks')
  1580. flexmock(module.command).should_receive('execute_hooks')
  1581. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1582. flexmock(module).should_receive('run_configuration').and_return([])
  1583. arguments = {'global': flexmock(dry_run=False)}
  1584. logs = tuple(
  1585. module.collect_configuration_run_summary_logs(
  1586. {'test.yaml': {}},
  1587. config_paths=['/tmp/test.yaml'],
  1588. arguments=arguments,
  1589. log_file_path=None,
  1590. )
  1591. )
  1592. assert {log.levelno for log in logs} == {logging.INFO}
  1593. def test_collect_configuration_run_summary_executes_hooks_for_create():
  1594. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1595. flexmock(module.command).should_receive('filter_hooks')
  1596. flexmock(module.command).should_receive('execute_hooks')
  1597. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1598. flexmock(module).should_receive('run_configuration').and_return([])
  1599. arguments = {
  1600. 'create': flexmock(),
  1601. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  1602. }
  1603. logs = tuple(
  1604. module.collect_configuration_run_summary_logs(
  1605. {'test.yaml': {}},
  1606. config_paths=['/tmp/test.yaml'],
  1607. arguments=arguments,
  1608. log_file_path=None,
  1609. )
  1610. )
  1611. assert {log.levelno for log in logs} == {logging.INFO}
  1612. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  1613. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1614. flexmock(module.command).should_receive('filter_hooks')
  1615. flexmock(module.command).should_receive('execute_hooks')
  1616. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1617. flexmock(module).should_receive('run_configuration').and_return([])
  1618. arguments = {
  1619. 'extract': flexmock(repository='repo'),
  1620. 'global': flexmock(dry_run=False),
  1621. }
  1622. logs = tuple(
  1623. module.collect_configuration_run_summary_logs(
  1624. {'test.yaml': {}},
  1625. config_paths=['/tmp/test.yaml'],
  1626. arguments=arguments,
  1627. log_file_path=None,
  1628. )
  1629. )
  1630. assert {log.levelno for log in logs} == {logging.INFO}
  1631. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  1632. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1633. ValueError
  1634. )
  1635. expected_logs = (flexmock(),)
  1636. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1637. arguments = {'extract': flexmock(repository='repo')}
  1638. logs = tuple(
  1639. module.collect_configuration_run_summary_logs(
  1640. {'test.yaml': {}},
  1641. config_paths=['/tmp/test.yaml'],
  1642. arguments=arguments,
  1643. log_file_path=None,
  1644. )
  1645. )
  1646. assert logs == expected_logs
  1647. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  1648. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1649. flexmock(module.command).should_receive('filter_hooks')
  1650. flexmock(module.command).should_receive('execute_hooks')
  1651. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1652. flexmock(module).should_receive('run_configuration').and_return([])
  1653. arguments = {
  1654. 'mount': flexmock(repository='repo'),
  1655. 'global': flexmock(dry_run=False),
  1656. }
  1657. logs = tuple(
  1658. module.collect_configuration_run_summary_logs(
  1659. {'test.yaml': {}},
  1660. config_paths=['/tmp/test.yaml'],
  1661. arguments=arguments,
  1662. log_file_path=None,
  1663. )
  1664. )
  1665. assert {log.levelno for log in logs} == {logging.INFO}
  1666. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  1667. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1668. ValueError
  1669. )
  1670. expected_logs = (flexmock(),)
  1671. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1672. arguments = {
  1673. 'mount': flexmock(repository='repo'),
  1674. 'global': flexmock(dry_run=False),
  1675. }
  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 logs == expected_logs
  1685. def test_collect_configuration_run_summary_logs_missing_configs_error():
  1686. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1687. flexmock(module.command).should_receive('filter_hooks')
  1688. flexmock(module.command).should_receive('execute_hooks')
  1689. arguments = {'global': flexmock(config_paths=[])}
  1690. expected_logs = (flexmock(),)
  1691. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1692. logs = tuple(
  1693. module.collect_configuration_run_summary_logs(
  1694. {}, config_paths=[], arguments=arguments, log_file_path=None
  1695. )
  1696. )
  1697. assert logs == expected_logs
  1698. def test_collect_configuration_run_summary_logs_pre_hook_error():
  1699. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1700. flexmock(module.command).should_receive('filter_hooks')
  1701. flexmock(module.command).should_receive('execute_hooks').and_raise(ValueError)
  1702. expected_logs = (flexmock(),)
  1703. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1704. arguments = {
  1705. 'create': flexmock(),
  1706. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  1707. }
  1708. logs = tuple(
  1709. module.collect_configuration_run_summary_logs(
  1710. {'test.yaml': {}},
  1711. config_paths=['/tmp/test.yaml'],
  1712. arguments=arguments,
  1713. log_file_path=None,
  1714. )
  1715. )
  1716. assert logs == expected_logs
  1717. def test_collect_configuration_run_summary_logs_post_hook_error():
  1718. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1719. flexmock(module.command).should_receive('filter_hooks')
  1720. flexmock(module.command).should_receive('execute_hooks').and_return(None).and_raise(ValueError)
  1721. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1722. flexmock(module).should_receive('run_configuration').and_return([])
  1723. expected_logs = (flexmock(),)
  1724. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1725. arguments = {
  1726. 'create': flexmock(),
  1727. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  1728. }
  1729. logs = tuple(
  1730. module.collect_configuration_run_summary_logs(
  1731. {'test.yaml': {}},
  1732. config_paths=['/tmp/test.yaml'],
  1733. arguments=arguments,
  1734. log_file_path=None,
  1735. )
  1736. )
  1737. assert expected_logs[0] in logs
  1738. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  1739. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1740. ValueError
  1741. )
  1742. expected_logs = (flexmock(),)
  1743. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1744. arguments = {
  1745. 'list': flexmock(repository='repo', archive='test'),
  1746. 'global': flexmock(dry_run=False),
  1747. }
  1748. logs = tuple(
  1749. module.collect_configuration_run_summary_logs(
  1750. {'test.yaml': {}},
  1751. config_paths=['/tmp/test.yaml'],
  1752. arguments=arguments,
  1753. log_file_path=None,
  1754. )
  1755. )
  1756. assert logs == expected_logs
  1757. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  1758. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1759. flexmock(module.command).should_receive('filter_hooks')
  1760. flexmock(module.command).should_receive('execute_hooks')
  1761. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1762. flexmock(module).should_receive('run_configuration').and_return([])
  1763. arguments = {
  1764. 'list': flexmock(repository='repo', archive=None),
  1765. 'global': flexmock(dry_run=False),
  1766. }
  1767. logs = tuple(
  1768. module.collect_configuration_run_summary_logs(
  1769. {'test.yaml': {}},
  1770. config_paths=['/tmp/test.yaml'],
  1771. arguments=arguments,
  1772. log_file_path=None,
  1773. )
  1774. )
  1775. assert {log.levelno for log in logs} == {logging.INFO}
  1776. def test_collect_configuration_run_summary_logs_run_configuration_error_logs():
  1777. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1778. flexmock(module.command).should_receive('filter_hooks')
  1779. flexmock(module.command).should_receive('execute_hooks')
  1780. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1781. flexmock(module).should_receive('run_configuration').and_return(
  1782. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1783. )
  1784. flexmock(module).should_receive('log_error_records').and_return([])
  1785. arguments = {'global': flexmock(dry_run=False)}
  1786. logs = tuple(
  1787. module.collect_configuration_run_summary_logs(
  1788. {'test.yaml': {}},
  1789. config_paths=['/tmp/test.yaml'],
  1790. arguments=arguments,
  1791. log_file_path=None,
  1792. )
  1793. )
  1794. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1795. def test_collect_configuration_run_summary_logs_run_umount_error():
  1796. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1797. flexmock(module.command).should_receive('filter_hooks')
  1798. flexmock(module.command).should_receive('execute_hooks')
  1799. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1800. flexmock(module).should_receive('run_configuration').and_return([])
  1801. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  1802. flexmock(module).should_receive('log_error_records').and_return(
  1803. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1804. )
  1805. arguments = {
  1806. 'umount': flexmock(mount_point='/mnt'),
  1807. 'global': flexmock(dry_run=False),
  1808. }
  1809. logs = tuple(
  1810. module.collect_configuration_run_summary_logs(
  1811. {'test.yaml': {}},
  1812. config_paths=['/tmp/test.yaml'],
  1813. arguments=arguments,
  1814. log_file_path=None,
  1815. )
  1816. )
  1817. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  1818. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  1819. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1820. flexmock(module.command).should_receive('filter_hooks')
  1821. flexmock(module.command).should_receive('execute_hooks')
  1822. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1823. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  1824. ['baz']
  1825. )
  1826. stdout = flexmock()
  1827. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  1828. flexmock(module.sys).stdout = stdout
  1829. arguments = {'global': flexmock(dry_run=False)}
  1830. tuple(
  1831. module.collect_configuration_run_summary_logs(
  1832. {'test.yaml': {}, 'test2.yaml': {}},
  1833. config_paths=['/tmp/test.yaml', '/tmp/test2.yaml'],
  1834. arguments=arguments,
  1835. log_file_path=None,
  1836. )
  1837. )
  1838. def test_check_and_show_help_on_no_args_shows_help_when_no_args_and_default_actions_false():
  1839. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1840. flexmock(module).should_receive('parse_arguments').with_args('--help').once()
  1841. flexmock(module.sys).should_receive('exit').with_args(0).once()
  1842. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}})
  1843. def test_check_and_show_help_on_no_args_does_not_show_help_when_no_args_and_default_actions_true():
  1844. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1845. flexmock(module).should_receive('parse_arguments').never()
  1846. flexmock(module.sys).should_receive('exit').never()
  1847. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': True}})
  1848. def test_check_and_show_help_on_no_args_does_not_show_help_when_args_provided():
  1849. flexmock(module.sys).should_receive('argv').and_return(['borgmatic', '--create'])
  1850. flexmock(module).should_receive('parse_arguments').never()
  1851. flexmock(module.sys).should_receive('exit').never()
  1852. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}})
  1853. def test_check_and_show_help_on_no_args_with_no_default_actions_in_all_configs():
  1854. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1855. # Both configs have default_actions set to False, so help should be shown
  1856. configs = {
  1857. 'config1.yaml': {'default_actions': False},
  1858. 'config2.yaml': {'default_actions': False},
  1859. }
  1860. # Expect help to be shown
  1861. flexmock(module).should_receive('parse_arguments').with_args('--help').once()
  1862. flexmock(module.sys).should_receive('exit').with_args(0).once()
  1863. module.check_and_show_help_on_no_args(configs)
  1864. def test_check_and_show_help_on_no_args_with_conflicting_configs():
  1865. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1866. # Simulate two config files with conflicting 'default_actions' values
  1867. configs = {
  1868. 'config1.yaml': {'default_actions': True},
  1869. 'config2.yaml': {'default_actions': False},
  1870. }
  1871. # Expect help not to be shown because at least one config enables default actions
  1872. flexmock(module).should_receive('parse_arguments').never()
  1873. flexmock(module.sys).should_receive('exit').never()
  1874. module.check_and_show_help_on_no_args(configs)
  1875. def test_get_singular_option_value_with_conflicting_values_exits():
  1876. flexmock(module).should_receive('configure_logging')
  1877. flexmock(module).should_receive('exit_with_help_link').once()
  1878. module.get_singular_option_value(
  1879. configs={
  1880. 'test1.yaml': {'foo': 1, 'bar': 'baz'},
  1881. 'test2.yaml': {'foo': 2, 'bar': 'baz'},
  1882. },
  1883. option_name='foo',
  1884. )
  1885. def test_get_singular_option_value_with_same_value_returns_it():
  1886. flexmock(module).should_receive('configure_logging').never()
  1887. flexmock(module).should_receive('exit_with_help_link').never()
  1888. assert (
  1889. module.get_singular_option_value(
  1890. configs={
  1891. 'test1.yaml': {'foo': 1, 'bar': 'baz'},
  1892. 'test2.yaml': {'foo': 1, 'bar': 'baz'},
  1893. },
  1894. option_name='foo',
  1895. )
  1896. == 1
  1897. )
  1898. def test_get_singular_option_value_with_no_values_returns_none():
  1899. flexmock(module).should_receive('configure_logging').never()
  1900. flexmock(module).should_receive('exit_with_help_link').never()
  1901. assert (
  1902. module.get_singular_option_value(
  1903. configs={
  1904. 'test1.yaml': {'bar': 'baz'},
  1905. 'test2.yaml': {'bar': 'baz'},
  1906. },
  1907. option_name='foo',
  1908. )
  1909. is None
  1910. )
  1911. def test_get_singular_option_value_with_no_config_returns_none():
  1912. flexmock(module).should_receive('configure_logging').never()
  1913. flexmock(module).should_receive('exit_with_help_link').never()
  1914. assert (
  1915. module.get_singular_option_value(
  1916. configs={},
  1917. option_name='foo',
  1918. )
  1919. is None
  1920. )