2
0

test_borgmatic.py 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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_info_for_success_with_bootstrap():
  810. flexmock(module.validate).should_receive('guard_single_repository_selected').never()
  811. flexmock(module.validate).should_receive('guard_configuration_contains_repository').never()
  812. flexmock(module).should_receive('run_configuration').never()
  813. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  814. arguments = {
  815. 'bootstrap': flexmock(repository='repo'),
  816. 'global': flexmock(monitoring_verbosity=1, dry_run=False),
  817. }
  818. logs = tuple(
  819. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  820. )
  821. assert {log.levelno for log in logs} == {logging.INFO}
  822. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  823. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  824. ValueError
  825. )
  826. expected_logs = (flexmock(),)
  827. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  828. arguments = {'extract': flexmock(repository='repo')}
  829. logs = tuple(
  830. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  831. )
  832. assert logs == expected_logs
  833. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  834. flexmock(module.validate).should_receive('guard_single_repository_selected')
  835. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  836. flexmock(module).should_receive('run_configuration').and_return([])
  837. arguments = {'mount': flexmock(repository='repo')}
  838. logs = tuple(
  839. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  840. )
  841. assert {log.levelno for log in logs} == {logging.INFO}
  842. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  843. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  844. ValueError
  845. )
  846. expected_logs = (flexmock(),)
  847. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  848. arguments = {'mount': flexmock(repository='repo')}
  849. logs = tuple(
  850. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  851. )
  852. assert logs == expected_logs
  853. def test_collect_configuration_run_summary_logs_missing_configs_error():
  854. arguments = {'global': flexmock(config_paths=[])}
  855. expected_logs = (flexmock(),)
  856. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  857. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  858. assert logs == expected_logs
  859. def test_collect_configuration_run_summary_logs_pre_hook_error():
  860. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  861. expected_logs = (flexmock(),)
  862. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  863. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  864. logs = tuple(
  865. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  866. )
  867. assert logs == expected_logs
  868. def test_collect_configuration_run_summary_logs_post_hook_error():
  869. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  870. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  871. flexmock(module).should_receive('run_configuration').and_return([])
  872. expected_logs = (flexmock(),)
  873. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  874. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  875. logs = tuple(
  876. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  877. )
  878. assert expected_logs[0] in logs
  879. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  880. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  881. ValueError
  882. )
  883. expected_logs = (flexmock(),)
  884. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  885. arguments = {'list': flexmock(repository='repo', archive='test')}
  886. logs = tuple(
  887. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  888. )
  889. assert logs == expected_logs
  890. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  891. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  892. flexmock(module).should_receive('run_configuration').and_return([])
  893. arguments = {'list': flexmock(repository='repo', archive=None)}
  894. logs = tuple(
  895. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  896. )
  897. assert {log.levelno for log in logs} == {logging.INFO}
  898. def test_collect_configuration_run_summary_logs_run_configuration_error():
  899. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  900. flexmock(module).should_receive('run_configuration').and_return(
  901. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  902. )
  903. flexmock(module).should_receive('log_error_records').and_return([])
  904. arguments = {}
  905. logs = tuple(
  906. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  907. )
  908. assert {log.levelno for log in logs} == {logging.CRITICAL}
  909. def test_collect_configuration_run_summary_logs_run_umount_error():
  910. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  911. flexmock(module).should_receive('run_configuration').and_return([])
  912. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  913. flexmock(module).should_receive('log_error_records').and_return(
  914. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  915. )
  916. arguments = {'umount': flexmock(mount_point='/mnt')}
  917. logs = tuple(
  918. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  919. )
  920. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  921. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  922. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  923. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  924. ['baz']
  925. )
  926. stdout = flexmock()
  927. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  928. flexmock(module.sys).stdout = stdout
  929. arguments = {}
  930. tuple(
  931. module.collect_configuration_run_summary_logs(
  932. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  933. )
  934. )