test_borgmatic.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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_borg_action():
  615. flexmock(module.validate).should_receive('repositories_match').and_return(True)
  616. flexmock(module.borg_rlist).should_receive('resolve_archive_name').and_return(flexmock())
  617. flexmock(module.borg_borg).should_receive('run_arbitrary_borg')
  618. arguments = {
  619. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  620. 'borg': flexmock(repository=flexmock(), archive=flexmock(), options=flexmock()),
  621. }
  622. list(
  623. module.run_actions(
  624. arguments=arguments,
  625. config_filename='test.yaml',
  626. location={'repositories': ['repo']},
  627. storage={},
  628. retention={},
  629. consistency={},
  630. hooks={},
  631. local_path=None,
  632. remote_path=None,
  633. local_borg_version=None,
  634. repository_path='repo',
  635. )
  636. )
  637. def test_load_configurations_collects_parsed_configurations_and_logs():
  638. configuration = flexmock()
  639. other_configuration = flexmock()
  640. test_expected_logs = [flexmock(), flexmock()]
  641. other_expected_logs = [flexmock(), flexmock()]
  642. flexmock(module.validate).should_receive('parse_configuration').and_return(
  643. configuration, test_expected_logs
  644. ).and_return(other_configuration, other_expected_logs)
  645. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  646. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  647. assert logs == test_expected_logs + other_expected_logs
  648. def test_load_configurations_logs_warning_for_permission_error():
  649. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  650. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  651. assert configs == {}
  652. assert {log.levelno for log in logs} == {logging.WARNING}
  653. def test_load_configurations_logs_critical_for_parse_error():
  654. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  655. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  656. assert configs == {}
  657. assert {log.levelno for log in logs} == {logging.CRITICAL}
  658. def test_log_record_does_not_raise():
  659. module.log_record(levelno=1, foo='bar', baz='quux')
  660. def test_log_record_with_suppress_does_not_raise():
  661. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  662. def test_log_error_records_generates_output_logs_for_message_only():
  663. flexmock(module).should_receive('log_record').replace_with(dict)
  664. logs = tuple(module.log_error_records('Error'))
  665. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  666. def test_log_error_records_generates_output_logs_for_called_process_error():
  667. flexmock(module).should_receive('log_record').replace_with(dict)
  668. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  669. logs = tuple(
  670. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  671. )
  672. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  673. assert any(log for log in logs if 'error output' in str(log))
  674. def test_log_error_records_generates_logs_for_value_error():
  675. flexmock(module).should_receive('log_record').replace_with(dict)
  676. logs = tuple(module.log_error_records('Error', ValueError()))
  677. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  678. def test_log_error_records_generates_logs_for_os_error():
  679. flexmock(module).should_receive('log_record').replace_with(dict)
  680. logs = tuple(module.log_error_records('Error', OSError()))
  681. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  682. def test_log_error_records_generates_nothing_for_other_error():
  683. flexmock(module).should_receive('log_record').replace_with(dict)
  684. logs = tuple(module.log_error_records('Error', KeyError()))
  685. assert logs == ()
  686. def test_get_local_path_uses_configuration_value():
  687. assert module.get_local_path({'test.yaml': {'location': {'local_path': 'borg1'}}}) == 'borg1'
  688. def test_get_local_path_without_location_defaults_to_borg():
  689. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  690. def test_get_local_path_without_local_path_defaults_to_borg():
  691. assert module.get_local_path({'test.yaml': {'location': {}}}) == 'borg'
  692. def test_collect_configuration_run_summary_logs_info_for_success():
  693. flexmock(module.command).should_receive('execute_hook').never()
  694. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  695. flexmock(module).should_receive('run_configuration').and_return([])
  696. arguments = {}
  697. logs = tuple(
  698. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  699. )
  700. assert {log.levelno for log in logs} == {logging.INFO}
  701. def test_collect_configuration_run_summary_executes_hooks_for_create():
  702. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  703. flexmock(module).should_receive('run_configuration').and_return([])
  704. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  705. logs = tuple(
  706. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  707. )
  708. assert {log.levelno for log in logs} == {logging.INFO}
  709. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  710. flexmock(module.validate).should_receive('guard_single_repository_selected')
  711. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  712. flexmock(module).should_receive('run_configuration').and_return([])
  713. arguments = {'extract': flexmock(repository='repo')}
  714. logs = tuple(
  715. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  716. )
  717. assert {log.levelno for log in logs} == {logging.INFO}
  718. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  719. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  720. ValueError
  721. )
  722. expected_logs = (flexmock(),)
  723. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  724. arguments = {'extract': flexmock(repository='repo')}
  725. logs = tuple(
  726. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  727. )
  728. assert logs == expected_logs
  729. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  730. flexmock(module.validate).should_receive('guard_single_repository_selected')
  731. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  732. flexmock(module).should_receive('run_configuration').and_return([])
  733. arguments = {'mount': flexmock(repository='repo')}
  734. logs = tuple(
  735. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  736. )
  737. assert {log.levelno for log in logs} == {logging.INFO}
  738. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  739. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  740. ValueError
  741. )
  742. expected_logs = (flexmock(),)
  743. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  744. arguments = {'mount': flexmock(repository='repo')}
  745. logs = tuple(
  746. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  747. )
  748. assert logs == expected_logs
  749. def test_collect_configuration_run_summary_logs_missing_configs_error():
  750. arguments = {'global': flexmock(config_paths=[])}
  751. expected_logs = (flexmock(),)
  752. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  753. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  754. assert logs == expected_logs
  755. def test_collect_configuration_run_summary_logs_pre_hook_error():
  756. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  757. expected_logs = (flexmock(),)
  758. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  759. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  760. logs = tuple(
  761. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  762. )
  763. assert logs == expected_logs
  764. def test_collect_configuration_run_summary_logs_post_hook_error():
  765. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  766. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  767. flexmock(module).should_receive('run_configuration').and_return([])
  768. expected_logs = (flexmock(),)
  769. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  770. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  771. logs = tuple(
  772. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  773. )
  774. assert expected_logs[0] in logs
  775. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  776. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  777. ValueError
  778. )
  779. expected_logs = (flexmock(),)
  780. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  781. arguments = {'list': flexmock(repository='repo', archive='test')}
  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_info_for_success_with_list():
  787. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  788. flexmock(module).should_receive('run_configuration').and_return([])
  789. arguments = {'list': flexmock(repository='repo', archive=None)}
  790. logs = tuple(
  791. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  792. )
  793. assert {log.levelno for log in logs} == {logging.INFO}
  794. def test_collect_configuration_run_summary_logs_run_configuration_error():
  795. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  796. flexmock(module).should_receive('run_configuration').and_return(
  797. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  798. )
  799. flexmock(module).should_receive('log_error_records').and_return([])
  800. arguments = {}
  801. logs = tuple(
  802. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  803. )
  804. assert {log.levelno for log in logs} == {logging.CRITICAL}
  805. def test_collect_configuration_run_summary_logs_run_umount_error():
  806. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  807. flexmock(module).should_receive('run_configuration').and_return([])
  808. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  809. flexmock(module).should_receive('log_error_records').and_return(
  810. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  811. )
  812. arguments = {'umount': flexmock(mount_point='/mnt')}
  813. logs = tuple(
  814. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  815. )
  816. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  817. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  818. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  819. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  820. ['baz']
  821. )
  822. stdout = flexmock()
  823. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  824. flexmock(module.sys).stdout = stdout
  825. arguments = {}
  826. tuple(
  827. module.collect_configuration_run_summary_logs(
  828. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  829. )
  830. )