test_borgmatic.py 40 KB

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