test_borgmatic.py 57 KB

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