test_borgmatic.py 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  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_run_configuration_runs_actions_for_each_repository():
  25. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  26. flexmock(module).should_receive('get_skip_actions').and_return([])
  27. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  28. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  29. expected_results = [flexmock(), flexmock()]
  30. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  31. flexmock(module).should_receive('run_actions').and_return(expected_results[:1]).and_return(
  32. expected_results[1:]
  33. )
  34. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  35. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock())}
  36. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  37. assert results == expected_results
  38. def test_run_configuration_with_skip_actions_does_not_raise():
  39. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  40. flexmock(module).should_receive('get_skip_actions').and_return(['compact'])
  41. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  42. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  43. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  44. flexmock(module).should_receive('run_actions').and_return(flexmock()).and_return(flexmock())
  45. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}], 'skip_actions': ['compact']}
  46. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock())}
  47. list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  48. def test_run_configuration_with_invalid_borg_version_errors():
  49. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  50. flexmock(module).should_receive('get_skip_actions').and_return([])
  51. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  52. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  53. flexmock(module.dispatch).should_receive('call_hooks').never()
  54. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  55. flexmock(module).should_receive('run_actions').never()
  56. config = {'repositories': [{'path': 'foo'}]}
  57. arguments = {
  58. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  59. 'prune': flexmock(),
  60. }
  61. list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  62. def test_run_configuration_logs_monitor_start_error():
  63. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  64. flexmock(module).should_receive('get_skip_actions').and_return([])
  65. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  66. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  67. flexmock(module.dispatch).should_receive('call_hooks').and_raise(OSError).and_return(
  68. None
  69. ).and_return(None).and_return(None)
  70. expected_results = [flexmock()]
  71. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  72. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  73. flexmock(module).should_receive('run_actions').never()
  74. flexmock(module.command).should_receive('filter_hooks')
  75. flexmock(module.command).should_receive('execute_hooks')
  76. config = {'repositories': [{'path': 'foo'}]}
  77. arguments = {
  78. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  79. 'create': flexmock(),
  80. }
  81. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  82. assert results == expected_results
  83. def test_run_configuration_bails_for_monitor_start_soft_failure():
  84. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  85. flexmock(module).should_receive('get_skip_actions').and_return([])
  86. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  87. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  88. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  89. flexmock(module.dispatch).should_receive('call_hooks').and_raise(error).and_return(None)
  90. flexmock(module).should_receive('log_error_records').never()
  91. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  92. flexmock(module).should_receive('run_actions').never()
  93. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  94. arguments = {
  95. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  96. 'create': flexmock(),
  97. }
  98. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  99. assert results == []
  100. def test_run_configuration_logs_actions_error():
  101. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  102. flexmock(module).should_receive('get_skip_actions').and_return([])
  103. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  104. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  105. flexmock(module.dispatch).should_receive('call_hooks')
  106. expected_results = [flexmock()]
  107. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  108. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  109. flexmock(module).should_receive('run_actions').and_raise(OSError)
  110. flexmock(module.command).should_receive('filter_hooks')
  111. flexmock(module.command).should_receive('execute_hooks')
  112. config = {'repositories': [{'path': 'foo'}]}
  113. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock())}
  114. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  115. assert results == expected_results
  116. def test_run_configuration_skips_remaining_actions_for_actions_soft_failure_but_still_runs_next_repository_actions():
  117. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  118. flexmock(module).should_receive('get_skip_actions').and_return([])
  119. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  120. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  121. flexmock(module.dispatch).should_receive('call_hooks').times(5)
  122. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  123. log = flexmock()
  124. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  125. flexmock(module).should_receive('run_actions').twice().and_raise(error).and_yield(log)
  126. flexmock(module).should_receive('log_error_records').never()
  127. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  128. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  129. arguments = {
  130. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  131. 'create': flexmock(),
  132. }
  133. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  134. assert results == [log]
  135. def test_run_configuration_logs_monitor_log_error():
  136. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  137. flexmock(module).should_receive('get_skip_actions').and_return([])
  138. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  139. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  140. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  141. None
  142. ).and_raise(OSError)
  143. expected_results = [flexmock()]
  144. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  145. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  146. flexmock(module).should_receive('run_actions').and_return([])
  147. flexmock(module.command).should_receive('filter_hooks')
  148. flexmock(module.command).should_receive('execute_hooks')
  149. config = {'repositories': [{'path': 'foo'}]}
  150. arguments = {
  151. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  152. 'create': flexmock(),
  153. }
  154. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  155. assert results == expected_results
  156. def test_run_configuration_logs_monitor_finish_error():
  157. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  158. flexmock(module).should_receive('get_skip_actions').and_return([])
  159. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  160. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  161. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  162. None
  163. ).and_return(None).and_raise(OSError)
  164. expected_results = [flexmock()]
  165. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  166. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  167. flexmock(module).should_receive('run_actions').and_return([])
  168. flexmock(module.command).should_receive('filter_hooks')
  169. flexmock(module.command).should_receive('execute_hooks')
  170. config = {'repositories': [{'path': 'foo'}]}
  171. arguments = {
  172. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  173. 'create': flexmock(),
  174. }
  175. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  176. assert results == expected_results
  177. def test_run_configuration_logs_monitor_fail_error():
  178. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  179. flexmock(module).should_receive('get_skip_actions').and_return([])
  180. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  181. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  182. flexmock(module.dispatch).should_receive('call_hooks')
  183. # Trigger an error in the monitor finish so that the monitor fail also gets triggered.
  184. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  185. 'ping_monitor',
  186. object,
  187. module.dispatch.Hook_type.MONITORING,
  188. object,
  189. module.monitor.State.FINISH,
  190. object,
  191. object,
  192. ).and_raise(OSError)
  193. flexmock(module.dispatch).should_receive('call_hooks').with_args(
  194. 'ping_monitor',
  195. object,
  196. module.dispatch.Hook_type.MONITORING,
  197. object,
  198. module.monitor.State.FAIL,
  199. object,
  200. object,
  201. ).and_raise(OSError).once()
  202. expected_results = [flexmock()]
  203. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  204. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  205. flexmock(module).should_receive('run_actions').and_return([])
  206. flexmock(module.command).should_receive('filter_hooks')
  207. flexmock(module.command).should_receive('execute_hooks')
  208. config = {'repositories': [{'path': 'foo'}]}
  209. arguments = {
  210. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  211. 'create': flexmock(),
  212. }
  213. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  214. assert results == expected_results + expected_results
  215. def test_run_configuration_does_not_call_monitoring_hooks_if_monitoring_hooks_are_disabled():
  216. flexmock(module).should_receive('verbosity_to_log_level').and_return(module.DISABLED)
  217. flexmock(module).should_receive('get_skip_actions').and_return([])
  218. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  219. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  220. flexmock(module.dispatch).should_receive('call_hooks').never()
  221. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  222. flexmock(module).should_receive('run_actions').and_return([])
  223. config = {'repositories': [{'path': 'foo'}]}
  224. arguments = {
  225. 'global': flexmock(monitoring_verbosity=-2, dry_run=False, log_file=flexmock()),
  226. 'create': flexmock(),
  227. }
  228. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  229. assert results == []
  230. def test_run_configuration_logs_on_error_hook_error():
  231. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  232. flexmock(module).should_receive('get_skip_actions').and_return([])
  233. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  234. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  235. flexmock(module.command).should_receive('filter_hooks')
  236. flexmock(module.command).should_receive('execute_hooks').and_raise(OSError)
  237. expected_results = [flexmock(), flexmock()]
  238. flexmock(module).should_receive('log_error_records').and_return(
  239. expected_results[:1]
  240. ).and_return(expected_results[1:])
  241. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  242. flexmock(module).should_receive('run_actions').and_raise(OSError)
  243. config = {'repositories': [{'path': 'foo'}]}
  244. arguments = {
  245. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  246. 'create': flexmock(),
  247. }
  248. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  249. assert results == expected_results
  250. def test_run_configuration_bails_for_on_error_hook_soft_failure():
  251. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  252. flexmock(module).should_receive('get_skip_actions').and_return([])
  253. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  254. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  255. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  256. flexmock(module.command).should_receive('filter_hooks')
  257. flexmock(module.command).should_receive('execute_hooks').and_raise(error)
  258. expected_results = [flexmock()]
  259. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  260. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  261. flexmock(module).should_receive('run_actions').and_raise(OSError)
  262. config = {'repositories': [{'path': 'foo'}]}
  263. arguments = {
  264. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  265. 'create': flexmock(),
  266. }
  267. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  268. assert results == expected_results
  269. def test_run_configuration_retries_soft_error():
  270. # Run action first fails, second passes.
  271. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  272. flexmock(module).should_receive('get_skip_actions').and_return([])
  273. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  274. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  275. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  276. flexmock(module).should_receive('run_actions').and_raise(OSError).and_return([])
  277. flexmock(module).should_receive('log_error_records').and_return([flexmock()]).once()
  278. flexmock(module.command).should_receive('filter_hooks').never()
  279. flexmock(module.command).should_receive('execute_hooks').never()
  280. config = {'repositories': [{'path': 'foo'}], 'retries': 1}
  281. arguments = {
  282. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  283. 'create': flexmock(),
  284. }
  285. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  286. assert results == []
  287. def test_run_configuration_retries_hard_error():
  288. # Run action fails twice.
  289. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  290. flexmock(module).should_receive('get_skip_actions').and_return([])
  291. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  292. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  293. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  294. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  295. flexmock(module).should_receive('log_error_records').with_args(
  296. 'Error running actions for repository',
  297. OSError,
  298. levelno=logging.WARNING,
  299. log_command_error_output=True,
  300. ).and_return([flexmock()])
  301. error_logs = [flexmock()]
  302. flexmock(module).should_receive('log_error_records').with_args(
  303. 'Error running actions for repository',
  304. OSError,
  305. ).and_return(error_logs)
  306. flexmock(module.command).should_receive('filter_hooks')
  307. flexmock(module.command).should_receive('execute_hooks')
  308. config = {'repositories': [{'path': 'foo'}], 'retries': 1}
  309. arguments = {
  310. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  311. 'create': flexmock(),
  312. }
  313. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  314. assert results == error_logs
  315. def test_run_configuration_retries_repositories_in_order():
  316. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  317. flexmock(module).should_receive('get_skip_actions').and_return([])
  318. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  319. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  320. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  321. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  322. expected_results = [flexmock(), flexmock()]
  323. flexmock(module).should_receive('log_error_records').with_args(
  324. 'Error running actions for repository', OSError
  325. ).and_return(expected_results[:1]).ordered()
  326. flexmock(module).should_receive('log_error_records').with_args(
  327. 'Error running actions for repository', OSError
  328. ).and_return(expected_results[1:]).ordered()
  329. flexmock(module.command).should_receive('filter_hooks')
  330. flexmock(module.command).should_receive('execute_hooks')
  331. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  332. arguments = {
  333. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  334. 'create': flexmock(),
  335. }
  336. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  337. assert results == expected_results
  338. def test_run_configuration_retries_round_robin():
  339. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  340. flexmock(module).should_receive('get_skip_actions').and_return([])
  341. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  342. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  343. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  344. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  345. flexmock(module).should_receive('log_error_records').with_args(
  346. 'Error running actions for repository',
  347. OSError,
  348. levelno=logging.WARNING,
  349. log_command_error_output=True,
  350. ).and_return([flexmock()]).ordered()
  351. flexmock(module).should_receive('log_error_records').with_args(
  352. 'Error running actions for repository',
  353. OSError,
  354. levelno=logging.WARNING,
  355. log_command_error_output=True,
  356. ).and_return([flexmock()]).ordered()
  357. foo_error_logs = [flexmock()]
  358. flexmock(module).should_receive('log_error_records').with_args(
  359. 'Error running actions for repository', OSError
  360. ).and_return(foo_error_logs).ordered()
  361. bar_error_logs = [flexmock()]
  362. flexmock(module).should_receive('log_error_records').with_args(
  363. 'Error running actions for repository', OSError
  364. ).and_return(bar_error_logs).ordered()
  365. flexmock(module.command).should_receive('filter_hooks')
  366. flexmock(module.command).should_receive('execute_hooks')
  367. config = {
  368. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  369. 'retries': 1,
  370. }
  371. arguments = {
  372. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  373. 'create': flexmock(),
  374. }
  375. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  376. assert results == foo_error_logs + bar_error_logs
  377. def test_run_configuration_with_one_retry():
  378. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  379. flexmock(module).should_receive('get_skip_actions').and_return([])
  380. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  381. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  382. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  383. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  384. []
  385. ).and_raise(OSError).times(4)
  386. flexmock(module).should_receive('log_error_records').with_args(
  387. 'Error running actions for repository',
  388. OSError,
  389. levelno=logging.WARNING,
  390. log_command_error_output=True,
  391. ).and_return([flexmock()]).ordered()
  392. flexmock(module).should_receive('log_error_records').with_args(
  393. 'Error running actions for repository',
  394. OSError,
  395. levelno=logging.WARNING,
  396. log_command_error_output=True,
  397. ).and_return(flexmock()).ordered()
  398. error_logs = [flexmock()]
  399. flexmock(module).should_receive('log_error_records').with_args(
  400. 'Error running actions for repository', OSError
  401. ).and_return(error_logs).ordered()
  402. flexmock(module.command).should_receive('filter_hooks')
  403. flexmock(module.command).should_receive('execute_hooks')
  404. config = {
  405. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  406. 'retries': 1,
  407. }
  408. arguments = {
  409. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  410. 'create': flexmock(),
  411. }
  412. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  413. assert results == error_logs
  414. def test_run_configuration_with_retry_wait_does_backoff_after_each_retry():
  415. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  416. flexmock(module).should_receive('get_skip_actions').and_return([])
  417. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  418. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  419. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  420. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  421. flexmock(module).should_receive('log_error_records').with_args(
  422. 'Error running actions for repository',
  423. OSError,
  424. levelno=logging.WARNING,
  425. log_command_error_output=True,
  426. ).and_return([flexmock()]).ordered()
  427. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  428. flexmock(module).should_receive('log_error_records').with_args(
  429. 'Error running actions for repository',
  430. OSError,
  431. levelno=logging.WARNING,
  432. log_command_error_output=True,
  433. ).and_return([flexmock()]).ordered()
  434. flexmock(time).should_receive('sleep').with_args(20).and_return().ordered()
  435. flexmock(module).should_receive('log_error_records').with_args(
  436. 'Error running actions for repository',
  437. OSError,
  438. levelno=logging.WARNING,
  439. log_command_error_output=True,
  440. ).and_return([flexmock()]).ordered()
  441. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  442. error_logs = [flexmock()]
  443. flexmock(module).should_receive('log_error_records').with_args(
  444. 'Error running actions for repository', OSError
  445. ).and_return(error_logs).ordered()
  446. flexmock(module.command).should_receive('filter_hooks')
  447. flexmock(module.command).should_receive('execute_hooks')
  448. config = {
  449. 'repositories': [{'path': 'foo'}],
  450. 'retries': 3,
  451. 'retry_wait': 10,
  452. }
  453. arguments = {
  454. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  455. 'create': flexmock(),
  456. }
  457. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  458. assert results == error_logs
  459. def test_run_configuration_with_multiple_repositories_retries_with_timeout():
  460. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  461. flexmock(module).should_receive('get_skip_actions').and_return([])
  462. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  463. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  464. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  465. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  466. []
  467. ).and_raise(OSError).times(4)
  468. flexmock(module).should_receive('log_error_records').with_args(
  469. 'Error running actions for repository',
  470. OSError,
  471. levelno=logging.WARNING,
  472. log_command_error_output=True,
  473. ).and_return([flexmock()]).ordered()
  474. flexmock(module).should_receive('log_error_records').with_args(
  475. 'Error running actions for repository',
  476. OSError,
  477. levelno=logging.WARNING,
  478. log_command_error_output=True,
  479. ).and_return([flexmock()]).ordered()
  480. # Sleep before retrying foo (and passing)
  481. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  482. # Sleep before retrying bar (and failing)
  483. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  484. error_logs = [flexmock()]
  485. flexmock(module).should_receive('log_error_records').with_args(
  486. 'Error running actions for repository', OSError
  487. ).and_return(error_logs).ordered()
  488. flexmock(module.command).should_receive('filter_hooks')
  489. flexmock(module.command).should_receive('execute_hooks')
  490. config = {
  491. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  492. 'retries': 1,
  493. 'retry_wait': 10,
  494. }
  495. arguments = {
  496. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  497. 'create': flexmock(),
  498. }
  499. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  500. assert results == error_logs
  501. def test_run_actions_runs_repo_create():
  502. flexmock(module).should_receive('add_custom_log_levels')
  503. flexmock(module).should_receive('get_skip_actions').and_return([])
  504. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  505. flexmock(borgmatic.actions.repo_create).should_receive('run_repo_create').once()
  506. tuple(
  507. module.run_actions(
  508. arguments={
  509. 'global': flexmock(dry_run=False, log_file='foo'),
  510. 'repo-create': flexmock(),
  511. },
  512. config_filename=flexmock(),
  513. config={'repositories': []},
  514. config_paths=[],
  515. local_path=flexmock(),
  516. remote_path=flexmock(),
  517. local_borg_version=flexmock(),
  518. repository={'path': 'repo'},
  519. )
  520. )
  521. def test_run_actions_adds_label_file_to_hook_context():
  522. flexmock(module).should_receive('add_custom_log_levels')
  523. flexmock(module).should_receive('get_skip_actions').and_return([])
  524. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  525. expected = flexmock()
  526. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  527. config_filename=object,
  528. repository={'path': 'repo', 'label': 'my repo'},
  529. config={'repositories': []},
  530. config_paths=[],
  531. local_borg_version=object,
  532. create_arguments=object,
  533. global_arguments=object,
  534. dry_run_label='',
  535. local_path=object,
  536. remote_path=object,
  537. ).once().and_return(expected)
  538. result = tuple(
  539. module.run_actions(
  540. arguments={'global': flexmock(dry_run=False, log_file=None), 'create': flexmock()},
  541. config_filename=flexmock(),
  542. config={'repositories': []},
  543. config_paths=[],
  544. local_path=flexmock(),
  545. remote_path=flexmock(),
  546. local_borg_version=flexmock(),
  547. repository={'path': 'repo', 'label': 'my repo'},
  548. )
  549. )
  550. assert result == (expected,)
  551. def test_run_actions_adds_log_file_to_hook_context():
  552. flexmock(module).should_receive('add_custom_log_levels')
  553. flexmock(module).should_receive('get_skip_actions').and_return([])
  554. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  555. expected = flexmock()
  556. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  557. config_filename=object,
  558. repository={'path': 'repo'},
  559. config={'repositories': []},
  560. config_paths=[],
  561. local_borg_version=object,
  562. create_arguments=object,
  563. global_arguments=object,
  564. dry_run_label='',
  565. local_path=object,
  566. remote_path=object,
  567. ).once().and_return(expected)
  568. result = tuple(
  569. module.run_actions(
  570. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  571. config_filename=flexmock(),
  572. config={'repositories': []},
  573. config_paths=[],
  574. local_path=flexmock(),
  575. remote_path=flexmock(),
  576. local_borg_version=flexmock(),
  577. repository={'path': 'repo'},
  578. )
  579. )
  580. assert result == (expected,)
  581. def test_run_actions_runs_transfer():
  582. flexmock(module).should_receive('add_custom_log_levels')
  583. flexmock(module).should_receive('get_skip_actions').and_return([])
  584. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  585. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  586. tuple(
  587. module.run_actions(
  588. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'transfer': flexmock()},
  589. config_filename=flexmock(),
  590. config={'repositories': []},
  591. config_paths=[],
  592. local_path=flexmock(),
  593. remote_path=flexmock(),
  594. local_borg_version=flexmock(),
  595. repository={'path': 'repo'},
  596. )
  597. )
  598. def test_run_actions_runs_create():
  599. flexmock(module).should_receive('add_custom_log_levels')
  600. flexmock(module).should_receive('get_skip_actions').and_return([])
  601. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  602. expected = flexmock()
  603. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  604. result = tuple(
  605. module.run_actions(
  606. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  607. config_filename=flexmock(),
  608. config={'repositories': []},
  609. config_paths=[],
  610. local_path=flexmock(),
  611. remote_path=flexmock(),
  612. local_borg_version=flexmock(),
  613. repository={'path': 'repo'},
  614. )
  615. )
  616. assert result == (expected,)
  617. def test_run_actions_with_skip_actions_skips_create():
  618. flexmock(module).should_receive('add_custom_log_levels')
  619. flexmock(module).should_receive('get_skip_actions').and_return(['create'])
  620. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  621. flexmock(borgmatic.actions.create).should_receive('run_create').never()
  622. tuple(
  623. module.run_actions(
  624. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  625. config_filename=flexmock(),
  626. config={'repositories': [], 'skip_actions': ['create']},
  627. config_paths=[],
  628. local_path=flexmock(),
  629. remote_path=flexmock(),
  630. local_borg_version=flexmock(),
  631. repository={'path': 'repo'},
  632. )
  633. )
  634. def test_run_actions_runs_prune():
  635. flexmock(module).should_receive('add_custom_log_levels')
  636. flexmock(module).should_receive('get_skip_actions').and_return([])
  637. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  638. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  639. tuple(
  640. module.run_actions(
  641. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  642. config_filename=flexmock(),
  643. config={'repositories': []},
  644. config_paths=[],
  645. local_path=flexmock(),
  646. remote_path=flexmock(),
  647. local_borg_version=flexmock(),
  648. repository={'path': 'repo'},
  649. )
  650. )
  651. def test_run_actions_with_skip_actions_skips_prune():
  652. flexmock(module).should_receive('add_custom_log_levels')
  653. flexmock(module).should_receive('get_skip_actions').and_return(['prune'])
  654. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  655. flexmock(borgmatic.actions.prune).should_receive('run_prune').never()
  656. tuple(
  657. module.run_actions(
  658. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  659. config_filename=flexmock(),
  660. config={'repositories': [], 'skip_actions': ['prune']},
  661. config_paths=[],
  662. local_path=flexmock(),
  663. remote_path=flexmock(),
  664. local_borg_version=flexmock(),
  665. repository={'path': 'repo'},
  666. )
  667. )
  668. def test_run_actions_runs_compact():
  669. flexmock(module).should_receive('add_custom_log_levels')
  670. flexmock(module).should_receive('get_skip_actions').and_return([])
  671. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  672. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  673. tuple(
  674. module.run_actions(
  675. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  676. config_filename=flexmock(),
  677. config={'repositories': []},
  678. config_paths=[],
  679. local_path=flexmock(),
  680. remote_path=flexmock(),
  681. local_borg_version=flexmock(),
  682. repository={'path': 'repo'},
  683. )
  684. )
  685. def test_run_actions_with_skip_actions_skips_compact():
  686. flexmock(module).should_receive('add_custom_log_levels')
  687. flexmock(module).should_receive('get_skip_actions').and_return(['compact'])
  688. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  689. flexmock(borgmatic.actions.compact).should_receive('run_compact').never()
  690. tuple(
  691. module.run_actions(
  692. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  693. config_filename=flexmock(),
  694. config={'repositories': [], 'skip_actions': ['compact']},
  695. config_paths=[],
  696. local_path=flexmock(),
  697. remote_path=flexmock(),
  698. local_borg_version=flexmock(),
  699. repository={'path': 'repo'},
  700. )
  701. )
  702. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  703. flexmock(module).should_receive('add_custom_log_levels')
  704. flexmock(module).should_receive('get_skip_actions').and_return([])
  705. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  706. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  707. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  708. tuple(
  709. module.run_actions(
  710. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  711. config_filename=flexmock(),
  712. config={'repositories': []},
  713. config_paths=[],
  714. local_path=flexmock(),
  715. remote_path=flexmock(),
  716. local_borg_version=flexmock(),
  717. repository={'path': 'repo'},
  718. )
  719. )
  720. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  721. flexmock(module).should_receive('add_custom_log_levels')
  722. flexmock(module).should_receive('get_skip_actions').and_return([])
  723. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  724. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  725. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  726. tuple(
  727. module.run_actions(
  728. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  729. config_filename=flexmock(),
  730. config={'repositories': []},
  731. config_paths=[],
  732. local_path=flexmock(),
  733. remote_path=flexmock(),
  734. local_borg_version=flexmock(),
  735. repository={'path': 'repo'},
  736. )
  737. )
  738. def test_run_actions_with_skip_actions_skips_check():
  739. flexmock(module).should_receive('add_custom_log_levels')
  740. flexmock(module).should_receive('get_skip_actions').and_return(['check'])
  741. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  742. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  743. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  744. tuple(
  745. module.run_actions(
  746. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  747. config_filename=flexmock(),
  748. config={'repositories': [], 'skip_actions': ['check']},
  749. config_paths=[],
  750. local_path=flexmock(),
  751. remote_path=flexmock(),
  752. local_borg_version=flexmock(),
  753. repository={'path': 'repo'},
  754. )
  755. )
  756. def test_run_actions_runs_extract():
  757. flexmock(module).should_receive('add_custom_log_levels')
  758. flexmock(module).should_receive('get_skip_actions').and_return([])
  759. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  760. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  761. tuple(
  762. module.run_actions(
  763. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'extract': flexmock()},
  764. config_filename=flexmock(),
  765. config={'repositories': []},
  766. config_paths=[],
  767. local_path=flexmock(),
  768. remote_path=flexmock(),
  769. local_borg_version=flexmock(),
  770. repository={'path': 'repo'},
  771. )
  772. )
  773. def test_run_actions_runs_export_tar():
  774. flexmock(module).should_receive('add_custom_log_levels')
  775. flexmock(module).should_receive('get_skip_actions').and_return([])
  776. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  777. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  778. tuple(
  779. module.run_actions(
  780. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export-tar': flexmock()},
  781. config_filename=flexmock(),
  782. config={'repositories': []},
  783. config_paths=[],
  784. local_path=flexmock(),
  785. remote_path=flexmock(),
  786. local_borg_version=flexmock(),
  787. repository={'path': 'repo'},
  788. )
  789. )
  790. def test_run_actions_runs_mount():
  791. flexmock(module).should_receive('add_custom_log_levels')
  792. flexmock(module).should_receive('get_skip_actions').and_return([])
  793. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  794. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  795. tuple(
  796. module.run_actions(
  797. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'mount': flexmock()},
  798. config_filename=flexmock(),
  799. config={'repositories': []},
  800. config_paths=[],
  801. local_path=flexmock(),
  802. remote_path=flexmock(),
  803. local_borg_version=flexmock(),
  804. repository={'path': 'repo'},
  805. )
  806. )
  807. def test_run_actions_runs_restore():
  808. flexmock(module).should_receive('add_custom_log_levels')
  809. flexmock(module).should_receive('get_skip_actions').and_return([])
  810. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  811. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  812. tuple(
  813. module.run_actions(
  814. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'restore': flexmock()},
  815. config_filename=flexmock(),
  816. config={'repositories': []},
  817. config_paths=[],
  818. local_path=flexmock(),
  819. remote_path=flexmock(),
  820. local_borg_version=flexmock(),
  821. repository={'path': 'repo'},
  822. )
  823. )
  824. def test_run_actions_runs_repo_list():
  825. flexmock(module).should_receive('add_custom_log_levels')
  826. flexmock(module).should_receive('get_skip_actions').and_return([])
  827. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  828. expected = flexmock()
  829. flexmock(borgmatic.actions.repo_list).should_receive('run_repo_list').and_yield(expected).once()
  830. result = tuple(
  831. module.run_actions(
  832. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'repo-list': flexmock()},
  833. config_filename=flexmock(),
  834. config={'repositories': []},
  835. config_paths=[],
  836. local_path=flexmock(),
  837. remote_path=flexmock(),
  838. local_borg_version=flexmock(),
  839. repository={'path': 'repo'},
  840. )
  841. )
  842. assert result == (expected,)
  843. def test_run_actions_runs_list():
  844. flexmock(module).should_receive('add_custom_log_levels')
  845. flexmock(module).should_receive('get_skip_actions').and_return([])
  846. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  847. expected = flexmock()
  848. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  849. result = tuple(
  850. module.run_actions(
  851. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'list': flexmock()},
  852. config_filename=flexmock(),
  853. config={'repositories': []},
  854. config_paths=[],
  855. local_path=flexmock(),
  856. remote_path=flexmock(),
  857. local_borg_version=flexmock(),
  858. repository={'path': 'repo'},
  859. )
  860. )
  861. assert result == (expected,)
  862. def test_run_actions_runs_repo_info():
  863. flexmock(module).should_receive('add_custom_log_levels')
  864. flexmock(module).should_receive('get_skip_actions').and_return([])
  865. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  866. expected = flexmock()
  867. flexmock(borgmatic.actions.repo_info).should_receive('run_repo_info').and_yield(expected).once()
  868. result = tuple(
  869. module.run_actions(
  870. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'repo-info': flexmock()},
  871. config_filename=flexmock(),
  872. config={'repositories': []},
  873. config_paths=[],
  874. local_path=flexmock(),
  875. remote_path=flexmock(),
  876. local_borg_version=flexmock(),
  877. repository={'path': 'repo'},
  878. )
  879. )
  880. assert result == (expected,)
  881. def test_run_actions_runs_info():
  882. flexmock(module).should_receive('add_custom_log_levels')
  883. flexmock(module).should_receive('get_skip_actions').and_return([])
  884. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  885. expected = flexmock()
  886. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  887. result = tuple(
  888. module.run_actions(
  889. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'info': 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. assert result == (expected,)
  900. def test_run_actions_runs_break_lock():
  901. flexmock(module).should_receive('add_custom_log_levels')
  902. flexmock(module).should_receive('get_skip_actions').and_return([])
  903. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  904. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  905. tuple(
  906. module.run_actions(
  907. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'break-lock': flexmock()},
  908. config_filename=flexmock(),
  909. config={'repositories': []},
  910. config_paths=[],
  911. local_path=flexmock(),
  912. remote_path=flexmock(),
  913. local_borg_version=flexmock(),
  914. repository={'path': 'repo'},
  915. )
  916. )
  917. def test_run_actions_runs_export_key():
  918. flexmock(module).should_receive('add_custom_log_levels')
  919. flexmock(module).should_receive('get_skip_actions').and_return([])
  920. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  921. flexmock(borgmatic.actions.export_key).should_receive('run_export_key').once()
  922. tuple(
  923. module.run_actions(
  924. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export': flexmock()},
  925. config_filename=flexmock(),
  926. config={'repositories': []},
  927. config_paths=[],
  928. local_path=flexmock(),
  929. remote_path=flexmock(),
  930. local_borg_version=flexmock(),
  931. repository={'path': 'repo'},
  932. )
  933. )
  934. def test_run_actions_runs_change_passphrase():
  935. flexmock(module).should_receive('add_custom_log_levels')
  936. flexmock(module).should_receive('get_skip_actions').and_return([])
  937. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  938. flexmock(borgmatic.actions.change_passphrase).should_receive('run_change_passphrase').once()
  939. tuple(
  940. module.run_actions(
  941. arguments={
  942. 'global': flexmock(dry_run=False, log_file='foo'),
  943. 'change-passphrase': flexmock(),
  944. },
  945. config_filename=flexmock(),
  946. config={'repositories': []},
  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_delete():
  955. flexmock(module).should_receive('add_custom_log_levels')
  956. flexmock(module).should_receive('get_skip_actions').and_return([])
  957. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  958. flexmock(borgmatic.actions.delete).should_receive('run_delete').once()
  959. tuple(
  960. module.run_actions(
  961. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'delete': flexmock()},
  962. config_filename=flexmock(),
  963. config={'repositories': []},
  964. config_paths=[],
  965. local_path=flexmock(),
  966. remote_path=flexmock(),
  967. local_borg_version=flexmock(),
  968. repository={'path': 'repo'},
  969. )
  970. )
  971. def test_run_actions_runs_repo_delete():
  972. flexmock(module).should_receive('add_custom_log_levels')
  973. flexmock(module).should_receive('get_skip_actions').and_return([])
  974. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  975. flexmock(borgmatic.actions.repo_delete).should_receive('run_repo_delete').once()
  976. tuple(
  977. module.run_actions(
  978. arguments={
  979. 'global': flexmock(dry_run=False, log_file='foo'),
  980. 'repo-delete': flexmock(),
  981. },
  982. config_filename=flexmock(),
  983. config={'repositories': []},
  984. config_paths=[],
  985. local_path=flexmock(),
  986. remote_path=flexmock(),
  987. local_borg_version=flexmock(),
  988. repository={'path': 'repo'},
  989. )
  990. )
  991. def test_run_actions_runs_borg():
  992. flexmock(module).should_receive('add_custom_log_levels')
  993. flexmock(module).should_receive('get_skip_actions').and_return([])
  994. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  995. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  996. tuple(
  997. module.run_actions(
  998. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'borg': flexmock()},
  999. config_filename=flexmock(),
  1000. config={'repositories': []},
  1001. config_paths=[],
  1002. local_path=flexmock(),
  1003. remote_path=flexmock(),
  1004. local_borg_version=flexmock(),
  1005. repository={'path': 'repo'},
  1006. )
  1007. )
  1008. def test_run_actions_runs_multiple_actions_in_argument_order():
  1009. flexmock(module).should_receive('add_custom_log_levels')
  1010. flexmock(module).should_receive('get_skip_actions').and_return([])
  1011. flexmock(module.command).should_receive('Before_after_hooks').and_return(flexmock())
  1012. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  1013. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  1014. tuple(
  1015. module.run_actions(
  1016. arguments={
  1017. 'global': flexmock(dry_run=False, log_file='foo'),
  1018. 'borg': flexmock(),
  1019. 'restore': flexmock(),
  1020. },
  1021. config_filename=flexmock(),
  1022. config={'repositories': []},
  1023. config_paths=[],
  1024. local_path=flexmock(),
  1025. remote_path=flexmock(),
  1026. local_borg_version=flexmock(),
  1027. repository={'path': 'repo'},
  1028. )
  1029. )
  1030. @pytest.mark.parametrize(
  1031. 'resolve_env',
  1032. ((True, False),),
  1033. )
  1034. def test_load_configurations_collects_parsed_configurations_and_logs(resolve_env):
  1035. configuration = flexmock()
  1036. other_configuration = flexmock()
  1037. test_expected_logs = [flexmock(), flexmock()]
  1038. other_expected_logs = [flexmock(), flexmock()]
  1039. flexmock(module.validate).should_receive('parse_configuration').and_return(
  1040. configuration, ['/tmp/test.yaml'], test_expected_logs
  1041. ).and_return(other_configuration, ['/tmp/other.yaml'], other_expected_logs)
  1042. configs, config_paths, logs = tuple(
  1043. module.load_configurations(
  1044. ('test.yaml', 'other.yaml'),
  1045. resolve_env=resolve_env,
  1046. )
  1047. )
  1048. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  1049. assert config_paths == ['/tmp/other.yaml', '/tmp/test.yaml']
  1050. assert set(logs) >= set(test_expected_logs + other_expected_logs)
  1051. def test_load_configurations_logs_warning_for_permission_error():
  1052. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  1053. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml',)))
  1054. assert configs == {}
  1055. assert config_paths == []
  1056. assert max(log.levelno for log in logs) == logging.WARNING
  1057. def test_load_configurations_logs_critical_for_parse_error():
  1058. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  1059. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml',)))
  1060. assert configs == {}
  1061. assert config_paths == []
  1062. assert max(log.levelno for log in logs) == logging.CRITICAL
  1063. def test_log_record_does_not_raise():
  1064. module.log_record(levelno=1, foo='bar', baz='quux')
  1065. def test_log_record_with_suppress_does_not_raise():
  1066. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  1067. def test_log_error_records_generates_output_logs_for_message_only():
  1068. flexmock(module).should_receive('log_record').replace_with(dict).once()
  1069. logs = tuple(module.log_error_records('Error'))
  1070. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1071. def test_log_error_records_generates_output_logs_for_called_process_error_with_bytes_ouput():
  1072. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  1073. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1074. logs = tuple(
  1075. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', b'error output'))
  1076. )
  1077. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1078. assert any(log for log in logs if 'error output' in str(log))
  1079. def test_log_error_records_generates_output_logs_for_called_process_error_with_string_ouput():
  1080. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  1081. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1082. logs = tuple(
  1083. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  1084. )
  1085. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1086. assert any(log for log in logs if 'error output' in str(log))
  1087. def test_log_error_records_generates_work_around_output_logs_for_called_process_error_with_repository_access_aborted_exit_code():
  1088. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  1089. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1090. logs = tuple(
  1091. module.log_error_records(
  1092. 'Error',
  1093. subprocess.CalledProcessError(
  1094. module.BORG_REPOSITORY_ACCESS_ABORTED_EXIT_CODE, 'ls', 'error output'
  1095. ),
  1096. )
  1097. )
  1098. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1099. assert any(log for log in logs if 'error output' in str(log))
  1100. assert any(log for log in logs if 'To work around this' in str(log))
  1101. def test_log_error_records_splits_called_process_error_with_multiline_ouput_into_multiple_logs():
  1102. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  1103. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  1104. logs = tuple(
  1105. module.log_error_records(
  1106. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output\nanother line')
  1107. )
  1108. )
  1109. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1110. assert any(log for log in logs if 'error output' in str(log))
  1111. def test_log_error_records_generates_logs_for_value_error():
  1112. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  1113. logs = tuple(module.log_error_records('Error', ValueError()))
  1114. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1115. def test_log_error_records_generates_logs_for_os_error():
  1116. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  1117. logs = tuple(module.log_error_records('Error', OSError()))
  1118. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  1119. def test_log_error_records_generates_nothing_for_other_error():
  1120. flexmock(module).should_receive('log_record').never()
  1121. logs = tuple(module.log_error_records('Error', KeyError()))
  1122. assert logs == ()
  1123. def test_get_local_path_uses_configuration_value():
  1124. assert module.get_local_path({'test.yaml': {'local_path': 'borg1'}}) == 'borg1'
  1125. def test_get_local_path_without_local_path_defaults_to_borg():
  1126. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  1127. def test_collect_highlander_action_summary_logs_info_for_success_with_bootstrap():
  1128. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1129. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  1130. arguments = {
  1131. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1132. 'global': flexmock(dry_run=False),
  1133. }
  1134. logs = tuple(
  1135. module.collect_highlander_action_summary_logs(
  1136. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1137. )
  1138. )
  1139. assert {log.levelno for log in logs} == {logging.ANSWER}
  1140. def test_collect_highlander_action_summary_logs_error_on_bootstrap_failure():
  1141. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1142. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  1143. ValueError
  1144. )
  1145. arguments = {
  1146. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1147. 'global': flexmock(dry_run=False),
  1148. }
  1149. logs = tuple(
  1150. module.collect_highlander_action_summary_logs(
  1151. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1152. )
  1153. )
  1154. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1155. def test_collect_highlander_action_summary_logs_error_on_bootstrap_local_borg_version_failure():
  1156. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  1157. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').never()
  1158. arguments = {
  1159. 'bootstrap': flexmock(repository='repo', local_path='borg7'),
  1160. 'global': flexmock(dry_run=False),
  1161. }
  1162. logs = tuple(
  1163. module.collect_highlander_action_summary_logs(
  1164. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1165. )
  1166. )
  1167. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1168. def test_collect_highlander_action_summary_logs_info_for_success_with_generate():
  1169. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate')
  1170. arguments = {
  1171. 'generate': flexmock(destination='test.yaml'),
  1172. 'global': flexmock(dry_run=False),
  1173. }
  1174. logs = tuple(
  1175. module.collect_highlander_action_summary_logs(
  1176. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1177. )
  1178. )
  1179. assert {log.levelno for log in logs} == {logging.ANSWER}
  1180. def test_collect_highlander_action_summary_logs_error_on_generate_failure():
  1181. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate').and_raise(
  1182. ValueError
  1183. )
  1184. arguments = {
  1185. 'generate': flexmock(destination='test.yaml'),
  1186. 'global': flexmock(dry_run=False),
  1187. }
  1188. logs = tuple(
  1189. module.collect_highlander_action_summary_logs(
  1190. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1191. )
  1192. )
  1193. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1194. def test_collect_highlander_action_summary_logs_info_for_success_with_validate():
  1195. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1196. arguments = {
  1197. 'validate': flexmock(),
  1198. 'global': flexmock(dry_run=False),
  1199. }
  1200. logs = tuple(
  1201. module.collect_highlander_action_summary_logs(
  1202. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1203. )
  1204. )
  1205. assert {log.levelno for log in logs} == {logging.ANSWER}
  1206. def test_collect_highlander_action_summary_logs_error_on_validate_parse_failure():
  1207. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1208. arguments = {
  1209. 'validate': flexmock(),
  1210. 'global': flexmock(dry_run=False),
  1211. }
  1212. logs = tuple(
  1213. module.collect_highlander_action_summary_logs(
  1214. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=True
  1215. )
  1216. )
  1217. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1218. def test_collect_highlander_action_summary_logs_error_on_run_validate_failure():
  1219. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate').and_raise(
  1220. ValueError
  1221. )
  1222. arguments = {
  1223. 'validate': flexmock(),
  1224. 'global': flexmock(dry_run=False),
  1225. }
  1226. logs = tuple(
  1227. module.collect_highlander_action_summary_logs(
  1228. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1229. )
  1230. )
  1231. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1232. def test_collect_configuration_run_summary_logs_info_for_success():
  1233. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1234. flexmock(module.command).should_receive('filter_hooks')
  1235. flexmock(module.command).should_receive('execute_hooks')
  1236. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1237. flexmock(module).should_receive('run_configuration').and_return([])
  1238. arguments = {'global': flexmock(dry_run=False, log_file=flexmock())}
  1239. logs = tuple(
  1240. module.collect_configuration_run_summary_logs(
  1241. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1242. )
  1243. )
  1244. assert {log.levelno for log in logs} == {logging.INFO}
  1245. def test_collect_configuration_run_summary_executes_hooks_for_create():
  1246. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1247. flexmock(module.command).should_receive('filter_hooks')
  1248. flexmock(module.command).should_receive('execute_hooks')
  1249. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1250. flexmock(module).should_receive('run_configuration').and_return([])
  1251. arguments = {
  1252. 'create': flexmock(),
  1253. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  1254. }
  1255. logs = tuple(
  1256. module.collect_configuration_run_summary_logs(
  1257. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1258. )
  1259. )
  1260. assert {log.levelno for log in logs} == {logging.INFO}
  1261. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  1262. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1263. flexmock(module.command).should_receive('filter_hooks')
  1264. flexmock(module.command).should_receive('execute_hooks')
  1265. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1266. flexmock(module).should_receive('run_configuration').and_return([])
  1267. arguments = {
  1268. 'extract': flexmock(repository='repo'),
  1269. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1270. }
  1271. logs = tuple(
  1272. module.collect_configuration_run_summary_logs(
  1273. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1274. )
  1275. )
  1276. assert {log.levelno for log in logs} == {logging.INFO}
  1277. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  1278. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1279. ValueError
  1280. )
  1281. expected_logs = (flexmock(),)
  1282. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1283. arguments = {'extract': flexmock(repository='repo', log_file=flexmock())}
  1284. logs = tuple(
  1285. module.collect_configuration_run_summary_logs(
  1286. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1287. )
  1288. )
  1289. assert logs == expected_logs
  1290. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  1291. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1292. flexmock(module.command).should_receive('filter_hooks')
  1293. flexmock(module.command).should_receive('execute_hooks')
  1294. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1295. flexmock(module).should_receive('run_configuration').and_return([])
  1296. arguments = {
  1297. 'mount': flexmock(repository='repo'),
  1298. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1299. }
  1300. logs = tuple(
  1301. module.collect_configuration_run_summary_logs(
  1302. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1303. )
  1304. )
  1305. assert {log.levelno for log in logs} == {logging.INFO}
  1306. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  1307. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1308. ValueError
  1309. )
  1310. expected_logs = (flexmock(),)
  1311. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1312. arguments = {
  1313. 'mount': flexmock(repository='repo'),
  1314. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1315. }
  1316. logs = tuple(
  1317. module.collect_configuration_run_summary_logs(
  1318. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1319. )
  1320. )
  1321. assert logs == expected_logs
  1322. def test_collect_configuration_run_summary_logs_missing_configs_error():
  1323. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1324. flexmock(module.command).should_receive('filter_hooks')
  1325. flexmock(module.command).should_receive('execute_hooks')
  1326. arguments = {'global': flexmock(config_paths=[], log_file=flexmock())}
  1327. expected_logs = (flexmock(),)
  1328. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1329. logs = tuple(
  1330. module.collect_configuration_run_summary_logs({}, config_paths=[], arguments=arguments)
  1331. )
  1332. assert logs == expected_logs
  1333. def test_collect_configuration_run_summary_logs_pre_hook_error():
  1334. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1335. flexmock(module.command).should_receive('filter_hooks')
  1336. flexmock(module.command).should_receive('execute_hooks').and_raise(ValueError)
  1337. expected_logs = (flexmock(),)
  1338. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1339. arguments = {
  1340. 'create': flexmock(),
  1341. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  1342. }
  1343. logs = tuple(
  1344. module.collect_configuration_run_summary_logs(
  1345. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1346. )
  1347. )
  1348. assert logs == expected_logs
  1349. def test_collect_configuration_run_summary_logs_post_hook_error():
  1350. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1351. flexmock(module.command).should_receive('filter_hooks')
  1352. flexmock(module.command).should_receive('execute_hooks').and_return(None).and_raise(ValueError)
  1353. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1354. flexmock(module).should_receive('run_configuration').and_return([])
  1355. expected_logs = (flexmock(),)
  1356. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1357. arguments = {
  1358. 'create': flexmock(),
  1359. 'global': flexmock(monitoring_verbosity=1, dry_run=False, log_file=flexmock()),
  1360. }
  1361. logs = tuple(
  1362. module.collect_configuration_run_summary_logs(
  1363. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1364. )
  1365. )
  1366. assert expected_logs[0] in logs
  1367. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  1368. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1369. ValueError
  1370. )
  1371. expected_logs = (flexmock(),)
  1372. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1373. arguments = {
  1374. 'list': flexmock(repository='repo', archive='test'),
  1375. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1376. }
  1377. logs = tuple(
  1378. module.collect_configuration_run_summary_logs(
  1379. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1380. )
  1381. )
  1382. assert logs == expected_logs
  1383. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  1384. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1385. flexmock(module.command).should_receive('filter_hooks')
  1386. flexmock(module.command).should_receive('execute_hooks')
  1387. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1388. flexmock(module).should_receive('run_configuration').and_return([])
  1389. arguments = {
  1390. 'list': flexmock(repository='repo', archive=None),
  1391. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1392. }
  1393. logs = tuple(
  1394. module.collect_configuration_run_summary_logs(
  1395. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1396. )
  1397. )
  1398. assert {log.levelno for log in logs} == {logging.INFO}
  1399. def test_collect_configuration_run_summary_logs_run_configuration_error_logs():
  1400. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1401. flexmock(module.command).should_receive('filter_hooks')
  1402. flexmock(module.command).should_receive('execute_hooks')
  1403. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1404. flexmock(module).should_receive('run_configuration').and_return(
  1405. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1406. )
  1407. flexmock(module).should_receive('log_error_records').and_return([])
  1408. arguments = {'global': flexmock(dry_run=False, log_file=flexmock())}
  1409. logs = tuple(
  1410. module.collect_configuration_run_summary_logs(
  1411. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1412. )
  1413. )
  1414. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1415. def test_collect_configuration_run_summary_logs_run_configuration_exception():
  1416. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1417. flexmock(module.command).should_receive('filter_hooks')
  1418. flexmock(module.command).should_receive('execute_hooks')
  1419. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1420. flexmock(module).should_receive('run_configuration').and_raise(ValueError)
  1421. flexmock(module).should_receive('log_error_records').and_return(
  1422. [flexmock(levelno=logging.CRITICAL)]
  1423. )
  1424. arguments = {'global': flexmock(dry_run=False, log_file=flexmock())}
  1425. logs = tuple(
  1426. module.collect_configuration_run_summary_logs(
  1427. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1428. )
  1429. )
  1430. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1431. def test_collect_configuration_run_summary_logs_run_umount_error():
  1432. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1433. flexmock(module.command).should_receive('filter_hooks')
  1434. flexmock(module.command).should_receive('execute_hooks')
  1435. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1436. flexmock(module).should_receive('run_configuration').and_return([])
  1437. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  1438. flexmock(module).should_receive('log_error_records').and_return(
  1439. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1440. )
  1441. arguments = {
  1442. 'umount': flexmock(mount_point='/mnt'),
  1443. 'global': flexmock(dry_run=False, log_file=flexmock()),
  1444. }
  1445. logs = tuple(
  1446. module.collect_configuration_run_summary_logs(
  1447. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1448. )
  1449. )
  1450. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  1451. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  1452. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1453. flexmock(module.command).should_receive('filter_hooks')
  1454. flexmock(module.command).should_receive('execute_hooks')
  1455. flexmock(module).should_receive('Log_prefix').and_return(flexmock())
  1456. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  1457. ['baz']
  1458. )
  1459. stdout = flexmock()
  1460. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  1461. flexmock(module.sys).stdout = stdout
  1462. arguments = {'global': flexmock(dry_run=False, log_file=flexmock())}
  1463. tuple(
  1464. module.collect_configuration_run_summary_logs(
  1465. {'test.yaml': {}, 'test2.yaml': {}},
  1466. config_paths=['/tmp/test.yaml', '/tmp/test2.yaml'],
  1467. arguments=arguments,
  1468. )
  1469. )