test_borgmatic.py 64 KB

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