2
0

test_borgmatic.py 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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').twice()
  333. arguments = {
  334. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  335. 'prune': flexmock(stats=flexmock(), files=flexmock()),
  336. }
  337. list(
  338. module.run_actions(
  339. arguments=arguments,
  340. config_filename='test.yaml',
  341. location={'repositories': ['repo']},
  342. storage={},
  343. retention={},
  344. consistency={},
  345. hooks={},
  346. local_path=None,
  347. remote_path=None,
  348. local_borg_version=None,
  349. repository_path='repo',
  350. )
  351. )
  352. def test_run_actions_calls_hooks_for_compact_action():
  353. flexmock(module.borg_feature).should_receive('available').and_return(True)
  354. flexmock(module.borg_compact).should_receive('compact_segments')
  355. flexmock(module.command).should_receive('execute_hook').twice()
  356. arguments = {
  357. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  358. 'compact': flexmock(progress=flexmock(), cleanup_commits=flexmock(), threshold=flexmock()),
  359. }
  360. list(
  361. module.run_actions(
  362. arguments=arguments,
  363. config_filename='test.yaml',
  364. location={'repositories': ['repo']},
  365. storage={},
  366. retention={},
  367. consistency={},
  368. hooks={},
  369. local_path=None,
  370. remote_path=None,
  371. local_borg_version=None,
  372. repository_path='repo',
  373. )
  374. )
  375. def test_run_actions_executes_and_calls_hooks_for_create_action():
  376. flexmock(module.borg_create).should_receive('create_archive')
  377. flexmock(module.command).should_receive('execute_hook').twice()
  378. flexmock(module.dispatch).should_receive('call_hooks').and_return({}).times(3)
  379. arguments = {
  380. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  381. 'create': flexmock(
  382. progress=flexmock(), stats=flexmock(), json=flexmock(), files=flexmock()
  383. ),
  384. }
  385. list(
  386. module.run_actions(
  387. arguments=arguments,
  388. config_filename='test.yaml',
  389. location={'repositories': ['repo']},
  390. storage={},
  391. retention={},
  392. consistency={},
  393. hooks={},
  394. local_path=None,
  395. remote_path=None,
  396. local_borg_version=None,
  397. repository_path='repo',
  398. )
  399. )
  400. def test_run_actions_calls_hooks_for_check_action():
  401. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  402. flexmock(module.borg_check).should_receive('check_archives')
  403. flexmock(module.command).should_receive('execute_hook').twice()
  404. arguments = {
  405. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  406. 'check': flexmock(
  407. progress=flexmock(), repair=flexmock(), only=flexmock(), force=flexmock()
  408. ),
  409. }
  410. list(
  411. module.run_actions(
  412. arguments=arguments,
  413. config_filename='test.yaml',
  414. location={'repositories': ['repo']},
  415. storage={},
  416. retention={},
  417. consistency={},
  418. hooks={},
  419. local_path=None,
  420. remote_path=None,
  421. local_borg_version=None,
  422. repository_path='repo',
  423. )
  424. )
  425. def test_run_actions_calls_hooks_for_extract_action():
  426. flexmock(module.validate).should_receive('repositories_match').and_return(True)
  427. flexmock(module.borg_extract).should_receive('extract_archive')
  428. flexmock(module.command).should_receive('execute_hook').twice()
  429. arguments = {
  430. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  431. 'extract': flexmock(
  432. paths=flexmock(),
  433. progress=flexmock(),
  434. destination=flexmock(),
  435. strip_components=flexmock(),
  436. archive=flexmock(),
  437. repository='repo',
  438. ),
  439. }
  440. list(
  441. module.run_actions(
  442. arguments=arguments,
  443. config_filename='test.yaml',
  444. location={'repositories': ['repo']},
  445. storage={},
  446. retention={},
  447. consistency={},
  448. hooks={},
  449. local_path=None,
  450. remote_path=None,
  451. local_borg_version=None,
  452. repository_path='repo',
  453. )
  454. )
  455. def test_run_actions_does_not_raise_for_export_tar_action():
  456. flexmock(module.validate).should_receive('repositories_match').and_return(True)
  457. flexmock(module.borg_export_tar).should_receive('export_tar_archive')
  458. arguments = {
  459. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  460. 'export-tar': flexmock(
  461. repository=flexmock(),
  462. archive=flexmock(),
  463. paths=flexmock(),
  464. destination=flexmock(),
  465. tar_filter=flexmock(),
  466. files=flexmock(),
  467. strip_components=flexmock(),
  468. ),
  469. }
  470. list(
  471. module.run_actions(
  472. arguments=arguments,
  473. config_filename='test.yaml',
  474. location={'repositories': ['repo']},
  475. storage={},
  476. retention={},
  477. consistency={},
  478. hooks={},
  479. local_path=None,
  480. remote_path=None,
  481. local_borg_version=None,
  482. repository_path='repo',
  483. )
  484. )
  485. def test_run_actions_does_not_raise_for_mount_action():
  486. flexmock(module.validate).should_receive('repositories_match').and_return(True)
  487. flexmock(module.borg_mount).should_receive('mount_archive')
  488. arguments = {
  489. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  490. 'mount': flexmock(
  491. repository=flexmock(),
  492. archive=flexmock(),
  493. mount_point=flexmock(),
  494. paths=flexmock(),
  495. foreground=flexmock(),
  496. options=flexmock(),
  497. ),
  498. }
  499. list(
  500. module.run_actions(
  501. arguments=arguments,
  502. config_filename='test.yaml',
  503. location={'repositories': ['repo']},
  504. storage={},
  505. retention={},
  506. consistency={},
  507. hooks={},
  508. local_path=None,
  509. remote_path=None,
  510. local_borg_version=None,
  511. repository_path='repo',
  512. )
  513. )
  514. def test_run_actions_does_not_raise_for_rlist_action():
  515. flexmock(module.validate).should_receive('repositories_match').and_return(True)
  516. flexmock(module.borg_rlist).should_receive('list_repository')
  517. arguments = {
  518. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  519. 'rlist': flexmock(repository=flexmock(), json=flexmock()),
  520. }
  521. list(
  522. module.run_actions(
  523. arguments=arguments,
  524. config_filename='test.yaml',
  525. location={'repositories': ['repo']},
  526. storage={},
  527. retention={},
  528. consistency={},
  529. hooks={},
  530. local_path=None,
  531. remote_path=None,
  532. local_borg_version=None,
  533. repository_path='repo',
  534. )
  535. )
  536. def test_run_actions_does_not_raise_for_list_action():
  537. flexmock(module.validate).should_receive('repositories_match').and_return(True)
  538. flexmock(module.borg_rlist).should_receive('resolve_archive_name').and_return(flexmock())
  539. flexmock(module.borg_list).should_receive('list_archive')
  540. arguments = {
  541. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  542. 'list': flexmock(repository=flexmock(), archive=flexmock(), json=flexmock()),
  543. }
  544. list(
  545. module.run_actions(
  546. arguments=arguments,
  547. config_filename='test.yaml',
  548. location={'repositories': ['repo']},
  549. storage={},
  550. retention={},
  551. consistency={},
  552. hooks={},
  553. local_path=None,
  554. remote_path=None,
  555. local_borg_version=None,
  556. repository_path='repo',
  557. )
  558. )
  559. def test_run_actions_does_not_raise_for_rinfo_action():
  560. flexmock(module.validate).should_receive('repositories_match').and_return(True)
  561. flexmock(module.borg_rinfo).should_receive('display_repository_info')
  562. arguments = {
  563. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  564. 'rinfo': flexmock(repository=flexmock(), json=flexmock()),
  565. }
  566. list(
  567. module.run_actions(
  568. arguments=arguments,
  569. config_filename='test.yaml',
  570. location={'repositories': ['repo']},
  571. storage={},
  572. retention={},
  573. consistency={},
  574. hooks={},
  575. local_path=None,
  576. remote_path=None,
  577. local_borg_version=None,
  578. repository_path='repo',
  579. )
  580. )
  581. def test_run_actions_does_not_raise_for_info_action():
  582. flexmock(module.validate).should_receive('repositories_match').and_return(True)
  583. flexmock(module.borg_rlist).should_receive('resolve_archive_name').and_return(flexmock())
  584. flexmock(module.borg_info).should_receive('display_archives_info')
  585. arguments = {
  586. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  587. 'info': flexmock(repository=flexmock(), archive=flexmock(), json=flexmock()),
  588. }
  589. list(
  590. module.run_actions(
  591. arguments=arguments,
  592. config_filename='test.yaml',
  593. location={'repositories': ['repo']},
  594. storage={},
  595. retention={},
  596. consistency={},
  597. hooks={},
  598. local_path=None,
  599. remote_path=None,
  600. local_borg_version=None,
  601. repository_path='repo',
  602. )
  603. )
  604. def test_run_actions_does_not_raise_for_borg_action():
  605. flexmock(module.validate).should_receive('repositories_match').and_return(True)
  606. flexmock(module.borg_rlist).should_receive('resolve_archive_name').and_return(flexmock())
  607. flexmock(module.borg_borg).should_receive('run_arbitrary_borg')
  608. arguments = {
  609. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  610. 'borg': flexmock(repository=flexmock(), archive=flexmock(), options=flexmock()),
  611. }
  612. list(
  613. module.run_actions(
  614. arguments=arguments,
  615. config_filename='test.yaml',
  616. location={'repositories': ['repo']},
  617. storage={},
  618. retention={},
  619. consistency={},
  620. hooks={},
  621. local_path=None,
  622. remote_path=None,
  623. local_borg_version=None,
  624. repository_path='repo',
  625. )
  626. )
  627. def test_load_configurations_collects_parsed_configurations_and_logs():
  628. configuration = flexmock()
  629. other_configuration = flexmock()
  630. test_expected_logs = [flexmock(), flexmock()]
  631. other_expected_logs = [flexmock(), flexmock()]
  632. flexmock(module.validate).should_receive('parse_configuration').and_return(
  633. configuration, test_expected_logs
  634. ).and_return(other_configuration, other_expected_logs)
  635. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  636. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  637. assert logs == test_expected_logs + other_expected_logs
  638. def test_load_configurations_logs_warning_for_permission_error():
  639. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  640. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  641. assert configs == {}
  642. assert {log.levelno for log in logs} == {logging.WARNING}
  643. def test_load_configurations_logs_critical_for_parse_error():
  644. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  645. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  646. assert configs == {}
  647. assert {log.levelno for log in logs} == {logging.CRITICAL}
  648. def test_log_record_does_not_raise():
  649. module.log_record(levelno=1, foo='bar', baz='quux')
  650. def test_log_record_with_suppress_does_not_raise():
  651. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  652. def test_log_error_records_generates_output_logs_for_message_only():
  653. flexmock(module).should_receive('log_record').replace_with(dict)
  654. logs = tuple(module.log_error_records('Error'))
  655. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  656. def test_log_error_records_generates_output_logs_for_called_process_error():
  657. flexmock(module).should_receive('log_record').replace_with(dict)
  658. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  659. logs = tuple(
  660. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  661. )
  662. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  663. assert any(log for log in logs if 'error output' in str(log))
  664. def test_log_error_records_generates_logs_for_value_error():
  665. flexmock(module).should_receive('log_record').replace_with(dict)
  666. logs = tuple(module.log_error_records('Error', ValueError()))
  667. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  668. def test_log_error_records_generates_logs_for_os_error():
  669. flexmock(module).should_receive('log_record').replace_with(dict)
  670. logs = tuple(module.log_error_records('Error', OSError()))
  671. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  672. def test_log_error_records_generates_nothing_for_other_error():
  673. flexmock(module).should_receive('log_record').replace_with(dict)
  674. logs = tuple(module.log_error_records('Error', KeyError()))
  675. assert logs == ()
  676. def test_get_local_path_uses_configuration_value():
  677. assert module.get_local_path({'test.yaml': {'location': {'local_path': 'borg1'}}}) == 'borg1'
  678. def test_get_local_path_without_location_defaults_to_borg():
  679. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  680. def test_get_local_path_without_local_path_defaults_to_borg():
  681. assert module.get_local_path({'test.yaml': {'location': {}}}) == 'borg'
  682. def test_collect_configuration_run_summary_logs_info_for_success():
  683. flexmock(module.command).should_receive('execute_hook').never()
  684. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  685. flexmock(module).should_receive('run_configuration').and_return([])
  686. arguments = {}
  687. logs = tuple(
  688. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  689. )
  690. assert {log.levelno for log in logs} == {logging.INFO}
  691. def test_collect_configuration_run_summary_executes_hooks_for_create():
  692. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  693. flexmock(module).should_receive('run_configuration').and_return([])
  694. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  695. logs = tuple(
  696. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  697. )
  698. assert {log.levelno for log in logs} == {logging.INFO}
  699. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  700. flexmock(module.validate).should_receive('guard_single_repository_selected')
  701. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  702. flexmock(module).should_receive('run_configuration').and_return([])
  703. arguments = {'extract': flexmock(repository='repo')}
  704. logs = tuple(
  705. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  706. )
  707. assert {log.levelno for log in logs} == {logging.INFO}
  708. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  709. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  710. ValueError
  711. )
  712. expected_logs = (flexmock(),)
  713. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  714. arguments = {'extract': flexmock(repository='repo')}
  715. logs = tuple(
  716. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  717. )
  718. assert logs == expected_logs
  719. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  720. flexmock(module.validate).should_receive('guard_single_repository_selected')
  721. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  722. flexmock(module).should_receive('run_configuration').and_return([])
  723. arguments = {'mount': flexmock(repository='repo')}
  724. logs = tuple(
  725. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  726. )
  727. assert {log.levelno for log in logs} == {logging.INFO}
  728. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  729. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  730. ValueError
  731. )
  732. expected_logs = (flexmock(),)
  733. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  734. arguments = {'mount': flexmock(repository='repo')}
  735. logs = tuple(
  736. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  737. )
  738. assert logs == expected_logs
  739. def test_collect_configuration_run_summary_logs_missing_configs_error():
  740. arguments = {'global': flexmock(config_paths=[])}
  741. expected_logs = (flexmock(),)
  742. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  743. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  744. assert logs == expected_logs
  745. def test_collect_configuration_run_summary_logs_pre_hook_error():
  746. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  747. expected_logs = (flexmock(),)
  748. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  749. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  750. logs = tuple(
  751. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  752. )
  753. assert logs == expected_logs
  754. def test_collect_configuration_run_summary_logs_post_hook_error():
  755. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  756. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  757. flexmock(module).should_receive('run_configuration').and_return([])
  758. expected_logs = (flexmock(),)
  759. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  760. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  761. logs = tuple(
  762. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  763. )
  764. assert expected_logs[0] in logs
  765. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  766. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  767. ValueError
  768. )
  769. expected_logs = (flexmock(),)
  770. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  771. arguments = {'list': flexmock(repository='repo', archive='test')}
  772. logs = tuple(
  773. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  774. )
  775. assert logs == expected_logs
  776. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  777. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  778. flexmock(module).should_receive('run_configuration').and_return([])
  779. arguments = {'list': flexmock(repository='repo', archive=None)}
  780. logs = tuple(
  781. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  782. )
  783. assert {log.levelno for log in logs} == {logging.INFO}
  784. def test_collect_configuration_run_summary_logs_run_configuration_error():
  785. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  786. flexmock(module).should_receive('run_configuration').and_return(
  787. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  788. )
  789. flexmock(module).should_receive('log_error_records').and_return([])
  790. arguments = {}
  791. logs = tuple(
  792. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  793. )
  794. assert {log.levelno for log in logs} == {logging.CRITICAL}
  795. def test_collect_configuration_run_summary_logs_run_umount_error():
  796. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  797. flexmock(module).should_receive('run_configuration').and_return([])
  798. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  799. flexmock(module).should_receive('log_error_records').and_return(
  800. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  801. )
  802. arguments = {'umount': flexmock(mount_point='/mnt')}
  803. logs = tuple(
  804. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  805. )
  806. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  807. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  808. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  809. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  810. ['baz']
  811. )
  812. stdout = flexmock()
  813. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  814. flexmock(module.sys).stdout = stdout
  815. arguments = {}
  816. tuple(
  817. module.collect_configuration_run_summary_logs(
  818. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  819. )
  820. )