test_borgmatic.py 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'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 = {'repositories': [{'path': 'foo'}], '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 = {'repositories': [{'path': 'foo'}], '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 = {'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. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  238. '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. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  268. '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. 'repositories': [{'path': 'foo'}],
  305. 'retries': 3,
  306. 'retry_wait': 10,
  307. }
  308. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  309. results = list(module.run_configuration('test.yaml', config, arguments))
  310. assert results == error_logs
  311. def test_run_configuration_retries_timeout_multiple_repos():
  312. flexmock(module).should_receive('verbosity_to_log_level').and_return(logging.INFO)
  313. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  314. flexmock(module.command).should_receive('execute_hook')
  315. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  316. []
  317. ).and_raise(OSError).times(4)
  318. flexmock(module).should_receive('log_error_records').with_args(
  319. 'foo: Error running actions for repository',
  320. OSError,
  321. levelno=logging.WARNING,
  322. log_command_error_output=True,
  323. ).and_return([flexmock()]).ordered()
  324. flexmock(module).should_receive('log_error_records').with_args(
  325. 'bar: Error running actions for repository',
  326. OSError,
  327. levelno=logging.WARNING,
  328. log_command_error_output=True,
  329. ).and_return([flexmock()]).ordered()
  330. # Sleep before retrying foo (and passing)
  331. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  332. # Sleep before retrying bar (and failing)
  333. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  334. error_logs = [flexmock()]
  335. flexmock(module).should_receive('log_error_records').with_args(
  336. 'bar: Error running actions for repository', OSError
  337. ).and_return(error_logs).ordered()
  338. config = {
  339. 'repositories': [{'path': 'foo'}, {'path': 'bar'}],
  340. 'retries': 1,
  341. 'retry_wait': 10,
  342. }
  343. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  344. results = list(module.run_configuration('test.yaml', config, arguments))
  345. assert results == error_logs
  346. def test_run_actions_runs_rcreate():
  347. flexmock(module).should_receive('add_custom_log_levels')
  348. flexmock(module.command).should_receive('execute_hook')
  349. flexmock(borgmatic.actions.rcreate).should_receive('run_rcreate').once()
  350. tuple(
  351. module.run_actions(
  352. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rcreate': flexmock()},
  353. config_filename=flexmock(),
  354. config={'repositories': []},
  355. local_path=flexmock(),
  356. remote_path=flexmock(),
  357. local_borg_version=flexmock(),
  358. repository={'path': 'repo'},
  359. )
  360. )
  361. def test_run_actions_adds_log_file_to_hook_context():
  362. flexmock(module).should_receive('add_custom_log_levels')
  363. flexmock(module.command).should_receive('execute_hook')
  364. expected = flexmock()
  365. flexmock(borgmatic.actions.create).should_receive('run_create').with_args(
  366. config_filename=object,
  367. repository={'path': 'repo'},
  368. config={'repositories': []},
  369. hook_context={'repository': 'repo', 'repositories': '', 'log_file': 'foo'},
  370. local_borg_version=object,
  371. create_arguments=object,
  372. global_arguments=object,
  373. dry_run_label='',
  374. local_path=object,
  375. remote_path=object,
  376. ).once().and_return(expected)
  377. result = tuple(
  378. module.run_actions(
  379. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  380. config_filename=flexmock(),
  381. config={'repositories': []},
  382. local_path=flexmock(),
  383. remote_path=flexmock(),
  384. local_borg_version=flexmock(),
  385. repository={'path': 'repo'},
  386. )
  387. )
  388. assert result == (expected,)
  389. def test_run_actions_runs_transfer():
  390. flexmock(module).should_receive('add_custom_log_levels')
  391. flexmock(module.command).should_receive('execute_hook')
  392. flexmock(borgmatic.actions.transfer).should_receive('run_transfer').once()
  393. tuple(
  394. module.run_actions(
  395. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'transfer': flexmock()},
  396. config_filename=flexmock(),
  397. config={'repositories': []},
  398. local_path=flexmock(),
  399. remote_path=flexmock(),
  400. local_borg_version=flexmock(),
  401. repository={'path': 'repo'},
  402. )
  403. )
  404. def test_run_actions_runs_create():
  405. flexmock(module).should_receive('add_custom_log_levels')
  406. flexmock(module.command).should_receive('execute_hook')
  407. expected = flexmock()
  408. flexmock(borgmatic.actions.create).should_receive('run_create').and_yield(expected).once()
  409. result = tuple(
  410. module.run_actions(
  411. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'create': flexmock()},
  412. config_filename=flexmock(),
  413. config={'repositories': []},
  414. local_path=flexmock(),
  415. remote_path=flexmock(),
  416. local_borg_version=flexmock(),
  417. repository={'path': 'repo'},
  418. )
  419. )
  420. assert result == (expected,)
  421. def test_run_actions_runs_prune():
  422. flexmock(module).should_receive('add_custom_log_levels')
  423. flexmock(module.command).should_receive('execute_hook')
  424. flexmock(borgmatic.actions.prune).should_receive('run_prune').once()
  425. tuple(
  426. module.run_actions(
  427. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'prune': flexmock()},
  428. config_filename=flexmock(),
  429. config={'repositories': []},
  430. local_path=flexmock(),
  431. remote_path=flexmock(),
  432. local_borg_version=flexmock(),
  433. repository={'path': 'repo'},
  434. )
  435. )
  436. def test_run_actions_runs_compact():
  437. flexmock(module).should_receive('add_custom_log_levels')
  438. flexmock(module.command).should_receive('execute_hook')
  439. flexmock(borgmatic.actions.compact).should_receive('run_compact').once()
  440. tuple(
  441. module.run_actions(
  442. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'compact': flexmock()},
  443. config_filename=flexmock(),
  444. config={'repositories': []},
  445. local_path=flexmock(),
  446. remote_path=flexmock(),
  447. local_borg_version=flexmock(),
  448. repository={'path': 'repo'},
  449. )
  450. )
  451. def test_run_actions_runs_check_when_repository_enabled_for_checks():
  452. flexmock(module).should_receive('add_custom_log_levels')
  453. flexmock(module.command).should_receive('execute_hook')
  454. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(True)
  455. flexmock(borgmatic.actions.check).should_receive('run_check').once()
  456. tuple(
  457. module.run_actions(
  458. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  459. config_filename=flexmock(),
  460. config={'repositories': []},
  461. local_path=flexmock(),
  462. remote_path=flexmock(),
  463. local_borg_version=flexmock(),
  464. repository={'path': 'repo'},
  465. )
  466. )
  467. def test_run_actions_skips_check_when_repository_not_enabled_for_checks():
  468. flexmock(module).should_receive('add_custom_log_levels')
  469. flexmock(module.command).should_receive('execute_hook')
  470. flexmock(module.checks).should_receive('repository_enabled_for_checks').and_return(False)
  471. flexmock(borgmatic.actions.check).should_receive('run_check').never()
  472. tuple(
  473. module.run_actions(
  474. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'check': flexmock()},
  475. config_filename=flexmock(),
  476. config={'repositories': []},
  477. local_path=flexmock(),
  478. remote_path=flexmock(),
  479. local_borg_version=flexmock(),
  480. repository={'path': 'repo'},
  481. )
  482. )
  483. def test_run_actions_runs_extract():
  484. flexmock(module).should_receive('add_custom_log_levels')
  485. flexmock(module.command).should_receive('execute_hook')
  486. flexmock(borgmatic.actions.extract).should_receive('run_extract').once()
  487. tuple(
  488. module.run_actions(
  489. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'extract': flexmock()},
  490. config_filename=flexmock(),
  491. config={'repositories': []},
  492. local_path=flexmock(),
  493. remote_path=flexmock(),
  494. local_borg_version=flexmock(),
  495. repository={'path': 'repo'},
  496. )
  497. )
  498. def test_run_actions_runs_export_tar():
  499. flexmock(module).should_receive('add_custom_log_levels')
  500. flexmock(module.command).should_receive('execute_hook')
  501. flexmock(borgmatic.actions.export_tar).should_receive('run_export_tar').once()
  502. tuple(
  503. module.run_actions(
  504. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export-tar': flexmock()},
  505. config_filename=flexmock(),
  506. config={'repositories': []},
  507. local_path=flexmock(),
  508. remote_path=flexmock(),
  509. local_borg_version=flexmock(),
  510. repository={'path': 'repo'},
  511. )
  512. )
  513. def test_run_actions_runs_mount():
  514. flexmock(module).should_receive('add_custom_log_levels')
  515. flexmock(module.command).should_receive('execute_hook')
  516. flexmock(borgmatic.actions.mount).should_receive('run_mount').once()
  517. tuple(
  518. module.run_actions(
  519. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'mount': flexmock()},
  520. config_filename=flexmock(),
  521. config={'repositories': []},
  522. local_path=flexmock(),
  523. remote_path=flexmock(),
  524. local_borg_version=flexmock(),
  525. repository={'path': 'repo'},
  526. )
  527. )
  528. def test_run_actions_runs_restore():
  529. flexmock(module).should_receive('add_custom_log_levels')
  530. flexmock(module.command).should_receive('execute_hook')
  531. flexmock(borgmatic.actions.restore).should_receive('run_restore').once()
  532. tuple(
  533. module.run_actions(
  534. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'restore': flexmock()},
  535. config_filename=flexmock(),
  536. config={'repositories': []},
  537. local_path=flexmock(),
  538. remote_path=flexmock(),
  539. local_borg_version=flexmock(),
  540. repository={'path': 'repo'},
  541. )
  542. )
  543. def test_run_actions_runs_rlist():
  544. flexmock(module).should_receive('add_custom_log_levels')
  545. flexmock(module.command).should_receive('execute_hook')
  546. expected = flexmock()
  547. flexmock(borgmatic.actions.rlist).should_receive('run_rlist').and_yield(expected).once()
  548. result = tuple(
  549. module.run_actions(
  550. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rlist': flexmock()},
  551. config_filename=flexmock(),
  552. config={'repositories': []},
  553. local_path=flexmock(),
  554. remote_path=flexmock(),
  555. local_borg_version=flexmock(),
  556. repository={'path': 'repo'},
  557. )
  558. )
  559. assert result == (expected,)
  560. def test_run_actions_runs_list():
  561. flexmock(module).should_receive('add_custom_log_levels')
  562. flexmock(module.command).should_receive('execute_hook')
  563. expected = flexmock()
  564. flexmock(borgmatic.actions.list).should_receive('run_list').and_yield(expected).once()
  565. result = tuple(
  566. module.run_actions(
  567. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'list': flexmock()},
  568. config_filename=flexmock(),
  569. config={'repositories': []},
  570. local_path=flexmock(),
  571. remote_path=flexmock(),
  572. local_borg_version=flexmock(),
  573. repository={'path': 'repo'},
  574. )
  575. )
  576. assert result == (expected,)
  577. def test_run_actions_runs_rinfo():
  578. flexmock(module).should_receive('add_custom_log_levels')
  579. flexmock(module.command).should_receive('execute_hook')
  580. expected = flexmock()
  581. flexmock(borgmatic.actions.rinfo).should_receive('run_rinfo').and_yield(expected).once()
  582. result = tuple(
  583. module.run_actions(
  584. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'rinfo': flexmock()},
  585. config_filename=flexmock(),
  586. config={'repositories': []},
  587. local_path=flexmock(),
  588. remote_path=flexmock(),
  589. local_borg_version=flexmock(),
  590. repository={'path': 'repo'},
  591. )
  592. )
  593. assert result == (expected,)
  594. def test_run_actions_runs_info():
  595. flexmock(module).should_receive('add_custom_log_levels')
  596. flexmock(module.command).should_receive('execute_hook')
  597. expected = flexmock()
  598. flexmock(borgmatic.actions.info).should_receive('run_info').and_yield(expected).once()
  599. result = tuple(
  600. module.run_actions(
  601. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'info': flexmock()},
  602. config_filename=flexmock(),
  603. config={'repositories': []},
  604. local_path=flexmock(),
  605. remote_path=flexmock(),
  606. local_borg_version=flexmock(),
  607. repository={'path': 'repo'},
  608. )
  609. )
  610. assert result == (expected,)
  611. def test_run_actions_runs_break_lock():
  612. flexmock(module).should_receive('add_custom_log_levels')
  613. flexmock(module.command).should_receive('execute_hook')
  614. flexmock(borgmatic.actions.break_lock).should_receive('run_break_lock').once()
  615. tuple(
  616. module.run_actions(
  617. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'break-lock': flexmock()},
  618. config_filename=flexmock(),
  619. config={'repositories': []},
  620. local_path=flexmock(),
  621. remote_path=flexmock(),
  622. local_borg_version=flexmock(),
  623. repository={'path': 'repo'},
  624. )
  625. )
  626. def test_run_actions_runs_export_key():
  627. flexmock(module).should_receive('add_custom_log_levels')
  628. flexmock(module.command).should_receive('execute_hook')
  629. flexmock(borgmatic.actions.export_key).should_receive('run_export_key').once()
  630. tuple(
  631. module.run_actions(
  632. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'export': flexmock()},
  633. config_filename=flexmock(),
  634. config={'repositories': []},
  635. local_path=flexmock(),
  636. remote_path=flexmock(),
  637. local_borg_version=flexmock(),
  638. repository={'path': 'repo'},
  639. )
  640. )
  641. def test_run_actions_runs_borg():
  642. flexmock(module).should_receive('add_custom_log_levels')
  643. flexmock(module.command).should_receive('execute_hook')
  644. flexmock(borgmatic.actions.borg).should_receive('run_borg').once()
  645. tuple(
  646. module.run_actions(
  647. arguments={'global': flexmock(dry_run=False, log_file='foo'), 'borg': flexmock()},
  648. config_filename=flexmock(),
  649. config={'repositories': []},
  650. local_path=flexmock(),
  651. remote_path=flexmock(),
  652. local_borg_version=flexmock(),
  653. repository={'path': 'repo'},
  654. )
  655. )
  656. def test_run_actions_runs_multiple_actions_in_argument_order():
  657. flexmock(module).should_receive('add_custom_log_levels')
  658. flexmock(module.command).should_receive('execute_hook')
  659. flexmock(borgmatic.actions.borg).should_receive('run_borg').once().ordered()
  660. flexmock(borgmatic.actions.restore).should_receive('run_restore').once().ordered()
  661. tuple(
  662. module.run_actions(
  663. arguments={
  664. 'global': flexmock(dry_run=False, log_file='foo'),
  665. 'borg': flexmock(),
  666. 'restore': flexmock(),
  667. },
  668. config_filename=flexmock(),
  669. config={'repositories': []},
  670. local_path=flexmock(),
  671. remote_path=flexmock(),
  672. local_borg_version=flexmock(),
  673. repository={'path': 'repo'},
  674. )
  675. )
  676. def test_load_configurations_collects_parsed_configurations_and_logs():
  677. configuration = flexmock()
  678. other_configuration = flexmock()
  679. test_expected_logs = [flexmock(), flexmock()]
  680. other_expected_logs = [flexmock(), flexmock()]
  681. flexmock(module.validate).should_receive('parse_configuration').and_return(
  682. configuration, test_expected_logs
  683. ).and_return(other_configuration, other_expected_logs)
  684. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  685. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  686. assert set(logs) >= set(test_expected_logs + other_expected_logs)
  687. def test_load_configurations_logs_warning_for_permission_error():
  688. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  689. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  690. assert configs == {}
  691. assert max(log.levelno for log in logs) == logging.WARNING
  692. def test_load_configurations_logs_critical_for_parse_error():
  693. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  694. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  695. assert configs == {}
  696. assert max(log.levelno for log in logs) == logging.CRITICAL
  697. def test_log_record_does_not_raise():
  698. module.log_record(levelno=1, foo='bar', baz='quux')
  699. def test_log_record_with_suppress_does_not_raise():
  700. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  701. def test_log_error_records_generates_output_logs_for_message_only():
  702. flexmock(module).should_receive('log_record').replace_with(dict).once()
  703. logs = tuple(module.log_error_records('Error'))
  704. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  705. def test_log_error_records_generates_output_logs_for_called_process_error_with_bytes_ouput():
  706. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  707. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  708. logs = tuple(
  709. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', b'error output'))
  710. )
  711. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  712. assert any(log for log in logs if 'error output' in str(log))
  713. def test_log_error_records_generates_output_logs_for_called_process_error_with_string_ouput():
  714. flexmock(module).should_receive('log_record').replace_with(dict).times(3)
  715. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  716. logs = tuple(
  717. module.log_error_records('Error', subprocess.CalledProcessError(1, 'ls', 'error output'))
  718. )
  719. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  720. assert any(log for log in logs if 'error output' in str(log))
  721. def test_log_error_records_splits_called_process_error_with_multiline_ouput_into_multiple_logs():
  722. flexmock(module).should_receive('log_record').replace_with(dict).times(4)
  723. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  724. logs = tuple(
  725. module.log_error_records(
  726. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output\nanother line')
  727. )
  728. )
  729. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  730. assert any(log for log in logs if 'error output' in str(log))
  731. def test_log_error_records_generates_logs_for_value_error():
  732. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  733. logs = tuple(module.log_error_records('Error', ValueError()))
  734. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  735. def test_log_error_records_generates_logs_for_os_error():
  736. flexmock(module).should_receive('log_record').replace_with(dict).twice()
  737. logs = tuple(module.log_error_records('Error', OSError()))
  738. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  739. def test_log_error_records_generates_nothing_for_other_error():
  740. flexmock(module).should_receive('log_record').never()
  741. logs = tuple(module.log_error_records('Error', KeyError()))
  742. assert logs == ()
  743. def test_get_local_path_uses_configuration_value():
  744. assert module.get_local_path({'test.yaml': {'local_path': 'borg1'}}) == 'borg1'
  745. def test_get_local_path_without_local_path_defaults_to_borg():
  746. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  747. def test_collect_highlander_action_summary_logs_info_for_success_with_bootstrap():
  748. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  749. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap')
  750. arguments = {
  751. 'bootstrap': flexmock(repository='repo'),
  752. 'global': flexmock(dry_run=False),
  753. }
  754. logs = tuple(
  755. module.collect_highlander_action_summary_logs(
  756. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  757. )
  758. )
  759. assert {log.levelno for log in logs} == {logging.ANSWER}
  760. def test_collect_highlander_action_summary_logs_error_on_bootstrap_failure():
  761. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  762. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').and_raise(
  763. ValueError
  764. )
  765. arguments = {
  766. 'bootstrap': flexmock(repository='repo'),
  767. 'global': flexmock(dry_run=False),
  768. }
  769. logs = tuple(
  770. module.collect_highlander_action_summary_logs(
  771. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  772. )
  773. )
  774. assert {log.levelno for log in logs} == {logging.CRITICAL}
  775. def test_collect_highlander_action_summary_logs_error_on_bootstrap_local_borg_version_failure():
  776. flexmock(module.borg_version).should_receive('local_borg_version').and_raise(ValueError)
  777. flexmock(module.borgmatic.actions.config.bootstrap).should_receive('run_bootstrap').never()
  778. arguments = {
  779. 'bootstrap': flexmock(repository='repo'),
  780. 'global': flexmock(dry_run=False),
  781. }
  782. logs = tuple(
  783. module.collect_highlander_action_summary_logs(
  784. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  785. )
  786. )
  787. assert {log.levelno for log in logs} == {logging.CRITICAL}
  788. def test_collect_highlander_action_summary_logs_info_for_success_with_generate():
  789. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate')
  790. arguments = {
  791. 'generate': flexmock(destination='test.yaml'),
  792. 'global': flexmock(dry_run=False),
  793. }
  794. logs = tuple(
  795. module.collect_highlander_action_summary_logs(
  796. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  797. )
  798. )
  799. assert {log.levelno for log in logs} == {logging.ANSWER}
  800. def test_collect_highlander_action_summary_logs_error_on_generate_failure():
  801. flexmock(module.borgmatic.actions.config.generate).should_receive('run_generate').and_raise(
  802. ValueError
  803. )
  804. arguments = {
  805. 'generate': flexmock(destination='test.yaml'),
  806. 'global': flexmock(dry_run=False),
  807. }
  808. logs = tuple(
  809. module.collect_highlander_action_summary_logs(
  810. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  811. )
  812. )
  813. assert {log.levelno for log in logs} == {logging.CRITICAL}
  814. def test_collect_highlander_action_summary_logs_info_for_success_with_validate():
  815. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  816. arguments = {
  817. 'validate': flexmock(),
  818. 'global': flexmock(dry_run=False),
  819. }
  820. logs = tuple(
  821. module.collect_highlander_action_summary_logs(
  822. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  823. )
  824. )
  825. assert {log.levelno for log in logs} == {logging.ANSWER}
  826. def test_collect_highlander_action_summary_logs_error_on_validate_parse_failure():
  827. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate')
  828. arguments = {
  829. 'validate': flexmock(),
  830. 'global': flexmock(dry_run=False),
  831. }
  832. logs = tuple(
  833. module.collect_highlander_action_summary_logs(
  834. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=True
  835. )
  836. )
  837. assert {log.levelno for log in logs} == {logging.CRITICAL}
  838. def test_collect_highlander_action_summary_logs_error_on_run_validate_failure():
  839. flexmock(module.borgmatic.actions.config.validate).should_receive('run_validate').and_raise(
  840. ValueError
  841. )
  842. arguments = {
  843. 'validate': flexmock(),
  844. 'global': flexmock(dry_run=False),
  845. }
  846. logs = tuple(
  847. module.collect_highlander_action_summary_logs(
  848. {'test.yaml': {}}, arguments=arguments, configuration_parse_errors=False
  849. )
  850. )
  851. assert {log.levelno for log in logs} == {logging.CRITICAL}
  852. def test_collect_configuration_run_summary_logs_info_for_success():
  853. flexmock(module.command).should_receive('execute_hook').never()
  854. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  855. flexmock(module).should_receive('run_configuration').and_return([])
  856. arguments = {}
  857. logs = tuple(
  858. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  859. )
  860. assert {log.levelno for log in logs} == {logging.INFO}
  861. def test_collect_configuration_run_summary_executes_hooks_for_create():
  862. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  863. flexmock(module).should_receive('run_configuration').and_return([])
  864. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  865. logs = tuple(
  866. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  867. )
  868. assert {log.levelno for log in logs} == {logging.INFO}
  869. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  870. flexmock(module.validate).should_receive('guard_single_repository_selected')
  871. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  872. flexmock(module).should_receive('run_configuration').and_return([])
  873. arguments = {'extract': flexmock(repository='repo')}
  874. logs = tuple(
  875. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  876. )
  877. assert {log.levelno for log in logs} == {logging.INFO}
  878. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  879. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  880. ValueError
  881. )
  882. expected_logs = (flexmock(),)
  883. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  884. arguments = {'extract': flexmock(repository='repo')}
  885. logs = tuple(
  886. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  887. )
  888. assert logs == expected_logs
  889. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  890. flexmock(module.validate).should_receive('guard_single_repository_selected')
  891. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  892. flexmock(module).should_receive('run_configuration').and_return([])
  893. arguments = {'mount': flexmock(repository='repo')}
  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_mount_with_repository_error():
  899. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  900. ValueError
  901. )
  902. expected_logs = (flexmock(),)
  903. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  904. arguments = {'mount': flexmock(repository='repo')}
  905. logs = tuple(
  906. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  907. )
  908. assert logs == expected_logs
  909. def test_collect_configuration_run_summary_logs_missing_configs_error():
  910. arguments = {'global': flexmock(config_paths=[])}
  911. expected_logs = (flexmock(),)
  912. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  913. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  914. assert logs == expected_logs
  915. def test_collect_configuration_run_summary_logs_pre_hook_error():
  916. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  917. expected_logs = (flexmock(),)
  918. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  919. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  920. logs = tuple(
  921. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  922. )
  923. assert logs == expected_logs
  924. def test_collect_configuration_run_summary_logs_post_hook_error():
  925. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  926. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  927. flexmock(module).should_receive('run_configuration').and_return([])
  928. expected_logs = (flexmock(),)
  929. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  930. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  931. logs = tuple(
  932. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  933. )
  934. assert expected_logs[0] in logs
  935. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  936. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  937. ValueError
  938. )
  939. expected_logs = (flexmock(),)
  940. flexmock(module).should_receive('log_error_records').and_return(expected_logs)
  941. arguments = {'list': flexmock(repository='repo', archive='test')}
  942. logs = tuple(
  943. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  944. )
  945. assert logs == expected_logs
  946. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  947. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  948. flexmock(module).should_receive('run_configuration').and_return([])
  949. arguments = {'list': flexmock(repository='repo', archive=None)}
  950. logs = tuple(
  951. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  952. )
  953. assert {log.levelno for log in logs} == {logging.INFO}
  954. def test_collect_configuration_run_summary_logs_run_configuration_error():
  955. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  956. flexmock(module).should_receive('run_configuration').and_return(
  957. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  958. )
  959. flexmock(module).should_receive('log_error_records').and_return([])
  960. arguments = {}
  961. logs = tuple(
  962. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  963. )
  964. assert {log.levelno for log in logs} == {logging.CRITICAL}
  965. def test_collect_configuration_run_summary_logs_run_umount_error():
  966. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  967. flexmock(module).should_receive('run_configuration').and_return([])
  968. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  969. flexmock(module).should_receive('log_error_records').and_return(
  970. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  971. )
  972. arguments = {'umount': flexmock(mount_point='/mnt')}
  973. logs = tuple(
  974. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  975. )
  976. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  977. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  978. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  979. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  980. ['baz']
  981. )
  982. stdout = flexmock()
  983. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  984. flexmock(module.sys).stdout = stdout
  985. arguments = {}
  986. tuple(
  987. module.collect_configuration_run_summary_logs(
  988. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  989. )
  990. )