test_borgmatic.py 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334
  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.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  794. flexmock()
  795. )
  796. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  797. flexmock(borgmatic.actions.repo_create).should_receive('run_repo_create').once()
  798. tuple(
  799. module.run_actions(
  800. arguments={
  801. 'global': flexmock(dry_run=False),
  802. 'repo-create': flexmock(),
  803. },
  804. config_filename=flexmock(),
  805. config={'repositories': []},
  806. config_paths=[],
  807. local_path=flexmock(),
  808. remote_path=flexmock(),
  809. local_borg_version=flexmock(),
  810. repository={'path': 'repo'},
  811. )
  812. )
  813. def test_run_actions_adds_label_file_to_hook_context():
  814. flexmock(module).should_receive('add_custom_log_levels')
  815. flexmock(module).should_receive('get_skip_actions').and_return([])
  816. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  817. flexmock()
  818. )
  819. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  820. expected = flexmock()
  821. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  822. config_filename=object,
  823. repository={'path': 'repo', 'label': 'my repo'},
  824. config={'repositories': []},
  825. config_paths=[],
  826. local_borg_version=object,
  827. create_arguments=object,
  828. global_arguments=object,
  829. dry_run_label='',
  830. local_path=object,
  831. remote_path=object,
  832. ).once().and_return(expected)
  833. result = tuple(
  834. module.run_actions(
  835. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  836. config_filename=flexmock(),
  837. config={'repositories': []},
  838. config_paths=[],
  839. local_path=flexmock(),
  840. remote_path=flexmock(),
  841. local_borg_version=flexmock(),
  842. repository={'path': 'repo', 'label': 'my repo'},
  843. )
  844. )
  845. assert result == (expected,)
  846. def test_run_actions_adds_log_file_to_hook_context():
  847. flexmock(module).should_receive('add_custom_log_levels')
  848. flexmock(module).should_receive('get_skip_actions').and_return([])
  849. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  850. flexmock()
  851. )
  852. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  853. expected = flexmock()
  854. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  855. config_filename=object,
  856. repository={'path': 'repo'},
  857. config={'repositories': [], 'log_file': 'foo'},
  858. config_paths=[],
  859. local_borg_version=object,
  860. create_arguments=object,
  861. global_arguments=object,
  862. dry_run_label='',
  863. local_path=object,
  864. remote_path=object,
  865. ).once().and_return(expected)
  866. result = tuple(
  867. module.run_actions(
  868. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  869. config_filename=flexmock(),
  870. config={'repositories': [], 'log_file': 'foo'},
  871. config_paths=[],
  872. local_path=flexmock(),
  873. remote_path=flexmock(),
  874. local_borg_version=flexmock(),
  875. repository={'path': 'repo'},
  876. )
  877. )
  878. assert result == (expected,)
  879. def test_run_actions_runs_transfer():
  880. flexmock(module).should_receive('add_custom_log_levels')
  881. flexmock(module).should_receive('get_skip_actions').and_return([])
  882. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  883. flexmock()
  884. )
  885. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  886. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  887. tuple(
  888. module.run_actions(
  889. arguments={'global': flexmock(dry_run=False), 'transfer': flexmock()},
  890. config_filename=flexmock(),
  891. config={'repositories': []},
  892. config_paths=[],
  893. local_path=flexmock(),
  894. remote_path=flexmock(),
  895. local_borg_version=flexmock(),
  896. repository={'path': 'repo'},
  897. )
  898. )
  899. def test_run_actions_runs_create():
  900. flexmock(module).should_receive('add_custom_log_levels')
  901. flexmock(module).should_receive('get_skip_actions').and_return([])
  902. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  903. flexmock()
  904. )
  905. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  906. expected = flexmock()
  907. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  908. result = tuple(
  909. module.run_actions(
  910. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  911. config_filename=flexmock(),
  912. config={'repositories': []},
  913. config_paths=[],
  914. local_path=flexmock(),
  915. remote_path=flexmock(),
  916. local_borg_version=flexmock(),
  917. repository={'path': 'repo'},
  918. )
  919. )
  920. assert result == (expected,)
  921. def test_run_actions_with_skip_actions_does_not_run_action_or_action_command_hooks():
  922. flexmock(module).should_receive('add_custom_log_levels')
  923. flexmock(module).should_receive('get_skip_actions').and_return(['create'])
  924. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  925. flexmock()
  926. )
  927. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  928. flexmock(module.command).should_receive('Before_after_hooks').with_args(
  929. command_hooks=object,
  930. before_after='action',
  931. umask=object,
  932. working_directory=object,
  933. dry_run=object,
  934. action_names=object,
  935. configuration_filename=object,
  936. repository_label=object,
  937. log_file=object,
  938. repositories=object,
  939. repository=object,
  940. ).never()
  941. flexmock(borgmatic.actions.create).should_receive('run_create').never()
  942. tuple(
  943. module.run_actions(
  944. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  945. config_filename=flexmock(),
  946. config={'repositories': [], 'skip_actions': ['create']},
  947. config_paths=[],
  948. local_path=flexmock(),
  949. remote_path=flexmock(),
  950. local_borg_version=flexmock(),
  951. repository={'path': 'repo'},
  952. )
  953. )
  954. def test_run_actions_runs_recreate():
  955. flexmock(module).should_receive('add_custom_log_levels')
  956. flexmock(module).should_receive('get_skip_actions').and_return([])
  957. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  958. flexmock()
  959. )
  960. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  961. flexmock(borgmatic.actions.recreate).should_receive('run_recreate').once()
  962. tuple(
  963. module.run_actions(
  964. arguments={'global': flexmock(dry_run=False), 'recreate': 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_runs_prune():
  975. flexmock(module).should_receive('add_custom_log_levels')
  976. flexmock(module).should_receive('get_skip_actions').and_return([])
  977. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  978. flexmock()
  979. )
  980. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  981. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  982. tuple(
  983. module.run_actions(
  984. arguments={'global': flexmock(dry_run=False), 'prune': flexmock()},
  985. config_filename=flexmock(),
  986. config={'repositories': []},
  987. config_paths=[],
  988. local_path=flexmock(),
  989. remote_path=flexmock(),
  990. local_borg_version=flexmock(),
  991. repository={'path': 'repo'},
  992. )
  993. )
  994. def test_run_actions_runs_compact():
  995. flexmock(module).should_receive('add_custom_log_levels')
  996. flexmock(module).should_receive('get_skip_actions').and_return([])
  997. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  998. flexmock()
  999. )
  1000. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1001. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  1002. tuple(
  1003. module.run_actions(
  1004. arguments={'global': flexmock(dry_run=False), 'compact': flexmock()},
  1005. config_filename=flexmock(),
  1006. config={'repositories': []},
  1007. config_paths=[],
  1008. local_path=flexmock(),
  1009. remote_path=flexmock(),
  1010. local_borg_version=flexmock(),
  1011. repository={'path': 'repo'},
  1012. )
  1013. )
  1014. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  1015. flexmock(module).should_receive('add_custom_log_levels')
  1016. flexmock(module).should_receive('get_skip_actions').and_return([])
  1017. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1018. flexmock()
  1019. )
  1020. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1021. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  1022. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  1023. tuple(
  1024. module.run_actions(
  1025. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  1026. config_filename=flexmock(),
  1027. config={'repositories': []},
  1028. config_paths=[],
  1029. local_path=flexmock(),
  1030. remote_path=flexmock(),
  1031. local_borg_version=flexmock(),
  1032. repository={'path': 'repo'},
  1033. )
  1034. )
  1035. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  1036. flexmock(module).should_receive('add_custom_log_levels')
  1037. flexmock(module).should_receive('get_skip_actions').and_return([])
  1038. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1039. flexmock()
  1040. )
  1041. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1042. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  1043. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  1044. tuple(
  1045. module.run_actions(
  1046. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  1047. config_filename=flexmock(),
  1048. config={'repositories': []},
  1049. config_paths=[],
  1050. local_path=flexmock(),
  1051. remote_path=flexmock(),
  1052. local_borg_version=flexmock(),
  1053. repository={'path': 'repo'},
  1054. )
  1055. )
  1056. def test_run_actions_runs_extract():
  1057. flexmock(module).should_receive('add_custom_log_levels')
  1058. flexmock(module).should_receive('get_skip_actions').and_return([])
  1059. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1060. flexmock()
  1061. )
  1062. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1063. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  1064. tuple(
  1065. module.run_actions(
  1066. arguments={'global': flexmock(dry_run=False), 'extract': flexmock()},
  1067. config_filename=flexmock(),
  1068. config={'repositories': []},
  1069. config_paths=[],
  1070. local_path=flexmock(),
  1071. remote_path=flexmock(),
  1072. local_borg_version=flexmock(),
  1073. repository={'path': 'repo'},
  1074. )
  1075. )
  1076. def test_run_actions_runs_export_tar():
  1077. flexmock(module).should_receive('add_custom_log_levels')
  1078. flexmock(module).should_receive('get_skip_actions').and_return([])
  1079. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1080. flexmock()
  1081. )
  1082. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1083. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  1084. tuple(
  1085. module.run_actions(
  1086. arguments={'global': flexmock(dry_run=False), 'export-tar': 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_mount():
  1097. flexmock(module).should_receive('add_custom_log_levels')
  1098. flexmock(module).should_receive('get_skip_actions').and_return([])
  1099. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1100. flexmock()
  1101. )
  1102. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1103. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  1104. tuple(
  1105. module.run_actions(
  1106. arguments={'global': flexmock(dry_run=False), 'mount': flexmock()},
  1107. config_filename=flexmock(),
  1108. config={'repositories': []},
  1109. config_paths=[],
  1110. local_path=flexmock(),
  1111. remote_path=flexmock(),
  1112. local_borg_version=flexmock(),
  1113. repository={'path': 'repo'},
  1114. )
  1115. )
  1116. def test_run_actions_runs_restore():
  1117. flexmock(module).should_receive('add_custom_log_levels')
  1118. flexmock(module).should_receive('get_skip_actions').and_return([])
  1119. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1120. flexmock()
  1121. )
  1122. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1123. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  1124. tuple(
  1125. module.run_actions(
  1126. arguments={'global': flexmock(dry_run=False), 'restore': flexmock()},
  1127. config_filename=flexmock(),
  1128. config={'repositories': []},
  1129. config_paths=[],
  1130. local_path=flexmock(),
  1131. remote_path=flexmock(),
  1132. local_borg_version=flexmock(),
  1133. repository={'path': 'repo'},
  1134. )
  1135. )
  1136. def test_run_actions_runs_repo_list():
  1137. flexmock(module).should_receive('add_custom_log_levels')
  1138. flexmock(module).should_receive('get_skip_actions').and_return([])
  1139. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1140. flexmock()
  1141. )
  1142. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1143. expected = flexmock()
  1144. flexmock(borgmatic.actions.repo_list).should_receive('run_repo_list').and_yield(expected).once()
  1145. result = tuple(
  1146. module.run_actions(
  1147. arguments={'global': flexmock(dry_run=False), 'repo-list': flexmock()},
  1148. config_filename=flexmock(),
  1149. config={'repositories': []},
  1150. config_paths=[],
  1151. local_path=flexmock(),
  1152. remote_path=flexmock(),
  1153. local_borg_version=flexmock(),
  1154. repository={'path': 'repo'},
  1155. )
  1156. )
  1157. assert result == (expected,)
  1158. def test_run_actions_runs_list():
  1159. flexmock(module).should_receive('add_custom_log_levels')
  1160. flexmock(module).should_receive('get_skip_actions').and_return([])
  1161. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1162. flexmock()
  1163. )
  1164. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1165. expected = flexmock()
  1166. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  1167. result = tuple(
  1168. module.run_actions(
  1169. arguments={'global': flexmock(dry_run=False), 'list': flexmock()},
  1170. config_filename=flexmock(),
  1171. config={'repositories': []},
  1172. config_paths=[],
  1173. local_path=flexmock(),
  1174. remote_path=flexmock(),
  1175. local_borg_version=flexmock(),
  1176. repository={'path': 'repo'},
  1177. )
  1178. )
  1179. assert result == (expected,)
  1180. def test_run_actions_runs_repo_info():
  1181. flexmock(module).should_receive('add_custom_log_levels')
  1182. flexmock(module).should_receive('get_skip_actions').and_return([])
  1183. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1184. flexmock()
  1185. )
  1186. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1187. expected = flexmock()
  1188. flexmock(borgmatic.actions.repo_info).should_receive('run_repo_info').and_yield(expected).once()
  1189. result = tuple(
  1190. module.run_actions(
  1191. arguments={'global': flexmock(dry_run=False), 'repo-info': flexmock()},
  1192. config_filename=flexmock(),
  1193. config={'repositories': []},
  1194. config_paths=[],
  1195. local_path=flexmock(),
  1196. remote_path=flexmock(),
  1197. local_borg_version=flexmock(),
  1198. repository={'path': 'repo'},
  1199. )
  1200. )
  1201. assert result == (expected,)
  1202. def test_run_actions_runs_info():
  1203. flexmock(module).should_receive('add_custom_log_levels')
  1204. flexmock(module).should_receive('get_skip_actions').and_return([])
  1205. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1206. flexmock()
  1207. )
  1208. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1209. expected = flexmock()
  1210. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  1211. result = tuple(
  1212. module.run_actions(
  1213. arguments={'global': flexmock(dry_run=False), 'info': flexmock()},
  1214. config_filename=flexmock(),
  1215. config={'repositories': []},
  1216. config_paths=[],
  1217. local_path=flexmock(),
  1218. remote_path=flexmock(),
  1219. local_borg_version=flexmock(),
  1220. repository={'path': 'repo'},
  1221. )
  1222. )
  1223. assert result == (expected,)
  1224. def test_run_actions_runs_break_lock():
  1225. flexmock(module).should_receive('add_custom_log_levels')
  1226. flexmock(module).should_receive('get_skip_actions').and_return([])
  1227. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1228. flexmock()
  1229. )
  1230. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1231. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  1232. tuple(
  1233. module.run_actions(
  1234. arguments={'global': flexmock(dry_run=False), 'break-lock': flexmock()},
  1235. config_filename=flexmock(),
  1236. config={'repositories': []},
  1237. config_paths=[],
  1238. local_path=flexmock(),
  1239. remote_path=flexmock(),
  1240. local_borg_version=flexmock(),
  1241. repository={'path': 'repo'},
  1242. )
  1243. )
  1244. def test_run_actions_runs_export_key():
  1245. flexmock(module).should_receive('add_custom_log_levels')
  1246. flexmock(module).should_receive('get_skip_actions').and_return([])
  1247. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1248. flexmock()
  1249. )
  1250. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1251. flexmock(borgmatic.actions.export_key).should_receive('run_export_key').once()
  1252. tuple(
  1253. module.run_actions(
  1254. arguments={'global': flexmock(dry_run=False), 'export': flexmock()},
  1255. config_filename=flexmock(),
  1256. config={'repositories': []},
  1257. config_paths=[],
  1258. local_path=flexmock(),
  1259. remote_path=flexmock(),
  1260. local_borg_version=flexmock(),
  1261. repository={'path': 'repo'},
  1262. )
  1263. )
  1264. def test_run_actions_runs_import_key():
  1265. flexmock(module).should_receive('add_custom_log_levels')
  1266. flexmock(module).should_receive('get_skip_actions').and_return([])
  1267. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1268. flexmock()
  1269. )
  1270. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1271. flexmock(borgmatic.actions.import_key).should_receive('run_import_key').once()
  1272. tuple(
  1273. module.run_actions(
  1274. arguments={'global': flexmock(dry_run=False), 'import': flexmock()},
  1275. config_filename=flexmock(),
  1276. config={'repositories': []},
  1277. config_paths=[],
  1278. local_path=flexmock(),
  1279. remote_path=flexmock(),
  1280. local_borg_version=flexmock(),
  1281. repository={'path': 'repo'},
  1282. )
  1283. )
  1284. def test_run_actions_runs_change_passphrase():
  1285. flexmock(module).should_receive('add_custom_log_levels')
  1286. flexmock(module).should_receive('get_skip_actions').and_return([])
  1287. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1288. flexmock()
  1289. )
  1290. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1291. flexmock(borgmatic.actions.change_passphrase).should_receive('run_change_passphrase').once()
  1292. tuple(
  1293. module.run_actions(
  1294. arguments={
  1295. 'global': flexmock(dry_run=False),
  1296. 'change-passphrase': flexmock(),
  1297. },
  1298. config_filename=flexmock(),
  1299. config={'repositories': []},
  1300. config_paths=[],
  1301. local_path=flexmock(),
  1302. remote_path=flexmock(),
  1303. local_borg_version=flexmock(),
  1304. repository={'path': 'repo'},
  1305. )
  1306. )
  1307. def test_run_actions_runs_delete():
  1308. flexmock(module).should_receive('add_custom_log_levels')
  1309. flexmock(module).should_receive('get_skip_actions').and_return([])
  1310. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1311. flexmock()
  1312. )
  1313. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1314. flexmock(borgmatic.actions.delete).should_receive('run_delete').once()
  1315. tuple(
  1316. module.run_actions(
  1317. arguments={'global': flexmock(dry_run=False), 'delete': flexmock()},
  1318. config_filename=flexmock(),
  1319. config={'repositories': []},
  1320. config_paths=[],
  1321. local_path=flexmock(),
  1322. remote_path=flexmock(),
  1323. local_borg_version=flexmock(),
  1324. repository={'path': 'repo'},
  1325. )
  1326. )
  1327. def test_run_actions_runs_repo_delete():
  1328. flexmock(module).should_receive('add_custom_log_levels')
  1329. flexmock(module).should_receive('get_skip_actions').and_return([])
  1330. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1331. flexmock()
  1332. )
  1333. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1334. flexmock(borgmatic.actions.repo_delete).should_receive('run_repo_delete').once()
  1335. tuple(
  1336. module.run_actions(
  1337. arguments={
  1338. 'global': flexmock(dry_run=False),
  1339. 'repo-delete': flexmock(),
  1340. },
  1341. config_filename=flexmock(),
  1342. config={'repositories': []},
  1343. config_paths=[],
  1344. local_path=flexmock(),
  1345. remote_path=flexmock(),
  1346. local_borg_version=flexmock(),
  1347. repository={'path': 'repo'},
  1348. )
  1349. )
  1350. def test_run_actions_runs_borg():
  1351. flexmock(module).should_receive('add_custom_log_levels')
  1352. flexmock(module).should_receive('get_skip_actions').and_return([])
  1353. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1354. flexmock()
  1355. )
  1356. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1357. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  1358. tuple(
  1359. module.run_actions(
  1360. arguments={'global': flexmock(dry_run=False), 'borg': flexmock()},
  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. def test_run_actions_runs_multiple_actions_in_argument_order():
  1371. flexmock(module).should_receive('add_custom_log_levels')
  1372. flexmock(module).should_receive('get_skip_actions').and_return([])
  1373. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1374. flexmock()
  1375. )
  1376. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1377. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  1378. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  1379. tuple(
  1380. module.run_actions(
  1381. arguments={
  1382. 'global': flexmock(dry_run=False),
  1383. 'borg': flexmock(),
  1384. 'restore': flexmock(),
  1385. },
  1386. config_filename=flexmock(),
  1387. config={'repositories': []},
  1388. config_paths=[],
  1389. local_path=flexmock(),
  1390. remote_path=flexmock(),
  1391. local_borg_version=flexmock(),
  1392. repository={'path': 'repo'},
  1393. )
  1394. )
  1395. def test_run_actions_runs_action_hooks_for_one_action_at_a_time():
  1396. flexmock(module).should_receive('add_custom_log_levels')
  1397. flexmock(module).should_receive('get_skip_actions').and_return([])
  1398. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1399. flexmock()
  1400. )
  1401. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1402. for action_name in ('borg', 'restore'):
  1403. flexmock(module.command).should_receive('Before_after_hooks').with_args(
  1404. command_hooks=object,
  1405. before_after='action',
  1406. umask=object,
  1407. working_directory=object,
  1408. dry_run=object,
  1409. action_names=(action_name,),
  1410. configuration_filename=object,
  1411. repository_label=object,
  1412. log_file=object,
  1413. repositories=object,
  1414. repository=object,
  1415. ).and_return(flexmock()).once()
  1416. flexmock(borgmatic.actions.borg).should_receive('run_borg')
  1417. flexmock(borgmatic.actions.restore).should_receive('run_restore')
  1418. tuple(
  1419. module.run_actions(
  1420. arguments={
  1421. 'global': flexmock(dry_run=False),
  1422. 'borg': flexmock(),
  1423. 'restore': flexmock(),
  1424. },
  1425. config_filename=flexmock(),
  1426. config={'repositories': []},
  1427. config_paths=[],
  1428. local_path=flexmock(),
  1429. remote_path=flexmock(),
  1430. local_borg_version=flexmock(),
  1431. repository={'path': 'repo'},
  1432. )
  1433. )
  1434. @pytest.mark.parametrize(
  1435. 'resolve_env',
  1436. ((True, False),),
  1437. )
  1438. def test_load_configurations_collects_parsed_configurations_and_logs(resolve_env):
  1439. configuration = flexmock()
  1440. other_configuration = flexmock()
  1441. test_expected_logs = [flexmock(), flexmock()]
  1442. other_expected_logs = [flexmock(), flexmock()]
  1443. flexmock(module.validate).should_receive('parse_configuration').and_return(
  1444. configuration, ['/tmp/test.yaml'], test_expected_logs
  1445. ).and_return(other_configuration, ['/tmp/other.yaml'], other_expected_logs)
  1446. configs, config_paths, logs = tuple(
  1447. module.load_configurations(
  1448. ('test.yaml', 'other.yaml'),
  1449. arguments=flexmock(),
  1450. resolve_env=resolve_env,
  1451. )
  1452. )
  1453. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  1454. assert config_paths == ['/tmp/other.yaml', '/tmp/test.yaml']
  1455. assert set(logs) >= set(test_expected_logs + other_expected_logs)
  1456. def test_load_configurations_logs_warning_for_permission_error():
  1457. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  1458. configs, config_paths, logs = tuple(
  1459. module.load_configurations(('test.yaml',), arguments=flexmock())
  1460. )
  1461. assert configs == {}
  1462. assert config_paths == []
  1463. assert max(log.levelno for log in logs) == logging.WARNING
  1464. def test_load_configurations_logs_critical_for_parse_error():
  1465. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  1466. configs, config_paths, logs = tuple(
  1467. module.load_configurations(('test.yaml',), arguments=flexmock())
  1468. )
  1469. assert configs == {}
  1470. assert config_paths == []
  1471. assert max(log.levelno for log in logs) == logging.CRITICAL
  1472. def test_log_record_does_not_raise():
  1473. module.log_record(levelno=1, foo='bar', baz='quux')
  1474. def test_log_record_with_suppress_does_not_raise():
  1475. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  1476. def test_log_error_records_generates_output_logs_for_message_only():
  1477. flexmock(module).should_receive('log_record').replace_with(dict).once()
  1478. logs = tuple(module.log_error_records('Error'))
  1479. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1480. def test_log_error_records_generates_output_logs_for_called_process_error_with_bytes_ouput():
  1481. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  1482. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1483. logs = tuple(
  1484. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', b'error output'))
  1485. )
  1486. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1487. assert any(log for log in logs if 'error output' in str(log))
  1488. def test_log_error_records_generates_output_logs_for_called_process_error_with_string_ouput():
  1489. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  1490. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1491. logs = tuple(
  1492. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  1493. )
  1494. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1495. assert any(log for log in logs if 'error output' in str(log))
  1496. def test_log_error_records_generates_work_around_output_logs_for_called_process_error_with_repository_access_aborted_exit_code():
  1497. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  1498. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1499. logs = tuple(
  1500. module.log_error_records(
  1501. 'Error',
  1502. subprocess.CalledProcessError(
  1503. module.BORG_REPOSITORY_ACCESS_ABORTED_EXIT_CODE, 'ls', 'error output'
  1504. ),
  1505. )
  1506. )
  1507. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1508. assert any(log for log in logs if 'error output' in str(log))
  1509. assert any(log for log in logs if 'To work around this' in str(log))
  1510. def test_log_error_records_splits_called_process_error_with_multiline_ouput_into_multiple_logs():
  1511. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  1512. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1513. logs = tuple(
  1514. module.log_error_records(
  1515. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output\nanother line')
  1516. )
  1517. )
  1518. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1519. assert any(log for log in logs if 'error output' in str(log))
  1520. def test_log_error_records_generates_logs_for_value_error():
  1521. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  1522. logs = tuple(module.log_error_records('Error', ValueError()))
  1523. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1524. def test_log_error_records_generates_logs_for_os_error():
  1525. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  1526. logs = tuple(module.log_error_records('Error', OSError()))
  1527. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1528. def test_log_error_records_generates_nothing_for_other_error():
  1529. flexmock(module).should_receive('log_record').never()
  1530. logs = tuple(module.log_error_records('Error', KeyError()))
  1531. assert logs == ()
  1532. def test_get_local_path_uses_configuration_value():
  1533. assert module.get_local_path({'test.yaml': {'local_path': 'borg1'}}) == 'borg1'
  1534. def test_get_local_path_without_local_path_defaults_to_borg():
  1535. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  1536. def test_collect_highlander_action_summary_logs_info_for_success_with_bootstrap():
  1537. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1538. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  1539. arguments = {
  1540. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1541. 'global': flexmock(dry_run=False),
  1542. }
  1543. logs = tuple(
  1544. module.collect_highlander_action_summary_logs(
  1545. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1546. )
  1547. )
  1548. assert {log.levelno for log in logs} == {logging.ANSWER}
  1549. def test_collect_highlander_action_summary_logs_error_on_bootstrap_failure():
  1550. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1551. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  1552. ValueError
  1553. )
  1554. arguments = {
  1555. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1556. 'global': flexmock(dry_run=False),
  1557. }
  1558. logs = tuple(
  1559. module.collect_highlander_action_summary_logs(
  1560. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1561. )
  1562. )
  1563. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1564. def test_collect_highlander_action_summary_logs_error_on_bootstrap_local_borg_version_failure():
  1565. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  1566. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').never()
  1567. arguments = {
  1568. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  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_highlander_action_summary_logs_info_for_success_with_generate():
  1578. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate')
  1579. arguments = {
  1580. 'generate': flexmock(destination='test.yaml'),
  1581. 'global': flexmock(dry_run=False),
  1582. }
  1583. logs = tuple(
  1584. module.collect_highlander_action_summary_logs(
  1585. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1586. )
  1587. )
  1588. assert {log.levelno for log in logs} == {logging.ANSWER}
  1589. def test_collect_highlander_action_summary_logs_error_on_generate_failure():
  1590. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate').and_raise(
  1591. ValueError
  1592. )
  1593. arguments = {
  1594. 'generate': flexmock(destination='test.yaml'),
  1595. 'global': flexmock(dry_run=False),
  1596. }
  1597. logs = tuple(
  1598. module.collect_highlander_action_summary_logs(
  1599. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1600. )
  1601. )
  1602. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1603. def test_collect_highlander_action_summary_logs_info_for_success_with_validate():
  1604. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1605. arguments = {
  1606. 'validate': flexmock(),
  1607. 'global': flexmock(dry_run=False),
  1608. }
  1609. logs = tuple(
  1610. module.collect_highlander_action_summary_logs(
  1611. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1612. )
  1613. )
  1614. assert {log.levelno for log in logs} == {logging.ANSWER}
  1615. def test_collect_highlander_action_summary_logs_error_on_validate_parse_failure():
  1616. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1617. arguments = {
  1618. 'validate': flexmock(),
  1619. 'global': flexmock(dry_run=False),
  1620. }
  1621. logs = tuple(
  1622. module.collect_highlander_action_summary_logs(
  1623. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=True
  1624. )
  1625. )
  1626. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1627. def test_collect_highlander_action_summary_logs_error_on_run_validate_failure():
  1628. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate').and_raise(
  1629. ValueError
  1630. )
  1631. arguments = {
  1632. 'validate': flexmock(),
  1633. 'global': flexmock(dry_run=False),
  1634. }
  1635. logs = tuple(
  1636. module.collect_highlander_action_summary_logs(
  1637. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1638. )
  1639. )
  1640. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1641. def test_collect_configuration_run_summary_logs_info_for_success():
  1642. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1643. flexmock(module.command).should_receive('filter_hooks')
  1644. flexmock(module.command).should_receive('execute_hooks')
  1645. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1646. flexmock(module).should_receive('run_configuration').and_return([])
  1647. arguments = {'global': flexmock(dry_run=False)}
  1648. logs = tuple(
  1649. module.collect_configuration_run_summary_logs(
  1650. {'test.yaml': {}},
  1651. config_paths=['/tmp/test.yaml'],
  1652. arguments=arguments,
  1653. log_file_path=None,
  1654. )
  1655. )
  1656. assert {log.levelno for log in logs} == {logging.INFO}
  1657. def test_collect_configuration_run_summary_executes_hooks_for_create():
  1658. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1659. flexmock(module.command).should_receive('filter_hooks')
  1660. flexmock(module.command).should_receive('execute_hooks')
  1661. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1662. flexmock(module).should_receive('run_configuration').and_return([])
  1663. arguments = {
  1664. 'create': flexmock(),
  1665. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  1666. }
  1667. logs = tuple(
  1668. module.collect_configuration_run_summary_logs(
  1669. {'test.yaml': {}},
  1670. config_paths=['/tmp/test.yaml'],
  1671. arguments=arguments,
  1672. log_file_path=None,
  1673. )
  1674. )
  1675. assert {log.levelno for log in logs} == {logging.INFO}
  1676. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  1677. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1678. flexmock(module.command).should_receive('filter_hooks')
  1679. flexmock(module.command).should_receive('execute_hooks')
  1680. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1681. flexmock(module).should_receive('run_configuration').and_return([])
  1682. arguments = {
  1683. 'extract': flexmock(repository='repo'),
  1684. 'global': flexmock(dry_run=False),
  1685. }
  1686. logs = tuple(
  1687. module.collect_configuration_run_summary_logs(
  1688. {'test.yaml': {}},
  1689. config_paths=['/tmp/test.yaml'],
  1690. arguments=arguments,
  1691. log_file_path=None,
  1692. )
  1693. )
  1694. assert {log.levelno for log in logs} == {logging.INFO}
  1695. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  1696. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1697. ValueError
  1698. )
  1699. expected_logs = (flexmock(),)
  1700. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1701. arguments = {'extract': flexmock(repository='repo')}
  1702. logs = tuple(
  1703. module.collect_configuration_run_summary_logs(
  1704. {'test.yaml': {}},
  1705. config_paths=['/tmp/test.yaml'],
  1706. arguments=arguments,
  1707. log_file_path=None,
  1708. )
  1709. )
  1710. assert logs == expected_logs
  1711. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  1712. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1713. flexmock(module.command).should_receive('filter_hooks')
  1714. flexmock(module.command).should_receive('execute_hooks')
  1715. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1716. flexmock(module).should_receive('run_configuration').and_return([])
  1717. arguments = {
  1718. 'mount': flexmock(repository='repo'),
  1719. 'global': flexmock(dry_run=False),
  1720. }
  1721. logs = tuple(
  1722. module.collect_configuration_run_summary_logs(
  1723. {'test.yaml': {}},
  1724. config_paths=['/tmp/test.yaml'],
  1725. arguments=arguments,
  1726. log_file_path=None,
  1727. )
  1728. )
  1729. assert {log.levelno for log in logs} == {logging.INFO}
  1730. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  1731. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1732. ValueError
  1733. )
  1734. expected_logs = (flexmock(),)
  1735. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1736. arguments = {
  1737. 'mount': flexmock(repository='repo'),
  1738. 'global': flexmock(dry_run=False),
  1739. }
  1740. logs = tuple(
  1741. module.collect_configuration_run_summary_logs(
  1742. {'test.yaml': {}},
  1743. config_paths=['/tmp/test.yaml'],
  1744. arguments=arguments,
  1745. log_file_path=None,
  1746. )
  1747. )
  1748. assert logs == expected_logs
  1749. def test_collect_configuration_run_summary_logs_missing_configs_error():
  1750. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1751. flexmock(module.command).should_receive('filter_hooks')
  1752. flexmock(module.command).should_receive('execute_hooks')
  1753. arguments = {'global': flexmock(config_paths=[])}
  1754. expected_logs = (flexmock(),)
  1755. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1756. logs = tuple(
  1757. module.collect_configuration_run_summary_logs(
  1758. {}, config_paths=[], arguments=arguments, log_file_path=None
  1759. )
  1760. )
  1761. assert logs == expected_logs
  1762. def test_collect_configuration_run_summary_logs_pre_hook_error():
  1763. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1764. flexmock(module.command).should_receive('filter_hooks')
  1765. flexmock(module.command).should_receive('execute_hooks').and_raise(ValueError)
  1766. expected_logs = (flexmock(),)
  1767. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1768. arguments = {
  1769. 'create': flexmock(),
  1770. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  1771. }
  1772. logs = tuple(
  1773. module.collect_configuration_run_summary_logs(
  1774. {'test.yaml': {}},
  1775. config_paths=['/tmp/test.yaml'],
  1776. arguments=arguments,
  1777. log_file_path=None,
  1778. )
  1779. )
  1780. assert logs == expected_logs
  1781. def test_collect_configuration_run_summary_logs_post_hook_error():
  1782. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1783. flexmock(module.command).should_receive('filter_hooks')
  1784. flexmock(module.command).should_receive('execute_hooks').and_return(None).and_raise(ValueError)
  1785. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1786. flexmock(module).should_receive('run_configuration').and_return([])
  1787. expected_logs = (flexmock(),)
  1788. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1789. arguments = {
  1790. 'create': flexmock(),
  1791. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  1792. }
  1793. logs = tuple(
  1794. module.collect_configuration_run_summary_logs(
  1795. {'test.yaml': {}},
  1796. config_paths=['/tmp/test.yaml'],
  1797. arguments=arguments,
  1798. log_file_path=None,
  1799. )
  1800. )
  1801. assert expected_logs[0] in logs
  1802. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  1803. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1804. ValueError
  1805. )
  1806. expected_logs = (flexmock(),)
  1807. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1808. arguments = {
  1809. 'list': flexmock(repository='repo', archive='test'),
  1810. 'global': flexmock(dry_run=False),
  1811. }
  1812. logs = tuple(
  1813. module.collect_configuration_run_summary_logs(
  1814. {'test.yaml': {}},
  1815. config_paths=['/tmp/test.yaml'],
  1816. arguments=arguments,
  1817. log_file_path=None,
  1818. )
  1819. )
  1820. assert logs == expected_logs
  1821. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  1822. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1823. flexmock(module.command).should_receive('filter_hooks')
  1824. flexmock(module.command).should_receive('execute_hooks')
  1825. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1826. flexmock(module).should_receive('run_configuration').and_return([])
  1827. arguments = {
  1828. 'list': flexmock(repository='repo', archive=None),
  1829. 'global': flexmock(dry_run=False),
  1830. }
  1831. logs = tuple(
  1832. module.collect_configuration_run_summary_logs(
  1833. {'test.yaml': {}},
  1834. config_paths=['/tmp/test.yaml'],
  1835. arguments=arguments,
  1836. log_file_path=None,
  1837. )
  1838. )
  1839. assert {log.levelno for log in logs} == {logging.INFO}
  1840. def test_collect_configuration_run_summary_logs_run_configuration_error_logs():
  1841. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1842. flexmock(module.command).should_receive('filter_hooks')
  1843. flexmock(module.command).should_receive('execute_hooks')
  1844. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1845. flexmock(module).should_receive('run_configuration').and_return(
  1846. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1847. )
  1848. flexmock(module).should_receive('log_error_records').and_return([])
  1849. arguments = {'global': flexmock(dry_run=False)}
  1850. logs = tuple(
  1851. module.collect_configuration_run_summary_logs(
  1852. {'test.yaml': {}},
  1853. config_paths=['/tmp/test.yaml'],
  1854. arguments=arguments,
  1855. log_file_path=None,
  1856. )
  1857. )
  1858. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1859. def test_collect_configuration_run_summary_logs_run_umount_error():
  1860. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1861. flexmock(module.command).should_receive('filter_hooks')
  1862. flexmock(module.command).should_receive('execute_hooks')
  1863. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1864. flexmock(module).should_receive('run_configuration').and_return([])
  1865. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  1866. flexmock(module).should_receive('log_error_records').and_return(
  1867. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1868. )
  1869. arguments = {
  1870. 'umount': flexmock(mount_point='/mnt'),
  1871. 'global': flexmock(dry_run=False),
  1872. }
  1873. logs = tuple(
  1874. module.collect_configuration_run_summary_logs(
  1875. {'test.yaml': {}},
  1876. config_paths=['/tmp/test.yaml'],
  1877. arguments=arguments,
  1878. log_file_path=None,
  1879. )
  1880. )
  1881. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  1882. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  1883. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1884. flexmock(module.command).should_receive('filter_hooks')
  1885. flexmock(module.command).should_receive('execute_hooks')
  1886. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1887. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  1888. ['baz']
  1889. )
  1890. stdout = flexmock()
  1891. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  1892. flexmock(module.sys).stdout = stdout
  1893. arguments = {'global': flexmock(dry_run=False)}
  1894. tuple(
  1895. module.collect_configuration_run_summary_logs(
  1896. {'test.yaml': {}, 'test2.yaml': {}},
  1897. config_paths=['/tmp/test.yaml', '/tmp/test2.yaml'],
  1898. arguments=arguments,
  1899. log_file_path=None,
  1900. )
  1901. )
  1902. def test_check_and_show_help_on_no_args_shows_help_when_no_args_and_default_actions_false():
  1903. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1904. flexmock(module).should_receive('parse_arguments').with_args('--help').once()
  1905. flexmock(module.sys).should_receive('exit').with_args(0).once()
  1906. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}})
  1907. def test_check_and_show_help_on_no_args_does_not_show_help_when_no_args_and_default_actions_true():
  1908. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1909. flexmock(module).should_receive('parse_arguments').never()
  1910. flexmock(module.sys).should_receive('exit').never()
  1911. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': True}})
  1912. def test_check_and_show_help_on_no_args_does_not_show_help_when_args_provided():
  1913. flexmock(module.sys).should_receive('argv').and_return(['borgmatic', '--create'])
  1914. flexmock(module).should_receive('parse_arguments').never()
  1915. flexmock(module.sys).should_receive('exit').never()
  1916. module.check_and_show_help_on_no_args({'test.yaml': {'default_actions': False}})
  1917. def test_check_and_show_help_on_no_args_with_no_default_actions_in_all_configs():
  1918. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1919. # Both configs have default_actions set to False, so help should be shown
  1920. configs = {
  1921. 'config1.yaml': {'default_actions': False},
  1922. 'config2.yaml': {'default_actions': False},
  1923. }
  1924. # Expect help to be shown
  1925. flexmock(module).should_receive('parse_arguments').with_args('--help').once()
  1926. flexmock(module.sys).should_receive('exit').with_args(0).once()
  1927. module.check_and_show_help_on_no_args(configs)
  1928. def test_check_and_show_help_on_no_args_with_conflicting_configs():
  1929. flexmock(module.sys).should_receive('argv').and_return(['borgmatic'])
  1930. # Simulate two config files with conflicting 'default_actions' values
  1931. configs = {
  1932. 'config1.yaml': {'default_actions': True},
  1933. 'config2.yaml': {'default_actions': False},
  1934. }
  1935. # Expect help not to be shown because at least one config enables default actions
  1936. flexmock(module).should_receive('parse_arguments').never()
  1937. flexmock(module.sys).should_receive('exit').never()
  1938. module.check_and_show_help_on_no_args(configs)
  1939. def test_get_singular_option_value_with_conflicting_values_exits():
  1940. flexmock(module).should_receive('configure_logging')
  1941. flexmock(module).should_receive('exit_with_help_link').once()
  1942. module.get_singular_option_value(
  1943. configs={
  1944. 'test1.yaml': {'foo': 1, 'bar': 'baz'},
  1945. 'test2.yaml': {'foo': 2, 'bar': 'baz'},
  1946. },
  1947. option_name='foo',
  1948. )
  1949. def test_get_singular_option_value_with_same_value_returns_it():
  1950. flexmock(module).should_receive('configure_logging').never()
  1951. flexmock(module).should_receive('exit_with_help_link').never()
  1952. assert (
  1953. module.get_singular_option_value(
  1954. configs={
  1955. 'test1.yaml': {'foo': 1, 'bar': 'baz'},
  1956. 'test2.yaml': {'foo': 1, 'bar': 'baz'},
  1957. },
  1958. option_name='foo',
  1959. )
  1960. == 1
  1961. )
  1962. def test_get_singular_option_value_with_no_values_returns_none():
  1963. flexmock(module).should_receive('configure_logging').never()
  1964. flexmock(module).should_receive('exit_with_help_link').never()
  1965. assert (
  1966. module.get_singular_option_value(
  1967. configs={
  1968. 'test1.yaml': {'bar': 'baz'},
  1969. 'test2.yaml': {'bar': 'baz'},
  1970. },
  1971. option_name='foo',
  1972. )
  1973. is None
  1974. )
  1975. def test_get_singular_option_value_with_no_config_returns_none():
  1976. flexmock(module).should_receive('configure_logging').never()
  1977. flexmock(module).should_receive('exit_with_help_link').never()
  1978. assert (
  1979. module.get_singular_option_value(
  1980. configs={},
  1981. option_name='foo',
  1982. )
  1983. is None
  1984. )