test_borgmatic.py 60 KB

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