test_borgmatic.py 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  1. import logging
  2. import subprocess
  3. import time
  4. import pytest
  5. from flexmock import flexmock
  6. import borgmatic.hooks.command
  7. from borgmatic.commands import borgmatic as module
  8. @pytest.mark.parametrize(
  9. 'config,arguments,expected_actions',
  10. (
  11. ({}, {}, []),
  12. ({'skip_actions': []}, {}, []),
  13. ({'skip_actions': ['prune', 'check']}, {}, ['prune', 'check']),
  14. (
  15. {'skip_actions': ['prune', 'check']},
  16. {'check': flexmock(force=False)},
  17. ['prune', 'check'],
  18. ),
  19. ({'skip_actions': ['prune', 'check']}, {'check': flexmock(force=True)}, ['prune']),
  20. ),
  21. )
  22. def test_get_skip_actions_uses_config_and_arguments(config, arguments, expected_actions):
  23. assert module.get_skip_actions(config, arguments) == expected_actions
  24. def test_run_configuration_runs_actions_for_each_repository():
  25. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  26. flexmock(module).should_receive('get_skip_actions').and_return([])
  27. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  28. expected_results = [flexmock(), flexmock()]
  29. flexmock(module).should_receive('run_actions').and_return(expected_results[:1]).and_return(
  30. expected_results[1:]
  31. )
  32. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  33. arguments = {'global': flexmock(monitoring_verbosity=1)}
  34. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  35. assert results == expected_results
  36. def test_run_configuration_with_skip_actions_does_not_raise():
  37. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  38. flexmock(module).should_receive('get_skip_actions').and_return(['compact'])
  39. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  40. flexmock(module).should_receive('run_actions').and_return(flexmock()).and_return(flexmock())
  41. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}], 'skip_actions': ['compact']}
  42. arguments = {'global': flexmock(monitoring_verbosity=1)}
  43. list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  44. def test_run_configuration_with_invalid_borg_version_errors():
  45. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  46. flexmock(module).should_receive('get_skip_actions').and_return([])
  47. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  48. flexmock(module.command).should_receive('execute_hook').never()
  49. flexmock(module.dispatch).should_receive('call_hooks').never()
  50. flexmock(module).should_receive('run_actions').never()
  51. config = {'repositories': [{'path': 'foo'}]}
  52. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'prune': flexmock()}
  53. list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  54. def test_run_configuration_logs_monitor_start_error():
  55. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  56. flexmock(module).should_receive('get_skip_actions').and_return([])
  57. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  58. flexmock(module.dispatch).should_receive('call_hooks').and_raise(OSError).and_return(
  59. None
  60. ).and_return(None).and_return(None)
  61. expected_results = [flexmock()]
  62. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  63. flexmock(module).should_receive('run_actions').never()
  64. config = {'repositories': [{'path': 'foo'}]}
  65. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  66. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  67. assert results == expected_results
  68. def test_run_configuration_bails_for_monitor_start_soft_failure():
  69. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  70. flexmock(module).should_receive('get_skip_actions').and_return([])
  71. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  72. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  73. flexmock(module.dispatch).should_receive('call_hooks').and_raise(error)
  74. flexmock(module).should_receive('log_error_records').never()
  75. flexmock(module).should_receive('run_actions').never()
  76. config = {'repositories': [{'path': 'foo'}]}
  77. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  78. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  79. assert results == []
  80. def test_run_configuration_logs_actions_error():
  81. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  82. flexmock(module).should_receive('get_skip_actions').and_return([])
  83. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  84. flexmock(module.command).should_receive('execute_hook')
  85. flexmock(module.dispatch).should_receive('call_hooks')
  86. expected_results = [flexmock()]
  87. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  88. flexmock(module).should_receive('run_actions').and_raise(OSError)
  89. config = {'repositories': [{'path': 'foo'}]}
  90. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  91. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  92. assert results == expected_results
  93. def test_run_configuration_skips_remaining_actions_for_actions_soft_failure_but_still_pings_monitor():
  94. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  95. flexmock(module).should_receive('get_skip_actions').and_return([])
  96. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  97. flexmock(module.dispatch).should_receive('call_hooks').times(5)
  98. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  99. flexmock(module).should_receive('run_actions').and_raise(error)
  100. flexmock(module).should_receive('log_error_records').never()
  101. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  102. config = {'repositories': [{'path': 'foo'}]}
  103. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  104. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  105. assert results == []
  106. def test_run_configuration_logs_monitor_log_error():
  107. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  108. flexmock(module).should_receive('get_skip_actions').and_return([])
  109. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  110. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  111. None
  112. ).and_raise(OSError)
  113. expected_results = [flexmock()]
  114. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  115. flexmock(module).should_receive('run_actions').and_return([])
  116. config = {'repositories': [{'path': 'foo'}]}
  117. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  118. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  119. assert results == expected_results
  120. def test_run_configuration_still_pings_monitor_for_monitor_log_soft_failure():
  121. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  122. flexmock(module).should_receive('get_skip_actions').and_return([])
  123. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  124. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  125. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  126. None
  127. ).and_raise(error).and_return(None).and_return(None).times(5)
  128. flexmock(module).should_receive('log_error_records').never()
  129. flexmock(module).should_receive('run_actions').and_return([])
  130. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  131. config = {'repositories': [{'path': 'foo'}]}
  132. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  133. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  134. assert results == []
  135. def test_run_configuration_logs_monitor_finish_error():
  136. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  137. flexmock(module).should_receive('get_skip_actions').and_return([])
  138. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  139. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  140. None
  141. ).and_return(None).and_raise(OSError)
  142. expected_results = [flexmock()]
  143. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  144. flexmock(module).should_receive('run_actions').and_return([])
  145. config = {'repositories': [{'path': 'foo'}]}
  146. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  147. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  148. assert results == expected_results
  149. def test_run_configuration_bails_for_monitor_finish_soft_failure():
  150. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  151. flexmock(module).should_receive('get_skip_actions').and_return([])
  152. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  153. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  154. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  155. None
  156. ).and_raise(None).and_raise(error)
  157. flexmock(module).should_receive('log_error_records').never()
  158. flexmock(module).should_receive('run_actions').and_return([])
  159. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  160. config = {'repositories': [{'path': 'foo'}]}
  161. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  162. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  163. assert results == []
  164. def test_run_configuration_does_not_call_monitoring_hooks_if_monitoring_hooks_are_disabled():
  165. flexmock(module).should_receive('verbosity_to_log_level').and_return(module.DISABLED)
  166. flexmock(module).should_receive('get_skip_actions').and_return([])
  167. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  168. flexmock(module.dispatch).should_receive('call_hooks').never()
  169. flexmock(module).should_receive('run_actions').and_return([])
  170. config = {'repositories': [{'path': 'foo'}]}
  171. arguments = {'global': flexmock(monitoring_verbosity=-2, dry_run=False), 'create': flexmock()}
  172. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  173. assert results == []
  174. def test_run_configuration_logs_on_error_hook_error():
  175. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  176. flexmock(module).should_receive('get_skip_actions').and_return([])
  177. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  178. flexmock(module.command).should_receive('execute_hook').and_raise(OSError)
  179. expected_results = [flexmock(), flexmock()]
  180. flexmock(module).should_receive('log_error_records').and_return(
  181. expected_results[:1]
  182. ).and_return(expected_results[1:])
  183. flexmock(module).should_receive('run_actions').and_raise(OSError)
  184. config = {'repositories': [{'path': 'foo'}]}
  185. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  186. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  187. assert results == expected_results
  188. def test_run_configuration_bails_for_on_error_hook_soft_failure():
  189. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  190. flexmock(module).should_receive('get_skip_actions').and_return([])
  191. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  192. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  193. flexmock(module.command).should_receive('execute_hook').and_raise(error)
  194. expected_results = [flexmock()]
  195. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  196. flexmock(module).should_receive('run_actions').and_raise(OSError)
  197. config = {'repositories': [{'path': 'foo'}]}
  198. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  199. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  200. assert results == expected_results
  201. def test_run_configuration_retries_soft_error():
  202. # Run action first fails, second passes.
  203. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  204. flexmock(module).should_receive('get_skip_actions').and_return([])
  205. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  206. flexmock(module.command).should_receive('execute_hook')
  207. flexmock(module).should_receive('run_actions').and_raise(OSError).and_return([])
  208. flexmock(module).should_receive('log_error_records').and_return([flexmock()]).once()
  209. config = {'repositories': [{'path': 'foo'}], 'retries': 1}
  210. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  211. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  212. assert results == []
  213. def test_run_configuration_retries_hard_error():
  214. # Run action fails twice.
  215. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  216. flexmock(module).should_receive('get_skip_actions').and_return([])
  217. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  218. flexmock(module.command).should_receive('execute_hook')
  219. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  220. flexmock(module).should_receive('log_error_records').with_args(
  221. 'foo: Error running actions for repository',
  222. OSError,
  223. levelno=logging.WARNING,
  224. log_command_error_output=True,
  225. ).and_return([flexmock()])
  226. error_logs = [flexmock()]
  227. flexmock(module).should_receive('log_error_records').with_args(
  228. 'foo: Error running actions for repository',
  229. OSError,
  230. ).and_return(error_logs)
  231. config = {'repositories': [{'path': 'foo'}], 'retries': 1}
  232. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  233. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  234. assert results == error_logs
  235. def test_run_configuration_repos_ordered():
  236. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  237. flexmock(module).should_receive('get_skip_actions').and_return([])
  238. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  239. flexmock(module.command).should_receive('execute_hook')
  240. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  241. expected_results = [flexmock(), flexmock()]
  242. flexmock(module).should_receive('log_error_records').with_args(
  243. 'foo: Error running actions for repository', OSError
  244. ).and_return(expected_results[:1]).ordered()
  245. flexmock(module).should_receive('log_error_records').with_args(
  246. 'bar: Error running actions for repository', OSError
  247. ).and_return(expected_results[1:]).ordered()
  248. config = {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}
  249. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  250. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  251. assert results == expected_results
  252. def test_run_configuration_retries_round_robin():
  253. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  254. flexmock(module).should_receive('get_skip_actions').and_return([])
  255. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  256. flexmock(module.command).should_receive('execute_hook')
  257. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  258. flexmock(module).should_receive('log_error_records').with_args(
  259. 'foo: Error running actions for repository',
  260. OSError,
  261. levelno=logging.WARNING,
  262. log_command_error_output=True,
  263. ).and_return([flexmock()]).ordered()
  264. flexmock(module).should_receive('log_error_records').with_args(
  265. 'bar: Error running actions for repository',
  266. OSError,
  267. levelno=logging.WARNING,
  268. log_command_error_output=True,
  269. ).and_return([flexmock()]).ordered()
  270. foo_error_logs = [flexmock()]
  271. flexmock(module).should_receive('log_error_records').with_args(
  272. 'foo: Error running actions for repository', OSError
  273. ).and_return(foo_error_logs).ordered()
  274. bar_error_logs = [flexmock()]
  275. flexmock(module).should_receive('log_error_records').with_args(
  276. 'bar: Error running actions for repository', OSError
  277. ).and_return(bar_error_logs).ordered()
  278. config = {
  279. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  280. 'retries': 1,
  281. }
  282. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  283. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  284. assert results == foo_error_logs + bar_error_logs
  285. def test_run_configuration_retries_one_passes():
  286. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  287. flexmock(module).should_receive('get_skip_actions').and_return([])
  288. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  289. flexmock(module.command).should_receive('execute_hook')
  290. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  291. []
  292. ).and_raise(OSError).times(4)
  293. flexmock(module).should_receive('log_error_records').with_args(
  294. 'foo: Error running actions for repository',
  295. OSError,
  296. levelno=logging.WARNING,
  297. log_command_error_output=True,
  298. ).and_return([flexmock()]).ordered()
  299. flexmock(module).should_receive('log_error_records').with_args(
  300. 'bar: Error running actions for repository',
  301. OSError,
  302. levelno=logging.WARNING,
  303. log_command_error_output=True,
  304. ).and_return(flexmock()).ordered()
  305. error_logs = [flexmock()]
  306. flexmock(module).should_receive('log_error_records').with_args(
  307. 'bar: Error running actions for repository', OSError
  308. ).and_return(error_logs).ordered()
  309. config = {
  310. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  311. 'retries': 1,
  312. }
  313. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  314. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  315. assert results == error_logs
  316. def test_run_configuration_retry_wait():
  317. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  318. flexmock(module).should_receive('get_skip_actions').and_return([])
  319. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  320. flexmock(module.command).should_receive('execute_hook')
  321. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  322. flexmock(module).should_receive('log_error_records').with_args(
  323. 'foo: Error running actions for repository',
  324. OSError,
  325. levelno=logging.WARNING,
  326. log_command_error_output=True,
  327. ).and_return([flexmock()]).ordered()
  328. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  329. flexmock(module).should_receive('log_error_records').with_args(
  330. 'foo: Error running actions for repository',
  331. OSError,
  332. levelno=logging.WARNING,
  333. log_command_error_output=True,
  334. ).and_return([flexmock()]).ordered()
  335. flexmock(time).should_receive('sleep').with_args(20).and_return().ordered()
  336. flexmock(module).should_receive('log_error_records').with_args(
  337. 'foo: Error running actions for repository',
  338. OSError,
  339. levelno=logging.WARNING,
  340. log_command_error_output=True,
  341. ).and_return([flexmock()]).ordered()
  342. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  343. error_logs = [flexmock()]
  344. flexmock(module).should_receive('log_error_records').with_args(
  345. 'foo: Error running actions for repository', OSError
  346. ).and_return(error_logs).ordered()
  347. config = {
  348. 'repositories': [{'path': 'foo'}],
  349. 'retries': 3,
  350. 'retry_wait': 10,
  351. }
  352. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  353. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  354. assert results == error_logs
  355. def test_run_configuration_retries_timeout_multiple_repos():
  356. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  357. flexmock(module).should_receive('get_skip_actions').and_return([])
  358. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  359. flexmock(module.command).should_receive('execute_hook')
  360. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  361. []
  362. ).and_raise(OSError).times(4)
  363. flexmock(module).should_receive('log_error_records').with_args(
  364. 'foo: Error running actions for repository',
  365. OSError,
  366. levelno=logging.WARNING,
  367. log_command_error_output=True,
  368. ).and_return([flexmock()]).ordered()
  369. flexmock(module).should_receive('log_error_records').with_args(
  370. 'bar: Error running actions for repository',
  371. OSError,
  372. levelno=logging.WARNING,
  373. log_command_error_output=True,
  374. ).and_return([flexmock()]).ordered()
  375. # Sleep before retrying foo (and passing)
  376. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  377. # Sleep before retrying bar (and failing)
  378. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  379. error_logs = [flexmock()]
  380. flexmock(module).should_receive('log_error_records').with_args(
  381. 'bar: Error running actions for repository', OSError
  382. ).and_return(error_logs).ordered()
  383. config = {
  384. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  385. 'retries': 1,
  386. 'retry_wait': 10,
  387. }
  388. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  389. results = list(module.run_configuration('test.yaml', config, ['/tmp/test.yaml'], arguments))
  390. assert results == error_logs
  391. def test_run_actions_runs_repo_create():
  392. flexmock(module).should_receive('add_custom_log_levels')
  393. flexmock(module).should_receive('get_skip_actions').and_return([])
  394. flexmock(module.command).should_receive('execute_hook')
  395. flexmock(borgmatic.actions.repo_create).should_receive('run_repo_create').once()
  396. tuple(
  397. module.run_actions(
  398. arguments={
  399. 'global': flexmock(dry_run=False, log_file='foo'),
  400. 'repo-create': flexmock(),
  401. },
  402. config_filename=flexmock(),
  403. config={'repositories': []},
  404. config_paths=[],
  405. local_path=flexmock(),
  406. remote_path=flexmock(),
  407. local_borg_version=flexmock(),
  408. repository={'path': 'repo'},
  409. )
  410. )
  411. def test_run_actions_adds_label_file_to_hook_context():
  412. flexmock(module).should_receive('add_custom_log_levels')
  413. flexmock(module).should_receive('get_skip_actions').and_return([])
  414. flexmock(module.command).should_receive('execute_hook')
  415. expected = flexmock()
  416. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  417. config_filename=object,
  418. repository={'path': 'repo', 'label': 'my repo'},
  419. config={'repositories': []},
  420. config_paths=[],
  421. hook_context={
  422. 'repository_label': 'my repo',
  423. 'log_file': '',
  424. 'repositories': '',
  425. 'repository': 'repo',
  426. },
  427. local_borg_version=object,
  428. create_arguments=object,
  429. global_arguments=object,
  430. dry_run_label='',
  431. local_path=object,
  432. remote_path=object,
  433. ).once().and_return(expected)
  434. result = tuple(
  435. module.run_actions(
  436. arguments={'global': flexmock(dry_run=False, log_file=None), 'create': flexmock()},
  437. config_filename=flexmock(),
  438. config={'repositories': []},
  439. config_paths=[],
  440. local_path=flexmock(),
  441. remote_path=flexmock(),
  442. local_borg_version=flexmock(),
  443. repository={'path': 'repo', 'label': 'my repo'},
  444. )
  445. )
  446. assert result == (expected,)
  447. def test_run_actions_adds_log_file_to_hook_context():
  448. flexmock(module).should_receive('add_custom_log_levels')
  449. flexmock(module).should_receive('get_skip_actions').and_return([])
  450. flexmock(module.command).should_receive('execute_hook')
  451. expected = flexmock()
  452. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  453. config_filename=object,
  454. repository={'path': 'repo'},
  455. config={'repositories': []},
  456. config_paths=[],
  457. hook_context={
  458. 'repository_label': '',
  459. 'log_file': 'foo',
  460. 'repositories': '',
  461. 'repository': 'repo',
  462. },
  463. local_borg_version=object,
  464. create_arguments=object,
  465. global_arguments=object,
  466. dry_run_label='',
  467. local_path=object,
  468. remote_path=object,
  469. ).once().and_return(expected)
  470. result = tuple(
  471. module.run_actions(
  472. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  473. config_filename=flexmock(),
  474. config={'repositories': []},
  475. config_paths=[],
  476. local_path=flexmock(),
  477. remote_path=flexmock(),
  478. local_borg_version=flexmock(),
  479. repository={'path': 'repo'},
  480. )
  481. )
  482. assert result == (expected,)
  483. def test_run_actions_runs_transfer():
  484. flexmock(module).should_receive('add_custom_log_levels')
  485. flexmock(module).should_receive('get_skip_actions').and_return([])
  486. flexmock(module.command).should_receive('execute_hook')
  487. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  488. tuple(
  489. module.run_actions(
  490. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'transfer': flexmock()},
  491. config_filename=flexmock(),
  492. config={'repositories': []},
  493. config_paths=[],
  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_create():
  501. flexmock(module).should_receive('add_custom_log_levels')
  502. flexmock(module).should_receive('get_skip_actions').and_return([])
  503. flexmock(module.command).should_receive('execute_hook')
  504. expected = flexmock()
  505. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  506. result = tuple(
  507. module.run_actions(
  508. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  509. config_filename=flexmock(),
  510. config={'repositories': []},
  511. config_paths=[],
  512. local_path=flexmock(),
  513. remote_path=flexmock(),
  514. local_borg_version=flexmock(),
  515. repository={'path': 'repo'},
  516. )
  517. )
  518. assert result == (expected,)
  519. def test_run_actions_with_skip_actions_skips_create():
  520. flexmock(module).should_receive('add_custom_log_levels')
  521. flexmock(module).should_receive('get_skip_actions').and_return(['create'])
  522. flexmock(module.command).should_receive('execute_hook')
  523. flexmock(borgmatic.actions.create).should_receive('run_create').never()
  524. tuple(
  525. module.run_actions(
  526. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  527. config_filename=flexmock(),
  528. config={'repositories': [], 'skip_actions': ['create']},
  529. config_paths=[],
  530. local_path=flexmock(),
  531. remote_path=flexmock(),
  532. local_borg_version=flexmock(),
  533. repository={'path': 'repo'},
  534. )
  535. )
  536. def test_run_actions_runs_prune():
  537. flexmock(module).should_receive('add_custom_log_levels')
  538. flexmock(module).should_receive('get_skip_actions').and_return([])
  539. flexmock(module.command).should_receive('execute_hook')
  540. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  541. tuple(
  542. module.run_actions(
  543. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  544. config_filename=flexmock(),
  545. config={'repositories': []},
  546. config_paths=[],
  547. local_path=flexmock(),
  548. remote_path=flexmock(),
  549. local_borg_version=flexmock(),
  550. repository={'path': 'repo'},
  551. )
  552. )
  553. def test_run_actions_with_skip_actions_skips_prune():
  554. flexmock(module).should_receive('add_custom_log_levels')
  555. flexmock(module).should_receive('get_skip_actions').and_return(['prune'])
  556. flexmock(module.command).should_receive('execute_hook')
  557. flexmock(borgmatic.actions.prune).should_receive('run_prune').never()
  558. tuple(
  559. module.run_actions(
  560. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  561. config_filename=flexmock(),
  562. config={'repositories': [], 'skip_actions': ['prune']},
  563. config_paths=[],
  564. local_path=flexmock(),
  565. remote_path=flexmock(),
  566. local_borg_version=flexmock(),
  567. repository={'path': 'repo'},
  568. )
  569. )
  570. def test_run_actions_runs_compact():
  571. flexmock(module).should_receive('add_custom_log_levels')
  572. flexmock(module).should_receive('get_skip_actions').and_return([])
  573. flexmock(module.command).should_receive('execute_hook')
  574. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  575. tuple(
  576. module.run_actions(
  577. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  578. config_filename=flexmock(),
  579. config={'repositories': []},
  580. config_paths=[],
  581. local_path=flexmock(),
  582. remote_path=flexmock(),
  583. local_borg_version=flexmock(),
  584. repository={'path': 'repo'},
  585. )
  586. )
  587. def test_run_actions_with_skip_actions_skips_compact():
  588. flexmock(module).should_receive('add_custom_log_levels')
  589. flexmock(module).should_receive('get_skip_actions').and_return(['compact'])
  590. flexmock(module.command).should_receive('execute_hook')
  591. flexmock(borgmatic.actions.compact).should_receive('run_compact').never()
  592. tuple(
  593. module.run_actions(
  594. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  595. config_filename=flexmock(),
  596. config={'repositories': [], 'skip_actions': ['compact']},
  597. config_paths=[],
  598. local_path=flexmock(),
  599. remote_path=flexmock(),
  600. local_borg_version=flexmock(),
  601. repository={'path': 'repo'},
  602. )
  603. )
  604. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  605. flexmock(module).should_receive('add_custom_log_levels')
  606. flexmock(module).should_receive('get_skip_actions').and_return([])
  607. flexmock(module.command).should_receive('execute_hook')
  608. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  609. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  610. tuple(
  611. module.run_actions(
  612. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  613. config_filename=flexmock(),
  614. config={'repositories': []},
  615. config_paths=[],
  616. local_path=flexmock(),
  617. remote_path=flexmock(),
  618. local_borg_version=flexmock(),
  619. repository={'path': 'repo'},
  620. )
  621. )
  622. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  623. flexmock(module).should_receive('add_custom_log_levels')
  624. flexmock(module).should_receive('get_skip_actions').and_return([])
  625. flexmock(module.command).should_receive('execute_hook')
  626. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  627. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  628. tuple(
  629. module.run_actions(
  630. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  631. config_filename=flexmock(),
  632. config={'repositories': []},
  633. config_paths=[],
  634. local_path=flexmock(),
  635. remote_path=flexmock(),
  636. local_borg_version=flexmock(),
  637. repository={'path': 'repo'},
  638. )
  639. )
  640. def test_run_actions_with_skip_actions_skips_check():
  641. flexmock(module).should_receive('add_custom_log_levels')
  642. flexmock(module).should_receive('get_skip_actions').and_return(['check'])
  643. flexmock(module.command).should_receive('execute_hook')
  644. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  645. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  646. tuple(
  647. module.run_actions(
  648. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  649. config_filename=flexmock(),
  650. config={'repositories': [], 'skip_actions': ['check']},
  651. config_paths=[],
  652. local_path=flexmock(),
  653. remote_path=flexmock(),
  654. local_borg_version=flexmock(),
  655. repository={'path': 'repo'},
  656. )
  657. )
  658. def test_run_actions_runs_extract():
  659. flexmock(module).should_receive('add_custom_log_levels')
  660. flexmock(module).should_receive('get_skip_actions').and_return([])
  661. flexmock(module.command).should_receive('execute_hook')
  662. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  663. tuple(
  664. module.run_actions(
  665. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'extract': flexmock()},
  666. config_filename=flexmock(),
  667. config={'repositories': []},
  668. config_paths=[],
  669. local_path=flexmock(),
  670. remote_path=flexmock(),
  671. local_borg_version=flexmock(),
  672. repository={'path': 'repo'},
  673. )
  674. )
  675. def test_run_actions_runs_export_tar():
  676. flexmock(module).should_receive('add_custom_log_levels')
  677. flexmock(module).should_receive('get_skip_actions').and_return([])
  678. flexmock(module.command).should_receive('execute_hook')
  679. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  680. tuple(
  681. module.run_actions(
  682. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export-tar': flexmock()},
  683. config_filename=flexmock(),
  684. config={'repositories': []},
  685. config_paths=[],
  686. local_path=flexmock(),
  687. remote_path=flexmock(),
  688. local_borg_version=flexmock(),
  689. repository={'path': 'repo'},
  690. )
  691. )
  692. def test_run_actions_runs_mount():
  693. flexmock(module).should_receive('add_custom_log_levels')
  694. flexmock(module).should_receive('get_skip_actions').and_return([])
  695. flexmock(module.command).should_receive('execute_hook')
  696. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  697. tuple(
  698. module.run_actions(
  699. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'mount': flexmock()},
  700. config_filename=flexmock(),
  701. config={'repositories': []},
  702. config_paths=[],
  703. local_path=flexmock(),
  704. remote_path=flexmock(),
  705. local_borg_version=flexmock(),
  706. repository={'path': 'repo'},
  707. )
  708. )
  709. def test_run_actions_runs_restore():
  710. flexmock(module).should_receive('add_custom_log_levels')
  711. flexmock(module).should_receive('get_skip_actions').and_return([])
  712. flexmock(module.command).should_receive('execute_hook')
  713. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  714. tuple(
  715. module.run_actions(
  716. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'restore': flexmock()},
  717. config_filename=flexmock(),
  718. config={'repositories': []},
  719. config_paths=[],
  720. local_path=flexmock(),
  721. remote_path=flexmock(),
  722. local_borg_version=flexmock(),
  723. repository={'path': 'repo'},
  724. )
  725. )
  726. def test_run_actions_runs_repo_list():
  727. flexmock(module).should_receive('add_custom_log_levels')
  728. flexmock(module).should_receive('get_skip_actions').and_return([])
  729. flexmock(module.command).should_receive('execute_hook')
  730. expected = flexmock()
  731. flexmock(borgmatic.actions.repo_list).should_receive('run_repo_list').and_yield(expected).once()
  732. result = tuple(
  733. module.run_actions(
  734. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'repo-list': flexmock()},
  735. config_filename=flexmock(),
  736. config={'repositories': []},
  737. config_paths=[],
  738. local_path=flexmock(),
  739. remote_path=flexmock(),
  740. local_borg_version=flexmock(),
  741. repository={'path': 'repo'},
  742. )
  743. )
  744. assert result == (expected,)
  745. def test_run_actions_runs_list():
  746. flexmock(module).should_receive('add_custom_log_levels')
  747. flexmock(module).should_receive('get_skip_actions').and_return([])
  748. flexmock(module.command).should_receive('execute_hook')
  749. expected = flexmock()
  750. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  751. result = tuple(
  752. module.run_actions(
  753. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'list': flexmock()},
  754. config_filename=flexmock(),
  755. config={'repositories': []},
  756. config_paths=[],
  757. local_path=flexmock(),
  758. remote_path=flexmock(),
  759. local_borg_version=flexmock(),
  760. repository={'path': 'repo'},
  761. )
  762. )
  763. assert result == (expected,)
  764. def test_run_actions_runs_repo_info():
  765. flexmock(module).should_receive('add_custom_log_levels')
  766. flexmock(module).should_receive('get_skip_actions').and_return([])
  767. flexmock(module.command).should_receive('execute_hook')
  768. expected = flexmock()
  769. flexmock(borgmatic.actions.repo_info).should_receive('run_repo_info').and_yield(expected).once()
  770. result = tuple(
  771. module.run_actions(
  772. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'repo-info': flexmock()},
  773. config_filename=flexmock(),
  774. config={'repositories': []},
  775. config_paths=[],
  776. local_path=flexmock(),
  777. remote_path=flexmock(),
  778. local_borg_version=flexmock(),
  779. repository={'path': 'repo'},
  780. )
  781. )
  782. assert result == (expected,)
  783. def test_run_actions_runs_info():
  784. flexmock(module).should_receive('add_custom_log_levels')
  785. flexmock(module).should_receive('get_skip_actions').and_return([])
  786. flexmock(module.command).should_receive('execute_hook')
  787. expected = flexmock()
  788. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  789. result = tuple(
  790. module.run_actions(
  791. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'info': flexmock()},
  792. config_filename=flexmock(),
  793. config={'repositories': []},
  794. config_paths=[],
  795. local_path=flexmock(),
  796. remote_path=flexmock(),
  797. local_borg_version=flexmock(),
  798. repository={'path': 'repo'},
  799. )
  800. )
  801. assert result == (expected,)
  802. def test_run_actions_runs_break_lock():
  803. flexmock(module).should_receive('add_custom_log_levels')
  804. flexmock(module).should_receive('get_skip_actions').and_return([])
  805. flexmock(module.command).should_receive('execute_hook')
  806. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  807. tuple(
  808. module.run_actions(
  809. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'break-lock': flexmock()},
  810. config_filename=flexmock(),
  811. config={'repositories': []},
  812. config_paths=[],
  813. local_path=flexmock(),
  814. remote_path=flexmock(),
  815. local_borg_version=flexmock(),
  816. repository={'path': 'repo'},
  817. )
  818. )
  819. def test_run_actions_runs_export_key():
  820. flexmock(module).should_receive('add_custom_log_levels')
  821. flexmock(module).should_receive('get_skip_actions').and_return([])
  822. flexmock(module.command).should_receive('execute_hook')
  823. flexmock(borgmatic.actions.export_key).should_receive('run_export_key').once()
  824. tuple(
  825. module.run_actions(
  826. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export': flexmock()},
  827. config_filename=flexmock(),
  828. config={'repositories': []},
  829. config_paths=[],
  830. local_path=flexmock(),
  831. remote_path=flexmock(),
  832. local_borg_version=flexmock(),
  833. repository={'path': 'repo'},
  834. )
  835. )
  836. def test_run_actions_runs_change_passphrase():
  837. flexmock(module).should_receive('add_custom_log_levels')
  838. flexmock(module).should_receive('get_skip_actions').and_return([])
  839. flexmock(module.command).should_receive('execute_hook')
  840. flexmock(borgmatic.actions.change_passphrase).should_receive('run_change_passphrase').once()
  841. tuple(
  842. module.run_actions(
  843. arguments={
  844. 'global': flexmock(dry_run=False, log_file='foo'),
  845. 'change-passphrase': flexmock(),
  846. },
  847. config_filename=flexmock(),
  848. config={'repositories': []},
  849. config_paths=[],
  850. local_path=flexmock(),
  851. remote_path=flexmock(),
  852. local_borg_version=flexmock(),
  853. repository={'path': 'repo'},
  854. )
  855. )
  856. def test_run_actions_runs_delete():
  857. flexmock(module).should_receive('add_custom_log_levels')
  858. flexmock(module).should_receive('get_skip_actions').and_return([])
  859. flexmock(module.command).should_receive('execute_hook')
  860. flexmock(borgmatic.actions.delete).should_receive('run_delete').once()
  861. tuple(
  862. module.run_actions(
  863. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'delete': flexmock()},
  864. config_filename=flexmock(),
  865. config={'repositories': []},
  866. config_paths=[],
  867. local_path=flexmock(),
  868. remote_path=flexmock(),
  869. local_borg_version=flexmock(),
  870. repository={'path': 'repo'},
  871. )
  872. )
  873. def test_run_actions_runs_repo_delete():
  874. flexmock(module).should_receive('add_custom_log_levels')
  875. flexmock(module).should_receive('get_skip_actions').and_return([])
  876. flexmock(module.command).should_receive('execute_hook')
  877. flexmock(borgmatic.actions.repo_delete).should_receive('run_repo_delete').once()
  878. tuple(
  879. module.run_actions(
  880. arguments={
  881. 'global': flexmock(dry_run=False, log_file='foo'),
  882. 'repo-delete': flexmock(),
  883. },
  884. config_filename=flexmock(),
  885. config={'repositories': []},
  886. config_paths=[],
  887. local_path=flexmock(),
  888. remote_path=flexmock(),
  889. local_borg_version=flexmock(),
  890. repository={'path': 'repo'},
  891. )
  892. )
  893. def test_run_actions_runs_borg():
  894. flexmock(module).should_receive('add_custom_log_levels')
  895. flexmock(module).should_receive('get_skip_actions').and_return([])
  896. flexmock(module.command).should_receive('execute_hook')
  897. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  898. tuple(
  899. module.run_actions(
  900. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'borg': flexmock()},
  901. config_filename=flexmock(),
  902. config={'repositories': []},
  903. config_paths=[],
  904. local_path=flexmock(),
  905. remote_path=flexmock(),
  906. local_borg_version=flexmock(),
  907. repository={'path': 'repo'},
  908. )
  909. )
  910. def test_run_actions_runs_multiple_actions_in_argument_order():
  911. flexmock(module).should_receive('add_custom_log_levels')
  912. flexmock(module).should_receive('get_skip_actions').and_return([])
  913. flexmock(module.command).should_receive('execute_hook')
  914. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  915. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  916. tuple(
  917. module.run_actions(
  918. arguments={
  919. 'global': flexmock(dry_run=False, log_file='foo'),
  920. 'borg': flexmock(),
  921. 'restore': flexmock(),
  922. },
  923. config_filename=flexmock(),
  924. config={'repositories': []},
  925. config_paths=[],
  926. local_path=flexmock(),
  927. remote_path=flexmock(),
  928. local_borg_version=flexmock(),
  929. repository={'path': 'repo'},
  930. )
  931. )
  932. def test_load_configurations_collects_parsed_configurations_and_logs():
  933. configuration = flexmock()
  934. other_configuration = flexmock()
  935. test_expected_logs = [flexmock(), flexmock()]
  936. other_expected_logs = [flexmock(), flexmock()]
  937. flexmock(module.validate).should_receive('parse_configuration').and_return(
  938. configuration, ['/tmp/test.yaml'], test_expected_logs
  939. ).and_return(other_configuration, ['/tmp/other.yaml'], other_expected_logs)
  940. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  941. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  942. assert config_paths == ['/tmp/other.yaml', '/tmp/test.yaml']
  943. assert set(logs) >= set(test_expected_logs + other_expected_logs)
  944. def test_load_configurations_logs_warning_for_permission_error():
  945. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  946. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml',)))
  947. assert configs == {}
  948. assert config_paths == []
  949. assert max(log.levelno for log in logs) == logging.WARNING
  950. def test_load_configurations_logs_critical_for_parse_error():
  951. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  952. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml',)))
  953. assert configs == {}
  954. assert config_paths == []
  955. assert max(log.levelno for log in logs) == logging.CRITICAL
  956. def test_log_record_does_not_raise():
  957. module.log_record(levelno=1, foo='bar', baz='quux')
  958. def test_log_record_with_suppress_does_not_raise():
  959. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  960. def test_log_error_records_generates_output_logs_for_message_only():
  961. flexmock(module).should_receive('log_record').replace_with(dict).once()
  962. logs = tuple(module.log_error_records('Error'))
  963. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  964. def test_log_error_records_generates_output_logs_for_called_process_error_with_bytes_ouput():
  965. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  966. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  967. logs = tuple(
  968. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', b'error output'))
  969. )
  970. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  971. assert any(log for log in logs if 'error output' in str(log))
  972. def test_log_error_records_generates_output_logs_for_called_process_error_with_string_ouput():
  973. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  974. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  975. logs = tuple(
  976. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  977. )
  978. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  979. assert any(log for log in logs if 'error output' in str(log))
  980. def test_log_error_records_splits_called_process_error_with_multiline_ouput_into_multiple_logs():
  981. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  982. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  983. logs = tuple(
  984. module.log_error_records(
  985. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output\nanother line')
  986. )
  987. )
  988. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  989. assert any(log for log in logs if 'error output' in str(log))
  990. def test_log_error_records_generates_logs_for_value_error():
  991. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  992. logs = tuple(module.log_error_records('Error', ValueError()))
  993. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  994. def test_log_error_records_generates_logs_for_os_error():
  995. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  996. logs = tuple(module.log_error_records('Error', OSError()))
  997. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  998. def test_log_error_records_generates_nothing_for_other_error():
  999. flexmock(module).should_receive('log_record').never()
  1000. logs = tuple(module.log_error_records('Error', KeyError()))
  1001. assert logs == ()
  1002. def test_get_local_path_uses_configuration_value():
  1003. assert module.get_local_path({'test.yaml': {'local_path': 'borg1'}}) == 'borg1'
  1004. def test_get_local_path_without_local_path_defaults_to_borg():
  1005. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  1006. def test_collect_highlander_action_summary_logs_info_for_success_with_bootstrap():
  1007. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1008. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  1009. arguments = {
  1010. 'bootstrap': flexmock(repository='repo'),
  1011. 'global': flexmock(dry_run=False),
  1012. }
  1013. logs = tuple(
  1014. module.collect_highlander_action_summary_logs(
  1015. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1016. )
  1017. )
  1018. assert {log.levelno for log in logs} == {logging.ANSWER}
  1019. def test_collect_highlander_action_summary_logs_error_on_bootstrap_failure():
  1020. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  1021. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  1022. ValueError
  1023. )
  1024. arguments = {
  1025. 'bootstrap': flexmock(repository='repo'),
  1026. 'global': flexmock(dry_run=False),
  1027. }
  1028. logs = tuple(
  1029. module.collect_highlander_action_summary_logs(
  1030. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1031. )
  1032. )
  1033. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1034. def test_collect_highlander_action_summary_logs_error_on_bootstrap_local_borg_version_failure():
  1035. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  1036. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').never()
  1037. arguments = {
  1038. 'bootstrap': flexmock(repository='repo'),
  1039. 'global': flexmock(dry_run=False),
  1040. }
  1041. logs = tuple(
  1042. module.collect_highlander_action_summary_logs(
  1043. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1044. )
  1045. )
  1046. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1047. def test_collect_highlander_action_summary_logs_info_for_success_with_generate():
  1048. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate')
  1049. arguments = {
  1050. 'generate': flexmock(destination='test.yaml'),
  1051. 'global': flexmock(dry_run=False),
  1052. }
  1053. logs = tuple(
  1054. module.collect_highlander_action_summary_logs(
  1055. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1056. )
  1057. )
  1058. assert {log.levelno for log in logs} == {logging.ANSWER}
  1059. def test_collect_highlander_action_summary_logs_error_on_generate_failure():
  1060. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate').and_raise(
  1061. ValueError
  1062. )
  1063. arguments = {
  1064. 'generate': flexmock(destination='test.yaml'),
  1065. 'global': flexmock(dry_run=False),
  1066. }
  1067. logs = tuple(
  1068. module.collect_highlander_action_summary_logs(
  1069. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1070. )
  1071. )
  1072. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1073. def test_collect_highlander_action_summary_logs_info_for_success_with_validate():
  1074. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1075. arguments = {
  1076. 'validate': flexmock(),
  1077. 'global': flexmock(dry_run=False),
  1078. }
  1079. logs = tuple(
  1080. module.collect_highlander_action_summary_logs(
  1081. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1082. )
  1083. )
  1084. assert {log.levelno for log in logs} == {logging.ANSWER}
  1085. def test_collect_highlander_action_summary_logs_error_on_validate_parse_failure():
  1086. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1087. arguments = {
  1088. 'validate': flexmock(),
  1089. 'global': flexmock(dry_run=False),
  1090. }
  1091. logs = tuple(
  1092. module.collect_highlander_action_summary_logs(
  1093. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=True
  1094. )
  1095. )
  1096. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1097. def test_collect_highlander_action_summary_logs_error_on_run_validate_failure():
  1098. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate').and_raise(
  1099. ValueError
  1100. )
  1101. arguments = {
  1102. 'validate': flexmock(),
  1103. 'global': flexmock(dry_run=False),
  1104. }
  1105. logs = tuple(
  1106. module.collect_highlander_action_summary_logs(
  1107. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1108. )
  1109. )
  1110. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1111. def test_collect_configuration_run_summary_logs_info_for_success():
  1112. flexmock(module.command).should_receive('execute_hook').never()
  1113. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1114. flexmock(module).should_receive('run_configuration').and_return([])
  1115. arguments = {}
  1116. logs = tuple(
  1117. module.collect_configuration_run_summary_logs(
  1118. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1119. )
  1120. )
  1121. assert {log.levelno for log in logs} == {logging.INFO}
  1122. def test_collect_configuration_run_summary_executes_hooks_for_create():
  1123. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1124. flexmock(module).should_receive('run_configuration').and_return([])
  1125. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  1126. logs = tuple(
  1127. module.collect_configuration_run_summary_logs(
  1128. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1129. )
  1130. )
  1131. assert {log.levelno for log in logs} == {logging.INFO}
  1132. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  1133. flexmock(module.validate).should_receive('guard_single_repository_selected')
  1134. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1135. flexmock(module).should_receive('run_configuration').and_return([])
  1136. arguments = {'extract': flexmock(repository='repo')}
  1137. logs = tuple(
  1138. module.collect_configuration_run_summary_logs(
  1139. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1140. )
  1141. )
  1142. assert {log.levelno for log in logs} == {logging.INFO}
  1143. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  1144. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1145. ValueError
  1146. )
  1147. expected_logs = (flexmock(),)
  1148. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1149. arguments = {'extract': flexmock(repository='repo')}
  1150. logs = tuple(
  1151. module.collect_configuration_run_summary_logs(
  1152. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1153. )
  1154. )
  1155. assert logs == expected_logs
  1156. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  1157. flexmock(module.validate).should_receive('guard_single_repository_selected')
  1158. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1159. flexmock(module).should_receive('run_configuration').and_return([])
  1160. arguments = {'mount': flexmock(repository='repo')}
  1161. logs = tuple(
  1162. module.collect_configuration_run_summary_logs(
  1163. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1164. )
  1165. )
  1166. assert {log.levelno for log in logs} == {logging.INFO}
  1167. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  1168. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1169. ValueError
  1170. )
  1171. expected_logs = (flexmock(),)
  1172. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1173. arguments = {'mount': flexmock(repository='repo')}
  1174. logs = tuple(
  1175. module.collect_configuration_run_summary_logs(
  1176. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1177. )
  1178. )
  1179. assert logs == expected_logs
  1180. def test_collect_configuration_run_summary_logs_missing_configs_error():
  1181. arguments = {'global': flexmock(config_paths=[])}
  1182. expected_logs = (flexmock(),)
  1183. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1184. logs = tuple(
  1185. module.collect_configuration_run_summary_logs({}, config_paths=[], arguments=arguments)
  1186. )
  1187. assert logs == expected_logs
  1188. def test_collect_configuration_run_summary_logs_pre_hook_error():
  1189. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  1190. expected_logs = (flexmock(),)
  1191. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1192. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  1193. logs = tuple(
  1194. module.collect_configuration_run_summary_logs(
  1195. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1196. )
  1197. )
  1198. assert logs == expected_logs
  1199. def test_collect_configuration_run_summary_logs_post_hook_error():
  1200. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  1201. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1202. flexmock(module).should_receive('run_configuration').and_return([])
  1203. expected_logs = (flexmock(),)
  1204. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1205. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  1206. logs = tuple(
  1207. module.collect_configuration_run_summary_logs(
  1208. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1209. )
  1210. )
  1211. assert expected_logs[0] in logs
  1212. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  1213. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1214. ValueError
  1215. )
  1216. expected_logs = (flexmock(),)
  1217. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1218. arguments = {'list': flexmock(repository='repo', archive='test')}
  1219. logs = tuple(
  1220. module.collect_configuration_run_summary_logs(
  1221. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1222. )
  1223. )
  1224. assert logs == expected_logs
  1225. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  1226. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1227. flexmock(module).should_receive('run_configuration').and_return([])
  1228. arguments = {'list': flexmock(repository='repo', archive=None)}
  1229. logs = tuple(
  1230. module.collect_configuration_run_summary_logs(
  1231. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1232. )
  1233. )
  1234. assert {log.levelno for log in logs} == {logging.INFO}
  1235. def test_collect_configuration_run_summary_logs_run_configuration_error():
  1236. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1237. flexmock(module).should_receive('run_configuration').and_return(
  1238. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1239. )
  1240. flexmock(module).should_receive('log_error_records').and_return([])
  1241. arguments = {}
  1242. logs = tuple(
  1243. module.collect_configuration_run_summary_logs(
  1244. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1245. )
  1246. )
  1247. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1248. def test_collect_configuration_run_summary_logs_run_umount_error():
  1249. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1250. flexmock(module).should_receive('run_configuration').and_return([])
  1251. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  1252. flexmock(module).should_receive('log_error_records').and_return(
  1253. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1254. )
  1255. arguments = {'umount': flexmock(mount_point='/mnt')}
  1256. logs = tuple(
  1257. module.collect_configuration_run_summary_logs(
  1258. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1259. )
  1260. )
  1261. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  1262. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  1263. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1264. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  1265. ['baz']
  1266. )
  1267. stdout = flexmock()
  1268. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  1269. flexmock(module.sys).stdout = stdout
  1270. arguments = {}
  1271. tuple(
  1272. module.collect_configuration_run_summary_logs(
  1273. {'test.yaml': {}, 'test2.yaml': {}},
  1274. config_paths=['/tmp/test.yaml', '/tmp/test2.yaml'],
  1275. arguments=arguments,
  1276. )
  1277. )