2
0

test_borgmatic.py 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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_rcreate():
  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.rcreate).should_receive('run_rcreate').once()
  396. tuple(
  397. module.run_actions(
  398. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rcreate': flexmock()},
  399. config_filename=flexmock(),
  400. config={'repositories': []},
  401. config_paths=[],
  402. local_path=flexmock(),
  403. remote_path=flexmock(),
  404. local_borg_version=flexmock(),
  405. repository={'path': 'repo'},
  406. )
  407. )
  408. def test_run_actions_adds_label_file_to_hook_context():
  409. flexmock(module).should_receive('add_custom_log_levels')
  410. flexmock(module).should_receive('get_skip_actions').and_return([])
  411. flexmock(module.command).should_receive('execute_hook')
  412. expected = flexmock()
  413. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  414. config_filename=object,
  415. repository={'path': 'repo', 'label': 'my repo'},
  416. config={'repositories': []},
  417. config_paths=[],
  418. hook_context={
  419. 'repository_label': 'my repo',
  420. 'log_file': '',
  421. 'repositories': '',
  422. 'repository': 'repo',
  423. },
  424. local_borg_version=object,
  425. create_arguments=object,
  426. global_arguments=object,
  427. dry_run_label='',
  428. local_path=object,
  429. remote_path=object,
  430. ).once().and_return(expected)
  431. result = tuple(
  432. module.run_actions(
  433. arguments={'global': flexmock(dry_run=False, log_file=None), 'create': flexmock()},
  434. config_filename=flexmock(),
  435. config={'repositories': []},
  436. config_paths=[],
  437. local_path=flexmock(),
  438. remote_path=flexmock(),
  439. local_borg_version=flexmock(),
  440. repository={'path': 'repo', 'label': 'my repo'},
  441. )
  442. )
  443. assert result == (expected,)
  444. def test_run_actions_adds_log_file_to_hook_context():
  445. flexmock(module).should_receive('add_custom_log_levels')
  446. flexmock(module).should_receive('get_skip_actions').and_return([])
  447. flexmock(module.command).should_receive('execute_hook')
  448. expected = flexmock()
  449. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  450. config_filename=object,
  451. repository={'path': 'repo'},
  452. config={'repositories': []},
  453. config_paths=[],
  454. hook_context={
  455. 'repository_label': '',
  456. 'log_file': 'foo',
  457. 'repositories': '',
  458. 'repository': 'repo',
  459. },
  460. local_borg_version=object,
  461. create_arguments=object,
  462. global_arguments=object,
  463. dry_run_label='',
  464. local_path=object,
  465. remote_path=object,
  466. ).once().and_return(expected)
  467. result = tuple(
  468. module.run_actions(
  469. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  470. config_filename=flexmock(),
  471. config={'repositories': []},
  472. config_paths=[],
  473. local_path=flexmock(),
  474. remote_path=flexmock(),
  475. local_borg_version=flexmock(),
  476. repository={'path': 'repo'},
  477. )
  478. )
  479. assert result == (expected,)
  480. def test_run_actions_runs_transfer():
  481. flexmock(module).should_receive('add_custom_log_levels')
  482. flexmock(module).should_receive('get_skip_actions').and_return([])
  483. flexmock(module.command).should_receive('execute_hook')
  484. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  485. tuple(
  486. module.run_actions(
  487. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'transfer': flexmock()},
  488. config_filename=flexmock(),
  489. config={'repositories': []},
  490. config_paths=[],
  491. local_path=flexmock(),
  492. remote_path=flexmock(),
  493. local_borg_version=flexmock(),
  494. repository={'path': 'repo'},
  495. )
  496. )
  497. def test_run_actions_runs_create():
  498. flexmock(module).should_receive('add_custom_log_levels')
  499. flexmock(module).should_receive('get_skip_actions').and_return([])
  500. flexmock(module.command).should_receive('execute_hook')
  501. expected = flexmock()
  502. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  503. result = tuple(
  504. module.run_actions(
  505. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  506. config_filename=flexmock(),
  507. config={'repositories': []},
  508. config_paths=[],
  509. local_path=flexmock(),
  510. remote_path=flexmock(),
  511. local_borg_version=flexmock(),
  512. repository={'path': 'repo'},
  513. )
  514. )
  515. assert result == (expected,)
  516. def test_run_actions_with_skip_actions_skips_create():
  517. flexmock(module).should_receive('add_custom_log_levels')
  518. flexmock(module).should_receive('get_skip_actions').and_return(['create'])
  519. flexmock(module.command).should_receive('execute_hook')
  520. flexmock(borgmatic.actions.create).should_receive('run_create').never()
  521. tuple(
  522. module.run_actions(
  523. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  524. config_filename=flexmock(),
  525. config={'repositories': [], 'skip_actions': ['create']},
  526. config_paths=[],
  527. local_path=flexmock(),
  528. remote_path=flexmock(),
  529. local_borg_version=flexmock(),
  530. repository={'path': 'repo'},
  531. )
  532. )
  533. def test_run_actions_runs_prune():
  534. flexmock(module).should_receive('add_custom_log_levels')
  535. flexmock(module).should_receive('get_skip_actions').and_return([])
  536. flexmock(module.command).should_receive('execute_hook')
  537. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  538. tuple(
  539. module.run_actions(
  540. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  541. config_filename=flexmock(),
  542. config={'repositories': []},
  543. config_paths=[],
  544. local_path=flexmock(),
  545. remote_path=flexmock(),
  546. local_borg_version=flexmock(),
  547. repository={'path': 'repo'},
  548. )
  549. )
  550. def test_run_actions_with_skip_actions_skips_prune():
  551. flexmock(module).should_receive('add_custom_log_levels')
  552. flexmock(module).should_receive('get_skip_actions').and_return(['prune'])
  553. flexmock(module.command).should_receive('execute_hook')
  554. flexmock(borgmatic.actions.prune).should_receive('run_prune').never()
  555. tuple(
  556. module.run_actions(
  557. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  558. config_filename=flexmock(),
  559. config={'repositories': [], 'skip_actions': ['prune']},
  560. config_paths=[],
  561. local_path=flexmock(),
  562. remote_path=flexmock(),
  563. local_borg_version=flexmock(),
  564. repository={'path': 'repo'},
  565. )
  566. )
  567. def test_run_actions_runs_compact():
  568. flexmock(module).should_receive('add_custom_log_levels')
  569. flexmock(module).should_receive('get_skip_actions').and_return([])
  570. flexmock(module.command).should_receive('execute_hook')
  571. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  572. tuple(
  573. module.run_actions(
  574. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  575. config_filename=flexmock(),
  576. config={'repositories': []},
  577. config_paths=[],
  578. local_path=flexmock(),
  579. remote_path=flexmock(),
  580. local_borg_version=flexmock(),
  581. repository={'path': 'repo'},
  582. )
  583. )
  584. def test_run_actions_with_skip_actions_skips_compact():
  585. flexmock(module).should_receive('add_custom_log_levels')
  586. flexmock(module).should_receive('get_skip_actions').and_return(['compact'])
  587. flexmock(module.command).should_receive('execute_hook')
  588. flexmock(borgmatic.actions.compact).should_receive('run_compact').never()
  589. tuple(
  590. module.run_actions(
  591. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  592. config_filename=flexmock(),
  593. config={'repositories': [], 'skip_actions': ['compact']},
  594. config_paths=[],
  595. local_path=flexmock(),
  596. remote_path=flexmock(),
  597. local_borg_version=flexmock(),
  598. repository={'path': 'repo'},
  599. )
  600. )
  601. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  602. flexmock(module).should_receive('add_custom_log_levels')
  603. flexmock(module).should_receive('get_skip_actions').and_return([])
  604. flexmock(module.command).should_receive('execute_hook')
  605. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  606. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  607. tuple(
  608. module.run_actions(
  609. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  610. config_filename=flexmock(),
  611. config={'repositories': []},
  612. config_paths=[],
  613. local_path=flexmock(),
  614. remote_path=flexmock(),
  615. local_borg_version=flexmock(),
  616. repository={'path': 'repo'},
  617. )
  618. )
  619. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  620. flexmock(module).should_receive('add_custom_log_levels')
  621. flexmock(module).should_receive('get_skip_actions').and_return([])
  622. flexmock(module.command).should_receive('execute_hook')
  623. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  624. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  625. tuple(
  626. module.run_actions(
  627. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  628. config_filename=flexmock(),
  629. config={'repositories': []},
  630. config_paths=[],
  631. local_path=flexmock(),
  632. remote_path=flexmock(),
  633. local_borg_version=flexmock(),
  634. repository={'path': 'repo'},
  635. )
  636. )
  637. def test_run_actions_with_skip_actions_skips_check():
  638. flexmock(module).should_receive('add_custom_log_levels')
  639. flexmock(module).should_receive('get_skip_actions').and_return(['check'])
  640. flexmock(module.command).should_receive('execute_hook')
  641. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  642. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  643. tuple(
  644. module.run_actions(
  645. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  646. config_filename=flexmock(),
  647. config={'repositories': [], 'skip_actions': ['check']},
  648. config_paths=[],
  649. local_path=flexmock(),
  650. remote_path=flexmock(),
  651. local_borg_version=flexmock(),
  652. repository={'path': 'repo'},
  653. )
  654. )
  655. def test_run_actions_runs_extract():
  656. flexmock(module).should_receive('add_custom_log_levels')
  657. flexmock(module).should_receive('get_skip_actions').and_return([])
  658. flexmock(module.command).should_receive('execute_hook')
  659. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  660. tuple(
  661. module.run_actions(
  662. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'extract': flexmock()},
  663. config_filename=flexmock(),
  664. config={'repositories': []},
  665. config_paths=[],
  666. local_path=flexmock(),
  667. remote_path=flexmock(),
  668. local_borg_version=flexmock(),
  669. repository={'path': 'repo'},
  670. )
  671. )
  672. def test_run_actions_runs_export_tar():
  673. flexmock(module).should_receive('add_custom_log_levels')
  674. flexmock(module).should_receive('get_skip_actions').and_return([])
  675. flexmock(module.command).should_receive('execute_hook')
  676. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  677. tuple(
  678. module.run_actions(
  679. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export-tar': flexmock()},
  680. config_filename=flexmock(),
  681. config={'repositories': []},
  682. config_paths=[],
  683. local_path=flexmock(),
  684. remote_path=flexmock(),
  685. local_borg_version=flexmock(),
  686. repository={'path': 'repo'},
  687. )
  688. )
  689. def test_run_actions_runs_mount():
  690. flexmock(module).should_receive('add_custom_log_levels')
  691. flexmock(module).should_receive('get_skip_actions').and_return([])
  692. flexmock(module.command).should_receive('execute_hook')
  693. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  694. tuple(
  695. module.run_actions(
  696. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'mount': flexmock()},
  697. config_filename=flexmock(),
  698. config={'repositories': []},
  699. config_paths=[],
  700. local_path=flexmock(),
  701. remote_path=flexmock(),
  702. local_borg_version=flexmock(),
  703. repository={'path': 'repo'},
  704. )
  705. )
  706. def test_run_actions_runs_restore():
  707. flexmock(module).should_receive('add_custom_log_levels')
  708. flexmock(module).should_receive('get_skip_actions').and_return([])
  709. flexmock(module.command).should_receive('execute_hook')
  710. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  711. tuple(
  712. module.run_actions(
  713. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'restore': flexmock()},
  714. config_filename=flexmock(),
  715. config={'repositories': []},
  716. config_paths=[],
  717. local_path=flexmock(),
  718. remote_path=flexmock(),
  719. local_borg_version=flexmock(),
  720. repository={'path': 'repo'},
  721. )
  722. )
  723. def test_run_actions_runs_rlist():
  724. flexmock(module).should_receive('add_custom_log_levels')
  725. flexmock(module).should_receive('get_skip_actions').and_return([])
  726. flexmock(module.command).should_receive('execute_hook')
  727. expected = flexmock()
  728. flexmock(borgmatic.actions.rlist).should_receive('run_rlist').and_yield(expected).once()
  729. result = tuple(
  730. module.run_actions(
  731. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rlist': flexmock()},
  732. config_filename=flexmock(),
  733. config={'repositories': []},
  734. config_paths=[],
  735. local_path=flexmock(),
  736. remote_path=flexmock(),
  737. local_borg_version=flexmock(),
  738. repository={'path': 'repo'},
  739. )
  740. )
  741. assert result == (expected,)
  742. def test_run_actions_runs_list():
  743. flexmock(module).should_receive('add_custom_log_levels')
  744. flexmock(module).should_receive('get_skip_actions').and_return([])
  745. flexmock(module.command).should_receive('execute_hook')
  746. expected = flexmock()
  747. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  748. result = tuple(
  749. module.run_actions(
  750. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'list': flexmock()},
  751. config_filename=flexmock(),
  752. config={'repositories': []},
  753. config_paths=[],
  754. local_path=flexmock(),
  755. remote_path=flexmock(),
  756. local_borg_version=flexmock(),
  757. repository={'path': 'repo'},
  758. )
  759. )
  760. assert result == (expected,)
  761. def test_run_actions_runs_rinfo():
  762. flexmock(module).should_receive('add_custom_log_levels')
  763. flexmock(module).should_receive('get_skip_actions').and_return([])
  764. flexmock(module.command).should_receive('execute_hook')
  765. expected = flexmock()
  766. flexmock(borgmatic.actions.rinfo).should_receive('run_rinfo').and_yield(expected).once()
  767. result = tuple(
  768. module.run_actions(
  769. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rinfo': flexmock()},
  770. config_filename=flexmock(),
  771. config={'repositories': []},
  772. config_paths=[],
  773. local_path=flexmock(),
  774. remote_path=flexmock(),
  775. local_borg_version=flexmock(),
  776. repository={'path': 'repo'},
  777. )
  778. )
  779. assert result == (expected,)
  780. def test_run_actions_runs_info():
  781. flexmock(module).should_receive('add_custom_log_levels')
  782. flexmock(module).should_receive('get_skip_actions').and_return([])
  783. flexmock(module.command).should_receive('execute_hook')
  784. expected = flexmock()
  785. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  786. result = tuple(
  787. module.run_actions(
  788. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'info': flexmock()},
  789. config_filename=flexmock(),
  790. config={'repositories': []},
  791. config_paths=[],
  792. local_path=flexmock(),
  793. remote_path=flexmock(),
  794. local_borg_version=flexmock(),
  795. repository={'path': 'repo'},
  796. )
  797. )
  798. assert result == (expected,)
  799. def test_run_actions_runs_break_lock():
  800. flexmock(module).should_receive('add_custom_log_levels')
  801. flexmock(module).should_receive('get_skip_actions').and_return([])
  802. flexmock(module.command).should_receive('execute_hook')
  803. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  804. tuple(
  805. module.run_actions(
  806. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'break-lock': flexmock()},
  807. config_filename=flexmock(),
  808. config={'repositories': []},
  809. config_paths=[],
  810. local_path=flexmock(),
  811. remote_path=flexmock(),
  812. local_borg_version=flexmock(),
  813. repository={'path': 'repo'},
  814. )
  815. )
  816. def test_run_actions_runs_export_key():
  817. flexmock(module).should_receive('add_custom_log_levels')
  818. flexmock(module).should_receive('get_skip_actions').and_return([])
  819. flexmock(module.command).should_receive('execute_hook')
  820. flexmock(borgmatic.actions.export_key).should_receive('run_export_key').once()
  821. tuple(
  822. module.run_actions(
  823. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export': flexmock()},
  824. config_filename=flexmock(),
  825. config={'repositories': []},
  826. config_paths=[],
  827. local_path=flexmock(),
  828. remote_path=flexmock(),
  829. local_borg_version=flexmock(),
  830. repository={'path': 'repo'},
  831. )
  832. )
  833. def test_run_actions_runs_borg():
  834. flexmock(module).should_receive('add_custom_log_levels')
  835. flexmock(module).should_receive('get_skip_actions').and_return([])
  836. flexmock(module.command).should_receive('execute_hook')
  837. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  838. tuple(
  839. module.run_actions(
  840. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'borg': flexmock()},
  841. config_filename=flexmock(),
  842. config={'repositories': []},
  843. config_paths=[],
  844. local_path=flexmock(),
  845. remote_path=flexmock(),
  846. local_borg_version=flexmock(),
  847. repository={'path': 'repo'},
  848. )
  849. )
  850. def test_run_actions_runs_multiple_actions_in_argument_order():
  851. flexmock(module).should_receive('add_custom_log_levels')
  852. flexmock(module).should_receive('get_skip_actions').and_return([])
  853. flexmock(module.command).should_receive('execute_hook')
  854. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  855. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  856. tuple(
  857. module.run_actions(
  858. arguments={
  859. 'global': flexmock(dry_run=False, log_file='foo'),
  860. 'borg': flexmock(),
  861. 'restore': flexmock(),
  862. },
  863. config_filename=flexmock(),
  864. config={'repositories': []},
  865. config_paths=[],
  866. local_path=flexmock(),
  867. remote_path=flexmock(),
  868. local_borg_version=flexmock(),
  869. repository={'path': 'repo'},
  870. )
  871. )
  872. def test_load_configurations_collects_parsed_configurations_and_logs():
  873. configuration = flexmock()
  874. other_configuration = flexmock()
  875. test_expected_logs = [flexmock(), flexmock()]
  876. other_expected_logs = [flexmock(), flexmock()]
  877. flexmock(module.validate).should_receive('parse_configuration').and_return(
  878. configuration, ['/tmp/test.yaml'], test_expected_logs
  879. ).and_return(other_configuration, ['/tmp/other.yaml'], other_expected_logs)
  880. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  881. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  882. assert config_paths == ['/tmp/other.yaml', '/tmp/test.yaml']
  883. assert set(logs) >= set(test_expected_logs + other_expected_logs)
  884. def test_load_configurations_logs_warning_for_permission_error():
  885. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  886. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml',)))
  887. assert configs == {}
  888. assert config_paths == []
  889. assert max(log.levelno for log in logs) == logging.WARNING
  890. def test_load_configurations_logs_critical_for_parse_error():
  891. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  892. configs, config_paths, logs = tuple(module.load_configurations(('test.yaml',)))
  893. assert configs == {}
  894. assert config_paths == []
  895. assert max(log.levelno for log in logs) == logging.CRITICAL
  896. def test_log_record_does_not_raise():
  897. module.log_record(levelno=1, foo='bar', baz='quux')
  898. def test_log_record_with_suppress_does_not_raise():
  899. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  900. def test_log_error_records_generates_output_logs_for_message_only():
  901. flexmock(module).should_receive('log_record').replace_with(dict).once()
  902. logs = tuple(module.log_error_records('Error'))
  903. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  904. def test_log_error_records_generates_output_logs_for_called_process_error_with_bytes_ouput():
  905. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  906. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  907. logs = tuple(
  908. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', b'error output'))
  909. )
  910. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  911. assert any(log for log in logs if 'error output' in str(log))
  912. def test_log_error_records_generates_output_logs_for_called_process_error_with_string_ouput():
  913. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  914. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  915. logs = tuple(
  916. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  917. )
  918. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  919. assert any(log for log in logs if 'error output' in str(log))
  920. def test_log_error_records_splits_called_process_error_with_multiline_ouput_into_multiple_logs():
  921. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  922. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  923. logs = tuple(
  924. module.log_error_records(
  925. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output\nanother line')
  926. )
  927. )
  928. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  929. assert any(log for log in logs if 'error output' in str(log))
  930. def test_log_error_records_generates_logs_for_value_error():
  931. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  932. logs = tuple(module.log_error_records('Error', ValueError()))
  933. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  934. def test_log_error_records_generates_logs_for_os_error():
  935. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  936. logs = tuple(module.log_error_records('Error', OSError()))
  937. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  938. def test_log_error_records_generates_nothing_for_other_error():
  939. flexmock(module).should_receive('log_record').never()
  940. logs = tuple(module.log_error_records('Error', KeyError()))
  941. assert logs == ()
  942. def test_get_local_path_uses_configuration_value():
  943. assert module.get_local_path({'test.yaml': {'local_path': 'borg1'}}) == 'borg1'
  944. def test_get_local_path_without_local_path_defaults_to_borg():
  945. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  946. def test_collect_highlander_action_summary_logs_info_for_success_with_bootstrap():
  947. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  948. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  949. arguments = {
  950. 'bootstrap': flexmock(repository='repo'),
  951. 'global': flexmock(dry_run=False),
  952. }
  953. logs = tuple(
  954. module.collect_highlander_action_summary_logs(
  955. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  956. )
  957. )
  958. assert {log.levelno for log in logs} == {logging.ANSWER}
  959. def test_collect_highlander_action_summary_logs_error_on_bootstrap_failure():
  960. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  961. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  962. ValueError
  963. )
  964. arguments = {
  965. 'bootstrap': flexmock(repository='repo'),
  966. 'global': flexmock(dry_run=False),
  967. }
  968. logs = tuple(
  969. module.collect_highlander_action_summary_logs(
  970. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  971. )
  972. )
  973. assert {log.levelno for log in logs} == {logging.CRITICAL}
  974. def test_collect_highlander_action_summary_logs_error_on_bootstrap_local_borg_version_failure():
  975. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  976. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').never()
  977. arguments = {
  978. 'bootstrap': flexmock(repository='repo'),
  979. 'global': flexmock(dry_run=False),
  980. }
  981. logs = tuple(
  982. module.collect_highlander_action_summary_logs(
  983. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  984. )
  985. )
  986. assert {log.levelno for log in logs} == {logging.CRITICAL}
  987. def test_collect_highlander_action_summary_logs_info_for_success_with_generate():
  988. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate')
  989. arguments = {
  990. 'generate': flexmock(destination='test.yaml'),
  991. 'global': flexmock(dry_run=False),
  992. }
  993. logs = tuple(
  994. module.collect_highlander_action_summary_logs(
  995. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  996. )
  997. )
  998. assert {log.levelno for log in logs} == {logging.ANSWER}
  999. def test_collect_highlander_action_summary_logs_error_on_generate_failure():
  1000. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate').and_raise(
  1001. ValueError
  1002. )
  1003. arguments = {
  1004. 'generate': flexmock(destination='test.yaml'),
  1005. 'global': flexmock(dry_run=False),
  1006. }
  1007. logs = tuple(
  1008. module.collect_highlander_action_summary_logs(
  1009. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1010. )
  1011. )
  1012. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1013. def test_collect_highlander_action_summary_logs_info_for_success_with_validate():
  1014. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1015. arguments = {
  1016. 'validate': flexmock(),
  1017. 'global': flexmock(dry_run=False),
  1018. }
  1019. logs = tuple(
  1020. module.collect_highlander_action_summary_logs(
  1021. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1022. )
  1023. )
  1024. assert {log.levelno for log in logs} == {logging.ANSWER}
  1025. def test_collect_highlander_action_summary_logs_error_on_validate_parse_failure():
  1026. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  1027. arguments = {
  1028. 'validate': flexmock(),
  1029. 'global': flexmock(dry_run=False),
  1030. }
  1031. logs = tuple(
  1032. module.collect_highlander_action_summary_logs(
  1033. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=True
  1034. )
  1035. )
  1036. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1037. def test_collect_highlander_action_summary_logs_error_on_run_validate_failure():
  1038. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate').and_raise(
  1039. ValueError
  1040. )
  1041. arguments = {
  1042. 'validate': flexmock(),
  1043. 'global': flexmock(dry_run=False),
  1044. }
  1045. logs = tuple(
  1046. module.collect_highlander_action_summary_logs(
  1047. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  1048. )
  1049. )
  1050. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1051. def test_collect_configuration_run_summary_logs_info_for_success():
  1052. flexmock(module.command).should_receive('execute_hook').never()
  1053. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1054. flexmock(module).should_receive('run_configuration').and_return([])
  1055. arguments = {}
  1056. logs = tuple(
  1057. module.collect_configuration_run_summary_logs(
  1058. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1059. )
  1060. )
  1061. assert {log.levelno for log in logs} == {logging.INFO}
  1062. def test_collect_configuration_run_summary_executes_hooks_for_create():
  1063. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1064. flexmock(module).should_receive('run_configuration').and_return([])
  1065. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  1066. logs = tuple(
  1067. module.collect_configuration_run_summary_logs(
  1068. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1069. )
  1070. )
  1071. assert {log.levelno for log in logs} == {logging.INFO}
  1072. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  1073. flexmock(module.validate).should_receive('guard_single_repository_selected')
  1074. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1075. flexmock(module).should_receive('run_configuration').and_return([])
  1076. arguments = {'extract': flexmock(repository='repo')}
  1077. logs = tuple(
  1078. module.collect_configuration_run_summary_logs(
  1079. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1080. )
  1081. )
  1082. assert {log.levelno for log in logs} == {logging.INFO}
  1083. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  1084. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1085. ValueError
  1086. )
  1087. expected_logs = (flexmock(),)
  1088. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1089. arguments = {'extract': flexmock(repository='repo')}
  1090. logs = tuple(
  1091. module.collect_configuration_run_summary_logs(
  1092. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1093. )
  1094. )
  1095. assert logs == expected_logs
  1096. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  1097. flexmock(module.validate).should_receive('guard_single_repository_selected')
  1098. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1099. flexmock(module).should_receive('run_configuration').and_return([])
  1100. arguments = {'mount': flexmock(repository='repo')}
  1101. logs = tuple(
  1102. module.collect_configuration_run_summary_logs(
  1103. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1104. )
  1105. )
  1106. assert {log.levelno for log in logs} == {logging.INFO}
  1107. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  1108. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1109. ValueError
  1110. )
  1111. expected_logs = (flexmock(),)
  1112. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1113. arguments = {'mount': flexmock(repository='repo')}
  1114. logs = tuple(
  1115. module.collect_configuration_run_summary_logs(
  1116. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1117. )
  1118. )
  1119. assert logs == expected_logs
  1120. def test_collect_configuration_run_summary_logs_missing_configs_error():
  1121. arguments = {'global': flexmock(config_paths=[])}
  1122. expected_logs = (flexmock(),)
  1123. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1124. logs = tuple(
  1125. module.collect_configuration_run_summary_logs({}, config_paths=[], arguments=arguments)
  1126. )
  1127. assert logs == expected_logs
  1128. def test_collect_configuration_run_summary_logs_pre_hook_error():
  1129. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  1130. expected_logs = (flexmock(),)
  1131. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1132. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  1133. logs = tuple(
  1134. module.collect_configuration_run_summary_logs(
  1135. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1136. )
  1137. )
  1138. assert logs == expected_logs
  1139. def test_collect_configuration_run_summary_logs_post_hook_error():
  1140. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  1141. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1142. flexmock(module).should_receive('run_configuration').and_return([])
  1143. expected_logs = (flexmock(),)
  1144. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1145. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  1146. logs = tuple(
  1147. module.collect_configuration_run_summary_logs(
  1148. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1149. )
  1150. )
  1151. assert expected_logs[0] in logs
  1152. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  1153. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  1154. ValueError
  1155. )
  1156. expected_logs = (flexmock(),)
  1157. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  1158. arguments = {'list': flexmock(repository='repo', archive='test')}
  1159. logs = tuple(
  1160. module.collect_configuration_run_summary_logs(
  1161. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1162. )
  1163. )
  1164. assert logs == expected_logs
  1165. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  1166. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1167. flexmock(module).should_receive('run_configuration').and_return([])
  1168. arguments = {'list': flexmock(repository='repo', archive=None)}
  1169. logs = tuple(
  1170. module.collect_configuration_run_summary_logs(
  1171. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1172. )
  1173. )
  1174. assert {log.levelno for log in logs} == {logging.INFO}
  1175. def test_collect_configuration_run_summary_logs_run_configuration_error():
  1176. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1177. flexmock(module).should_receive('run_configuration').and_return(
  1178. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1179. )
  1180. flexmock(module).should_receive('log_error_records').and_return([])
  1181. arguments = {}
  1182. logs = tuple(
  1183. module.collect_configuration_run_summary_logs(
  1184. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1185. )
  1186. )
  1187. assert {log.levelno for log in logs} == {logging.CRITICAL}
  1188. def test_collect_configuration_run_summary_logs_run_umount_error():
  1189. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1190. flexmock(module).should_receive('run_configuration').and_return([])
  1191. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  1192. flexmock(module).should_receive('log_error_records').and_return(
  1193. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  1194. )
  1195. arguments = {'umount': flexmock(mount_point='/mnt')}
  1196. logs = tuple(
  1197. module.collect_configuration_run_summary_logs(
  1198. {'test.yaml': {}}, config_paths=['/tmp/test.yaml'], arguments=arguments
  1199. )
  1200. )
  1201. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  1202. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  1203. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  1204. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  1205. ['baz']
  1206. )
  1207. stdout = flexmock()
  1208. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  1209. flexmock(module.sys).stdout = stdout
  1210. arguments = {}
  1211. tuple(
  1212. module.collect_configuration_run_summary_logs(
  1213. {'test.yaml': {}, 'test2.yaml': {}},
  1214. config_paths=['/tmp/test.yaml', '/tmp/test2.yaml'],
  1215. arguments=arguments,
  1216. )
  1217. )