test_borgmatic.py 38 KB

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