test_borgmatic.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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.borg_environment).should_receive('initialize')
  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': ['foo', '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.borg_environment).should_receive('initialize')
  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_calls_hooks_for_prune_action():
  28. flexmock(module.borg_environment).should_receive('initialize')
  29. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  30. flexmock(module.command).should_receive('execute_hook').twice()
  31. flexmock(module.dispatch).should_receive('call_hooks').at_least().twice()
  32. flexmock(module).should_receive('run_actions').and_return([])
  33. config = {'location': {'repositories': ['foo']}}
  34. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'prune': flexmock()}
  35. list(module.run_configuration('test.yaml', config, arguments))
  36. def test_run_configuration_calls_hooks_for_compact_action():
  37. flexmock(module.borg_environment).should_receive('initialize')
  38. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  39. flexmock(module.command).should_receive('execute_hook').twice()
  40. flexmock(module).should_receive('run_actions').and_return([])
  41. config = {'location': {'repositories': ['foo']}}
  42. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'compact': flexmock()}
  43. list(module.run_configuration('test.yaml', config, arguments))
  44. def test_run_configuration_executes_and_calls_hooks_for_create_action():
  45. flexmock(module.borg_environment).should_receive('initialize')
  46. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  47. flexmock(module.command).should_receive('execute_hook').twice()
  48. flexmock(module.dispatch).should_receive('call_hooks').at_least().twice()
  49. flexmock(module).should_receive('run_actions').and_return([])
  50. config = {'location': {'repositories': ['foo']}}
  51. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  52. list(module.run_configuration('test.yaml', config, arguments))
  53. def test_run_configuration_calls_hooks_for_check_action():
  54. flexmock(module.borg_environment).should_receive('initialize')
  55. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  56. flexmock(module.command).should_receive('execute_hook').twice()
  57. flexmock(module.dispatch).should_receive('call_hooks').at_least().twice()
  58. flexmock(module).should_receive('run_actions').and_return([])
  59. config = {'location': {'repositories': ['foo']}}
  60. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'check': flexmock()}
  61. list(module.run_configuration('test.yaml', config, arguments))
  62. def test_run_configuration_calls_hooks_for_extract_action():
  63. flexmock(module.borg_environment).should_receive('initialize')
  64. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  65. flexmock(module.command).should_receive('execute_hook').twice()
  66. flexmock(module.dispatch).should_receive('call_hooks').never()
  67. flexmock(module).should_receive('run_actions').and_return([])
  68. config = {'location': {'repositories': ['foo']}}
  69. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'extract': flexmock()}
  70. list(module.run_configuration('test.yaml', config, arguments))
  71. def test_run_configuration_does_not_trigger_hooks_for_list_action():
  72. flexmock(module.borg_environment).should_receive('initialize')
  73. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  74. flexmock(module.command).should_receive('execute_hook').never()
  75. flexmock(module.dispatch).should_receive('call_hooks').never()
  76. flexmock(module).should_receive('run_actions').and_return([])
  77. config = {'location': {'repositories': ['foo']}}
  78. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'list': flexmock()}
  79. list(module.run_configuration('test.yaml', config, arguments))
  80. def test_run_configuration_logs_actions_error():
  81. flexmock(module.borg_environment).should_receive('initialize')
  82. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  83. flexmock(module.command).should_receive('execute_hook')
  84. flexmock(module.dispatch).should_receive('call_hooks')
  85. expected_results = [flexmock()]
  86. flexmock(module).should_receive('make_error_log_records').and_return(expected_results)
  87. flexmock(module).should_receive('run_actions').and_raise(OSError)
  88. config = {'location': {'repositories': ['foo']}}
  89. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  90. results = list(module.run_configuration('test.yaml', config, arguments))
  91. assert results == expected_results
  92. def test_run_configuration_logs_pre_hook_error():
  93. flexmock(module.borg_environment).should_receive('initialize')
  94. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  95. flexmock(module.command).should_receive('execute_hook').and_raise(OSError).and_return(None)
  96. expected_results = [flexmock()]
  97. flexmock(module).should_receive('make_error_log_records').and_return(expected_results)
  98. flexmock(module).should_receive('run_actions').never()
  99. config = {'location': {'repositories': ['foo']}}
  100. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  101. results = list(module.run_configuration('test.yaml', config, arguments))
  102. assert results == expected_results
  103. def test_run_configuration_bails_for_pre_hook_soft_failure():
  104. flexmock(module.borg_environment).should_receive('initialize')
  105. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  106. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  107. flexmock(module.command).should_receive('execute_hook').and_raise(error).and_return(None)
  108. flexmock(module).should_receive('make_error_log_records').never()
  109. flexmock(module).should_receive('run_actions').never()
  110. config = {'location': {'repositories': ['foo']}}
  111. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  112. results = list(module.run_configuration('test.yaml', config, arguments))
  113. assert results == []
  114. def test_run_configuration_logs_post_hook_error():
  115. flexmock(module.borg_environment).should_receive('initialize')
  116. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  117. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(
  118. OSError
  119. ).and_return(None)
  120. flexmock(module.dispatch).should_receive('call_hooks')
  121. expected_results = [flexmock()]
  122. flexmock(module).should_receive('make_error_log_records').and_return(expected_results)
  123. flexmock(module).should_receive('run_actions').and_return([])
  124. config = {'location': {'repositories': ['foo']}}
  125. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  126. results = list(module.run_configuration('test.yaml', config, arguments))
  127. assert results == expected_results
  128. def test_run_configuration_bails_for_post_hook_soft_failure():
  129. flexmock(module.borg_environment).should_receive('initialize')
  130. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  131. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  132. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(
  133. error
  134. ).and_return(None)
  135. flexmock(module.dispatch).should_receive('call_hooks')
  136. flexmock(module).should_receive('make_error_log_records').never()
  137. flexmock(module).should_receive('run_actions').and_return([])
  138. config = {'location': {'repositories': ['foo']}}
  139. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  140. results = list(module.run_configuration('test.yaml', config, arguments))
  141. assert results == []
  142. def test_run_configuration_logs_on_error_hook_error():
  143. flexmock(module.borg_environment).should_receive('initialize')
  144. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  145. flexmock(module.command).should_receive('execute_hook').and_raise(OSError)
  146. expected_results = [flexmock(), flexmock()]
  147. flexmock(module).should_receive('make_error_log_records').and_return(
  148. expected_results[:1]
  149. ).and_return(expected_results[1:])
  150. flexmock(module).should_receive('run_actions').and_raise(OSError)
  151. config = {'location': {'repositories': ['foo']}}
  152. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  153. results = list(module.run_configuration('test.yaml', config, arguments))
  154. assert results == expected_results
  155. def test_run_configuration_bails_for_on_error_hook_soft_failure():
  156. flexmock(module.borg_environment).should_receive('initialize')
  157. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  158. error = subprocess.CalledProcessError(borgmatic.hooks.command.SOFT_FAIL_EXIT_CODE, 'try again')
  159. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(error)
  160. expected_results = [flexmock()]
  161. flexmock(module).should_receive('make_error_log_records').and_return(expected_results)
  162. flexmock(module).should_receive('run_actions').and_raise(OSError)
  163. config = {'location': {'repositories': ['foo']}}
  164. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  165. results = list(module.run_configuration('test.yaml', config, arguments))
  166. assert results == expected_results
  167. def test_run_configuration_retries_soft_error():
  168. # Run action first fails, second passes
  169. flexmock(module.borg_environment).should_receive('initialize')
  170. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  171. flexmock(module.command).should_receive('execute_hook')
  172. flexmock(module).should_receive('run_actions').and_raise(OSError).and_return([])
  173. expected_results = [flexmock()]
  174. flexmock(module).should_receive('make_error_log_records').and_return(expected_results).once()
  175. config = {'location': {'repositories': ['foo']}, 'storage': {'retries': 1}}
  176. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  177. results = list(module.run_configuration('test.yaml', config, arguments))
  178. assert results == expected_results
  179. def test_run_configuration_retries_hard_error():
  180. # Run action fails twice
  181. flexmock(module.borg_environment).should_receive('initialize')
  182. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  183. flexmock(module.command).should_receive('execute_hook')
  184. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  185. expected_results = [flexmock(), flexmock()]
  186. flexmock(module).should_receive('make_error_log_records').with_args(
  187. 'foo: Error running actions for repository', OSError
  188. ).and_return(expected_results[:1]).with_args(
  189. 'foo: Error running actions for repository', OSError
  190. ).and_return(
  191. expected_results[1:]
  192. ).twice()
  193. config = {'location': {'repositories': ['foo']}, 'storage': {'retries': 1}}
  194. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  195. results = list(module.run_configuration('test.yaml', config, arguments))
  196. assert results == expected_results
  197. def test_run_repos_ordered():
  198. flexmock(module.borg_environment).should_receive('initialize')
  199. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  200. flexmock(module.command).should_receive('execute_hook')
  201. flexmock(module).should_receive('run_actions').and_raise(OSError).times(2)
  202. expected_results = [flexmock(), flexmock()]
  203. flexmock(module).should_receive('make_error_log_records').with_args(
  204. 'foo: Error running actions for repository', OSError
  205. ).and_return(expected_results[:1]).ordered()
  206. flexmock(module).should_receive('make_error_log_records').with_args(
  207. 'bar: Error running actions for repository', OSError
  208. ).and_return(expected_results[1:]).ordered()
  209. config = {'location': {'repositories': ['foo', 'bar']}}
  210. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  211. results = list(module.run_configuration('test.yaml', config, arguments))
  212. assert results == expected_results
  213. def test_run_configuration_retries_round_robbin():
  214. flexmock(module.borg_environment).should_receive('initialize')
  215. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  216. flexmock(module.command).should_receive('execute_hook')
  217. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  218. expected_results = [flexmock(), flexmock(), flexmock(), flexmock()]
  219. flexmock(module).should_receive('make_error_log_records').with_args(
  220. 'foo: Error running actions for repository', OSError
  221. ).and_return(expected_results[0:1]).ordered()
  222. flexmock(module).should_receive('make_error_log_records').with_args(
  223. 'bar: Error running actions for repository', OSError
  224. ).and_return(expected_results[1:2]).ordered()
  225. flexmock(module).should_receive('make_error_log_records').with_args(
  226. 'foo: Error running actions for repository', OSError
  227. ).and_return(expected_results[2:3]).ordered()
  228. flexmock(module).should_receive('make_error_log_records').with_args(
  229. 'bar: Error running actions for repository', OSError
  230. ).and_return(expected_results[3:4]).ordered()
  231. config = {'location': {'repositories': ['foo', 'bar']}, 'storage': {'retries': 1}}
  232. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  233. results = list(module.run_configuration('test.yaml', config, arguments))
  234. assert results == expected_results
  235. def test_run_configuration_retries_one_passes():
  236. flexmock(module.borg_environment).should_receive('initialize')
  237. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  238. flexmock(module.command).should_receive('execute_hook')
  239. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  240. []
  241. ).and_raise(OSError).times(4)
  242. expected_results = [flexmock(), flexmock(), flexmock()]
  243. flexmock(module).should_receive('make_error_log_records').with_args(
  244. 'foo: Error running actions for repository', OSError
  245. ).and_return(expected_results[0:1]).ordered()
  246. flexmock(module).should_receive('make_error_log_records').with_args(
  247. 'bar: Error running actions for repository', OSError
  248. ).and_return(expected_results[1:2]).ordered()
  249. flexmock(module).should_receive('make_error_log_records').with_args(
  250. 'bar: Error running actions for repository', OSError
  251. ).and_return(expected_results[2:3]).ordered()
  252. config = {'location': {'repositories': ['foo', 'bar']}, 'storage': {'retries': 1}}
  253. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  254. results = list(module.run_configuration('test.yaml', config, arguments))
  255. assert results == expected_results
  256. def test_run_configuration_retry_wait():
  257. flexmock(module.borg_environment).should_receive('initialize')
  258. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  259. flexmock(module.command).should_receive('execute_hook')
  260. flexmock(module).should_receive('run_actions').and_raise(OSError).times(4)
  261. expected_results = [flexmock(), flexmock(), flexmock(), flexmock()]
  262. flexmock(module).should_receive('make_error_log_records').with_args(
  263. 'foo: Error running actions for repository', OSError
  264. ).and_return(expected_results[0:1]).ordered()
  265. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  266. flexmock(module).should_receive('make_error_log_records').with_args(
  267. 'foo: Error running actions for repository', OSError
  268. ).and_return(expected_results[1:2]).ordered()
  269. flexmock(time).should_receive('sleep').with_args(20).and_return().ordered()
  270. flexmock(module).should_receive('make_error_log_records').with_args(
  271. 'foo: Error running actions for repository', OSError
  272. ).and_return(expected_results[2:3]).ordered()
  273. flexmock(time).should_receive('sleep').with_args(30).and_return().ordered()
  274. flexmock(module).should_receive('make_error_log_records').with_args(
  275. 'foo: Error running actions for repository', OSError
  276. ).and_return(expected_results[3:4]).ordered()
  277. config = {'location': {'repositories': ['foo']}, 'storage': {'retries': 3, 'retry_wait': 10}}
  278. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  279. results = list(module.run_configuration('test.yaml', config, arguments))
  280. assert results == expected_results
  281. def test_run_configuration_retries_timeout_multiple_repos():
  282. flexmock(module.borg_environment).should_receive('initialize')
  283. flexmock(module.borg_version).should_receive('local_borg_version').and_return(flexmock())
  284. flexmock(module.command).should_receive('execute_hook')
  285. flexmock(module).should_receive('run_actions').and_raise(OSError).and_raise(OSError).and_return(
  286. []
  287. ).and_raise(OSError).times(4)
  288. expected_results = [flexmock(), flexmock(), flexmock()]
  289. flexmock(module).should_receive('make_error_log_records').with_args(
  290. 'foo: Error running actions for repository', OSError
  291. ).and_return(expected_results[0:1]).ordered()
  292. flexmock(module).should_receive('make_error_log_records').with_args(
  293. 'bar: Error running actions for repository', OSError
  294. ).and_return(expected_results[1:2]).ordered()
  295. # Sleep before retrying foo (and passing)
  296. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  297. # Sleep before retrying bar (and failing)
  298. flexmock(time).should_receive('sleep').with_args(10).and_return().ordered()
  299. flexmock(module).should_receive('make_error_log_records').with_args(
  300. 'bar: Error running actions for repository', OSError
  301. ).and_return(expected_results[2:3]).ordered()
  302. config = {
  303. 'location': {'repositories': ['foo', 'bar']},
  304. 'storage': {'retries': 1, 'retry_wait': 10},
  305. }
  306. arguments = {'global': flexmock(monitoring_verbosity=1, dry_run=False), 'create': flexmock()}
  307. results = list(module.run_configuration('test.yaml', config, arguments))
  308. assert results == expected_results
  309. def test_load_configurations_collects_parsed_configurations():
  310. configuration = flexmock()
  311. other_configuration = flexmock()
  312. flexmock(module.validate).should_receive('parse_configuration').and_return(
  313. configuration
  314. ).and_return(other_configuration)
  315. configs, logs = tuple(module.load_configurations(('test.yaml', 'other.yaml')))
  316. assert configs == {'test.yaml': configuration, 'other.yaml': other_configuration}
  317. assert logs == []
  318. def test_load_configurations_logs_critical_for_parse_error():
  319. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  320. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  321. assert configs == {}
  322. assert {log.levelno for log in logs} == {logging.CRITICAL}
  323. def test_log_record_does_not_raise():
  324. module.log_record(levelno=1, foo='bar', baz='quux')
  325. def test_log_record_with_suppress_does_not_raise():
  326. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  327. def test_make_error_log_records_generates_output_logs_for_message_only():
  328. flexmock(module).should_receive('log_record').replace_with(dict)
  329. logs = tuple(module.make_error_log_records('Error'))
  330. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  331. def test_make_error_log_records_generates_output_logs_for_called_process_error():
  332. flexmock(module).should_receive('log_record').replace_with(dict)
  333. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  334. logs = tuple(
  335. module.make_error_log_records(
  336. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output')
  337. )
  338. )
  339. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  340. assert any(log for log in logs if 'error output' in str(log))
  341. def test_make_error_log_records_generates_logs_for_value_error():
  342. flexmock(module).should_receive('log_record').replace_with(dict)
  343. logs = tuple(module.make_error_log_records('Error', ValueError()))
  344. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  345. def test_make_error_log_records_generates_logs_for_os_error():
  346. flexmock(module).should_receive('log_record').replace_with(dict)
  347. logs = tuple(module.make_error_log_records('Error', OSError()))
  348. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  349. def test_make_error_log_records_generates_nothing_for_other_error():
  350. flexmock(module).should_receive('log_record').replace_with(dict)
  351. logs = tuple(module.make_error_log_records('Error', KeyError()))
  352. assert logs == ()
  353. def test_get_local_path_uses_configuration_value():
  354. assert module.get_local_path({'test.yaml': {'location': {'local_path': 'borg1'}}}) == 'borg1'
  355. def test_get_local_path_without_location_defaults_to_borg():
  356. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  357. def test_get_local_path_without_local_path_defaults_to_borg():
  358. assert module.get_local_path({'test.yaml': {'location': {}}}) == 'borg'
  359. def test_collect_configuration_run_summary_logs_info_for_success():
  360. flexmock(module.command).should_receive('execute_hook').never()
  361. flexmock(module).should_receive('run_configuration').and_return([])
  362. arguments = {}
  363. logs = tuple(
  364. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  365. )
  366. assert {log.levelno for log in logs} == {logging.INFO}
  367. def test_collect_configuration_run_summary_executes_hooks_for_create():
  368. flexmock(module).should_receive('run_configuration').and_return([])
  369. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  370. logs = tuple(
  371. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  372. )
  373. assert {log.levelno for log in logs} == {logging.INFO}
  374. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  375. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  376. flexmock(module).should_receive('run_configuration').and_return([])
  377. arguments = {'extract': flexmock(repository='repo')}
  378. logs = tuple(
  379. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  380. )
  381. assert {log.levelno for log in logs} == {logging.INFO}
  382. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  383. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  384. ValueError
  385. )
  386. expected_logs = (flexmock(),)
  387. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  388. arguments = {'extract': flexmock(repository='repo')}
  389. logs = tuple(
  390. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  391. )
  392. assert logs == expected_logs
  393. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  394. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  395. flexmock(module).should_receive('run_configuration').and_return([])
  396. arguments = {'mount': flexmock(repository='repo')}
  397. logs = tuple(
  398. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  399. )
  400. assert {log.levelno for log in logs} == {logging.INFO}
  401. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  402. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  403. ValueError
  404. )
  405. expected_logs = (flexmock(),)
  406. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  407. arguments = {'mount': flexmock(repository='repo')}
  408. logs = tuple(
  409. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  410. )
  411. assert logs == expected_logs
  412. def test_collect_configuration_run_summary_logs_missing_configs_error():
  413. arguments = {'global': flexmock(config_paths=[])}
  414. expected_logs = (flexmock(),)
  415. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  416. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  417. assert logs == expected_logs
  418. def test_collect_configuration_run_summary_logs_pre_hook_error():
  419. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  420. expected_logs = (flexmock(),)
  421. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  422. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  423. logs = tuple(
  424. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  425. )
  426. assert logs == expected_logs
  427. def test_collect_configuration_run_summary_logs_post_hook_error():
  428. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  429. flexmock(module).should_receive('run_configuration').and_return([])
  430. expected_logs = (flexmock(),)
  431. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  432. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  433. logs = tuple(
  434. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  435. )
  436. assert expected_logs[0] in logs
  437. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  438. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  439. ValueError
  440. )
  441. expected_logs = (flexmock(),)
  442. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  443. arguments = {'list': flexmock(repository='repo', archive='test')}
  444. logs = tuple(
  445. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  446. )
  447. assert logs == expected_logs
  448. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  449. flexmock(module).should_receive('run_configuration').and_return([])
  450. arguments = {'list': flexmock(repository='repo', archive=None)}
  451. logs = tuple(
  452. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  453. )
  454. assert {log.levelno for log in logs} == {logging.INFO}
  455. def test_collect_configuration_run_summary_logs_run_configuration_error():
  456. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  457. flexmock(module).should_receive('run_configuration').and_return(
  458. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  459. )
  460. flexmock(module).should_receive('make_error_log_records').and_return([])
  461. arguments = {}
  462. logs = tuple(
  463. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  464. )
  465. assert {log.levelno for log in logs} == {logging.CRITICAL}
  466. def test_collect_configuration_run_summary_logs_run_umount_error():
  467. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  468. flexmock(module).should_receive('run_configuration').and_return([])
  469. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  470. flexmock(module).should_receive('make_error_log_records').and_return(
  471. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  472. )
  473. arguments = {'umount': flexmock(mount_point='/mnt')}
  474. logs = tuple(
  475. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  476. )
  477. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  478. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  479. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  480. ['baz']
  481. )
  482. stdout = flexmock()
  483. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  484. flexmock(module.sys).stdout = stdout
  485. arguments = {}
  486. tuple(
  487. module.collect_configuration_run_summary_logs(
  488. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  489. )
  490. )