test_borgmatic.py 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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': ['foo', '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)
  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': ['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': ['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_finish_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': ['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_finish_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': ['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_on_error_hook_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.command).should_receive('execute_hook').and_raise(OSError)
  106. expected_results = [flexmock(), flexmock()]
  107. flexmock(module).should_receive('log_error_records').and_return(
  108. expected_results[:1]
  109. ).and_return(expected_results[1:])
  110. flexmock(module).should_receive('run_actions').and_raise(OSError)
  111. config = {'location': {'repositories': ['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_on_error_hook_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.command).should_receive('execute_hook').and_raise(error)
  120. expected_results = [flexmock()]
  121. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  122. flexmock(module).should_receive('run_actions').and_raise(OSError)
  123. config = {'location': {'repositories': ['foo']}}
  124. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  125. results = list(module.run_configuration('test.yaml', config, arguments))
  126. assert results == expected_results
  127. def test_run_configuration_retries_soft_error():
  128. # Run action first fails, second passes
  129. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  130. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  131. flexmock(module.command).should_receive('execute_hook')
  132. flexmock(module).should_receive('run_actions').and_raise(OSError).and_return([])
  133. flexmock(module).should_receive('log_error_records').and_return([flexmock()]).once()
  134. config = {'location': {'repositories': ['foo']}, 'storage': {'retries': 1}}
  135. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  136. results = list(module.run_configuration('test.yaml', config, arguments))
  137. assert results == []
  138. def test_run_configuration_retries_hard_error():
  139. # Run action fails twice
  140. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  141. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  142. flexmock(module.command).should_receive('execute_hook')
  143. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  144. flexmock(module).should_receive('log_error_records').with_args(
  145. 'foo: Error running actions for repository',
  146. OSError,
  147. levelno=logging.WARNING,
  148. log_command_error_output=True,
  149. ).and_return([flexmock()])
  150. error_logs = [flexmock()]
  151. flexmock(module).should_receive('log_error_records').with_args(
  152. 'foo: Error running actions for repository', OSError,
  153. ).and_return(error_logs)
  154. config = {'location': {'repositories': ['foo']}, 'storage': {'retries': 1}}
  155. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  156. results = list(module.run_configuration('test.yaml', config, arguments))
  157. assert results == error_logs
  158. def test_run_configuration_repos_ordered():
  159. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  160. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  161. flexmock(module.command).should_receive('execute_hook')
  162. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  163. expected_results = [flexmock(), flexmock()]
  164. flexmock(module).should_receive('log_error_records').with_args(
  165. 'foo: Error running actions for repository', OSError
  166. ).and_return(expected_results[:1]).ordered()
  167. flexmock(module).should_receive('log_error_records').with_args(
  168. 'bar: Error running actions for repository', OSError
  169. ).and_return(expected_results[1:]).ordered()
  170. config = {'location': {'repositories': ['foo', 'bar']}}
  171. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  172. results = list(module.run_configuration('test.yaml', config, arguments))
  173. assert results == expected_results
  174. def test_run_configuration_retries_round_robbin():
  175. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  176. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  177. flexmock(module.command).should_receive('execute_hook')
  178. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  179. flexmock(module).should_receive('log_error_records').with_args(
  180. 'foo: Error running actions for repository',
  181. OSError,
  182. levelno=logging.WARNING,
  183. log_command_error_output=True,
  184. ).and_return([flexmock()]).ordered()
  185. flexmock(module).should_receive('log_error_records').with_args(
  186. 'bar: Error running actions for repository',
  187. OSError,
  188. levelno=logging.WARNING,
  189. log_command_error_output=True,
  190. ).and_return([flexmock()]).ordered()
  191. foo_error_logs = [flexmock()]
  192. flexmock(module).should_receive('log_error_records').with_args(
  193. 'foo: Error running actions for repository', OSError
  194. ).and_return(foo_error_logs).ordered()
  195. bar_error_logs = [flexmock()]
  196. flexmock(module).should_receive('log_error_records').with_args(
  197. 'bar: Error running actions for repository', OSError
  198. ).and_return(bar_error_logs).ordered()
  199. config = {'location': {'repositories': ['foo', 'bar']}, 'storage': {'retries': 1}}
  200. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  201. results = list(module.run_configuration('test.yaml', config, arguments))
  202. assert results == foo_error_logs + bar_error_logs
  203. def test_run_configuration_retries_one_passes():
  204. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  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_raise(OSError).and_return(
  208. []
  209. ).and_raise(OSError).times(4)
  210. flexmock(module).should_receive('log_error_records').with_args(
  211. 'foo: Error running actions for repository',
  212. OSError,
  213. levelno=logging.WARNING,
  214. log_command_error_output=True,
  215. ).and_return([flexmock()]).ordered()
  216. flexmock(module).should_receive('log_error_records').with_args(
  217. 'bar: Error running actions for repository',
  218. OSError,
  219. levelno=logging.WARNING,
  220. log_command_error_output=True,
  221. ).and_return(flexmock()).ordered()
  222. error_logs = [flexmock()]
  223. flexmock(module).should_receive('log_error_records').with_args(
  224. 'bar: Error running actions for repository', OSError
  225. ).and_return(error_logs).ordered()
  226. config = {'location': {'repositories': ['foo', 'bar']}, 'storage': {'retries': 1}}
  227. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  228. results = list(module.run_configuration('test.yaml', config, arguments))
  229. assert results == error_logs
  230. def test_run_configuration_retry_wait():
  231. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  232. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  233. flexmock(module.command).should_receive('execute_hook')
  234. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  235. flexmock(module).should_receive('log_error_records').with_args(
  236. 'foo: Error running actions for repository',
  237. OSError,
  238. levelno=logging.WARNING,
  239. log_command_error_output=True,
  240. ).and_return([flexmock()]).ordered()
  241. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  242. flexmock(module).should_receive('log_error_records').with_args(
  243. 'foo: Error running actions for repository',
  244. OSError,
  245. levelno=logging.WARNING,
  246. log_command_error_output=True,
  247. ).and_return([flexmock()]).ordered()
  248. flexmock(time).should_receive('sleep').with_args(20).and_return().ordered()
  249. flexmock(module).should_receive('log_error_records').with_args(
  250. 'foo: Error running actions for repository',
  251. OSError,
  252. levelno=logging.WARNING,
  253. log_command_error_output=True,
  254. ).and_return([flexmock()]).ordered()
  255. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  256. error_logs = [flexmock()]
  257. flexmock(module).should_receive('log_error_records').with_args(
  258. 'foo: Error running actions for repository', OSError
  259. ).and_return(error_logs).ordered()
  260. config = {'location': {'repositories': ['foo']}, 'storage': {'retries': 3, 'retry_wait': 10}}
  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_retries_timeout_multiple_repos():
  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).and_raise(OSError).and_return(
  269. []
  270. ).and_raise(OSError).times(4)
  271. flexmock(module).should_receive('log_error_records').with_args(
  272. 'foo: Error running actions for repository',
  273. OSError,
  274. levelno=logging.WARNING,
  275. log_command_error_output=True,
  276. ).and_return([flexmock()]).ordered()
  277. flexmock(module).should_receive('log_error_records').with_args(
  278. 'bar: Error running actions for repository',
  279. OSError,
  280. levelno=logging.WARNING,
  281. log_command_error_output=True,
  282. ).and_return([flexmock()]).ordered()
  283. # Sleep before retrying foo (and passing)
  284. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  285. # Sleep before retrying bar (and failing)
  286. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  287. error_logs = [flexmock()]
  288. flexmock(module).should_receive('log_error_records').with_args(
  289. 'bar: Error running actions for repository', OSError
  290. ).and_return(error_logs).ordered()
  291. config = {
  292. 'location': {'repositories': ['foo', 'bar']},
  293. 'storage': {'retries': 1, 'retry_wait': 10},
  294. }
  295. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  296. results = list(module.run_configuration('test.yaml', config, arguments))
  297. assert results == error_logs
  298. def test_run_actions_runs_rcreate():
  299. flexmock(module).should_receive('add_custom_log_levels')
  300. flexmock(module.command).should_receive('execute_hook')
  301. flexmock(borgmatic.actions.rcreate).should_receive('run_rcreate').once()
  302. tuple(
  303. module.run_actions(
  304. arguments={'global': flexmock(dry_run=False), 'rcreate': flexmock()},
  305. config_filename=flexmock(),
  306. location={'repositories': []},
  307. storage=flexmock(),
  308. retention=flexmock(),
  309. consistency=flexmock(),
  310. hooks={},
  311. local_path=flexmock(),
  312. remote_path=flexmock(),
  313. local_borg_version=flexmock(),
  314. repository_path='repo',
  315. )
  316. )
  317. def test_run_actions_runs_transfer():
  318. flexmock(module).should_receive('add_custom_log_levels')
  319. flexmock(module.command).should_receive('execute_hook')
  320. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  321. tuple(
  322. module.run_actions(
  323. arguments={'global': flexmock(dry_run=False), 'transfer': flexmock()},
  324. config_filename=flexmock(),
  325. location={'repositories': []},
  326. storage=flexmock(),
  327. retention=flexmock(),
  328. consistency=flexmock(),
  329. hooks={},
  330. local_path=flexmock(),
  331. remote_path=flexmock(),
  332. local_borg_version=flexmock(),
  333. repository_path='repo',
  334. )
  335. )
  336. def test_run_actions_runs_prune():
  337. flexmock(module).should_receive('add_custom_log_levels')
  338. flexmock(module.command).should_receive('execute_hook')
  339. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  340. tuple(
  341. module.run_actions(
  342. arguments={'global': flexmock(dry_run=False), 'prune': flexmock()},
  343. config_filename=flexmock(),
  344. location={'repositories': []},
  345. storage=flexmock(),
  346. retention=flexmock(),
  347. consistency=flexmock(),
  348. hooks={},
  349. local_path=flexmock(),
  350. remote_path=flexmock(),
  351. local_borg_version=flexmock(),
  352. repository_path='repo',
  353. )
  354. )
  355. def test_run_actions_runs_compact():
  356. flexmock(module).should_receive('add_custom_log_levels')
  357. flexmock(module.command).should_receive('execute_hook')
  358. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  359. tuple(
  360. module.run_actions(
  361. arguments={'global': flexmock(dry_run=False), 'compact': flexmock()},
  362. config_filename=flexmock(),
  363. location={'repositories': []},
  364. storage=flexmock(),
  365. retention=flexmock(),
  366. consistency=flexmock(),
  367. hooks={},
  368. local_path=flexmock(),
  369. remote_path=flexmock(),
  370. local_borg_version=flexmock(),
  371. repository_path='repo',
  372. )
  373. )
  374. def test_run_actions_runs_create():
  375. flexmock(module).should_receive('add_custom_log_levels')
  376. flexmock(module.command).should_receive('execute_hook')
  377. expected = flexmock()
  378. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  379. result = tuple(
  380. module.run_actions(
  381. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  382. config_filename=flexmock(),
  383. location={'repositories': []},
  384. storage=flexmock(),
  385. retention=flexmock(),
  386. consistency=flexmock(),
  387. hooks={},
  388. local_path=flexmock(),
  389. remote_path=flexmock(),
  390. local_borg_version=flexmock(),
  391. repository_path='repo',
  392. )
  393. )
  394. assert result == (expected,)
  395. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  396. flexmock(module).should_receive('add_custom_log_levels')
  397. flexmock(module.command).should_receive('execute_hook')
  398. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  399. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  400. tuple(
  401. module.run_actions(
  402. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  403. config_filename=flexmock(),
  404. location={'repositories': []},
  405. storage=flexmock(),
  406. retention=flexmock(),
  407. consistency=flexmock(),
  408. hooks={},
  409. local_path=flexmock(),
  410. remote_path=flexmock(),
  411. local_borg_version=flexmock(),
  412. repository_path='repo',
  413. )
  414. )
  415. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  416. flexmock(module).should_receive('add_custom_log_levels')
  417. flexmock(module.command).should_receive('execute_hook')
  418. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  419. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  420. tuple(
  421. module.run_actions(
  422. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  423. config_filename=flexmock(),
  424. location={'repositories': []},
  425. storage=flexmock(),
  426. retention=flexmock(),
  427. consistency=flexmock(),
  428. hooks={},
  429. local_path=flexmock(),
  430. remote_path=flexmock(),
  431. local_borg_version=flexmock(),
  432. repository_path='repo',
  433. )
  434. )
  435. def test_run_actions_runs_extract():
  436. flexmock(module).should_receive('add_custom_log_levels')
  437. flexmock(module.command).should_receive('execute_hook')
  438. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  439. tuple(
  440. module.run_actions(
  441. arguments={'global': flexmock(dry_run=False), 'extract': flexmock()},
  442. config_filename=flexmock(),
  443. location={'repositories': []},
  444. storage=flexmock(),
  445. retention=flexmock(),
  446. consistency=flexmock(),
  447. hooks={},
  448. local_path=flexmock(),
  449. remote_path=flexmock(),
  450. local_borg_version=flexmock(),
  451. repository_path='repo',
  452. )
  453. )
  454. def test_run_actions_runs_export_tar():
  455. flexmock(module).should_receive('add_custom_log_levels')
  456. flexmock(module.command).should_receive('execute_hook')
  457. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  458. tuple(
  459. module.run_actions(
  460. arguments={'global': flexmock(dry_run=False), 'export-tar': flexmock()},
  461. config_filename=flexmock(),
  462. location={'repositories': []},
  463. storage=flexmock(),
  464. retention=flexmock(),
  465. consistency=flexmock(),
  466. hooks={},
  467. local_path=flexmock(),
  468. remote_path=flexmock(),
  469. local_borg_version=flexmock(),
  470. repository_path='repo',
  471. )
  472. )
  473. def test_run_actions_runs_mount():
  474. flexmock(module).should_receive('add_custom_log_levels')
  475. flexmock(module.command).should_receive('execute_hook')
  476. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  477. tuple(
  478. module.run_actions(
  479. arguments={'global': flexmock(dry_run=False), 'mount': flexmock()},
  480. config_filename=flexmock(),
  481. location={'repositories': []},
  482. storage=flexmock(),
  483. retention=flexmock(),
  484. consistency=flexmock(),
  485. hooks={},
  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_restore():
  493. flexmock(module).should_receive('add_custom_log_levels')
  494. flexmock(module.command).should_receive('execute_hook')
  495. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  496. tuple(
  497. module.run_actions(
  498. arguments={'global': flexmock(dry_run=False), 'restore': flexmock()},
  499. config_filename=flexmock(),
  500. location={'repositories': []},
  501. storage=flexmock(),
  502. retention=flexmock(),
  503. consistency=flexmock(),
  504. hooks={},
  505. local_path=flexmock(),
  506. remote_path=flexmock(),
  507. local_borg_version=flexmock(),
  508. repository_path='repo',
  509. )
  510. )
  511. def test_run_actions_runs_rlist():
  512. flexmock(module).should_receive('add_custom_log_levels')
  513. flexmock(module.command).should_receive('execute_hook')
  514. expected = flexmock()
  515. flexmock(borgmatic.actions.rlist).should_receive('run_rlist').and_yield(expected).once()
  516. result = tuple(
  517. module.run_actions(
  518. arguments={'global': flexmock(dry_run=False), 'rlist': flexmock()},
  519. config_filename=flexmock(),
  520. location={'repositories': []},
  521. storage=flexmock(),
  522. retention=flexmock(),
  523. consistency=flexmock(),
  524. hooks={},
  525. local_path=flexmock(),
  526. remote_path=flexmock(),
  527. local_borg_version=flexmock(),
  528. repository_path='repo',
  529. )
  530. )
  531. assert result == (expected,)
  532. def test_run_actions_runs_list():
  533. flexmock(module).should_receive('add_custom_log_levels')
  534. flexmock(module.command).should_receive('execute_hook')
  535. expected = flexmock()
  536. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  537. result = tuple(
  538. module.run_actions(
  539. arguments={'global': flexmock(dry_run=False), 'list': flexmock()},
  540. config_filename=flexmock(),
  541. location={'repositories': []},
  542. storage=flexmock(),
  543. retention=flexmock(),
  544. consistency=flexmock(),
  545. hooks={},
  546. local_path=flexmock(),
  547. remote_path=flexmock(),
  548. local_borg_version=flexmock(),
  549. repository_path='repo',
  550. )
  551. )
  552. assert result == (expected,)
  553. def test_run_actions_runs_rinfo():
  554. flexmock(module).should_receive('add_custom_log_levels')
  555. flexmock(module.command).should_receive('execute_hook')
  556. expected = flexmock()
  557. flexmock(borgmatic.actions.rinfo).should_receive('run_rinfo').and_yield(expected).once()
  558. result = tuple(
  559. module.run_actions(
  560. arguments={'global': flexmock(dry_run=False), 'rinfo': flexmock()},
  561. config_filename=flexmock(),
  562. location={'repositories': []},
  563. storage=flexmock(),
  564. retention=flexmock(),
  565. consistency=flexmock(),
  566. hooks={},
  567. local_path=flexmock(),
  568. remote_path=flexmock(),
  569. local_borg_version=flexmock(),
  570. repository_path='repo',
  571. )
  572. )
  573. assert result == (expected,)
  574. def test_run_actions_runs_info():
  575. flexmock(module).should_receive('add_custom_log_levels')
  576. flexmock(module.command).should_receive('execute_hook')
  577. expected = flexmock()
  578. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  579. result = tuple(
  580. module.run_actions(
  581. arguments={'global': flexmock(dry_run=False), 'info': flexmock()},
  582. config_filename=flexmock(),
  583. location={'repositories': []},
  584. storage=flexmock(),
  585. retention=flexmock(),
  586. consistency=flexmock(),
  587. hooks={},
  588. local_path=flexmock(),
  589. remote_path=flexmock(),
  590. local_borg_version=flexmock(),
  591. repository_path='repo',
  592. )
  593. )
  594. assert result == (expected,)
  595. def test_run_actions_runs_break_lock():
  596. flexmock(module).should_receive('add_custom_log_levels')
  597. flexmock(module.command).should_receive('execute_hook')
  598. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  599. tuple(
  600. module.run_actions(
  601. arguments={'global': flexmock(dry_run=False), 'break-lock': flexmock()},
  602. config_filename=flexmock(),
  603. location={'repositories': []},
  604. storage=flexmock(),
  605. retention=flexmock(),
  606. consistency=flexmock(),
  607. hooks={},
  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_borg():
  615. flexmock(module).should_receive('add_custom_log_levels')
  616. flexmock(module.command).should_receive('execute_hook')
  617. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  618. tuple(
  619. module.run_actions(
  620. arguments={'global': flexmock(dry_run=False), 'borg': flexmock()},
  621. config_filename=flexmock(),
  622. location={'repositories': []},
  623. storage=flexmock(),
  624. retention=flexmock(),
  625. consistency=flexmock(),
  626. hooks={},
  627. local_path=flexmock(),
  628. remote_path=flexmock(),
  629. local_borg_version=flexmock(),
  630. repository_path='repo',
  631. )
  632. )
  633. def test_load_configurations_collects_parsed_configurations_and_logs():
  634. configuration = flexmock()
  635. other_configuration = flexmock()
  636. test_expected_logs = [flexmock(), flexmock()]
  637. other_expected_logs = [flexmock(), flexmock()]
  638. flexmock(module.validate).should_receive('parse_configuration').and_return(
  639. configuration, test_expected_logs
  640. ).and_return(other_configuration, other_expected_logs)
  641. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  642. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  643. assert logs == test_expected_logs + other_expected_logs
  644. def test_load_configurations_logs_warning_for_permission_error():
  645. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  646. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  647. assert configs == {}
  648. assert {log.levelno for log in logs} == {logging.WARNING}
  649. def test_load_configurations_logs_critical_for_parse_error():
  650. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  651. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  652. assert configs == {}
  653. assert {log.levelno for log in logs} == {logging.CRITICAL}
  654. def test_log_record_does_not_raise():
  655. module.log_record(levelno=1, foo='bar', baz='quux')
  656. def test_log_record_with_suppress_does_not_raise():
  657. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  658. def test_log_error_records_generates_output_logs_for_message_only():
  659. flexmock(module).should_receive('log_record').replace_with(dict)
  660. logs = tuple(module.log_error_records('Error'))
  661. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  662. def test_log_error_records_generates_output_logs_for_called_process_error():
  663. flexmock(module).should_receive('log_record').replace_with(dict)
  664. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  665. logs = tuple(
  666. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  667. )
  668. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  669. assert any(log for log in logs if 'error output' in str(log))
  670. def test_log_error_records_generates_logs_for_value_error():
  671. flexmock(module).should_receive('log_record').replace_with(dict)
  672. logs = tuple(module.log_error_records('Error', ValueError()))
  673. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  674. def test_log_error_records_generates_logs_for_os_error():
  675. flexmock(module).should_receive('log_record').replace_with(dict)
  676. logs = tuple(module.log_error_records('Error', OSError()))
  677. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  678. def test_log_error_records_generates_nothing_for_other_error():
  679. flexmock(module).should_receive('log_record').replace_with(dict)
  680. logs = tuple(module.log_error_records('Error', KeyError()))
  681. assert logs == ()
  682. def test_get_local_path_uses_configuration_value():
  683. assert module.get_local_path({'test.yaml': {'location': {'local_path': 'borg1'}}}) == 'borg1'
  684. def test_get_local_path_without_location_defaults_to_borg():
  685. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  686. def test_get_local_path_without_local_path_defaults_to_borg():
  687. assert module.get_local_path({'test.yaml': {'location': {}}}) == 'borg'
  688. def test_collect_configuration_run_summary_logs_info_for_success():
  689. flexmock(module.command).should_receive('execute_hook').never()
  690. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  691. flexmock(module).should_receive('run_configuration').and_return([])
  692. arguments = {}
  693. logs = tuple(
  694. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  695. )
  696. assert {log.levelno for log in logs} == {logging.INFO}
  697. def test_collect_configuration_run_summary_executes_hooks_for_create():
  698. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  699. flexmock(module).should_receive('run_configuration').and_return([])
  700. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  701. logs = tuple(
  702. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  703. )
  704. assert {log.levelno for log in logs} == {logging.INFO}
  705. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  706. flexmock(module.validate).should_receive('guard_single_repository_selected')
  707. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  708. flexmock(module).should_receive('run_configuration').and_return([])
  709. arguments = {'extract': flexmock(repository='repo')}
  710. logs = tuple(
  711. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  712. )
  713. assert {log.levelno for log in logs} == {logging.INFO}
  714. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  715. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  716. ValueError
  717. )
  718. expected_logs = (flexmock(),)
  719. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  720. arguments = {'extract': flexmock(repository='repo')}
  721. logs = tuple(
  722. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  723. )
  724. assert logs == expected_logs
  725. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  726. flexmock(module.validate).should_receive('guard_single_repository_selected')
  727. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  728. flexmock(module).should_receive('run_configuration').and_return([])
  729. arguments = {'mount': flexmock(repository='repo')}
  730. logs = tuple(
  731. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  732. )
  733. assert {log.levelno for log in logs} == {logging.INFO}
  734. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  735. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  736. ValueError
  737. )
  738. expected_logs = (flexmock(),)
  739. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  740. arguments = {'mount': flexmock(repository='repo')}
  741. logs = tuple(
  742. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  743. )
  744. assert logs == expected_logs
  745. def test_collect_configuration_run_summary_logs_missing_configs_error():
  746. arguments = {'global': flexmock(config_paths=[])}
  747. expected_logs = (flexmock(),)
  748. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  749. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  750. assert logs == expected_logs
  751. def test_collect_configuration_run_summary_logs_pre_hook_error():
  752. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  753. expected_logs = (flexmock(),)
  754. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  755. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  756. logs = tuple(
  757. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  758. )
  759. assert logs == expected_logs
  760. def test_collect_configuration_run_summary_logs_post_hook_error():
  761. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  762. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  763. flexmock(module).should_receive('run_configuration').and_return([])
  764. expected_logs = (flexmock(),)
  765. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  766. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  767. logs = tuple(
  768. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  769. )
  770. assert expected_logs[0] in logs
  771. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  772. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  773. ValueError
  774. )
  775. expected_logs = (flexmock(),)
  776. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  777. arguments = {'list': flexmock(repository='repo', archive='test')}
  778. logs = tuple(
  779. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  780. )
  781. assert logs == expected_logs
  782. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  783. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  784. flexmock(module).should_receive('run_configuration').and_return([])
  785. arguments = {'list': flexmock(repository='repo', archive=None)}
  786. logs = tuple(
  787. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  788. )
  789. assert {log.levelno for log in logs} == {logging.INFO}
  790. def test_collect_configuration_run_summary_logs_run_configuration_error():
  791. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  792. flexmock(module).should_receive('run_configuration').and_return(
  793. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  794. )
  795. flexmock(module).should_receive('log_error_records').and_return([])
  796. arguments = {}
  797. logs = tuple(
  798. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  799. )
  800. assert {log.levelno for log in logs} == {logging.CRITICAL}
  801. def test_collect_configuration_run_summary_logs_run_umount_error():
  802. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  803. flexmock(module).should_receive('run_configuration').and_return([])
  804. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  805. flexmock(module).should_receive('log_error_records').and_return(
  806. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  807. )
  808. arguments = {'umount': flexmock(mount_point='/mnt')}
  809. logs = tuple(
  810. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  811. )
  812. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  813. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  814. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  815. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  816. ['baz']
  817. )
  818. stdout = flexmock()
  819. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  820. flexmock(module.sys).stdout = stdout
  821. arguments = {}
  822. tuple(
  823. module.collect_configuration_run_summary_logs(
  824. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  825. )
  826. )