test_borgmatic.py 38 KB

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