test_borgmatic.py 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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': [{'path': 'foo'}, {'path': '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': [{'path': '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': [{'path': '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': [{'path': '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': [{'path': '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': [{'path': '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': [{'path': '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': [{'path': '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': [{'path': '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': [{'path': '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': [{'path': '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': [{'path': 'foo'}, {'path': '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_robin():
  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 = {
  227. 'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]},
  228. 'storage': {'retries': 1},
  229. }
  230. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  231. results = list(module.run_configuration('test.yaml', config, arguments))
  232. assert results == foo_error_logs + bar_error_logs
  233. def test_run_configuration_retries_one_passes():
  234. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  235. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  236. flexmock(module.command).should_receive('execute_hook')
  237. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  238. []
  239. ).and_raise(OSError).times(4)
  240. flexmock(module).should_receive('log_error_records').with_args(
  241. 'foo: Error running actions for repository',
  242. OSError,
  243. levelno=logging.WARNING,
  244. log_command_error_output=True,
  245. ).and_return([flexmock()]).ordered()
  246. flexmock(module).should_receive('log_error_records').with_args(
  247. 'bar: Error running actions for repository',
  248. OSError,
  249. levelno=logging.WARNING,
  250. log_command_error_output=True,
  251. ).and_return(flexmock()).ordered()
  252. error_logs = [flexmock()]
  253. flexmock(module).should_receive('log_error_records').with_args(
  254. 'bar: Error running actions for repository', OSError
  255. ).and_return(error_logs).ordered()
  256. config = {
  257. 'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]},
  258. 'storage': {'retries': 1},
  259. }
  260. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  261. results = list(module.run_configuration('test.yaml', config, arguments))
  262. assert results == error_logs
  263. def test_run_configuration_retry_wait():
  264. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  265. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  266. flexmock(module.command).should_receive('execute_hook')
  267. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  268. flexmock(module).should_receive('log_error_records').with_args(
  269. 'foo: Error running actions for repository',
  270. OSError,
  271. levelno=logging.WARNING,
  272. log_command_error_output=True,
  273. ).and_return([flexmock()]).ordered()
  274. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  275. flexmock(module).should_receive('log_error_records').with_args(
  276. 'foo: Error running actions for repository',
  277. OSError,
  278. levelno=logging.WARNING,
  279. log_command_error_output=True,
  280. ).and_return([flexmock()]).ordered()
  281. flexmock(time).should_receive('sleep').with_args(20).and_return().ordered()
  282. flexmock(module).should_receive('log_error_records').with_args(
  283. 'foo: Error running actions for repository',
  284. OSError,
  285. levelno=logging.WARNING,
  286. log_command_error_output=True,
  287. ).and_return([flexmock()]).ordered()
  288. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  289. error_logs = [flexmock()]
  290. flexmock(module).should_receive('log_error_records').with_args(
  291. 'foo: Error running actions for repository', OSError
  292. ).and_return(error_logs).ordered()
  293. config = {
  294. 'location': {'repositories': [{'path': 'foo'}]},
  295. 'storage': {'retries': 3, 'retry_wait': 10},
  296. }
  297. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  298. results = list(module.run_configuration('test.yaml', config, arguments))
  299. assert results == error_logs
  300. def test_run_configuration_retries_timeout_multiple_repos():
  301. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  302. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  303. flexmock(module.command).should_receive('execute_hook')
  304. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  305. []
  306. ).and_raise(OSError).times(4)
  307. flexmock(module).should_receive('log_error_records').with_args(
  308. 'foo: Error running actions for repository',
  309. OSError,
  310. levelno=logging.WARNING,
  311. log_command_error_output=True,
  312. ).and_return([flexmock()]).ordered()
  313. flexmock(module).should_receive('log_error_records').with_args(
  314. 'bar: Error running actions for repository',
  315. OSError,
  316. levelno=logging.WARNING,
  317. log_command_error_output=True,
  318. ).and_return([flexmock()]).ordered()
  319. # Sleep before retrying foo (and passing)
  320. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  321. # Sleep before retrying bar (and failing)
  322. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  323. error_logs = [flexmock()]
  324. flexmock(module).should_receive('log_error_records').with_args(
  325. 'bar: Error running actions for repository', OSError
  326. ).and_return(error_logs).ordered()
  327. config = {
  328. 'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]},
  329. 'storage': {'retries': 1, 'retry_wait': 10},
  330. }
  331. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  332. results = list(module.run_configuration('test.yaml', config, arguments))
  333. assert results == error_logs
  334. def test_run_actions_runs_rcreate():
  335. flexmock(module).should_receive('add_custom_log_levels')
  336. flexmock(module.command).should_receive('execute_hook')
  337. flexmock(borgmatic.actions.rcreate).should_receive('run_rcreate').once()
  338. tuple(
  339. module.run_actions(
  340. arguments={'global': flexmock(dry_run=False), 'rcreate': flexmock()},
  341. config_filename=flexmock(),
  342. location={'repositories': []},
  343. storage=flexmock(),
  344. retention=flexmock(),
  345. consistency=flexmock(),
  346. hooks={},
  347. local_path=flexmock(),
  348. remote_path=flexmock(),
  349. local_borg_version=flexmock(),
  350. repository={'path': 'repo'},
  351. )
  352. )
  353. def test_run_actions_runs_transfer():
  354. flexmock(module).should_receive('add_custom_log_levels')
  355. flexmock(module.command).should_receive('execute_hook')
  356. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  357. tuple(
  358. module.run_actions(
  359. arguments={'global': flexmock(dry_run=False), 'transfer': flexmock()},
  360. config_filename=flexmock(),
  361. location={'repositories': []},
  362. storage=flexmock(),
  363. retention=flexmock(),
  364. consistency=flexmock(),
  365. hooks={},
  366. local_path=flexmock(),
  367. remote_path=flexmock(),
  368. local_borg_version=flexmock(),
  369. repository={'path': 'repo'},
  370. )
  371. )
  372. def test_run_actions_runs_create():
  373. flexmock(module).should_receive('add_custom_log_levels')
  374. flexmock(module.command).should_receive('execute_hook')
  375. expected = flexmock()
  376. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  377. result = tuple(
  378. module.run_actions(
  379. arguments={'global': flexmock(dry_run=False), 'create': flexmock()},
  380. config_filename=flexmock(),
  381. location={'repositories': []},
  382. storage=flexmock(),
  383. retention=flexmock(),
  384. consistency=flexmock(),
  385. hooks={},
  386. local_path=flexmock(),
  387. remote_path=flexmock(),
  388. local_borg_version=flexmock(),
  389. repository={'path': 'repo'},
  390. )
  391. )
  392. assert result == (expected,)
  393. def test_run_actions_runs_prune():
  394. flexmock(module).should_receive('add_custom_log_levels')
  395. flexmock(module.command).should_receive('execute_hook')
  396. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  397. tuple(
  398. module.run_actions(
  399. arguments={'global': flexmock(dry_run=False), 'prune': flexmock()},
  400. config_filename=flexmock(),
  401. location={'repositories': []},
  402. storage=flexmock(),
  403. retention=flexmock(),
  404. consistency=flexmock(),
  405. hooks={},
  406. local_path=flexmock(),
  407. remote_path=flexmock(),
  408. local_borg_version=flexmock(),
  409. repository={'path': 'repo'},
  410. )
  411. )
  412. def test_run_actions_runs_compact():
  413. flexmock(module).should_receive('add_custom_log_levels')
  414. flexmock(module.command).should_receive('execute_hook')
  415. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  416. tuple(
  417. module.run_actions(
  418. arguments={'global': flexmock(dry_run=False), 'compact': flexmock()},
  419. config_filename=flexmock(),
  420. location={'repositories': []},
  421. storage=flexmock(),
  422. retention=flexmock(),
  423. consistency=flexmock(),
  424. hooks={},
  425. local_path=flexmock(),
  426. remote_path=flexmock(),
  427. local_borg_version=flexmock(),
  428. repository={'path': 'repo'},
  429. )
  430. )
  431. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  432. flexmock(module).should_receive('add_custom_log_levels')
  433. flexmock(module.command).should_receive('execute_hook')
  434. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  435. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  436. tuple(
  437. module.run_actions(
  438. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  439. config_filename=flexmock(),
  440. location={'repositories': []},
  441. storage=flexmock(),
  442. retention=flexmock(),
  443. consistency=flexmock(),
  444. hooks={},
  445. local_path=flexmock(),
  446. remote_path=flexmock(),
  447. local_borg_version=flexmock(),
  448. repository={'path': 'repo'},
  449. )
  450. )
  451. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  452. flexmock(module).should_receive('add_custom_log_levels')
  453. flexmock(module.command).should_receive('execute_hook')
  454. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  455. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  456. tuple(
  457. module.run_actions(
  458. arguments={'global': flexmock(dry_run=False), 'check': flexmock()},
  459. config_filename=flexmock(),
  460. location={'repositories': []},
  461. storage=flexmock(),
  462. retention=flexmock(),
  463. consistency=flexmock(),
  464. hooks={},
  465. local_path=flexmock(),
  466. remote_path=flexmock(),
  467. local_borg_version=flexmock(),
  468. repository={'path': 'repo'},
  469. )
  470. )
  471. def test_run_actions_runs_extract():
  472. flexmock(module).should_receive('add_custom_log_levels')
  473. flexmock(module.command).should_receive('execute_hook')
  474. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  475. tuple(
  476. module.run_actions(
  477. arguments={'global': flexmock(dry_run=False), 'extract': flexmock()},
  478. config_filename=flexmock(),
  479. location={'repositories': []},
  480. storage=flexmock(),
  481. retention=flexmock(),
  482. consistency=flexmock(),
  483. hooks={},
  484. local_path=flexmock(),
  485. remote_path=flexmock(),
  486. local_borg_version=flexmock(),
  487. repository={'path': 'repo'},
  488. )
  489. )
  490. def test_run_actions_runs_export_tar():
  491. flexmock(module).should_receive('add_custom_log_levels')
  492. flexmock(module.command).should_receive('execute_hook')
  493. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  494. tuple(
  495. module.run_actions(
  496. arguments={'global': flexmock(dry_run=False), 'export-tar': flexmock()},
  497. config_filename=flexmock(),
  498. location={'repositories': []},
  499. storage=flexmock(),
  500. retention=flexmock(),
  501. consistency=flexmock(),
  502. hooks={},
  503. local_path=flexmock(),
  504. remote_path=flexmock(),
  505. local_borg_version=flexmock(),
  506. repository={'path': 'repo'},
  507. )
  508. )
  509. def test_run_actions_runs_mount():
  510. flexmock(module).should_receive('add_custom_log_levels')
  511. flexmock(module.command).should_receive('execute_hook')
  512. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  513. tuple(
  514. module.run_actions(
  515. arguments={'global': flexmock(dry_run=False), 'mount': flexmock()},
  516. config_filename=flexmock(),
  517. location={'repositories': []},
  518. storage=flexmock(),
  519. retention=flexmock(),
  520. consistency=flexmock(),
  521. hooks={},
  522. local_path=flexmock(),
  523. remote_path=flexmock(),
  524. local_borg_version=flexmock(),
  525. repository={'path': 'repo'},
  526. )
  527. )
  528. def test_run_actions_runs_restore():
  529. flexmock(module).should_receive('add_custom_log_levels')
  530. flexmock(module.command).should_receive('execute_hook')
  531. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  532. tuple(
  533. module.run_actions(
  534. arguments={'global': flexmock(dry_run=False), 'restore': flexmock()},
  535. config_filename=flexmock(),
  536. location={'repositories': []},
  537. storage=flexmock(),
  538. retention=flexmock(),
  539. consistency=flexmock(),
  540. hooks={},
  541. local_path=flexmock(),
  542. remote_path=flexmock(),
  543. local_borg_version=flexmock(),
  544. repository={'path': 'repo'},
  545. )
  546. )
  547. def test_run_actions_runs_rlist():
  548. flexmock(module).should_receive('add_custom_log_levels')
  549. flexmock(module.command).should_receive('execute_hook')
  550. expected = flexmock()
  551. flexmock(borgmatic.actions.rlist).should_receive('run_rlist').and_yield(expected).once()
  552. result = tuple(
  553. module.run_actions(
  554. arguments={'global': flexmock(dry_run=False), 'rlist': flexmock()},
  555. config_filename=flexmock(),
  556. location={'repositories': []},
  557. storage=flexmock(),
  558. retention=flexmock(),
  559. consistency=flexmock(),
  560. hooks={},
  561. local_path=flexmock(),
  562. remote_path=flexmock(),
  563. local_borg_version=flexmock(),
  564. repository={'path': 'repo'},
  565. )
  566. )
  567. assert result == (expected,)
  568. def test_run_actions_runs_list():
  569. flexmock(module).should_receive('add_custom_log_levels')
  570. flexmock(module.command).should_receive('execute_hook')
  571. expected = flexmock()
  572. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  573. result = tuple(
  574. module.run_actions(
  575. arguments={'global': flexmock(dry_run=False), 'list': flexmock()},
  576. config_filename=flexmock(),
  577. location={'repositories': []},
  578. storage=flexmock(),
  579. retention=flexmock(),
  580. consistency=flexmock(),
  581. hooks={},
  582. local_path=flexmock(),
  583. remote_path=flexmock(),
  584. local_borg_version=flexmock(),
  585. repository={'path': 'repo'},
  586. )
  587. )
  588. assert result == (expected,)
  589. def test_run_actions_runs_rinfo():
  590. flexmock(module).should_receive('add_custom_log_levels')
  591. flexmock(module.command).should_receive('execute_hook')
  592. expected = flexmock()
  593. flexmock(borgmatic.actions.rinfo).should_receive('run_rinfo').and_yield(expected).once()
  594. result = tuple(
  595. module.run_actions(
  596. arguments={'global': flexmock(dry_run=False), 'rinfo': flexmock()},
  597. config_filename=flexmock(),
  598. location={'repositories': []},
  599. storage=flexmock(),
  600. retention=flexmock(),
  601. consistency=flexmock(),
  602. hooks={},
  603. local_path=flexmock(),
  604. remote_path=flexmock(),
  605. local_borg_version=flexmock(),
  606. repository={'path': 'repo'},
  607. )
  608. )
  609. assert result == (expected,)
  610. def test_run_actions_runs_info():
  611. flexmock(module).should_receive('add_custom_log_levels')
  612. flexmock(module.command).should_receive('execute_hook')
  613. expected = flexmock()
  614. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  615. result = tuple(
  616. module.run_actions(
  617. arguments={'global': flexmock(dry_run=False), 'info': flexmock()},
  618. config_filename=flexmock(),
  619. location={'repositories': []},
  620. storage=flexmock(),
  621. retention=flexmock(),
  622. consistency=flexmock(),
  623. hooks={},
  624. local_path=flexmock(),
  625. remote_path=flexmock(),
  626. local_borg_version=flexmock(),
  627. repository={'path': 'repo'},
  628. )
  629. )
  630. assert result == (expected,)
  631. def test_run_actions_runs_break_lock():
  632. flexmock(module).should_receive('add_custom_log_levels')
  633. flexmock(module.command).should_receive('execute_hook')
  634. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  635. tuple(
  636. module.run_actions(
  637. arguments={'global': flexmock(dry_run=False), 'break-lock': flexmock()},
  638. config_filename=flexmock(),
  639. location={'repositories': []},
  640. storage=flexmock(),
  641. retention=flexmock(),
  642. consistency=flexmock(),
  643. hooks={},
  644. local_path=flexmock(),
  645. remote_path=flexmock(),
  646. local_borg_version=flexmock(),
  647. repository={'path': 'repo'},
  648. )
  649. )
  650. def test_run_actions_runs_borg():
  651. flexmock(module).should_receive('add_custom_log_levels')
  652. flexmock(module.command).should_receive('execute_hook')
  653. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  654. tuple(
  655. module.run_actions(
  656. arguments={'global': flexmock(dry_run=False), 'borg': flexmock()},
  657. config_filename=flexmock(),
  658. location={'repositories': []},
  659. storage=flexmock(),
  660. retention=flexmock(),
  661. consistency=flexmock(),
  662. hooks={},
  663. local_path=flexmock(),
  664. remote_path=flexmock(),
  665. local_borg_version=flexmock(),
  666. repository={'path': 'repo'},
  667. )
  668. )
  669. def test_run_actions_runs_multiple_actions_in_argument_order():
  670. flexmock(module).should_receive('add_custom_log_levels')
  671. flexmock(module.command).should_receive('execute_hook')
  672. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  673. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  674. tuple(
  675. module.run_actions(
  676. arguments={
  677. 'global': flexmock(dry_run=False),
  678. 'borg': flexmock(),
  679. 'restore': flexmock(),
  680. },
  681. config_filename=flexmock(),
  682. location={'repositories': []},
  683. storage=flexmock(),
  684. retention=flexmock(),
  685. consistency=flexmock(),
  686. hooks={},
  687. local_path=flexmock(),
  688. remote_path=flexmock(),
  689. local_borg_version=flexmock(),
  690. repository={'path': 'repo'},
  691. )
  692. )
  693. def test_load_configurations_collects_parsed_configurations_and_logs():
  694. configuration = flexmock()
  695. other_configuration = flexmock()
  696. test_expected_logs = [flexmock(), flexmock()]
  697. other_expected_logs = [flexmock(), flexmock()]
  698. flexmock(module.validate).should_receive('parse_configuration').and_return(
  699. configuration, test_expected_logs
  700. ).and_return(other_configuration, other_expected_logs)
  701. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  702. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  703. assert logs == test_expected_logs + other_expected_logs
  704. def test_load_configurations_logs_warning_for_permission_error():
  705. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  706. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  707. assert configs == {}
  708. assert {log.levelno for log in logs} == {logging.WARNING}
  709. def test_load_configurations_logs_critical_for_parse_error():
  710. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  711. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  712. assert configs == {}
  713. assert {log.levelno for log in logs} == {logging.CRITICAL}
  714. def test_log_record_does_not_raise():
  715. module.log_record(levelno=1, foo='bar', baz='quux')
  716. def test_log_record_with_suppress_does_not_raise():
  717. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  718. def test_log_error_records_generates_output_logs_for_message_only():
  719. flexmock(module).should_receive('log_record').replace_with(dict)
  720. logs = tuple(module.log_error_records('Error'))
  721. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  722. def test_log_error_records_generates_output_logs_for_called_process_error():
  723. flexmock(module).should_receive('log_record').replace_with(dict)
  724. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  725. logs = tuple(
  726. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  727. )
  728. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  729. assert any(log for log in logs if 'error output' in str(log))
  730. def test_log_error_records_generates_logs_for_value_error():
  731. flexmock(module).should_receive('log_record').replace_with(dict)
  732. logs = tuple(module.log_error_records('Error', ValueError()))
  733. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  734. def test_log_error_records_generates_logs_for_os_error():
  735. flexmock(module).should_receive('log_record').replace_with(dict)
  736. logs = tuple(module.log_error_records('Error', OSError()))
  737. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  738. def test_log_error_records_generates_nothing_for_other_error():
  739. flexmock(module).should_receive('log_record').replace_with(dict)
  740. logs = tuple(module.log_error_records('Error', KeyError()))
  741. assert logs == ()
  742. def test_get_local_path_uses_configuration_value():
  743. assert module.get_local_path({'test.yaml': {'location': {'local_path': 'borg1'}}}) == 'borg1'
  744. def test_get_local_path_without_location_defaults_to_borg():
  745. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  746. def test_get_local_path_without_local_path_defaults_to_borg():
  747. assert module.get_local_path({'test.yaml': {'location': {}}}) == 'borg'
  748. def test_collect_configuration_run_summary_logs_info_for_success():
  749. flexmock(module.command).should_receive('execute_hook').never()
  750. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  751. flexmock(module).should_receive('run_configuration').and_return([])
  752. arguments = {}
  753. logs = tuple(
  754. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  755. )
  756. assert {log.levelno for log in logs} == {logging.INFO}
  757. def test_collect_configuration_run_summary_executes_hooks_for_create():
  758. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  759. flexmock(module).should_receive('run_configuration').and_return([])
  760. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  761. logs = tuple(
  762. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  763. )
  764. assert {log.levelno for log in logs} == {logging.INFO}
  765. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  766. flexmock(module.validate).should_receive('guard_single_repository_selected')
  767. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  768. flexmock(module).should_receive('run_configuration').and_return([])
  769. arguments = {'extract': flexmock(repository='repo')}
  770. logs = tuple(
  771. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  772. )
  773. assert {log.levelno for log in logs} == {logging.INFO}
  774. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  775. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  776. ValueError
  777. )
  778. expected_logs = (flexmock(),)
  779. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  780. arguments = {'extract': flexmock(repository='repo')}
  781. logs = tuple(
  782. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  783. )
  784. assert logs == expected_logs
  785. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  786. flexmock(module.validate).should_receive('guard_single_repository_selected')
  787. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  788. flexmock(module).should_receive('run_configuration').and_return([])
  789. arguments = {'mount': flexmock(repository='repo')}
  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_mount_with_repository_error():
  795. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  796. ValueError
  797. )
  798. expected_logs = (flexmock(),)
  799. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  800. arguments = {'mount': flexmock(repository='repo')}
  801. logs = tuple(
  802. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  803. )
  804. assert logs == expected_logs
  805. def test_collect_configuration_run_summary_logs_missing_configs_error():
  806. arguments = {'global': flexmock(config_paths=[])}
  807. expected_logs = (flexmock(),)
  808. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  809. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  810. assert logs == expected_logs
  811. def test_collect_configuration_run_summary_logs_pre_hook_error():
  812. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  813. expected_logs = (flexmock(),)
  814. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  815. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  816. logs = tuple(
  817. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  818. )
  819. assert logs == expected_logs
  820. def test_collect_configuration_run_summary_logs_post_hook_error():
  821. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  822. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  823. flexmock(module).should_receive('run_configuration').and_return([])
  824. expected_logs = (flexmock(),)
  825. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  826. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  827. logs = tuple(
  828. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  829. )
  830. assert expected_logs[0] in logs
  831. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  832. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  833. ValueError
  834. )
  835. expected_logs = (flexmock(),)
  836. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  837. arguments = {'list': flexmock(repository='repo', archive='test')}
  838. logs = tuple(
  839. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  840. )
  841. assert logs == expected_logs
  842. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  843. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  844. flexmock(module).should_receive('run_configuration').and_return([])
  845. arguments = {'list': flexmock(repository='repo', archive=None)}
  846. logs = tuple(
  847. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  848. )
  849. assert {log.levelno for log in logs} == {logging.INFO}
  850. def test_collect_configuration_run_summary_logs_run_configuration_error():
  851. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  852. flexmock(module).should_receive('run_configuration').and_return(
  853. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  854. )
  855. flexmock(module).should_receive('log_error_records').and_return([])
  856. arguments = {}
  857. logs = tuple(
  858. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  859. )
  860. assert {log.levelno for log in logs} == {logging.CRITICAL}
  861. def test_collect_configuration_run_summary_logs_run_umount_error():
  862. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  863. flexmock(module).should_receive('run_configuration').and_return([])
  864. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  865. flexmock(module).should_receive('log_error_records').and_return(
  866. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  867. )
  868. arguments = {'umount': flexmock(mount_point='/mnt')}
  869. logs = tuple(
  870. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  871. )
  872. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  873. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  874. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  875. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  876. ['baz']
  877. )
  878. stdout = flexmock()
  879. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  880. flexmock(module.sys).stdout = stdout
  881. arguments = {}
  882. tuple(
  883. module.collect_configuration_run_summary_logs(
  884. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  885. )
  886. )