test_borgmatic.py 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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': ['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': ['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': ['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_bails_for_actions_soft_failure():
  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')
  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_bails_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)
  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_log_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'},
  416. config={'repositories': []},
  417. config_paths=[],
  418. hook_context={'repository': 'repo', 'repositories': '', 'log_file': 'foo'},
  419. local_borg_version=object,
  420. create_arguments=object,
  421. global_arguments=object,
  422. dry_run_label='',
  423. local_path=object,
  424. remote_path=object,
  425. ).once().and_return(expected)
  426. result = tuple(
  427. module.run_actions(
  428. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  429. config_filename=flexmock(),
  430. config={'repositories': []},
  431. config_paths=[],
  432. local_path=flexmock(),
  433. remote_path=flexmock(),
  434. local_borg_version=flexmock(),
  435. repository={'path': 'repo'},
  436. )
  437. )
  438. assert result == (expected,)
  439. def test_run_actions_runs_transfer():
  440. flexmock(module).should_receive('add_custom_log_levels')
  441. flexmock(module).should_receive('get_skip_actions').and_return([])
  442. flexmock(module.command).should_receive('execute_hook')
  443. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  444. tuple(
  445. module.run_actions(
  446. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'transfer': flexmock()},
  447. config_filename=flexmock(),
  448. config={'repositories': []},
  449. config_paths=[],
  450. local_path=flexmock(),
  451. remote_path=flexmock(),
  452. local_borg_version=flexmock(),
  453. repository={'path': 'repo'},
  454. )
  455. )
  456. def test_run_actions_runs_create():
  457. flexmock(module).should_receive('add_custom_log_levels')
  458. flexmock(module).should_receive('get_skip_actions').and_return([])
  459. flexmock(module.command).should_receive('execute_hook')
  460. expected = flexmock()
  461. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  462. result = tuple(
  463. module.run_actions(
  464. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  465. config_filename=flexmock(),
  466. config={'repositories': []},
  467. config_paths=[],
  468. local_path=flexmock(),
  469. remote_path=flexmock(),
  470. local_borg_version=flexmock(),
  471. repository={'path': 'repo'},
  472. )
  473. )
  474. assert result == (expected,)
  475. def test_run_actions_with_skip_actions_skips_create():
  476. flexmock(module).should_receive('add_custom_log_levels')
  477. flexmock(module).should_receive('get_skip_actions').and_return(['create'])
  478. flexmock(module.command).should_receive('execute_hook')
  479. flexmock(borgmatic.actions.create).should_receive('run_create').never()
  480. tuple(
  481. module.run_actions(
  482. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  483. config_filename=flexmock(),
  484. config={'repositories': [], 'skip_actions': ['create']},
  485. config_paths=[],
  486. local_path=flexmock(),
  487. remote_path=flexmock(),
  488. local_borg_version=flexmock(),
  489. repository={'path': 'repo'},
  490. )
  491. )
  492. def test_run_actions_runs_prune():
  493. flexmock(module).should_receive('add_custom_log_levels')
  494. flexmock(module).should_receive('get_skip_actions').and_return([])
  495. flexmock(module.command).should_receive('execute_hook')
  496. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  497. tuple(
  498. module.run_actions(
  499. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  500. config_filename=flexmock(),
  501. config={'repositories': []},
  502. config_paths=[],
  503. local_path=flexmock(),
  504. remote_path=flexmock(),
  505. local_borg_version=flexmock(),
  506. repository={'path': 'repo'},
  507. )
  508. )
  509. def test_run_actions_with_skip_actions_skips_prune():
  510. flexmock(module).should_receive('add_custom_log_levels')
  511. flexmock(module).should_receive('get_skip_actions').and_return(['prune'])
  512. flexmock(module.command).should_receive('execute_hook')
  513. flexmock(borgmatic.actions.prune).should_receive('run_prune').never()
  514. tuple(
  515. module.run_actions(
  516. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  517. config_filename=flexmock(),
  518. config={'repositories': [], 'skip_actions': ['prune']},
  519. config_paths=[],
  520. local_path=flexmock(),
  521. remote_path=flexmock(),
  522. local_borg_version=flexmock(),
  523. repository={'path': 'repo'},
  524. )
  525. )
  526. def test_run_actions_runs_compact():
  527. flexmock(module).should_receive('add_custom_log_levels')
  528. flexmock(module).should_receive('get_skip_actions').and_return([])
  529. flexmock(module.command).should_receive('execute_hook')
  530. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  531. tuple(
  532. module.run_actions(
  533. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  534. config_filename=flexmock(),
  535. config={'repositories': []},
  536. config_paths=[],
  537. local_path=flexmock(),
  538. remote_path=flexmock(),
  539. local_borg_version=flexmock(),
  540. repository={'path': 'repo'},
  541. )
  542. )
  543. def test_run_actions_with_skip_actions_skips_compact():
  544. flexmock(module).should_receive('add_custom_log_levels')
  545. flexmock(module).should_receive('get_skip_actions').and_return(['compact'])
  546. flexmock(module.command).should_receive('execute_hook')
  547. flexmock(borgmatic.actions.compact).should_receive('run_compact').never()
  548. tuple(
  549. module.run_actions(
  550. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  551. config_filename=flexmock(),
  552. config={'repositories': [], 'skip_actions': ['compact']},
  553. config_paths=[],
  554. local_path=flexmock(),
  555. remote_path=flexmock(),
  556. local_borg_version=flexmock(),
  557. repository={'path': 'repo'},
  558. )
  559. )
  560. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  561. flexmock(module).should_receive('add_custom_log_levels')
  562. flexmock(module).should_receive('get_skip_actions').and_return([])
  563. flexmock(module.command).should_receive('execute_hook')
  564. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  565. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  566. tuple(
  567. module.run_actions(
  568. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  569. config_filename=flexmock(),
  570. config={'repositories': []},
  571. config_paths=[],
  572. local_path=flexmock(),
  573. remote_path=flexmock(),
  574. local_borg_version=flexmock(),
  575. repository={'path': 'repo'},
  576. )
  577. )
  578. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  579. flexmock(module).should_receive('add_custom_log_levels')
  580. flexmock(module).should_receive('get_skip_actions').and_return([])
  581. flexmock(module.command).should_receive('execute_hook')
  582. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  583. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  584. tuple(
  585. module.run_actions(
  586. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  587. config_filename=flexmock(),
  588. config={'repositories': []},
  589. config_paths=[],
  590. local_path=flexmock(),
  591. remote_path=flexmock(),
  592. local_borg_version=flexmock(),
  593. repository={'path': 'repo'},
  594. )
  595. )
  596. def test_run_actions_with_skip_actions_skips_check():
  597. flexmock(module).should_receive('add_custom_log_levels')
  598. flexmock(module).should_receive('get_skip_actions').and_return(['check'])
  599. flexmock(module.command).should_receive('execute_hook')
  600. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  601. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  602. tuple(
  603. module.run_actions(
  604. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  605. config_filename=flexmock(),
  606. config={'repositories': [], 'skip_actions': ['check']},
  607. config_paths=[],
  608. local_path=flexmock(),
  609. remote_path=flexmock(),
  610. local_borg_version=flexmock(),
  611. repository={'path': 'repo'},
  612. )
  613. )
  614. def test_run_actions_runs_extract():
  615. flexmock(module).should_receive('add_custom_log_levels')
  616. flexmock(module).should_receive('get_skip_actions').and_return([])
  617. flexmock(module.command).should_receive('execute_hook')
  618. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  619. tuple(
  620. module.run_actions(
  621. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'extract': flexmock()},
  622. config_filename=flexmock(),
  623. config={'repositories': []},
  624. config_paths=[],
  625. local_path=flexmock(),
  626. remote_path=flexmock(),
  627. local_borg_version=flexmock(),
  628. repository={'path': 'repo'},
  629. )
  630. )
  631. def test_run_actions_runs_export_tar():
  632. flexmock(module).should_receive('add_custom_log_levels')
  633. flexmock(module).should_receive('get_skip_actions').and_return([])
  634. flexmock(module.command).should_receive('execute_hook')
  635. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  636. tuple(
  637. module.run_actions(
  638. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export-tar': flexmock()},
  639. config_filename=flexmock(),
  640. config={'repositories': []},
  641. config_paths=[],
  642. local_path=flexmock(),
  643. remote_path=flexmock(),
  644. local_borg_version=flexmock(),
  645. repository={'path': 'repo'},
  646. )
  647. )
  648. def test_run_actions_runs_mount():
  649. flexmock(module).should_receive('add_custom_log_levels')
  650. flexmock(module).should_receive('get_skip_actions').and_return([])
  651. flexmock(module.command).should_receive('execute_hook')
  652. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  653. tuple(
  654. module.run_actions(
  655. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'mount': flexmock()},
  656. config_filename=flexmock(),
  657. config={'repositories': []},
  658. config_paths=[],
  659. local_path=flexmock(),
  660. remote_path=flexmock(),
  661. local_borg_version=flexmock(),
  662. repository={'path': 'repo'},
  663. )
  664. )
  665. def test_run_actions_runs_restore():
  666. flexmock(module).should_receive('add_custom_log_levels')
  667. flexmock(module).should_receive('get_skip_actions').and_return([])
  668. flexmock(module.command).should_receive('execute_hook')
  669. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  670. tuple(
  671. module.run_actions(
  672. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'restore': flexmock()},
  673. config_filename=flexmock(),
  674. config={'repositories': []},
  675. config_paths=[],
  676. local_path=flexmock(),
  677. remote_path=flexmock(),
  678. local_borg_version=flexmock(),
  679. repository={'path': 'repo'},
  680. )
  681. )
  682. def test_run_actions_runs_rlist():
  683. flexmock(module).should_receive('add_custom_log_levels')
  684. flexmock(module).should_receive('get_skip_actions').and_return([])
  685. flexmock(module.command).should_receive('execute_hook')
  686. expected = flexmock()
  687. flexmock(borgmatic.actions.rlist).should_receive('run_rlist').and_yield(expected).once()
  688. result = tuple(
  689. module.run_actions(
  690. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rlist': flexmock()},
  691. config_filename=flexmock(),
  692. config={'repositories': []},
  693. config_paths=[],
  694. local_path=flexmock(),
  695. remote_path=flexmock(),
  696. local_borg_version=flexmock(),
  697. repository={'path': 'repo'},
  698. )
  699. )
  700. assert result == (expected,)
  701. def test_run_actions_runs_list():
  702. flexmock(module).should_receive('add_custom_log_levels')
  703. flexmock(module).should_receive('get_skip_actions').and_return([])
  704. flexmock(module.command).should_receive('execute_hook')
  705. expected = flexmock()
  706. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  707. result = tuple(
  708. module.run_actions(
  709. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'list': flexmock()},
  710. config_filename=flexmock(),
  711. config={'repositories': []},
  712. config_paths=[],
  713. local_path=flexmock(),
  714. remote_path=flexmock(),
  715. local_borg_version=flexmock(),
  716. repository={'path': 'repo'},
  717. )
  718. )
  719. assert result == (expected,)
  720. def test_run_actions_runs_rinfo():
  721. flexmock(module).should_receive('add_custom_log_levels')
  722. flexmock(module).should_receive('get_skip_actions').and_return([])
  723. flexmock(module.command).should_receive('execute_hook')
  724. expected = flexmock()
  725. flexmock(borgmatic.actions.rinfo).should_receive('run_rinfo').and_yield(expected).once()
  726. result = tuple(
  727. module.run_actions(
  728. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rinfo': flexmock()},
  729. config_filename=flexmock(),
  730. config={'repositories': []},
  731. config_paths=[],
  732. local_path=flexmock(),
  733. remote_path=flexmock(),
  734. local_borg_version=flexmock(),
  735. repository={'path': 'repo'},
  736. )
  737. )
  738. assert result == (expected,)
  739. def test_run_actions_runs_info():
  740. flexmock(module).should_receive('add_custom_log_levels')
  741. flexmock(module).should_receive('get_skip_actions').and_return([])
  742. flexmock(module.command).should_receive('execute_hook')
  743. expected = flexmock()
  744. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  745. result = tuple(
  746. module.run_actions(
  747. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'info': flexmock()},
  748. config_filename=flexmock(),
  749. config={'repositories': []},
  750. config_paths=[],
  751. local_path=flexmock(),
  752. remote_path=flexmock(),
  753. local_borg_version=flexmock(),
  754. repository={'path': 'repo'},
  755. )
  756. )
  757. assert result == (expected,)
  758. def test_run_actions_runs_break_lock():
  759. flexmock(module).should_receive('add_custom_log_levels')
  760. flexmock(module).should_receive('get_skip_actions').and_return([])
  761. flexmock(module.command).should_receive('execute_hook')
  762. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  763. tuple(
  764. module.run_actions(
  765. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'break-lock': flexmock()},
  766. config_filename=flexmock(),
  767. config={'repositories': []},
  768. config_paths=[],
  769. local_path=flexmock(),
  770. remote_path=flexmock(),
  771. local_borg_version=flexmock(),
  772. repository={'path': 'repo'},
  773. )
  774. )
  775. def test_run_actions_runs_export_key():
  776. flexmock(module).should_receive('add_custom_log_levels')
  777. flexmock(module).should_receive('get_skip_actions').and_return([])
  778. flexmock(module.command).should_receive('execute_hook')
  779. flexmock(borgmatic.actions.export_key).should_receive('run_export_key').once()
  780. tuple(
  781. module.run_actions(
  782. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export': flexmock()},
  783. config_filename=flexmock(),
  784. config={'repositories': []},
  785. config_paths=[],
  786. local_path=flexmock(),
  787. remote_path=flexmock(),
  788. local_borg_version=flexmock(),
  789. repository={'path': 'repo'},
  790. )
  791. )
  792. def test_run_actions_runs_borg():
  793. flexmock(module).should_receive('add_custom_log_levels')
  794. flexmock(module).should_receive('get_skip_actions').and_return([])
  795. flexmock(module.command).should_receive('execute_hook')
  796. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  797. tuple(
  798. module.run_actions(
  799. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'borg': flexmock()},
  800. config_filename=flexmock(),
  801. config={'repositories': []},
  802. config_paths=[],
  803. local_path=flexmock(),
  804. remote_path=flexmock(),
  805. local_borg_version=flexmock(),
  806. repository={'path': 'repo'},
  807. )
  808. )
  809. def test_run_actions_runs_multiple_actions_in_argument_order():
  810. flexmock(module).should_receive('add_custom_log_levels')
  811. flexmock(module).should_receive('get_skip_actions').and_return([])
  812. flexmock(module.command).should_receive('execute_hook')
  813. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  814. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  815. tuple(
  816. module.run_actions(
  817. arguments={
  818. 'global': flexmock(dry_run=False, log_file='foo'),
  819. 'borg': flexmock(),
  820. 'restore': flexmock(),
  821. },
  822. config_filename=flexmock(),
  823. config={'repositories': []},
  824. config_paths=[],
  825. local_path=flexmock(),
  826. remote_path=flexmock(),
  827. local_borg_version=flexmock(),
  828. repository={'path': 'repo'},
  829. )
  830. )
  831. def test_load_configurations_collects_parsed_configurations_and_logs():
  832. configuration = flexmock()
  833. other_configuration = flexmock()
  834. test_expected_logs = [flexmock(), flexmock()]
  835. other_expected_logs = [flexmock(), flexmock()]
  836. flexmock(module.validate).should_receive('parse_configuration').and_return(
  837. configuration, ['/tmp/test.yaml'], test_expected_logs
  838. ).and_return(other_configuration, ['/tmp/other.yaml'], other_expected_logs)
  839. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  840. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  841. assert config_paths == ['/tmp/other.yaml', '/tmp/test.yaml']
  842. assert set(logs) >= set(test_expected_logs + other_expected_logs)
  843. def test_load_configurations_logs_warning_for_permission_error():
  844. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  845. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml',)))
  846. assert configs == {}
  847. assert config_paths == []
  848. assert max(log.levelno for log in logs) == logging.WARNING
  849. def test_load_configurations_logs_critical_for_parse_error():
  850. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  851. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml',)))
  852. assert configs == {}
  853. assert config_paths == []
  854. assert max(log.levelno for log in logs) == logging.CRITICAL
  855. def test_log_record_does_not_raise():
  856. module.log_record(levelno=1, foo='bar', baz='quux')
  857. def test_log_record_with_suppress_does_not_raise():
  858. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  859. def test_log_error_records_generates_output_logs_for_message_only():
  860. flexmock(module).should_receive('log_record').replace_with(dict).once()
  861. logs = tuple(module.log_error_records('Error'))
  862. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  863. def test_log_error_records_generates_output_logs_for_called_process_error_with_bytes_ouput():
  864. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  865. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  866. logs = tuple(
  867. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', b'error output'))
  868. )
  869. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  870. assert any(log for log in logs if 'error output' in str(log))
  871. def test_log_error_records_generates_output_logs_for_called_process_error_with_string_ouput():
  872. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  873. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  874. logs = tuple(
  875. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  876. )
  877. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  878. assert any(log for log in logs if 'error output' in str(log))
  879. def test_log_error_records_splits_called_process_error_with_multiline_ouput_into_multiple_logs():
  880. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  881. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  882. logs = tuple(
  883. module.log_error_records(
  884. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output\nanother line')
  885. )
  886. )
  887. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  888. assert any(log for log in logs if 'error output' in str(log))
  889. def test_log_error_records_generates_logs_for_value_error():
  890. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  891. logs = tuple(module.log_error_records('Error', ValueError()))
  892. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  893. def test_log_error_records_generates_logs_for_os_error():
  894. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  895. logs = tuple(module.log_error_records('Error', OSError()))
  896. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  897. def test_log_error_records_generates_nothing_for_other_error():
  898. flexmock(module).should_receive('log_record').never()
  899. logs = tuple(module.log_error_records('Error', KeyError()))
  900. assert logs == ()
  901. def test_get_local_path_uses_configuration_value():
  902. assert module.get_local_path({'test.yaml': {'local_path': 'borg1'}}) == 'borg1'
  903. def test_get_local_path_without_local_path_defaults_to_borg():
  904. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  905. def test_collect_highlander_action_summary_logs_info_for_success_with_bootstrap():
  906. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  907. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  908. arguments = {
  909. 'bootstrap': flexmock(repository='repo'),
  910. 'global': flexmock(dry_run=False),
  911. }
  912. logs = tuple(
  913. module.collect_highlander_action_summary_logs(
  914. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  915. )
  916. )
  917. assert {log.levelno for log in logs} == {logging.ANSWER}
  918. def test_collect_highlander_action_summary_logs_error_on_bootstrap_failure():
  919. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  920. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  921. ValueError
  922. )
  923. arguments = {
  924. 'bootstrap': flexmock(repository='repo'),
  925. 'global': flexmock(dry_run=False),
  926. }
  927. logs = tuple(
  928. module.collect_highlander_action_summary_logs(
  929. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  930. )
  931. )
  932. assert {log.levelno for log in logs} == {logging.CRITICAL}
  933. def test_collect_highlander_action_summary_logs_error_on_bootstrap_local_borg_version_failure():
  934. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  935. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').never()
  936. arguments = {
  937. 'bootstrap': flexmock(repository='repo'),
  938. 'global': flexmock(dry_run=False),
  939. }
  940. logs = tuple(
  941. module.collect_highlander_action_summary_logs(
  942. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  943. )
  944. )
  945. assert {log.levelno for log in logs} == {logging.CRITICAL}
  946. def test_collect_highlander_action_summary_logs_info_for_success_with_generate():
  947. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate')
  948. arguments = {
  949. 'generate': flexmock(destination='test.yaml'),
  950. 'global': flexmock(dry_run=False),
  951. }
  952. logs = tuple(
  953. module.collect_highlander_action_summary_logs(
  954. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  955. )
  956. )
  957. assert {log.levelno for log in logs} == {logging.ANSWER}
  958. def test_collect_highlander_action_summary_logs_error_on_generate_failure():
  959. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate').and_raise(
  960. ValueError
  961. )
  962. arguments = {
  963. 'generate': flexmock(destination='test.yaml'),
  964. 'global': flexmock(dry_run=False),
  965. }
  966. logs = tuple(
  967. module.collect_highlander_action_summary_logs(
  968. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  969. )
  970. )
  971. assert {log.levelno for log in logs} == {logging.CRITICAL}
  972. def test_collect_highlander_action_summary_logs_info_for_success_with_validate():
  973. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  974. arguments = {
  975. 'validate': flexmock(),
  976. 'global': flexmock(dry_run=False),
  977. }
  978. logs = tuple(
  979. module.collect_highlander_action_summary_logs(
  980. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  981. )
  982. )
  983. assert {log.levelno for log in logs} == {logging.ANSWER}
  984. def test_collect_highlander_action_summary_logs_error_on_validate_parse_failure():
  985. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  986. arguments = {
  987. 'validate': flexmock(),
  988. 'global': flexmock(dry_run=False),
  989. }
  990. logs = tuple(
  991. module.collect_highlander_action_summary_logs(
  992. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=True
  993. )
  994. )
  995. assert {log.levelno for log in logs} == {logging.CRITICAL}
  996. def test_collect_highlander_action_summary_logs_error_on_run_validate_failure():
  997. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate').and_raise(
  998. ValueError
  999. )
  1000. arguments = {
  1001. 'validate': flexmock(),
  1002. 'global': flexmock(dry_run=False),
  1003. }
  1004. logs = tuple(
  1005. module.collect_highlander_action_summary_logs(
  1006. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1007. )
  1008. )
  1009. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1010. def test_collect_configuration_run_summary_logs_info_for_success():
  1011. flexmock(module.command).should_receive('execute_hook').never()
  1012. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1013. flexmock(module).should_receive('run_configuration').and_return([])
  1014. arguments = {}
  1015. logs = tuple(
  1016. module.collect_configuration_run_summary_logs(
  1017. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1018. )
  1019. )
  1020. assert {log.levelno for log in logs} == {logging.INFO}
  1021. def test_collect_configuration_run_summary_executes_hooks_for_create():
  1022. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1023. flexmock(module).should_receive('run_configuration').and_return([])
  1024. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  1025. logs = tuple(
  1026. module.collect_configuration_run_summary_logs(
  1027. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1028. )
  1029. )
  1030. assert {log.levelno for log in logs} == {logging.INFO}
  1031. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  1032. flexmock(module.validate).should_receive('guard_single_repository_selected')
  1033. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1034. flexmock(module).should_receive('run_configuration').and_return([])
  1035. arguments = {'extract': flexmock(repository='repo')}
  1036. logs = tuple(
  1037. module.collect_configuration_run_summary_logs(
  1038. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1039. )
  1040. )
  1041. assert {log.levelno for log in logs} == {logging.INFO}
  1042. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  1043. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1044. ValueError
  1045. )
  1046. expected_logs = (flexmock(),)
  1047. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1048. arguments = {'extract': flexmock(repository='repo')}
  1049. logs = tuple(
  1050. module.collect_configuration_run_summary_logs(
  1051. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1052. )
  1053. )
  1054. assert logs == expected_logs
  1055. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  1056. flexmock(module.validate).should_receive('guard_single_repository_selected')
  1057. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1058. flexmock(module).should_receive('run_configuration').and_return([])
  1059. arguments = {'mount': flexmock(repository='repo')}
  1060. logs = tuple(
  1061. module.collect_configuration_run_summary_logs(
  1062. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1063. )
  1064. )
  1065. assert {log.levelno for log in logs} == {logging.INFO}
  1066. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  1067. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1068. ValueError
  1069. )
  1070. expected_logs = (flexmock(),)
  1071. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1072. arguments = {'mount': flexmock(repository='repo')}
  1073. logs = tuple(
  1074. module.collect_configuration_run_summary_logs(
  1075. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1076. )
  1077. )
  1078. assert logs == expected_logs
  1079. def test_collect_configuration_run_summary_logs_missing_configs_error():
  1080. arguments = {'global': flexmock(config_paths=[])}
  1081. expected_logs = (flexmock(),)
  1082. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1083. logs = tuple(
  1084. module.collect_configuration_run_summary_logs({}, config_paths=[], arguments=arguments)
  1085. )
  1086. assert logs == expected_logs
  1087. def test_collect_configuration_run_summary_logs_pre_hook_error():
  1088. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  1089. expected_logs = (flexmock(),)
  1090. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1091. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  1092. logs = tuple(
  1093. module.collect_configuration_run_summary_logs(
  1094. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1095. )
  1096. )
  1097. assert logs == expected_logs
  1098. def test_collect_configuration_run_summary_logs_post_hook_error():
  1099. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  1100. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1101. flexmock(module).should_receive('run_configuration').and_return([])
  1102. expected_logs = (flexmock(),)
  1103. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1104. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  1105. logs = tuple(
  1106. module.collect_configuration_run_summary_logs(
  1107. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1108. )
  1109. )
  1110. assert expected_logs[0] in logs
  1111. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  1112. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1113. ValueError
  1114. )
  1115. expected_logs = (flexmock(),)
  1116. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1117. arguments = {'list': flexmock(repository='repo', archive='test')}
  1118. logs = tuple(
  1119. module.collect_configuration_run_summary_logs(
  1120. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1121. )
  1122. )
  1123. assert logs == expected_logs
  1124. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  1125. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1126. flexmock(module).should_receive('run_configuration').and_return([])
  1127. arguments = {'list': flexmock(repository='repo', archive=None)}
  1128. logs = tuple(
  1129. module.collect_configuration_run_summary_logs(
  1130. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1131. )
  1132. )
  1133. assert {log.levelno for log in logs} == {logging.INFO}
  1134. def test_collect_configuration_run_summary_logs_run_configuration_error():
  1135. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1136. flexmock(module).should_receive('run_configuration').and_return(
  1137. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1138. )
  1139. flexmock(module).should_receive('log_error_records').and_return([])
  1140. arguments = {}
  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.CRITICAL}
  1147. def test_collect_configuration_run_summary_logs_run_umount_error():
  1148. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1149. flexmock(module).should_receive('run_configuration').and_return([])
  1150. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  1151. flexmock(module).should_receive('log_error_records').and_return(
  1152. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1153. )
  1154. arguments = {'umount': flexmock(mount_point='/mnt')}
  1155. logs = tuple(
  1156. module.collect_configuration_run_summary_logs(
  1157. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1158. )
  1159. )
  1160. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  1161. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  1162. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1163. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  1164. ['baz']
  1165. )
  1166. stdout = flexmock()
  1167. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  1168. flexmock(module.sys).stdout = stdout
  1169. arguments = {}
  1170. tuple(
  1171. module.collect_configuration_run_summary_logs(
  1172. {'test.yaml': {}, 'test2.yaml': {}},
  1173. config_paths=['/tmp/test.yaml', '/tmp/test2.yaml'],
  1174. arguments=arguments,
  1175. )
  1176. )