test_borgmatic.py 46 KB

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