test_borgmatic.py 40 KB

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