test_borgmatic.py 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. import logging
  2. import subprocess
  3. import time
  4. from flexmock import flexmock
  5. import borgmatic.hooks.command
  6. from borgmatic.commands import borgmatic as module
  7. def test_run_configuration_runs_actions_for_each_repository():
  8. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  9. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  10. expected_results = [flexmock(), flexmock()]
  11. flexmock(module).should_receive('run_actions').and_return(expected_results[:1]).and_return(
  12. expected_results[1:]
  13. )
  14. config = {'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}}
  15. arguments = {'global': flexmock(monitoring_verbosity=1)}
  16. results = list(module.run_configuration('test.yaml', config, arguments))
  17. assert results == expected_results
  18. def test_run_configuration_with_invalid_borg_version_errors():
  19. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  20. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  21. flexmock(module.command).should_receive('execute_hook').never()
  22. flexmock(module.dispatch).should_receive('call_hooks').never()
  23. flexmock(module).should_receive('run_actions').never()
  24. config = {'location': {'repositories': ['foo']}}
  25. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'prune': flexmock()}
  26. list(module.run_configuration('test.yaml', config, arguments))
  27. def test_run_configuration_logs_monitor_start_error():
  28. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  29. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  30. flexmock(module.dispatch).should_receive('call_hooks').and_raise(OSError).and_return(
  31. None
  32. ).and_return(None).and_return(None)
  33. expected_results = [flexmock()]
  34. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  35. flexmock(module).should_receive('run_actions').never()
  36. config = {'location': {'repositories': ['foo']}}
  37. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  38. results = list(module.run_configuration('test.yaml', config, arguments))
  39. assert results == expected_results
  40. def test_run_configuration_bails_for_monitor_start_soft_failure():
  41. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  42. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  43. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  44. flexmock(module.dispatch).should_receive('call_hooks').and_raise(error)
  45. flexmock(module).should_receive('log_error_records').never()
  46. flexmock(module).should_receive('run_actions').never()
  47. config = {'location': {'repositories': ['foo']}}
  48. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  49. results = list(module.run_configuration('test.yaml', config, arguments))
  50. assert results == []
  51. def test_run_configuration_logs_actions_error():
  52. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  53. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  54. flexmock(module.command).should_receive('execute_hook')
  55. flexmock(module.dispatch).should_receive('call_hooks')
  56. expected_results = [flexmock()]
  57. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  58. flexmock(module).should_receive('run_actions').and_raise(OSError)
  59. config = {'location': {'repositories': [{'path': 'foo'}]}}
  60. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  61. results = list(module.run_configuration('test.yaml', config, arguments))
  62. assert results == expected_results
  63. def test_run_configuration_bails_for_actions_soft_failure():
  64. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  65. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  66. flexmock(module.dispatch).should_receive('call_hooks')
  67. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  68. flexmock(module).should_receive('run_actions').and_raise(error)
  69. flexmock(module).should_receive('log_error_records').never()
  70. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  71. config = {'location': {'repositories': [{'path': 'foo'}]}}
  72. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  73. results = list(module.run_configuration('test.yaml', config, arguments))
  74. assert results == []
  75. def test_run_configuration_logs_monitor_log_error():
  76. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  77. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  78. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  79. None
  80. ).and_raise(OSError)
  81. expected_results = [flexmock()]
  82. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  83. flexmock(module).should_receive('run_actions').and_return([])
  84. config = {'location': {'repositories': [{'path': 'foo'}]}}
  85. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  86. results = list(module.run_configuration('test.yaml', config, arguments))
  87. assert results == expected_results
  88. def test_run_configuration_bails_for_monitor_log_soft_failure():
  89. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  90. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  91. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  92. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  93. None
  94. ).and_raise(error)
  95. flexmock(module).should_receive('log_error_records').never()
  96. flexmock(module).should_receive('run_actions').and_return([])
  97. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  98. config = {'location': {'repositories': [{'path': 'foo'}]}}
  99. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  100. results = list(module.run_configuration('test.yaml', config, arguments))
  101. assert results == []
  102. def test_run_configuration_logs_monitor_finish_error():
  103. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  104. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  105. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  106. None
  107. ).and_return(None).and_raise(OSError)
  108. expected_results = [flexmock()]
  109. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  110. flexmock(module).should_receive('run_actions').and_return([])
  111. config = {'location': {'repositories': [{'path': 'foo'}]}}
  112. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  113. results = list(module.run_configuration('test.yaml', config, arguments))
  114. assert results == expected_results
  115. def test_run_configuration_bails_for_monitor_finish_soft_failure():
  116. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  117. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  118. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  119. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  120. None
  121. ).and_raise(None).and_raise(error)
  122. flexmock(module).should_receive('log_error_records').never()
  123. flexmock(module).should_receive('run_actions').and_return([])
  124. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  125. config = {'location': {'repositories': [{'path': 'foo'}]}}
  126. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  127. results = list(module.run_configuration('test.yaml', config, arguments))
  128. assert results == []
  129. def test_run_configuration_logs_on_error_hook_error():
  130. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  131. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  132. flexmock(module.command).should_receive('execute_hook').and_raise(OSError)
  133. expected_results = [flexmock(), flexmock()]
  134. flexmock(module).should_receive('log_error_records').and_return(
  135. expected_results[:1]
  136. ).and_return(expected_results[1:])
  137. flexmock(module).should_receive('run_actions').and_raise(OSError)
  138. config = {'location': {'repositories': [{'path': 'foo'}]}}
  139. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  140. results = list(module.run_configuration('test.yaml', config, arguments))
  141. assert results == expected_results
  142. def test_run_configuration_bails_for_on_error_hook_soft_failure():
  143. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  144. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  145. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  146. flexmock(module.command).should_receive('execute_hook').and_raise(error)
  147. expected_results = [flexmock()]
  148. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  149. flexmock(module).should_receive('run_actions').and_raise(OSError)
  150. config = {'location': {'repositories': [{'path': 'foo'}]}}
  151. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  152. results = list(module.run_configuration('test.yaml', config, arguments))
  153. assert results == expected_results
  154. def test_run_configuration_retries_soft_error():
  155. # Run action first fails, second passes
  156. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  157. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  158. flexmock(module.command).should_receive('execute_hook')
  159. flexmock(module).should_receive('run_actions').and_raise(OSError).and_return([])
  160. flexmock(module).should_receive('log_error_records').and_return([flexmock()]).once()
  161. config = {'location': {'repositories': [{'path': 'foo'}]}, 'storage': {'retries': 1}}
  162. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  163. results = list(module.run_configuration('test.yaml', config, arguments))
  164. assert results == []
  165. def test_run_configuration_retries_hard_error():
  166. # Run action fails twice
  167. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  168. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  169. flexmock(module.command).should_receive('execute_hook')
  170. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  171. flexmock(module).should_receive('log_error_records').with_args(
  172. 'foo: Error running actions for repository',
  173. OSError,
  174. levelno=logging.WARNING,
  175. log_command_error_output=True,
  176. ).and_return([flexmock()])
  177. error_logs = [flexmock()]
  178. flexmock(module).should_receive('log_error_records').with_args(
  179. 'foo: Error running actions for repository',
  180. OSError,
  181. ).and_return(error_logs)
  182. config = {'location': {'repositories': [{'path': 'foo'}]}, 'storage': {'retries': 1}}
  183. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  184. results = list(module.run_configuration('test.yaml', config, arguments))
  185. assert results == error_logs
  186. def test_run_configuration_repos_ordered():
  187. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  188. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  189. flexmock(module.command).should_receive('execute_hook')
  190. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  191. expected_results = [flexmock(), flexmock()]
  192. flexmock(module).should_receive('log_error_records').with_args(
  193. 'foo: Error running actions for repository', OSError
  194. ).and_return(expected_results[:1]).ordered()
  195. flexmock(module).should_receive('log_error_records').with_args(
  196. 'bar: Error running actions for repository', OSError
  197. ).and_return(expected_results[1:]).ordered()
  198. config = {'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}}
  199. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  200. results = list(module.run_configuration('test.yaml', config, arguments))
  201. assert results == expected_results
  202. def test_run_configuration_retries_round_robin():
  203. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  204. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  205. flexmock(module.command).should_receive('execute_hook')
  206. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  207. flexmock(module).should_receive('log_error_records').with_args(
  208. 'foo: Error running actions for repository',
  209. OSError,
  210. levelno=logging.WARNING,
  211. log_command_error_output=True,
  212. ).and_return([flexmock()]).ordered()
  213. flexmock(module).should_receive('log_error_records').with_args(
  214. 'bar: Error running actions for repository',
  215. OSError,
  216. levelno=logging.WARNING,
  217. log_command_error_output=True,
  218. ).and_return([flexmock()]).ordered()
  219. foo_error_logs = [flexmock()]
  220. flexmock(module).should_receive('log_error_records').with_args(
  221. 'foo: Error running actions for repository', OSError
  222. ).and_return(foo_error_logs).ordered()
  223. bar_error_logs = [flexmock()]
  224. flexmock(module).should_receive('log_error_records').with_args(
  225. 'bar: Error running actions for repository', OSError
  226. ).and_return(bar_error_logs).ordered()
  227. config = {
  228. 'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]},
  229. 'storage': {'retries': 1},
  230. }
  231. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  232. results = list(module.run_configuration('test.yaml', config, arguments))
  233. assert results == foo_error_logs + bar_error_logs
  234. def test_run_configuration_retries_one_passes():
  235. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  236. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  237. flexmock(module.command).should_receive('execute_hook')
  238. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  239. []
  240. ).and_raise(OSError).times(4)
  241. flexmock(module).should_receive('log_error_records').with_args(
  242. 'foo: Error running actions for repository',
  243. OSError,
  244. levelno=logging.WARNING,
  245. log_command_error_output=True,
  246. ).and_return([flexmock()]).ordered()
  247. flexmock(module).should_receive('log_error_records').with_args(
  248. 'bar: Error running actions for repository',
  249. OSError,
  250. levelno=logging.WARNING,
  251. log_command_error_output=True,
  252. ).and_return(flexmock()).ordered()
  253. error_logs = [flexmock()]
  254. flexmock(module).should_receive('log_error_records').with_args(
  255. 'bar: Error running actions for repository', OSError
  256. ).and_return(error_logs).ordered()
  257. config = {
  258. 'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]},
  259. 'storage': {'retries': 1},
  260. }
  261. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  262. results = list(module.run_configuration('test.yaml', config, arguments))
  263. assert results == error_logs
  264. def test_run_configuration_retry_wait():
  265. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  266. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  267. flexmock(module.command).should_receive('execute_hook')
  268. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  269. flexmock(module).should_receive('log_error_records').with_args(
  270. 'foo: Error running actions for repository',
  271. OSError,
  272. levelno=logging.WARNING,
  273. log_command_error_output=True,
  274. ).and_return([flexmock()]).ordered()
  275. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  276. flexmock(module).should_receive('log_error_records').with_args(
  277. 'foo: Error running actions for repository',
  278. OSError,
  279. levelno=logging.WARNING,
  280. log_command_error_output=True,
  281. ).and_return([flexmock()]).ordered()
  282. flexmock(time).should_receive('sleep').with_args(20).and_return().ordered()
  283. flexmock(module).should_receive('log_error_records').with_args(
  284. 'foo: Error running actions for repository',
  285. OSError,
  286. levelno=logging.WARNING,
  287. log_command_error_output=True,
  288. ).and_return([flexmock()]).ordered()
  289. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  290. error_logs = [flexmock()]
  291. flexmock(module).should_receive('log_error_records').with_args(
  292. 'foo: Error running actions for repository', OSError
  293. ).and_return(error_logs).ordered()
  294. config = {
  295. 'location': {'repositories': [{'path': 'foo'}]},
  296. 'storage': {'retries': 3, 'retry_wait': 10},
  297. }
  298. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  299. results = list(module.run_configuration('test.yaml', config, arguments))
  300. assert results == error_logs
  301. def test_run_configuration_retries_timeout_multiple_repos():
  302. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  303. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  304. flexmock(module.command).should_receive('execute_hook')
  305. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  306. []
  307. ).and_raise(OSError).times(4)
  308. flexmock(module).should_receive('log_error_records').with_args(
  309. 'foo: Error running actions for repository',
  310. OSError,
  311. levelno=logging.WARNING,
  312. log_command_error_output=True,
  313. ).and_return([flexmock()]).ordered()
  314. flexmock(module).should_receive('log_error_records').with_args(
  315. 'bar: Error running actions for repository',
  316. OSError,
  317. levelno=logging.WARNING,
  318. log_command_error_output=True,
  319. ).and_return([flexmock()]).ordered()
  320. # Sleep before retrying foo (and passing)
  321. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  322. # Sleep before retrying bar (and failing)
  323. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  324. error_logs = [flexmock()]
  325. flexmock(module).should_receive('log_error_records').with_args(
  326. 'bar: Error running actions for repository', OSError
  327. ).and_return(error_logs).ordered()
  328. config = {
  329. 'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]},
  330. 'storage': {'retries': 1, 'retry_wait': 10},
  331. }
  332. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  333. results = list(module.run_configuration('test.yaml', config, arguments))
  334. assert results == error_logs
  335. def test_run_actions_runs_rcreate():
  336. flexmock(module).should_receive('add_custom_log_levels')
  337. flexmock(module.command).should_receive('execute_hook')
  338. flexmock(borgmatic.actions.rcreate).should_receive('run_rcreate').once()
  339. tuple(
  340. module.run_actions(
  341. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rcreate': flexmock()},
  342. config_filename=flexmock(),
  343. location={'repositories': []},
  344. storage=flexmock(),
  345. retention=flexmock(),
  346. consistency=flexmock(),
  347. hooks={},
  348. local_path=flexmock(),
  349. remote_path=flexmock(),
  350. local_borg_version=flexmock(),
  351. repository={'path': 'repo'},
  352. )
  353. )
  354. def test_run_actions_adds_log_file_to_hook_context():
  355. flexmock(module).should_receive('add_custom_log_levels')
  356. flexmock(module.command).should_receive('execute_hook')
  357. expected = flexmock()
  358. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  359. config_filename=object,
  360. repository={'path': 'repo'},
  361. location={'repositories': []},
  362. storage=object,
  363. hooks={},
  364. hook_context={'repository': 'repo', 'repositories': '', 'log_file': 'foo'},
  365. local_borg_version=object,
  366. create_arguments=object,
  367. global_arguments=object,
  368. dry_run_label='',
  369. local_path=object,
  370. remote_path=object,
  371. ).once().and_return(expected)
  372. result = tuple(
  373. module.run_actions(
  374. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  375. config_filename=flexmock(),
  376. location={'repositories': []},
  377. storage=flexmock(),
  378. retention=flexmock(),
  379. consistency=flexmock(),
  380. hooks={},
  381. local_path=flexmock(),
  382. remote_path=flexmock(),
  383. local_borg_version=flexmock(),
  384. repository={'path': 'repo'},
  385. )
  386. )
  387. assert result == (expected,)
  388. def test_run_actions_runs_transfer():
  389. flexmock(module).should_receive('add_custom_log_levels')
  390. flexmock(module.command).should_receive('execute_hook')
  391. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  392. tuple(
  393. module.run_actions(
  394. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'transfer': flexmock()},
  395. config_filename=flexmock(),
  396. location={'repositories': []},
  397. storage=flexmock(),
  398. retention=flexmock(),
  399. consistency=flexmock(),
  400. hooks={},
  401. local_path=flexmock(),
  402. remote_path=flexmock(),
  403. local_borg_version=flexmock(),
  404. repository={'path': 'repo'},
  405. )
  406. )
  407. def test_run_actions_runs_create():
  408. flexmock(module).should_receive('add_custom_log_levels')
  409. flexmock(module.command).should_receive('execute_hook')
  410. expected = flexmock()
  411. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  412. result = tuple(
  413. module.run_actions(
  414. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  415. config_filename=flexmock(),
  416. location={'repositories': []},
  417. storage=flexmock(),
  418. retention=flexmock(),
  419. consistency=flexmock(),
  420. hooks={},
  421. local_path=flexmock(),
  422. remote_path=flexmock(),
  423. local_borg_version=flexmock(),
  424. repository={'path': 'repo'},
  425. )
  426. )
  427. assert result == (expected,)
  428. def test_run_actions_runs_prune():
  429. flexmock(module).should_receive('add_custom_log_levels')
  430. flexmock(module.command).should_receive('execute_hook')
  431. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  432. tuple(
  433. module.run_actions(
  434. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  435. config_filename=flexmock(),
  436. location={'repositories': []},
  437. storage=flexmock(),
  438. retention=flexmock(),
  439. consistency=flexmock(),
  440. hooks={},
  441. local_path=flexmock(),
  442. remote_path=flexmock(),
  443. local_borg_version=flexmock(),
  444. repository={'path': 'repo'},
  445. )
  446. )
  447. def test_run_actions_runs_compact():
  448. flexmock(module).should_receive('add_custom_log_levels')
  449. flexmock(module.command).should_receive('execute_hook')
  450. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  451. tuple(
  452. module.run_actions(
  453. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  454. config_filename=flexmock(),
  455. location={'repositories': []},
  456. storage=flexmock(),
  457. retention=flexmock(),
  458. consistency=flexmock(),
  459. hooks={},
  460. local_path=flexmock(),
  461. remote_path=flexmock(),
  462. local_borg_version=flexmock(),
  463. repository={'path': 'repo'},
  464. )
  465. )
  466. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  467. flexmock(module).should_receive('add_custom_log_levels')
  468. flexmock(module.command).should_receive('execute_hook')
  469. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  470. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  471. tuple(
  472. module.run_actions(
  473. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  474. config_filename=flexmock(),
  475. location={'repositories': []},
  476. storage=flexmock(),
  477. retention=flexmock(),
  478. consistency=flexmock(),
  479. hooks={},
  480. local_path=flexmock(),
  481. remote_path=flexmock(),
  482. local_borg_version=flexmock(),
  483. repository={'path': 'repo'},
  484. )
  485. )
  486. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  487. flexmock(module).should_receive('add_custom_log_levels')
  488. flexmock(module.command).should_receive('execute_hook')
  489. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  490. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  491. tuple(
  492. module.run_actions(
  493. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  494. config_filename=flexmock(),
  495. location={'repositories': []},
  496. storage=flexmock(),
  497. retention=flexmock(),
  498. consistency=flexmock(),
  499. hooks={},
  500. local_path=flexmock(),
  501. remote_path=flexmock(),
  502. local_borg_version=flexmock(),
  503. repository={'path': 'repo'},
  504. )
  505. )
  506. def test_run_actions_runs_extract():
  507. flexmock(module).should_receive('add_custom_log_levels')
  508. flexmock(module.command).should_receive('execute_hook')
  509. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  510. tuple(
  511. module.run_actions(
  512. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'extract': flexmock()},
  513. config_filename=flexmock(),
  514. location={'repositories': []},
  515. storage=flexmock(),
  516. retention=flexmock(),
  517. consistency=flexmock(),
  518. hooks={},
  519. local_path=flexmock(),
  520. remote_path=flexmock(),
  521. local_borg_version=flexmock(),
  522. repository={'path': 'repo'},
  523. )
  524. )
  525. def test_run_actions_runs_export_tar():
  526. flexmock(module).should_receive('add_custom_log_levels')
  527. flexmock(module.command).should_receive('execute_hook')
  528. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  529. tuple(
  530. module.run_actions(
  531. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export-tar': flexmock()},
  532. config_filename=flexmock(),
  533. location={'repositories': []},
  534. storage=flexmock(),
  535. retention=flexmock(),
  536. consistency=flexmock(),
  537. hooks={},
  538. local_path=flexmock(),
  539. remote_path=flexmock(),
  540. local_borg_version=flexmock(),
  541. repository={'path': 'repo'},
  542. )
  543. )
  544. def test_run_actions_runs_mount():
  545. flexmock(module).should_receive('add_custom_log_levels')
  546. flexmock(module.command).should_receive('execute_hook')
  547. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  548. tuple(
  549. module.run_actions(
  550. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'mount': flexmock()},
  551. config_filename=flexmock(),
  552. location={'repositories': []},
  553. storage=flexmock(),
  554. retention=flexmock(),
  555. consistency=flexmock(),
  556. hooks={},
  557. local_path=flexmock(),
  558. remote_path=flexmock(),
  559. local_borg_version=flexmock(),
  560. repository={'path': 'repo'},
  561. )
  562. )
  563. def test_run_actions_runs_restore():
  564. flexmock(module).should_receive('add_custom_log_levels')
  565. flexmock(module.command).should_receive('execute_hook')
  566. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  567. tuple(
  568. module.run_actions(
  569. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'restore': flexmock()},
  570. config_filename=flexmock(),
  571. location={'repositories': []},
  572. storage=flexmock(),
  573. retention=flexmock(),
  574. consistency=flexmock(),
  575. hooks={},
  576. local_path=flexmock(),
  577. remote_path=flexmock(),
  578. local_borg_version=flexmock(),
  579. repository={'path': 'repo'},
  580. )
  581. )
  582. def test_run_actions_runs_rlist():
  583. flexmock(module).should_receive('add_custom_log_levels')
  584. flexmock(module.command).should_receive('execute_hook')
  585. expected = flexmock()
  586. flexmock(borgmatic.actions.rlist).should_receive('run_rlist').and_yield(expected).once()
  587. result = tuple(
  588. module.run_actions(
  589. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rlist': flexmock()},
  590. config_filename=flexmock(),
  591. location={'repositories': []},
  592. storage=flexmock(),
  593. retention=flexmock(),
  594. consistency=flexmock(),
  595. hooks={},
  596. local_path=flexmock(),
  597. remote_path=flexmock(),
  598. local_borg_version=flexmock(),
  599. repository={'path': 'repo'},
  600. )
  601. )
  602. assert result == (expected,)
  603. def test_run_actions_runs_list():
  604. flexmock(module).should_receive('add_custom_log_levels')
  605. flexmock(module.command).should_receive('execute_hook')
  606. expected = flexmock()
  607. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  608. result = tuple(
  609. module.run_actions(
  610. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'list': flexmock()},
  611. config_filename=flexmock(),
  612. location={'repositories': []},
  613. storage=flexmock(),
  614. retention=flexmock(),
  615. consistency=flexmock(),
  616. hooks={},
  617. local_path=flexmock(),
  618. remote_path=flexmock(),
  619. local_borg_version=flexmock(),
  620. repository={'path': 'repo'},
  621. )
  622. )
  623. assert result == (expected,)
  624. def test_run_actions_runs_rinfo():
  625. flexmock(module).should_receive('add_custom_log_levels')
  626. flexmock(module.command).should_receive('execute_hook')
  627. expected = flexmock()
  628. flexmock(borgmatic.actions.rinfo).should_receive('run_rinfo').and_yield(expected).once()
  629. result = tuple(
  630. module.run_actions(
  631. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rinfo': flexmock()},
  632. config_filename=flexmock(),
  633. location={'repositories': []},
  634. storage=flexmock(),
  635. retention=flexmock(),
  636. consistency=flexmock(),
  637. hooks={},
  638. local_path=flexmock(),
  639. remote_path=flexmock(),
  640. local_borg_version=flexmock(),
  641. repository={'path': 'repo'},
  642. )
  643. )
  644. assert result == (expected,)
  645. def test_run_actions_runs_info():
  646. flexmock(module).should_receive('add_custom_log_levels')
  647. flexmock(module.command).should_receive('execute_hook')
  648. expected = flexmock()
  649. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  650. result = tuple(
  651. module.run_actions(
  652. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'info': flexmock()},
  653. config_filename=flexmock(),
  654. location={'repositories': []},
  655. storage=flexmock(),
  656. retention=flexmock(),
  657. consistency=flexmock(),
  658. hooks={},
  659. local_path=flexmock(),
  660. remote_path=flexmock(),
  661. local_borg_version=flexmock(),
  662. repository={'path': 'repo'},
  663. )
  664. )
  665. assert result == (expected,)
  666. def test_run_actions_runs_break_lock():
  667. flexmock(module).should_receive('add_custom_log_levels')
  668. flexmock(module.command).should_receive('execute_hook')
  669. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  670. tuple(
  671. module.run_actions(
  672. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'break-lock': flexmock()},
  673. config_filename=flexmock(),
  674. location={'repositories': []},
  675. storage=flexmock(),
  676. retention=flexmock(),
  677. consistency=flexmock(),
  678. hooks={},
  679. local_path=flexmock(),
  680. remote_path=flexmock(),
  681. local_borg_version=flexmock(),
  682. repository={'path': 'repo'},
  683. )
  684. )
  685. def test_run_actions_runs_borg():
  686. flexmock(module).should_receive('add_custom_log_levels')
  687. flexmock(module.command).should_receive('execute_hook')
  688. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  689. tuple(
  690. module.run_actions(
  691. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'borg': flexmock()},
  692. config_filename=flexmock(),
  693. location={'repositories': []},
  694. storage=flexmock(),
  695. retention=flexmock(),
  696. consistency=flexmock(),
  697. hooks={},
  698. local_path=flexmock(),
  699. remote_path=flexmock(),
  700. local_borg_version=flexmock(),
  701. repository={'path': 'repo'},
  702. )
  703. )
  704. def test_run_actions_runs_multiple_actions_in_argument_order():
  705. flexmock(module).should_receive('add_custom_log_levels')
  706. flexmock(module.command).should_receive('execute_hook')
  707. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  708. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  709. tuple(
  710. module.run_actions(
  711. arguments={
  712. 'global': flexmock(dry_run=False, log_file='foo'),
  713. 'borg': flexmock(),
  714. 'restore': flexmock(),
  715. },
  716. config_filename=flexmock(),
  717. location={'repositories': []},
  718. storage=flexmock(),
  719. retention=flexmock(),
  720. consistency=flexmock(),
  721. hooks={},
  722. local_path=flexmock(),
  723. remote_path=flexmock(),
  724. local_borg_version=flexmock(),
  725. repository={'path': 'repo'},
  726. )
  727. )
  728. def test_load_configurations_collects_parsed_configurations_and_logs():
  729. configuration = flexmock()
  730. other_configuration = flexmock()
  731. test_expected_logs = [flexmock(), flexmock()]
  732. other_expected_logs = [flexmock(), flexmock()]
  733. flexmock(module.validate).should_receive('parse_configuration').and_return(
  734. configuration, test_expected_logs
  735. ).and_return(other_configuration, other_expected_logs)
  736. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  737. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  738. assert logs == test_expected_logs + other_expected_logs
  739. def test_load_configurations_logs_warning_for_permission_error():
  740. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  741. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  742. assert configs == {}
  743. assert {log.levelno for log in logs} == {logging.WARNING}
  744. def test_load_configurations_logs_critical_for_parse_error():
  745. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  746. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  747. assert configs == {}
  748. assert {log.levelno for log in logs} == {logging.CRITICAL}
  749. def test_log_record_does_not_raise():
  750. module.log_record(levelno=1, foo='bar', baz='quux')
  751. def test_log_record_with_suppress_does_not_raise():
  752. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  753. def test_log_error_records_generates_output_logs_for_message_only():
  754. flexmock(module).should_receive('log_record').replace_with(dict)
  755. logs = tuple(module.log_error_records('Error'))
  756. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  757. def test_log_error_records_generates_output_logs_for_called_process_error():
  758. flexmock(module).should_receive('log_record').replace_with(dict)
  759. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  760. logs = tuple(
  761. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  762. )
  763. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  764. assert any(log for log in logs if 'error output' in str(log))
  765. def test_log_error_records_generates_logs_for_value_error():
  766. flexmock(module).should_receive('log_record').replace_with(dict)
  767. logs = tuple(module.log_error_records('Error', ValueError()))
  768. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  769. def test_log_error_records_generates_logs_for_os_error():
  770. flexmock(module).should_receive('log_record').replace_with(dict)
  771. logs = tuple(module.log_error_records('Error', OSError()))
  772. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  773. def test_log_error_records_generates_nothing_for_other_error():
  774. flexmock(module).should_receive('log_record').replace_with(dict)
  775. logs = tuple(module.log_error_records('Error', KeyError()))
  776. assert logs == ()
  777. def test_get_local_path_uses_configuration_value():
  778. assert module.get_local_path({'test.yaml': {'location': {'local_path': 'borg1'}}}) == 'borg1'
  779. def test_get_local_path_without_location_defaults_to_borg():
  780. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  781. def test_get_local_path_without_local_path_defaults_to_borg():
  782. assert module.get_local_path({'test.yaml': {'location': {}}}) == 'borg'
  783. def test_collect_configuration_run_summary_logs_info_for_success():
  784. flexmock(module.command).should_receive('execute_hook').never()
  785. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  786. flexmock(module).should_receive('run_configuration').and_return([])
  787. arguments = {}
  788. logs = tuple(
  789. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  790. )
  791. assert {log.levelno for log in logs} == {logging.INFO}
  792. def test_collect_configuration_run_summary_executes_hooks_for_create():
  793. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  794. flexmock(module).should_receive('run_configuration').and_return([])
  795. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  796. logs = tuple(
  797. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  798. )
  799. assert {log.levelno for log in logs} == {logging.INFO}
  800. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  801. flexmock(module.validate).should_receive('guard_single_repository_selected')
  802. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  803. flexmock(module).should_receive('run_configuration').and_return([])
  804. arguments = {'extract': flexmock(repository='repo')}
  805. logs = tuple(
  806. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  807. )
  808. assert {log.levelno for log in logs} == {logging.INFO}
  809. def test_collect_configuration_run_summary_logs_info_for_success_with_bootstrap():
  810. flexmock(module.validate).should_receive('guard_single_repository_selected').never()
  811. flexmock(module.validate).should_receive('guard_configuration_contains_repository').never()
  812. flexmock(module).should_receive('run_configuration').never()
  813. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  814. arguments = {
  815. 'bootstrap': flexmock(repository='repo'),
  816. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  817. }
  818. logs = tuple(
  819. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  820. )
  821. assert {log.levelno for log in logs} == {logging.INFO}
  822. def test_collect_configuration_run_summary_logs_error_on_bootstrap_failure():
  823. flexmock(module.validate).should_receive('guard_single_repository_selected').never()
  824. flexmock(module.validate).should_receive('guard_configuration_contains_repository').never()
  825. flexmock(module).should_receive('run_configuration').never()
  826. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  827. ValueError
  828. )
  829. arguments = {
  830. 'bootstrap': flexmock(repository='repo'),
  831. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  832. }
  833. logs = tuple(
  834. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  835. )
  836. assert {log.levelno for log in logs} == {logging.CRITICAL}
  837. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  838. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  839. ValueError
  840. )
  841. expected_logs = (flexmock(),)
  842. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  843. arguments = {'extract': flexmock(repository='repo')}
  844. logs = tuple(
  845. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  846. )
  847. assert logs == expected_logs
  848. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  849. flexmock(module.validate).should_receive('guard_single_repository_selected')
  850. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  851. flexmock(module).should_receive('run_configuration').and_return([])
  852. arguments = {'mount': flexmock(repository='repo')}
  853. logs = tuple(
  854. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  855. )
  856. assert {log.levelno for log in logs} == {logging.INFO}
  857. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  858. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  859. ValueError
  860. )
  861. expected_logs = (flexmock(),)
  862. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  863. arguments = {'mount': flexmock(repository='repo')}
  864. logs = tuple(
  865. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  866. )
  867. assert logs == expected_logs
  868. def test_collect_configuration_run_summary_logs_missing_configs_error():
  869. arguments = {'global': flexmock(config_paths=[])}
  870. expected_logs = (flexmock(),)
  871. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  872. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  873. assert logs == expected_logs
  874. def test_collect_configuration_run_summary_logs_pre_hook_error():
  875. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  876. expected_logs = (flexmock(),)
  877. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  878. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  879. logs = tuple(
  880. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  881. )
  882. assert logs == expected_logs
  883. def test_collect_configuration_run_summary_logs_post_hook_error():
  884. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  885. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  886. flexmock(module).should_receive('run_configuration').and_return([])
  887. expected_logs = (flexmock(),)
  888. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  889. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  890. logs = tuple(
  891. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  892. )
  893. assert expected_logs[0] in logs
  894. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  895. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  896. ValueError
  897. )
  898. expected_logs = (flexmock(),)
  899. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  900. arguments = {'list': flexmock(repository='repo', archive='test')}
  901. logs = tuple(
  902. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  903. )
  904. assert logs == expected_logs
  905. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  906. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  907. flexmock(module).should_receive('run_configuration').and_return([])
  908. arguments = {'list': flexmock(repository='repo', archive=None)}
  909. logs = tuple(
  910. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  911. )
  912. assert {log.levelno for log in logs} == {logging.INFO}
  913. def test_collect_configuration_run_summary_logs_run_configuration_error():
  914. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  915. flexmock(module).should_receive('run_configuration').and_return(
  916. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  917. )
  918. flexmock(module).should_receive('log_error_records').and_return([])
  919. arguments = {}
  920. logs = tuple(
  921. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  922. )
  923. assert {log.levelno for log in logs} == {logging.CRITICAL}
  924. def test_collect_configuration_run_summary_logs_run_umount_error():
  925. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  926. flexmock(module).should_receive('run_configuration').and_return([])
  927. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  928. flexmock(module).should_receive('log_error_records').and_return(
  929. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  930. )
  931. arguments = {'umount': flexmock(mount_point='/mnt')}
  932. logs = tuple(
  933. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  934. )
  935. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  936. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  937. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  938. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  939. ['baz']
  940. )
  941. stdout = flexmock()
  942. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  943. flexmock(module.sys).stdout = stdout
  944. arguments = {}
  945. tuple(
  946. module.collect_configuration_run_summary_logs(
  947. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  948. )
  949. )