test_borgmatic.py 62 KB

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