test_borgmatic.py 51 KB

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