test_borgmatic.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. import logging
  2. import subprocess
  3. import time
  4. from flexmock import flexmock
  5. import borgmatic.hooks.command
  6. from borgmatic.commands import borgmatic as module
  7. def test_run_configuration_runs_actions_for_each_repository():
  8. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  9. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  10. expected_results = [flexmock(), flexmock()]
  11. flexmock(module).should_receive('run_actions').and_return(expected_results[:1]).and_return(
  12. expected_results[1:]
  13. )
  14. config = {'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}}
  15. arguments = {'global': flexmock(monitoring_verbosity=1)}
  16. results = list(module.run_configuration('test.yaml', config, arguments))
  17. assert results == expected_results
  18. def test_run_configuration_with_invalid_borg_version_errors():
  19. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  20. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  21. flexmock(module.command).should_receive('execute_hook').never()
  22. flexmock(module.dispatch).should_receive('call_hooks').never()
  23. flexmock(module).should_receive('run_actions').never()
  24. config = {'location': {'repositories': ['foo']}}
  25. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'prune': flexmock()}
  26. list(module.run_configuration('test.yaml', config, arguments))
  27. def test_run_configuration_logs_monitor_start_error():
  28. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  29. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  30. flexmock(module.dispatch).should_receive('call_hooks').and_raise(OSError).and_return(
  31. None
  32. ).and_return(None).and_return(None)
  33. expected_results = [flexmock()]
  34. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  35. flexmock(module).should_receive('run_actions').never()
  36. config = {'location': {'repositories': ['foo']}}
  37. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  38. results = list(module.run_configuration('test.yaml', config, arguments))
  39. assert results == expected_results
  40. def test_run_configuration_bails_for_monitor_start_soft_failure():
  41. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  42. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  43. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  44. flexmock(module.dispatch).should_receive('call_hooks').and_raise(error)
  45. flexmock(module).should_receive('log_error_records').never()
  46. flexmock(module).should_receive('run_actions').never()
  47. config = {'location': {'repositories': ['foo']}}
  48. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  49. results = list(module.run_configuration('test.yaml', config, arguments))
  50. assert results == []
  51. def test_run_configuration_logs_actions_error():
  52. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  53. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  54. flexmock(module.command).should_receive('execute_hook')
  55. flexmock(module.dispatch).should_receive('call_hooks')
  56. expected_results = [flexmock()]
  57. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  58. flexmock(module).should_receive('run_actions').and_raise(OSError)
  59. config = {'location': {'repositories': [{'path': 'foo'}]}}
  60. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  61. results = list(module.run_configuration('test.yaml', config, arguments))
  62. assert results == expected_results
  63. def test_run_configuration_bails_for_actions_soft_failure():
  64. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  65. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  66. flexmock(module.dispatch).should_receive('call_hooks')
  67. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  68. flexmock(module).should_receive('run_actions').and_raise(error)
  69. flexmock(module).should_receive('log_error_records').never()
  70. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  71. config = {'location': {'repositories': [{'path': 'foo'}]}}
  72. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  73. results = list(module.run_configuration('test.yaml', config, arguments))
  74. assert results == []
  75. def test_run_configuration_logs_monitor_log_error():
  76. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  77. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  78. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  79. None
  80. ).and_raise(OSError)
  81. expected_results = [flexmock()]
  82. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  83. flexmock(module).should_receive('run_actions').and_return([])
  84. config = {'location': {'repositories': [{'path': 'foo'}]}}
  85. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  86. results = list(module.run_configuration('test.yaml', config, arguments))
  87. assert results == expected_results
  88. def test_run_configuration_bails_for_monitor_log_soft_failure():
  89. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  90. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  91. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  92. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  93. None
  94. ).and_raise(error)
  95. flexmock(module).should_receive('log_error_records').never()
  96. flexmock(module).should_receive('run_actions').and_return([])
  97. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  98. config = {'location': {'repositories': [{'path': 'foo'}]}}
  99. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  100. results = list(module.run_configuration('test.yaml', config, arguments))
  101. assert results == []
  102. def test_run_configuration_logs_monitor_finish_error():
  103. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  104. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  105. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  106. None
  107. ).and_return(None).and_raise(OSError)
  108. expected_results = [flexmock()]
  109. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  110. flexmock(module).should_receive('run_actions').and_return([])
  111. config = {'location': {'repositories': [{'path': 'foo'}]}}
  112. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  113. results = list(module.run_configuration('test.yaml', config, arguments))
  114. assert results == expected_results
  115. def test_run_configuration_bails_for_monitor_finish_soft_failure():
  116. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  117. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  118. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  119. flexmock(module.dispatch).should_receive('call_hooks').and_return(None).and_return(
  120. None
  121. ).and_raise(None).and_raise(error)
  122. flexmock(module).should_receive('log_error_records').never()
  123. flexmock(module).should_receive('run_actions').and_return([])
  124. flexmock(module.command).should_receive('considered_soft_failure').and_return(True)
  125. config = {'location': {'repositories': [{'path': 'foo'}]}}
  126. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  127. results = list(module.run_configuration('test.yaml', config, arguments))
  128. assert results == []
  129. def test_run_configuration_does_not_call_monitoring_hooks_if_monitoring_hooks_are_disabled():
  130. flexmock(module).should_receive('verbosity_to_log_level').and_return(module.DISABLED)
  131. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  132. flexmock(module.dispatch).should_receive('call_hooks').never()
  133. flexmock(module).should_receive('run_actions').and_return([])
  134. config = {'location': {'repositories': [{'path': 'foo'}]}}
  135. arguments = {'global': flexmock(monitoring_verbosity=-2, dry_run=False), 'create': flexmock()}
  136. results = list(module.run_configuration('test.yaml', config, arguments))
  137. assert results == []
  138. def test_run_configuration_logs_on_error_hook_error():
  139. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  140. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  141. flexmock(module.command).should_receive('execute_hook').and_raise(OSError)
  142. expected_results = [flexmock(), flexmock()]
  143. flexmock(module).should_receive('log_error_records').and_return(
  144. expected_results[:1]
  145. ).and_return(expected_results[1:])
  146. flexmock(module).should_receive('run_actions').and_raise(OSError)
  147. config = {'location': {'repositories': [{'path': 'foo'}]}}
  148. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  149. results = list(module.run_configuration('test.yaml', config, arguments))
  150. assert results == expected_results
  151. def test_run_configuration_bails_for_on_error_hook_soft_failure():
  152. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  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.command).should_receive('execute_hook').and_raise(error)
  156. expected_results = [flexmock()]
  157. flexmock(module).should_receive('log_error_records').and_return(expected_results)
  158. flexmock(module).should_receive('run_actions').and_raise(OSError)
  159. config = {'location': {'repositories': [{'path': 'foo'}]}}
  160. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  161. results = list(module.run_configuration('test.yaml', config, arguments))
  162. assert results == expected_results
  163. def test_run_configuration_retries_soft_error():
  164. # Run action first fails, second passes
  165. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  166. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  167. flexmock(module.command).should_receive('execute_hook')
  168. flexmock(module).should_receive('run_actions').and_raise(OSError).and_return([])
  169. flexmock(module).should_receive('log_error_records').and_return([flexmock()]).once()
  170. config = {'location': {'repositories': [{'path': 'foo'}]}, 'storage': {'retries': 1}}
  171. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  172. results = list(module.run_configuration('test.yaml', config, arguments))
  173. assert results == []
  174. def test_run_configuration_retries_hard_error():
  175. # Run action fails twice
  176. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  177. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  178. flexmock(module.command).should_receive('execute_hook')
  179. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  180. flexmock(module).should_receive('log_error_records').with_args(
  181. 'foo: Error running actions for repository',
  182. OSError,
  183. levelno=logging.WARNING,
  184. log_command_error_output=True,
  185. ).and_return([flexmock()])
  186. error_logs = [flexmock()]
  187. flexmock(module).should_receive('log_error_records').with_args(
  188. 'foo: Error running actions for repository', OSError,
  189. ).and_return(error_logs)
  190. config = {'location': {'repositories': [{'path': 'foo'}]}, 'storage': {'retries': 1}}
  191. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  192. results = list(module.run_configuration('test.yaml', config, arguments))
  193. assert results == error_logs
  194. def test_run_configuration_repos_ordered():
  195. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  196. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  197. flexmock(module.command).should_receive('execute_hook')
  198. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  199. expected_results = [flexmock(), flexmock()]
  200. flexmock(module).should_receive('log_error_records').with_args(
  201. 'foo: Error running actions for repository', OSError
  202. ).and_return(expected_results[:1]).ordered()
  203. flexmock(module).should_receive('log_error_records').with_args(
  204. 'bar: Error running actions for repository', OSError
  205. ).and_return(expected_results[1:]).ordered()
  206. config = {'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]}}
  207. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  208. results = list(module.run_configuration('test.yaml', config, arguments))
  209. assert results == expected_results
  210. def test_run_configuration_retries_round_robin():
  211. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  212. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  213. flexmock(module.command).should_receive('execute_hook')
  214. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  215. flexmock(module).should_receive('log_error_records').with_args(
  216. 'foo: Error running actions for repository',
  217. OSError,
  218. levelno=logging.WARNING,
  219. log_command_error_output=True,
  220. ).and_return([flexmock()]).ordered()
  221. flexmock(module).should_receive('log_error_records').with_args(
  222. 'bar: Error running actions for repository',
  223. OSError,
  224. levelno=logging.WARNING,
  225. log_command_error_output=True,
  226. ).and_return([flexmock()]).ordered()
  227. foo_error_logs = [flexmock()]
  228. flexmock(module).should_receive('log_error_records').with_args(
  229. 'foo: Error running actions for repository', OSError
  230. ).and_return(foo_error_logs).ordered()
  231. bar_error_logs = [flexmock()]
  232. flexmock(module).should_receive('log_error_records').with_args(
  233. 'bar: Error running actions for repository', OSError
  234. ).and_return(bar_error_logs).ordered()
  235. config = {
  236. 'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]},
  237. 'storage': {'retries': 1},
  238. }
  239. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  240. results = list(module.run_configuration('test.yaml', config, arguments))
  241. assert results == foo_error_logs + bar_error_logs
  242. def test_run_configuration_retries_one_passes():
  243. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  244. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  245. flexmock(module.command).should_receive('execute_hook')
  246. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  247. []
  248. ).and_raise(OSError).times(4)
  249. flexmock(module).should_receive('log_error_records').with_args(
  250. 'foo: Error running actions for repository',
  251. OSError,
  252. levelno=logging.WARNING,
  253. log_command_error_output=True,
  254. ).and_return([flexmock()]).ordered()
  255. flexmock(module).should_receive('log_error_records').with_args(
  256. 'bar: Error running actions for repository',
  257. OSError,
  258. levelno=logging.WARNING,
  259. log_command_error_output=True,
  260. ).and_return(flexmock()).ordered()
  261. error_logs = [flexmock()]
  262. flexmock(module).should_receive('log_error_records').with_args(
  263. 'bar: Error running actions for repository', OSError
  264. ).and_return(error_logs).ordered()
  265. config = {
  266. 'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]},
  267. 'storage': {'retries': 1},
  268. }
  269. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  270. results = list(module.run_configuration('test.yaml', config, arguments))
  271. assert results == error_logs
  272. def test_run_configuration_retry_wait():
  273. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  274. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  275. flexmock(module.command).should_receive('execute_hook')
  276. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  277. flexmock(module).should_receive('log_error_records').with_args(
  278. 'foo: Error running actions for repository',
  279. OSError,
  280. levelno=logging.WARNING,
  281. log_command_error_output=True,
  282. ).and_return([flexmock()]).ordered()
  283. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  284. flexmock(module).should_receive('log_error_records').with_args(
  285. 'foo: Error running actions for repository',
  286. OSError,
  287. levelno=logging.WARNING,
  288. log_command_error_output=True,
  289. ).and_return([flexmock()]).ordered()
  290. flexmock(time).should_receive('sleep').with_args(20).and_return().ordered()
  291. flexmock(module).should_receive('log_error_records').with_args(
  292. 'foo: Error running actions for repository',
  293. OSError,
  294. levelno=logging.WARNING,
  295. log_command_error_output=True,
  296. ).and_return([flexmock()]).ordered()
  297. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  298. error_logs = [flexmock()]
  299. flexmock(module).should_receive('log_error_records').with_args(
  300. 'foo: Error running actions for repository', OSError
  301. ).and_return(error_logs).ordered()
  302. config = {
  303. 'location': {'repositories': [{'path': 'foo'}]},
  304. 'storage': {'retries': 3, 'retry_wait': 10},
  305. }
  306. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  307. results = list(module.run_configuration('test.yaml', config, arguments))
  308. assert results == error_logs
  309. def test_run_configuration_retries_timeout_multiple_repos():
  310. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  311. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  312. flexmock(module.command).should_receive('execute_hook')
  313. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  314. []
  315. ).and_raise(OSError).times(4)
  316. flexmock(module).should_receive('log_error_records').with_args(
  317. 'foo: Error running actions for repository',
  318. OSError,
  319. levelno=logging.WARNING,
  320. log_command_error_output=True,
  321. ).and_return([flexmock()]).ordered()
  322. flexmock(module).should_receive('log_error_records').with_args(
  323. 'bar: Error running actions for repository',
  324. OSError,
  325. levelno=logging.WARNING,
  326. log_command_error_output=True,
  327. ).and_return([flexmock()]).ordered()
  328. # Sleep before retrying foo (and passing)
  329. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  330. # Sleep before retrying bar (and failing)
  331. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  332. error_logs = [flexmock()]
  333. flexmock(module).should_receive('log_error_records').with_args(
  334. 'bar: Error running actions for repository', OSError
  335. ).and_return(error_logs).ordered()
  336. config = {
  337. 'location': {'repositories': [{'path': 'foo'}, {'path': 'bar'}]},
  338. 'storage': {'retries': 1, 'retry_wait': 10},
  339. }
  340. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  341. results = list(module.run_configuration('test.yaml', config, arguments))
  342. assert results == error_logs
  343. def test_run_actions_runs_rcreate():
  344. flexmock(module).should_receive('add_custom_log_levels')
  345. flexmock(module.command).should_receive('execute_hook')
  346. flexmock(borgmatic.actions.rcreate).should_receive('run_rcreate').once()
  347. tuple(
  348. module.run_actions(
  349. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rcreate': flexmock()},
  350. config_filename=flexmock(),
  351. location={'repositories': []},
  352. storage=flexmock(),
  353. retention=flexmock(),
  354. consistency=flexmock(),
  355. hooks={},
  356. local_path=flexmock(),
  357. remote_path=flexmock(),
  358. local_borg_version=flexmock(),
  359. repository={'path': 'repo'},
  360. )
  361. )
  362. def test_run_actions_adds_log_file_to_hook_context():
  363. flexmock(module).should_receive('add_custom_log_levels')
  364. flexmock(module.command).should_receive('execute_hook')
  365. expected = flexmock()
  366. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  367. config_filename=object,
  368. repository={'path': 'repo'},
  369. location={'repositories': []},
  370. storage=object,
  371. hooks={},
  372. hook_context={'repository': 'repo', 'repositories': '', 'log_file': 'foo'},
  373. local_borg_version=object,
  374. create_arguments=object,
  375. global_arguments=object,
  376. dry_run_label='',
  377. local_path=object,
  378. remote_path=object,
  379. ).once().and_return(expected)
  380. result = tuple(
  381. module.run_actions(
  382. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  383. config_filename=flexmock(),
  384. location={'repositories': []},
  385. storage=flexmock(),
  386. retention=flexmock(),
  387. consistency=flexmock(),
  388. hooks={},
  389. local_path=flexmock(),
  390. remote_path=flexmock(),
  391. local_borg_version=flexmock(),
  392. repository={'path': 'repo'},
  393. )
  394. )
  395. assert result == (expected,)
  396. def test_run_actions_runs_transfer():
  397. flexmock(module).should_receive('add_custom_log_levels')
  398. flexmock(module.command).should_receive('execute_hook')
  399. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  400. tuple(
  401. module.run_actions(
  402. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'transfer': flexmock()},
  403. config_filename=flexmock(),
  404. location={'repositories': []},
  405. storage=flexmock(),
  406. retention=flexmock(),
  407. consistency=flexmock(),
  408. hooks={},
  409. local_path=flexmock(),
  410. remote_path=flexmock(),
  411. local_borg_version=flexmock(),
  412. repository={'path': 'repo'},
  413. )
  414. )
  415. def test_run_actions_runs_create():
  416. flexmock(module).should_receive('add_custom_log_levels')
  417. flexmock(module.command).should_receive('execute_hook')
  418. expected = flexmock()
  419. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  420. result = tuple(
  421. module.run_actions(
  422. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  423. config_filename=flexmock(),
  424. location={'repositories': []},
  425. storage=flexmock(),
  426. retention=flexmock(),
  427. consistency=flexmock(),
  428. hooks={},
  429. local_path=flexmock(),
  430. remote_path=flexmock(),
  431. local_borg_version=flexmock(),
  432. repository={'path': 'repo'},
  433. )
  434. )
  435. assert result == (expected,)
  436. def test_run_actions_runs_prune():
  437. flexmock(module).should_receive('add_custom_log_levels')
  438. flexmock(module.command).should_receive('execute_hook')
  439. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  440. tuple(
  441. module.run_actions(
  442. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  443. config_filename=flexmock(),
  444. location={'repositories': []},
  445. storage=flexmock(),
  446. retention=flexmock(),
  447. consistency=flexmock(),
  448. hooks={},
  449. local_path=flexmock(),
  450. remote_path=flexmock(),
  451. local_borg_version=flexmock(),
  452. repository={'path': 'repo'},
  453. )
  454. )
  455. def test_run_actions_runs_compact():
  456. flexmock(module).should_receive('add_custom_log_levels')
  457. flexmock(module.command).should_receive('execute_hook')
  458. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  459. tuple(
  460. module.run_actions(
  461. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  462. config_filename=flexmock(),
  463. location={'repositories': []},
  464. storage=flexmock(),
  465. retention=flexmock(),
  466. consistency=flexmock(),
  467. hooks={},
  468. local_path=flexmock(),
  469. remote_path=flexmock(),
  470. local_borg_version=flexmock(),
  471. repository={'path': 'repo'},
  472. )
  473. )
  474. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  475. flexmock(module).should_receive('add_custom_log_levels')
  476. flexmock(module.command).should_receive('execute_hook')
  477. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  478. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  479. tuple(
  480. module.run_actions(
  481. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  482. config_filename=flexmock(),
  483. location={'repositories': []},
  484. storage=flexmock(),
  485. retention=flexmock(),
  486. consistency=flexmock(),
  487. hooks={},
  488. local_path=flexmock(),
  489. remote_path=flexmock(),
  490. local_borg_version=flexmock(),
  491. repository={'path': 'repo'},
  492. )
  493. )
  494. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  495. flexmock(module).should_receive('add_custom_log_levels')
  496. flexmock(module.command).should_receive('execute_hook')
  497. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  498. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  499. tuple(
  500. module.run_actions(
  501. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  502. config_filename=flexmock(),
  503. location={'repositories': []},
  504. storage=flexmock(),
  505. retention=flexmock(),
  506. consistency=flexmock(),
  507. hooks={},
  508. local_path=flexmock(),
  509. remote_path=flexmock(),
  510. local_borg_version=flexmock(),
  511. repository={'path': 'repo'},
  512. )
  513. )
  514. def test_run_actions_runs_extract():
  515. flexmock(module).should_receive('add_custom_log_levels')
  516. flexmock(module.command).should_receive('execute_hook')
  517. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  518. tuple(
  519. module.run_actions(
  520. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'extract': flexmock()},
  521. config_filename=flexmock(),
  522. location={'repositories': []},
  523. storage=flexmock(),
  524. retention=flexmock(),
  525. consistency=flexmock(),
  526. hooks={},
  527. local_path=flexmock(),
  528. remote_path=flexmock(),
  529. local_borg_version=flexmock(),
  530. repository={'path': 'repo'},
  531. )
  532. )
  533. def test_run_actions_runs_export_tar():
  534. flexmock(module).should_receive('add_custom_log_levels')
  535. flexmock(module.command).should_receive('execute_hook')
  536. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  537. tuple(
  538. module.run_actions(
  539. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export-tar': flexmock()},
  540. config_filename=flexmock(),
  541. location={'repositories': []},
  542. storage=flexmock(),
  543. retention=flexmock(),
  544. consistency=flexmock(),
  545. hooks={},
  546. local_path=flexmock(),
  547. remote_path=flexmock(),
  548. local_borg_version=flexmock(),
  549. repository={'path': 'repo'},
  550. )
  551. )
  552. def test_run_actions_runs_mount():
  553. flexmock(module).should_receive('add_custom_log_levels')
  554. flexmock(module.command).should_receive('execute_hook')
  555. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  556. tuple(
  557. module.run_actions(
  558. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'mount': flexmock()},
  559. config_filename=flexmock(),
  560. location={'repositories': []},
  561. storage=flexmock(),
  562. retention=flexmock(),
  563. consistency=flexmock(),
  564. hooks={},
  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_restore():
  572. flexmock(module).should_receive('add_custom_log_levels')
  573. flexmock(module.command).should_receive('execute_hook')
  574. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  575. tuple(
  576. module.run_actions(
  577. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'restore': flexmock()},
  578. config_filename=flexmock(),
  579. location={'repositories': []},
  580. storage=flexmock(),
  581. retention=flexmock(),
  582. consistency=flexmock(),
  583. hooks={},
  584. local_path=flexmock(),
  585. remote_path=flexmock(),
  586. local_borg_version=flexmock(),
  587. repository={'path': 'repo'},
  588. )
  589. )
  590. def test_run_actions_runs_rlist():
  591. flexmock(module).should_receive('add_custom_log_levels')
  592. flexmock(module.command).should_receive('execute_hook')
  593. expected = flexmock()
  594. flexmock(borgmatic.actions.rlist).should_receive('run_rlist').and_yield(expected).once()
  595. result = tuple(
  596. module.run_actions(
  597. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rlist': flexmock()},
  598. config_filename=flexmock(),
  599. location={'repositories': []},
  600. storage=flexmock(),
  601. retention=flexmock(),
  602. consistency=flexmock(),
  603. hooks={},
  604. local_path=flexmock(),
  605. remote_path=flexmock(),
  606. local_borg_version=flexmock(),
  607. repository={'path': 'repo'},
  608. )
  609. )
  610. assert result == (expected,)
  611. def test_run_actions_runs_list():
  612. flexmock(module).should_receive('add_custom_log_levels')
  613. flexmock(module.command).should_receive('execute_hook')
  614. expected = flexmock()
  615. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  616. result = tuple(
  617. module.run_actions(
  618. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'list': flexmock()},
  619. config_filename=flexmock(),
  620. location={'repositories': []},
  621. storage=flexmock(),
  622. retention=flexmock(),
  623. consistency=flexmock(),
  624. hooks={},
  625. local_path=flexmock(),
  626. remote_path=flexmock(),
  627. local_borg_version=flexmock(),
  628. repository={'path': 'repo'},
  629. )
  630. )
  631. assert result == (expected,)
  632. def test_run_actions_runs_rinfo():
  633. flexmock(module).should_receive('add_custom_log_levels')
  634. flexmock(module.command).should_receive('execute_hook')
  635. expected = flexmock()
  636. flexmock(borgmatic.actions.rinfo).should_receive('run_rinfo').and_yield(expected).once()
  637. result = tuple(
  638. module.run_actions(
  639. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rinfo': flexmock()},
  640. config_filename=flexmock(),
  641. location={'repositories': []},
  642. storage=flexmock(),
  643. retention=flexmock(),
  644. consistency=flexmock(),
  645. hooks={},
  646. local_path=flexmock(),
  647. remote_path=flexmock(),
  648. local_borg_version=flexmock(),
  649. repository={'path': 'repo'},
  650. )
  651. )
  652. assert result == (expected,)
  653. def test_run_actions_runs_info():
  654. flexmock(module).should_receive('add_custom_log_levels')
  655. flexmock(module.command).should_receive('execute_hook')
  656. expected = flexmock()
  657. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  658. result = tuple(
  659. module.run_actions(
  660. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'info': flexmock()},
  661. config_filename=flexmock(),
  662. location={'repositories': []},
  663. storage=flexmock(),
  664. retention=flexmock(),
  665. consistency=flexmock(),
  666. hooks={},
  667. local_path=flexmock(),
  668. remote_path=flexmock(),
  669. local_borg_version=flexmock(),
  670. repository={'path': 'repo'},
  671. )
  672. )
  673. assert result == (expected,)
  674. def test_run_actions_runs_break_lock():
  675. flexmock(module).should_receive('add_custom_log_levels')
  676. flexmock(module.command).should_receive('execute_hook')
  677. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  678. tuple(
  679. module.run_actions(
  680. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'break-lock': flexmock()},
  681. config_filename=flexmock(),
  682. location={'repositories': []},
  683. storage=flexmock(),
  684. retention=flexmock(),
  685. consistency=flexmock(),
  686. hooks={},
  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_borg():
  694. flexmock(module).should_receive('add_custom_log_levels')
  695. flexmock(module.command).should_receive('execute_hook')
  696. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  697. tuple(
  698. module.run_actions(
  699. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'borg': flexmock()},
  700. config_filename=flexmock(),
  701. location={'repositories': []},
  702. storage=flexmock(),
  703. retention=flexmock(),
  704. consistency=flexmock(),
  705. hooks={},
  706. local_path=flexmock(),
  707. remote_path=flexmock(),
  708. local_borg_version=flexmock(),
  709. repository={'path': 'repo'},
  710. )
  711. )
  712. def test_run_actions_runs_multiple_actions_in_argument_order():
  713. flexmock(module).should_receive('add_custom_log_levels')
  714. flexmock(module.command).should_receive('execute_hook')
  715. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  716. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  717. tuple(
  718. module.run_actions(
  719. arguments={
  720. 'global': flexmock(dry_run=False, log_file='foo'),
  721. 'borg': flexmock(),
  722. 'restore': flexmock(),
  723. },
  724. config_filename=flexmock(),
  725. location={'repositories': []},
  726. storage=flexmock(),
  727. retention=flexmock(),
  728. consistency=flexmock(),
  729. hooks={},
  730. local_path=flexmock(),
  731. remote_path=flexmock(),
  732. local_borg_version=flexmock(),
  733. repository={'path': 'repo'},
  734. )
  735. )
  736. def test_load_configurations_collects_parsed_configurations_and_logs():
  737. configuration = flexmock()
  738. other_configuration = flexmock()
  739. test_expected_logs = [flexmock(), flexmock()]
  740. other_expected_logs = [flexmock(), flexmock()]
  741. flexmock(module.validate).should_receive('parse_configuration').and_return(
  742. configuration, test_expected_logs
  743. ).and_return(other_configuration, other_expected_logs)
  744. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  745. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  746. assert logs == test_expected_logs + other_expected_logs
  747. def test_load_configurations_logs_warning_for_permission_error():
  748. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  749. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  750. assert configs == {}
  751. assert {log.levelno for log in logs} == {logging.WARNING}
  752. def test_load_configurations_logs_critical_for_parse_error():
  753. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  754. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  755. assert configs == {}
  756. assert {log.levelno for log in logs} == {logging.CRITICAL}
  757. def test_log_record_does_not_raise():
  758. module.log_record(levelno=1, foo='bar', baz='quux')
  759. def test_log_record_with_suppress_does_not_raise():
  760. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  761. def test_log_error_records_generates_output_logs_for_message_only():
  762. flexmock(module).should_receive('log_record').replace_with(dict)
  763. logs = tuple(module.log_error_records('Error'))
  764. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  765. def test_log_error_records_generates_output_logs_for_called_process_error():
  766. flexmock(module).should_receive('log_record').replace_with(dict)
  767. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  768. logs = tuple(
  769. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  770. )
  771. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  772. assert any(log for log in logs if 'error output' in str(log))
  773. def test_log_error_records_generates_logs_for_value_error():
  774. flexmock(module).should_receive('log_record').replace_with(dict)
  775. logs = tuple(module.log_error_records('Error', ValueError()))
  776. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  777. def test_log_error_records_generates_logs_for_os_error():
  778. flexmock(module).should_receive('log_record').replace_with(dict)
  779. logs = tuple(module.log_error_records('Error', OSError()))
  780. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  781. def test_log_error_records_generates_nothing_for_other_error():
  782. flexmock(module).should_receive('log_record').replace_with(dict)
  783. logs = tuple(module.log_error_records('Error', KeyError()))
  784. assert logs == ()
  785. def test_get_local_path_uses_configuration_value():
  786. assert module.get_local_path({'test.yaml': {'location': {'local_path': 'borg1'}}}) == 'borg1'
  787. def test_get_local_path_without_location_defaults_to_borg():
  788. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  789. def test_get_local_path_without_local_path_defaults_to_borg():
  790. assert module.get_local_path({'test.yaml': {'location': {}}}) == 'borg'
  791. def test_collect_configuration_run_summary_logs_info_for_success():
  792. flexmock(module.command).should_receive('execute_hook').never()
  793. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  794. flexmock(module).should_receive('run_configuration').and_return([])
  795. arguments = {}
  796. logs = tuple(
  797. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  798. )
  799. assert {log.levelno for log in logs} == {logging.INFO}
  800. def test_collect_configuration_run_summary_executes_hooks_for_create():
  801. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  802. flexmock(module).should_receive('run_configuration').and_return([])
  803. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  804. logs = tuple(
  805. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  806. )
  807. assert {log.levelno for log in logs} == {logging.INFO}
  808. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  809. flexmock(module.validate).should_receive('guard_single_repository_selected')
  810. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  811. flexmock(module).should_receive('run_configuration').and_return([])
  812. arguments = {'extract': flexmock(repository='repo')}
  813. logs = tuple(
  814. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  815. )
  816. assert {log.levelno for log in logs} == {logging.INFO}
  817. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  818. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  819. ValueError
  820. )
  821. expected_logs = (flexmock(),)
  822. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  823. arguments = {'extract': flexmock(repository='repo')}
  824. logs = tuple(
  825. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  826. )
  827. assert logs == expected_logs
  828. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  829. flexmock(module.validate).should_receive('guard_single_repository_selected')
  830. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  831. flexmock(module).should_receive('run_configuration').and_return([])
  832. arguments = {'mount': flexmock(repository='repo')}
  833. logs = tuple(
  834. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  835. )
  836. assert {log.levelno for log in logs} == {logging.INFO}
  837. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  838. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  839. ValueError
  840. )
  841. expected_logs = (flexmock(),)
  842. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  843. arguments = {'mount': flexmock(repository='repo')}
  844. logs = tuple(
  845. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  846. )
  847. assert logs == expected_logs
  848. def test_collect_configuration_run_summary_logs_missing_configs_error():
  849. arguments = {'global': flexmock(config_paths=[])}
  850. expected_logs = (flexmock(),)
  851. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  852. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  853. assert logs == expected_logs
  854. def test_collect_configuration_run_summary_logs_pre_hook_error():
  855. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  856. expected_logs = (flexmock(),)
  857. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  858. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  859. logs = tuple(
  860. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  861. )
  862. assert logs == expected_logs
  863. def test_collect_configuration_run_summary_logs_post_hook_error():
  864. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  865. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  866. flexmock(module).should_receive('run_configuration').and_return([])
  867. expected_logs = (flexmock(),)
  868. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  869. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  870. logs = tuple(
  871. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  872. )
  873. assert expected_logs[0] in logs
  874. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  875. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  876. ValueError
  877. )
  878. expected_logs = (flexmock(),)
  879. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  880. arguments = {'list': flexmock(repository='repo', archive='test')}
  881. logs = tuple(
  882. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  883. )
  884. assert logs == expected_logs
  885. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  886. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  887. flexmock(module).should_receive('run_configuration').and_return([])
  888. arguments = {'list': flexmock(repository='repo', archive=None)}
  889. logs = tuple(
  890. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  891. )
  892. assert {log.levelno for log in logs} == {logging.INFO}
  893. def test_collect_configuration_run_summary_logs_run_configuration_error():
  894. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  895. flexmock(module).should_receive('run_configuration').and_return(
  896. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  897. )
  898. flexmock(module).should_receive('log_error_records').and_return([])
  899. arguments = {}
  900. logs = tuple(
  901. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  902. )
  903. assert {log.levelno for log in logs} == {logging.CRITICAL}
  904. def test_collect_configuration_run_summary_logs_run_umount_error():
  905. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  906. flexmock(module).should_receive('run_configuration').and_return([])
  907. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  908. flexmock(module).should_receive('log_error_records').and_return(
  909. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  910. )
  911. arguments = {'umount': flexmock(mount_point='/mnt')}
  912. logs = tuple(
  913. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  914. )
  915. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  916. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  917. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  918. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  919. ['baz']
  920. )
  921. stdout = flexmock()
  922. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  923. flexmock(module.sys).stdout = stdout
  924. arguments = {}
  925. tuple(
  926. module.collect_configuration_run_summary_logs(
  927. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  928. )
  929. )