test_borgmatic.py 64 KB

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