test_borgmatic.py 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  9. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  10. expected_results = [flexmock(), flexmock()]
  11. flexmock(module).should_receive('run_actions').and_return(expected_results[:1]).and_return(
  12. expected_results[1:]
  13. )
  14. config = {'location': {'repositories': ['foo', 'bar']}}
  15. arguments = {'global': flexmock(monitoring_verbosity=1)}
  16. results = list(module.run_configuration('test.yaml', config, arguments))
  17. assert results == expected_results
  18. def test_run_configuration_with_invalid_borg_version_errors():
  19. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  20. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  21. flexmock(module.command).should_receive('execute_hook').never()
  22. flexmock(module.dispatch).should_receive('call_hooks').never()
  23. flexmock(module).should_receive('run_actions').never()
  24. config = {'location': {'repositories': ['foo']}}
  25. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'prune': flexmock()}
  26. list(module.run_configuration('test.yaml', config, arguments))
  27. def test_run_configuration_logs_monitor_start_error():
  28. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  29. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  30. flexmock(module.dispatch).should_receive('call_hooks').and_raise(OSError).and_return(
  31. None
  32. ).and_return(None).and_return(None)
  33. expected_results = [flexmock()]
  34. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  35. flexmock(module).should_receive('run_actions').never()
  36. config = {'location': {'repositories': ['foo']}}
  37. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  38. results = list(module.run_configuration('test.yaml', config, arguments))
  39. assert results == expected_results
  40. def test_run_configuration_bails_for_monitor_start_soft_failure():
  41. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  42. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  43. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  44. flexmock(module.dispatch).should_receive('call_hooks').and_raise(error)
  45. flexmock(module).should_receive('log_error_records').never()
  46. flexmock(module).should_receive('run_actions').never()
  47. config = {'location': {'repositories': ['foo']}}
  48. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  49. results = list(module.run_configuration('test.yaml', config, arguments))
  50. assert results == []
  51. def test_run_configuration_logs_actions_error():
  52. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  53. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  54. flexmock(module.command).should_receive('execute_hook')
  55. flexmock(module.dispatch).should_receive('call_hooks')
  56. expected_results = [flexmock()]
  57. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  58. flexmock(module).should_receive('run_actions').and_raise(OSError)
  59. config = {'location': {'repositories': ['foo']}}
  60. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  61. results = list(module.run_configuration('test.yaml', config, arguments))
  62. assert results == expected_results
  63. def test_run_configuration_bails_for_actions_soft_failure():
  64. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  65. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  66. flexmock(module.dispatch).should_receive('call_hooks')
  67. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  68. flexmock(module).should_receive('run_actions').and_raise(error)
  69. flexmock(module).should_receive('log_error_records').never()
  70. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  71. config = {'location': {'repositories': ['foo']}}
  72. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  73. results = list(module.run_configuration('test.yaml', config, arguments))
  74. assert results == []
  75. def test_run_configuration_logs_monitor_log_error():
  76. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  77. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  78. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  79. None
  80. ).and_raise(OSError)
  81. expected_results = [flexmock()]
  82. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  83. flexmock(module).should_receive('run_actions').and_return([])
  84. config = {'location': {'repositories': ['foo']}}
  85. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  86. results = list(module.run_configuration('test.yaml', config, arguments))
  87. assert results == expected_results
  88. def test_run_configuration_bails_for_monitor_log_soft_failure():
  89. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  90. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  91. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  92. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  93. None
  94. ).and_raise(error)
  95. flexmock(module).should_receive('log_error_records').never()
  96. flexmock(module).should_receive('run_actions').and_return([])
  97. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  98. config = {'location': {'repositories': ['foo']}}
  99. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  100. results = list(module.run_configuration('test.yaml', config, arguments))
  101. assert results == []
  102. def test_run_configuration_logs_monitor_finish_error():
  103. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  104. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  105. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  106. None
  107. ).and_return(None).and_raise(OSError)
  108. expected_results = [flexmock()]
  109. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  110. flexmock(module).should_receive('run_actions').and_return([])
  111. config = {'location': {'repositories': ['foo']}}
  112. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  113. results = list(module.run_configuration('test.yaml', config, arguments))
  114. assert results == expected_results
  115. def test_run_configuration_bails_for_monitor_finish_soft_failure():
  116. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  117. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  118. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  119. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  120. None
  121. ).and_raise(None).and_raise(error)
  122. flexmock(module).should_receive('log_error_records').never()
  123. flexmock(module).should_receive('run_actions').and_return([])
  124. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  125. config = {'location': {'repositories': ['foo']}}
  126. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  127. results = list(module.run_configuration('test.yaml', config, arguments))
  128. assert results == []
  129. def test_run_configuration_logs_on_error_hook_error():
  130. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  131. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  132. flexmock(module.command).should_receive('execute_hook').and_raise(OSError)
  133. expected_results = [flexmock(), flexmock()]
  134. flexmock(module).should_receive('log_error_records').and_return(
  135. expected_results[:1]
  136. ).and_return(expected_results[1:])
  137. flexmock(module).should_receive('run_actions').and_raise(OSError)
  138. config = {'location': {'repositories': ['foo']}}
  139. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  140. results = list(module.run_configuration('test.yaml', config, arguments))
  141. assert results == expected_results
  142. def test_run_configuration_bails_for_on_error_hook_soft_failure():
  143. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  144. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  145. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  146. flexmock(module.command).should_receive('execute_hook').and_raise(error)
  147. expected_results = [flexmock()]
  148. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  149. flexmock(module).should_receive('run_actions').and_raise(OSError)
  150. config = {'location': {'repositories': ['foo']}}
  151. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  152. results = list(module.run_configuration('test.yaml', config, arguments))
  153. assert results == expected_results
  154. def test_run_configuration_retries_soft_error():
  155. # Run action first fails, second passes
  156. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  157. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  158. flexmock(module.command).should_receive('execute_hook')
  159. flexmock(module).should_receive('run_actions').and_raise(OSError).and_return([])
  160. flexmock(module).should_receive('log_error_records').and_return([flexmock()]).once()
  161. config = {'location': {'repositories': ['foo']}, 'storage': {'retries': 1}}
  162. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  163. results = list(module.run_configuration('test.yaml', config, arguments))
  164. assert results == []
  165. def test_run_configuration_retries_hard_error():
  166. # Run action fails twice
  167. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  168. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  169. flexmock(module.command).should_receive('execute_hook')
  170. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  171. flexmock(module).should_receive('log_error_records').with_args(
  172. 'foo: Error running actions for repository',
  173. OSError,
  174. levelno=logging.WARNING,
  175. log_command_error_output=True,
  176. ).and_return([flexmock()])
  177. error_logs = [flexmock()]
  178. flexmock(module).should_receive('log_error_records').with_args(
  179. 'foo: Error running actions for repository', OSError,
  180. ).and_return(error_logs)
  181. config = {'location': {'repositories': ['foo']}, 'storage': {'retries': 1}}
  182. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  183. results = list(module.run_configuration('test.yaml', config, arguments))
  184. assert results == error_logs
  185. def test_run_configuration_repos_ordered():
  186. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  187. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  188. flexmock(module.command).should_receive('execute_hook')
  189. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  190. expected_results = [flexmock(), flexmock()]
  191. flexmock(module).should_receive('log_error_records').with_args(
  192. 'foo: Error running actions for repository', OSError
  193. ).and_return(expected_results[:1]).ordered()
  194. flexmock(module).should_receive('log_error_records').with_args(
  195. 'bar: Error running actions for repository', OSError
  196. ).and_return(expected_results[1:]).ordered()
  197. config = {'location': {'repositories': ['foo', 'bar']}}
  198. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  199. results = list(module.run_configuration('test.yaml', config, arguments))
  200. assert results == expected_results
  201. def test_run_configuration_retries_round_robbin():
  202. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  203. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  204. flexmock(module.command).should_receive('execute_hook')
  205. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  206. flexmock(module).should_receive('log_error_records').with_args(
  207. 'foo: Error running actions for repository',
  208. OSError,
  209. levelno=logging.WARNING,
  210. log_command_error_output=True,
  211. ).and_return([flexmock()]).ordered()
  212. flexmock(module).should_receive('log_error_records').with_args(
  213. 'bar: Error running actions for repository',
  214. OSError,
  215. levelno=logging.WARNING,
  216. log_command_error_output=True,
  217. ).and_return([flexmock()]).ordered()
  218. foo_error_logs = [flexmock()]
  219. flexmock(module).should_receive('log_error_records').with_args(
  220. 'foo: Error running actions for repository', OSError
  221. ).and_return(foo_error_logs).ordered()
  222. bar_error_logs = [flexmock()]
  223. flexmock(module).should_receive('log_error_records').with_args(
  224. 'bar: Error running actions for repository', OSError
  225. ).and_return(bar_error_logs).ordered()
  226. config = {'location': {'repositories': ['foo', 'bar']}, 'storage': {'retries': 1}}
  227. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  228. results = list(module.run_configuration('test.yaml', config, arguments))
  229. assert results == foo_error_logs + bar_error_logs
  230. def test_run_configuration_retries_one_passes():
  231. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  232. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  233. flexmock(module.command).should_receive('execute_hook')
  234. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  235. []
  236. ).and_raise(OSError).times(4)
  237. flexmock(module).should_receive('log_error_records').with_args(
  238. 'foo: Error running actions for repository',
  239. OSError,
  240. levelno=logging.WARNING,
  241. log_command_error_output=True,
  242. ).and_return([flexmock()]).ordered()
  243. flexmock(module).should_receive('log_error_records').with_args(
  244. 'bar: Error running actions for repository',
  245. OSError,
  246. levelno=logging.WARNING,
  247. log_command_error_output=True,
  248. ).and_return(flexmock()).ordered()
  249. error_logs = [flexmock()]
  250. flexmock(module).should_receive('log_error_records').with_args(
  251. 'bar: Error running actions for repository', OSError
  252. ).and_return(error_logs).ordered()
  253. config = {'location': {'repositories': ['foo', 'bar']}, 'storage': {'retries': 1}}
  254. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  255. results = list(module.run_configuration('test.yaml', config, arguments))
  256. assert results == error_logs
  257. def test_run_configuration_retry_wait():
  258. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  259. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  260. flexmock(module.command).should_receive('execute_hook')
  261. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  262. flexmock(module).should_receive('log_error_records').with_args(
  263. 'foo: Error running actions for repository',
  264. OSError,
  265. levelno=logging.WARNING,
  266. log_command_error_output=True,
  267. ).and_return([flexmock()]).ordered()
  268. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  269. flexmock(module).should_receive('log_error_records').with_args(
  270. 'foo: Error running actions for repository',
  271. OSError,
  272. levelno=logging.WARNING,
  273. log_command_error_output=True,
  274. ).and_return([flexmock()]).ordered()
  275. flexmock(time).should_receive('sleep').with_args(20).and_return().ordered()
  276. flexmock(module).should_receive('log_error_records').with_args(
  277. 'foo: Error running actions for repository',
  278. OSError,
  279. levelno=logging.WARNING,
  280. log_command_error_output=True,
  281. ).and_return([flexmock()]).ordered()
  282. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  283. error_logs = [flexmock()]
  284. flexmock(module).should_receive('log_error_records').with_args(
  285. 'foo: Error running actions for repository', OSError
  286. ).and_return(error_logs).ordered()
  287. config = {'location': {'repositories': ['foo']}, 'storage': {'retries': 3, 'retry_wait': 10}}
  288. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  289. results = list(module.run_configuration('test.yaml', config, arguments))
  290. assert results == error_logs
  291. def test_run_configuration_retries_timeout_multiple_repos():
  292. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  293. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  294. flexmock(module.command).should_receive('execute_hook')
  295. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  296. []
  297. ).and_raise(OSError).times(4)
  298. flexmock(module).should_receive('log_error_records').with_args(
  299. 'foo: Error running actions for repository',
  300. OSError,
  301. levelno=logging.WARNING,
  302. log_command_error_output=True,
  303. ).and_return([flexmock()]).ordered()
  304. flexmock(module).should_receive('log_error_records').with_args(
  305. 'bar: Error running actions for repository',
  306. OSError,
  307. levelno=logging.WARNING,
  308. log_command_error_output=True,
  309. ).and_return([flexmock()]).ordered()
  310. # Sleep before retrying foo (and passing)
  311. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  312. # Sleep before retrying bar (and failing)
  313. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  314. error_logs = [flexmock()]
  315. flexmock(module).should_receive('log_error_records').with_args(
  316. 'bar: Error running actions for repository', OSError
  317. ).and_return(error_logs).ordered()
  318. config = {
  319. 'location': {'repositories': ['foo', 'bar']},
  320. 'storage': {'retries': 1, 'retry_wait': 10},
  321. }
  322. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  323. results = list(module.run_configuration('test.yaml', config, arguments))
  324. assert results == error_logs
  325. def test_run_actions_runs_rcreate():
  326. flexmock(module).should_receive('add_custom_log_levels')
  327. flexmock(module.command).should_receive('execute_hook')
  328. flexmock(borgmatic.actions.rcreate).should_receive('run_rcreate').once()
  329. tuple(
  330. module.run_actions(
  331. arguments={'global': flexmock(dry_run=False), 'rcreate': flexmock()},
  332. config_filename=flexmock(),
  333. location={'repositories': []},
  334. storage=flexmock(),
  335. retention=flexmock(),
  336. consistency=flexmock(),
  337. hooks={},
  338. local_path=flexmock(),
  339. remote_path=flexmock(),
  340. local_borg_version=flexmock(),
  341. repository_path='repo',
  342. )
  343. )
  344. def test_run_actions_runs_transfer():
  345. flexmock(module).should_receive('add_custom_log_levels')
  346. flexmock(module.command).should_receive('execute_hook')
  347. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  348. tuple(
  349. module.run_actions(
  350. arguments={'global': flexmock(dry_run=False), 'transfer': flexmock()},
  351. config_filename=flexmock(),
  352. location={'repositories': []},
  353. storage=flexmock(),
  354. retention=flexmock(),
  355. consistency=flexmock(),
  356. hooks={},
  357. local_path=flexmock(),
  358. remote_path=flexmock(),
  359. local_borg_version=flexmock(),
  360. repository_path='repo',
  361. )
  362. )
  363. def test_run_actions_runs_prune():
  364. flexmock(module).should_receive('add_custom_log_levels')
  365. flexmock(module.command).should_receive('execute_hook')
  366. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  367. tuple(
  368. module.run_actions(
  369. arguments={'global': flexmock(dry_run=False), 'prune': flexmock()},
  370. config_filename=flexmock(),
  371. location={'repositories': []},
  372. storage=flexmock(),
  373. retention=flexmock(),
  374. consistency=flexmock(),
  375. hooks={},
  376. local_path=flexmock(),
  377. remote_path=flexmock(),
  378. local_borg_version=flexmock(),
  379. repository_path='repo',
  380. )
  381. )
  382. def test_run_actions_runs_compact():
  383. flexmock(module).should_receive('add_custom_log_levels')
  384. flexmock(module.command).should_receive('execute_hook')
  385. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  386. tuple(
  387. module.run_actions(
  388. arguments={'global': flexmock(dry_run=False), 'compact': flexmock()},
  389. config_filename=flexmock(),
  390. location={'repositories': []},
  391. storage=flexmock(),
  392. retention=flexmock(),
  393. consistency=flexmock(),
  394. hooks={},
  395. local_path=flexmock(),
  396. remote_path=flexmock(),
  397. local_borg_version=flexmock(),
  398. repository_path='repo',
  399. )
  400. )
  401. def test_run_actions_runs_create():
  402. flexmock(module).should_receive('add_custom_log_levels')
  403. flexmock(module.command).should_receive('execute_hook')
  404. expected = flexmock()
  405. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  406. result = tuple(
  407. module.run_actions(
  408. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  409. config_filename=flexmock(),
  410. location={'repositories': []},
  411. storage=flexmock(),
  412. retention=flexmock(),
  413. consistency=flexmock(),
  414. hooks={},
  415. local_path=flexmock(),
  416. remote_path=flexmock(),
  417. local_borg_version=flexmock(),
  418. repository_path='repo',
  419. )
  420. )
  421. assert result == (expected,)
  422. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  423. flexmock(module).should_receive('add_custom_log_levels')
  424. flexmock(module.command).should_receive('execute_hook')
  425. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  426. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  427. tuple(
  428. module.run_actions(
  429. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  430. config_filename=flexmock(),
  431. location={'repositories': []},
  432. storage=flexmock(),
  433. retention=flexmock(),
  434. consistency=flexmock(),
  435. hooks={},
  436. local_path=flexmock(),
  437. remote_path=flexmock(),
  438. local_borg_version=flexmock(),
  439. repository_path='repo',
  440. )
  441. )
  442. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  443. flexmock(module).should_receive('add_custom_log_levels')
  444. flexmock(module.command).should_receive('execute_hook')
  445. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  446. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  447. tuple(
  448. module.run_actions(
  449. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  450. config_filename=flexmock(),
  451. location={'repositories': []},
  452. storage=flexmock(),
  453. retention=flexmock(),
  454. consistency=flexmock(),
  455. hooks={},
  456. local_path=flexmock(),
  457. remote_path=flexmock(),
  458. local_borg_version=flexmock(),
  459. repository_path='repo',
  460. )
  461. )
  462. def test_run_actions_runs_extract():
  463. flexmock(module).should_receive('add_custom_log_levels')
  464. flexmock(module.command).should_receive('execute_hook')
  465. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  466. tuple(
  467. module.run_actions(
  468. arguments={'global': flexmock(dry_run=False), 'extract': flexmock()},
  469. config_filename=flexmock(),
  470. location={'repositories': []},
  471. storage=flexmock(),
  472. retention=flexmock(),
  473. consistency=flexmock(),
  474. hooks={},
  475. local_path=flexmock(),
  476. remote_path=flexmock(),
  477. local_borg_version=flexmock(),
  478. repository_path='repo',
  479. )
  480. )
  481. def test_run_actions_runs_export_tar():
  482. flexmock(module).should_receive('add_custom_log_levels')
  483. flexmock(module.command).should_receive('execute_hook')
  484. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  485. tuple(
  486. module.run_actions(
  487. arguments={'global': flexmock(dry_run=False), 'export-tar': flexmock()},
  488. config_filename=flexmock(),
  489. location={'repositories': []},
  490. storage=flexmock(),
  491. retention=flexmock(),
  492. consistency=flexmock(),
  493. hooks={},
  494. local_path=flexmock(),
  495. remote_path=flexmock(),
  496. local_borg_version=flexmock(),
  497. repository_path='repo',
  498. )
  499. )
  500. def test_run_actions_runs_mount():
  501. flexmock(module).should_receive('add_custom_log_levels')
  502. flexmock(module.command).should_receive('execute_hook')
  503. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  504. tuple(
  505. module.run_actions(
  506. arguments={'global': flexmock(dry_run=False), 'mount': flexmock()},
  507. config_filename=flexmock(),
  508. location={'repositories': []},
  509. storage=flexmock(),
  510. retention=flexmock(),
  511. consistency=flexmock(),
  512. hooks={},
  513. local_path=flexmock(),
  514. remote_path=flexmock(),
  515. local_borg_version=flexmock(),
  516. repository_path='repo',
  517. )
  518. )
  519. def test_run_actions_runs_restore():
  520. flexmock(module).should_receive('add_custom_log_levels')
  521. flexmock(module.command).should_receive('execute_hook')
  522. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  523. tuple(
  524. module.run_actions(
  525. arguments={'global': flexmock(dry_run=False), 'restore': flexmock()},
  526. config_filename=flexmock(),
  527. location={'repositories': []},
  528. storage=flexmock(),
  529. retention=flexmock(),
  530. consistency=flexmock(),
  531. hooks={},
  532. local_path=flexmock(),
  533. remote_path=flexmock(),
  534. local_borg_version=flexmock(),
  535. repository_path='repo',
  536. )
  537. )
  538. def test_run_actions_runs_rlist():
  539. flexmock(module).should_receive('add_custom_log_levels')
  540. flexmock(module.command).should_receive('execute_hook')
  541. expected = flexmock()
  542. flexmock(borgmatic.actions.rlist).should_receive('run_rlist').and_yield(expected).once()
  543. result = tuple(
  544. module.run_actions(
  545. arguments={'global': flexmock(dry_run=False), 'rlist': flexmock()},
  546. config_filename=flexmock(),
  547. location={'repositories': []},
  548. storage=flexmock(),
  549. retention=flexmock(),
  550. consistency=flexmock(),
  551. hooks={},
  552. local_path=flexmock(),
  553. remote_path=flexmock(),
  554. local_borg_version=flexmock(),
  555. repository_path='repo',
  556. )
  557. )
  558. assert result == (expected,)
  559. def test_run_actions_runs_list():
  560. flexmock(module).should_receive('add_custom_log_levels')
  561. flexmock(module.command).should_receive('execute_hook')
  562. expected = flexmock()
  563. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  564. result = tuple(
  565. module.run_actions(
  566. arguments={'global': flexmock(dry_run=False), 'list': flexmock()},
  567. config_filename=flexmock(),
  568. location={'repositories': []},
  569. storage=flexmock(),
  570. retention=flexmock(),
  571. consistency=flexmock(),
  572. hooks={},
  573. local_path=flexmock(),
  574. remote_path=flexmock(),
  575. local_borg_version=flexmock(),
  576. repository_path='repo',
  577. )
  578. )
  579. assert result == (expected,)
  580. def test_run_actions_runs_rinfo():
  581. flexmock(module).should_receive('add_custom_log_levels')
  582. flexmock(module.command).should_receive('execute_hook')
  583. expected = flexmock()
  584. flexmock(borgmatic.actions.rinfo).should_receive('run_rinfo').and_yield(expected).once()
  585. result = tuple(
  586. module.run_actions(
  587. arguments={'global': flexmock(dry_run=False), 'rinfo': flexmock()},
  588. config_filename=flexmock(),
  589. location={'repositories': []},
  590. storage=flexmock(),
  591. retention=flexmock(),
  592. consistency=flexmock(),
  593. hooks={},
  594. local_path=flexmock(),
  595. remote_path=flexmock(),
  596. local_borg_version=flexmock(),
  597. repository_path='repo',
  598. )
  599. )
  600. assert result == (expected,)
  601. def test_run_actions_runs_info():
  602. flexmock(module).should_receive('add_custom_log_levels')
  603. flexmock(module.command).should_receive('execute_hook')
  604. expected = flexmock()
  605. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  606. result = tuple(
  607. module.run_actions(
  608. arguments={'global': flexmock(dry_run=False), 'info': flexmock()},
  609. config_filename=flexmock(),
  610. location={'repositories': []},
  611. storage=flexmock(),
  612. retention=flexmock(),
  613. consistency=flexmock(),
  614. hooks={},
  615. local_path=flexmock(),
  616. remote_path=flexmock(),
  617. local_borg_version=flexmock(),
  618. repository_path='repo',
  619. )
  620. )
  621. assert result == (expected,)
  622. def test_run_actions_runs_break_lock():
  623. flexmock(module).should_receive('add_custom_log_levels')
  624. flexmock(module.command).should_receive('execute_hook')
  625. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  626. tuple(
  627. module.run_actions(
  628. arguments={'global': flexmock(dry_run=False), 'break-lock': flexmock()},
  629. config_filename=flexmock(),
  630. location={'repositories': []},
  631. storage=flexmock(),
  632. retention=flexmock(),
  633. consistency=flexmock(),
  634. hooks={},
  635. local_path=flexmock(),
  636. remote_path=flexmock(),
  637. local_borg_version=flexmock(),
  638. repository_path='repo',
  639. )
  640. )
  641. def test_run_actions_runs_borg():
  642. flexmock(module).should_receive('add_custom_log_levels')
  643. flexmock(module.command).should_receive('execute_hook')
  644. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  645. tuple(
  646. module.run_actions(
  647. arguments={'global': flexmock(dry_run=False), 'borg': flexmock()},
  648. config_filename=flexmock(),
  649. location={'repositories': []},
  650. storage=flexmock(),
  651. retention=flexmock(),
  652. consistency=flexmock(),
  653. hooks={},
  654. local_path=flexmock(),
  655. remote_path=flexmock(),
  656. local_borg_version=flexmock(),
  657. repository_path='repo',
  658. )
  659. )
  660. def test_load_configurations_collects_parsed_configurations_and_logs():
  661. configuration = flexmock()
  662. other_configuration = flexmock()
  663. test_expected_logs = [flexmock(), flexmock()]
  664. other_expected_logs = [flexmock(), flexmock()]
  665. flexmock(module.validate).should_receive('parse_configuration').and_return(
  666. configuration, test_expected_logs
  667. ).and_return(other_configuration, other_expected_logs)
  668. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  669. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  670. assert logs == test_expected_logs + other_expected_logs
  671. def test_load_configurations_logs_warning_for_permission_error():
  672. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  673. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  674. assert configs == {}
  675. assert {log.levelno for log in logs} == {logging.WARNING}
  676. def test_load_configurations_logs_critical_for_parse_error():
  677. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  678. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  679. assert configs == {}
  680. assert {log.levelno for log in logs} == {logging.CRITICAL}
  681. def test_log_record_does_not_raise():
  682. module.log_record(levelno=1, foo='bar', baz='quux')
  683. def test_log_record_with_suppress_does_not_raise():
  684. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  685. def test_log_error_records_generates_output_logs_for_message_only():
  686. flexmock(module).should_receive('log_record').replace_with(dict)
  687. logs = tuple(module.log_error_records('Error'))
  688. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  689. def test_log_error_records_generates_output_logs_for_called_process_error():
  690. flexmock(module).should_receive('log_record').replace_with(dict)
  691. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  692. logs = tuple(
  693. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  694. )
  695. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  696. assert any(log for log in logs if 'error output' in str(log))
  697. def test_log_error_records_generates_logs_for_value_error():
  698. flexmock(module).should_receive('log_record').replace_with(dict)
  699. logs = tuple(module.log_error_records('Error', ValueError()))
  700. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  701. def test_log_error_records_generates_logs_for_os_error():
  702. flexmock(module).should_receive('log_record').replace_with(dict)
  703. logs = tuple(module.log_error_records('Error', OSError()))
  704. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  705. def test_log_error_records_generates_nothing_for_other_error():
  706. flexmock(module).should_receive('log_record').replace_with(dict)
  707. logs = tuple(module.log_error_records('Error', KeyError()))
  708. assert logs == ()
  709. def test_get_local_path_uses_configuration_value():
  710. assert module.get_local_path({'test.yaml': {'location': {'local_path': 'borg1'}}}) == 'borg1'
  711. def test_get_local_path_without_location_defaults_to_borg():
  712. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  713. def test_get_local_path_without_local_path_defaults_to_borg():
  714. assert module.get_local_path({'test.yaml': {'location': {}}}) == 'borg'
  715. def test_collect_configuration_run_summary_logs_info_for_success():
  716. flexmock(module.command).should_receive('execute_hook').never()
  717. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  718. flexmock(module).should_receive('run_configuration').and_return([])
  719. arguments = {}
  720. logs = tuple(
  721. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  722. )
  723. assert {log.levelno for log in logs} == {logging.INFO}
  724. def test_collect_configuration_run_summary_executes_hooks_for_create():
  725. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  726. flexmock(module).should_receive('run_configuration').and_return([])
  727. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  728. logs = tuple(
  729. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  730. )
  731. assert {log.levelno for log in logs} == {logging.INFO}
  732. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  733. flexmock(module.validate).should_receive('guard_single_repository_selected')
  734. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  735. flexmock(module).should_receive('run_configuration').and_return([])
  736. arguments = {'extract': flexmock(repository='repo')}
  737. logs = tuple(
  738. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  739. )
  740. assert {log.levelno for log in logs} == {logging.INFO}
  741. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  742. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  743. ValueError
  744. )
  745. expected_logs = (flexmock(),)
  746. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  747. arguments = {'extract': flexmock(repository='repo')}
  748. logs = tuple(
  749. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  750. )
  751. assert logs == expected_logs
  752. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  753. flexmock(module.validate).should_receive('guard_single_repository_selected')
  754. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  755. flexmock(module).should_receive('run_configuration').and_return([])
  756. arguments = {'mount': flexmock(repository='repo')}
  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_mount_with_repository_error():
  762. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  763. ValueError
  764. )
  765. expected_logs = (flexmock(),)
  766. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  767. arguments = {'mount': flexmock(repository='repo')}
  768. logs = tuple(
  769. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  770. )
  771. assert logs == expected_logs
  772. def test_collect_configuration_run_summary_logs_missing_configs_error():
  773. arguments = {'global': flexmock(config_paths=[])}
  774. expected_logs = (flexmock(),)
  775. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  776. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  777. assert logs == expected_logs
  778. def test_collect_configuration_run_summary_logs_pre_hook_error():
  779. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  780. expected_logs = (flexmock(),)
  781. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  782. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  783. logs = tuple(
  784. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  785. )
  786. assert logs == expected_logs
  787. def test_collect_configuration_run_summary_logs_post_hook_error():
  788. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  789. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  790. flexmock(module).should_receive('run_configuration').and_return([])
  791. expected_logs = (flexmock(),)
  792. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  793. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  794. logs = tuple(
  795. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  796. )
  797. assert expected_logs[0] in logs
  798. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  799. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  800. ValueError
  801. )
  802. expected_logs = (flexmock(),)
  803. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  804. arguments = {'list': flexmock(repository='repo', archive='test')}
  805. logs = tuple(
  806. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  807. )
  808. assert logs == expected_logs
  809. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  810. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  811. flexmock(module).should_receive('run_configuration').and_return([])
  812. arguments = {'list': flexmock(repository='repo', archive=None)}
  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}
  817. def test_collect_configuration_run_summary_logs_run_configuration_error():
  818. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  819. flexmock(module).should_receive('run_configuration').and_return(
  820. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  821. )
  822. flexmock(module).should_receive('log_error_records').and_return([])
  823. arguments = {}
  824. logs = tuple(
  825. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  826. )
  827. assert {log.levelno for log in logs} == {logging.CRITICAL}
  828. def test_collect_configuration_run_summary_logs_run_umount_error():
  829. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  830. flexmock(module).should_receive('run_configuration').and_return([])
  831. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  832. flexmock(module).should_receive('log_error_records').and_return(
  833. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  834. )
  835. arguments = {'umount': flexmock(mount_point='/mnt')}
  836. logs = tuple(
  837. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  838. )
  839. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  840. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  841. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  842. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  843. ['baz']
  844. )
  845. stdout = flexmock()
  846. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  847. flexmock(module.sys).stdout = stdout
  848. arguments = {}
  849. tuple(
  850. module.collect_configuration_run_summary_logs(
  851. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  852. )
  853. )