test_borgmatic.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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_warning_for_permission_error():
  319. flexmock(module.validate).should_receive('parse_configuration').and_raise(PermissionError)
  320. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  321. assert configs == {}
  322. assert {log.levelno for log in logs} == {logging.WARNING}
  323. def test_load_configurations_logs_critical_for_parse_error():
  324. flexmock(module.validate).should_receive('parse_configuration').and_raise(ValueError)
  325. configs, logs = tuple(module.load_configurations(('test.yaml',)))
  326. assert configs == {}
  327. assert {log.levelno for log in logs} == {logging.CRITICAL}
  328. def test_log_record_does_not_raise():
  329. module.log_record(levelno=1, foo='bar', baz='quux')
  330. def test_log_record_with_suppress_does_not_raise():
  331. module.log_record(levelno=1, foo='bar', baz='quux', suppress_log=True)
  332. def test_make_error_log_records_generates_output_logs_for_message_only():
  333. flexmock(module).should_receive('log_record').replace_with(dict)
  334. logs = tuple(module.make_error_log_records('Error'))
  335. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  336. def test_make_error_log_records_generates_output_logs_for_called_process_error():
  337. flexmock(module).should_receive('log_record').replace_with(dict)
  338. flexmock(module.logger).should_receive('getEffectiveLevel').and_return(logging.WARNING)
  339. logs = tuple(
  340. module.make_error_log_records(
  341. 'Error', subprocess.CalledProcessError(1, 'ls', 'error output')
  342. )
  343. )
  344. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  345. assert any(log for log in logs if 'error output' in str(log))
  346. def test_make_error_log_records_generates_logs_for_value_error():
  347. flexmock(module).should_receive('log_record').replace_with(dict)
  348. logs = tuple(module.make_error_log_records('Error', ValueError()))
  349. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  350. def test_make_error_log_records_generates_logs_for_os_error():
  351. flexmock(module).should_receive('log_record').replace_with(dict)
  352. logs = tuple(module.make_error_log_records('Error', OSError()))
  353. assert {log['levelno'] for log in logs} == {logging.CRITICAL}
  354. def test_make_error_log_records_generates_nothing_for_other_error():
  355. flexmock(module).should_receive('log_record').replace_with(dict)
  356. logs = tuple(module.make_error_log_records('Error', KeyError()))
  357. assert logs == ()
  358. def test_get_local_path_uses_configuration_value():
  359. assert module.get_local_path({'test.yaml': {'location': {'local_path': 'borg1'}}}) == 'borg1'
  360. def test_get_local_path_without_location_defaults_to_borg():
  361. assert module.get_local_path({'test.yaml': {}}) == 'borg'
  362. def test_get_local_path_without_local_path_defaults_to_borg():
  363. assert module.get_local_path({'test.yaml': {'location': {}}}) == 'borg'
  364. def test_collect_configuration_run_summary_logs_info_for_success():
  365. flexmock(module.command).should_receive('execute_hook').never()
  366. flexmock(module).should_receive('run_configuration').and_return([])
  367. arguments = {}
  368. logs = tuple(
  369. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  370. )
  371. assert {log.levelno for log in logs} == {logging.INFO}
  372. def test_collect_configuration_run_summary_executes_hooks_for_create():
  373. flexmock(module).should_receive('run_configuration').and_return([])
  374. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  375. logs = tuple(
  376. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  377. )
  378. assert {log.levelno for log in logs} == {logging.INFO}
  379. def test_collect_configuration_run_summary_logs_info_for_success_with_extract():
  380. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  381. flexmock(module).should_receive('run_configuration').and_return([])
  382. arguments = {'extract': flexmock(repository='repo')}
  383. logs = tuple(
  384. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  385. )
  386. assert {log.levelno for log in logs} == {logging.INFO}
  387. def test_collect_configuration_run_summary_logs_extract_with_repository_error():
  388. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  389. ValueError
  390. )
  391. expected_logs = (flexmock(),)
  392. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  393. arguments = {'extract': flexmock(repository='repo')}
  394. logs = tuple(
  395. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  396. )
  397. assert logs == expected_logs
  398. def test_collect_configuration_run_summary_logs_info_for_success_with_mount():
  399. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  400. flexmock(module).should_receive('run_configuration').and_return([])
  401. arguments = {'mount': flexmock(repository='repo')}
  402. logs = tuple(
  403. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  404. )
  405. assert {log.levelno for log in logs} == {logging.INFO}
  406. def test_collect_configuration_run_summary_logs_mount_with_repository_error():
  407. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  408. ValueError
  409. )
  410. expected_logs = (flexmock(),)
  411. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  412. arguments = {'mount': flexmock(repository='repo')}
  413. logs = tuple(
  414. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  415. )
  416. assert logs == expected_logs
  417. def test_collect_configuration_run_summary_logs_missing_configs_error():
  418. arguments = {'global': flexmock(config_paths=[])}
  419. expected_logs = (flexmock(),)
  420. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  421. logs = tuple(module.collect_configuration_run_summary_logs({}, arguments=arguments))
  422. assert logs == expected_logs
  423. def test_collect_configuration_run_summary_logs_pre_hook_error():
  424. flexmock(module.command).should_receive('execute_hook').and_raise(ValueError)
  425. expected_logs = (flexmock(),)
  426. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  427. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  428. logs = tuple(
  429. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  430. )
  431. assert logs == expected_logs
  432. def test_collect_configuration_run_summary_logs_post_hook_error():
  433. flexmock(module.command).should_receive('execute_hook').and_return(None).and_raise(ValueError)
  434. flexmock(module).should_receive('run_configuration').and_return([])
  435. expected_logs = (flexmock(),)
  436. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  437. arguments = {'create': flexmock(), 'global': flexmock(monitoring_verbosity=1, dry_run=False)}
  438. logs = tuple(
  439. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  440. )
  441. assert expected_logs[0] in logs
  442. def test_collect_configuration_run_summary_logs_for_list_with_archive_and_repository_error():
  443. flexmock(module.validate).should_receive('guard_configuration_contains_repository').and_raise(
  444. ValueError
  445. )
  446. expected_logs = (flexmock(),)
  447. flexmock(module).should_receive('make_error_log_records').and_return(expected_logs)
  448. arguments = {'list': flexmock(repository='repo', archive='test')}
  449. logs = tuple(
  450. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  451. )
  452. assert logs == expected_logs
  453. def test_collect_configuration_run_summary_logs_info_for_success_with_list():
  454. flexmock(module).should_receive('run_configuration').and_return([])
  455. arguments = {'list': flexmock(repository='repo', archive=None)}
  456. logs = tuple(
  457. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  458. )
  459. assert {log.levelno for log in logs} == {logging.INFO}
  460. def test_collect_configuration_run_summary_logs_run_configuration_error():
  461. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  462. flexmock(module).should_receive('run_configuration').and_return(
  463. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  464. )
  465. flexmock(module).should_receive('make_error_log_records').and_return([])
  466. arguments = {}
  467. logs = tuple(
  468. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  469. )
  470. assert {log.levelno for log in logs} == {logging.CRITICAL}
  471. def test_collect_configuration_run_summary_logs_run_umount_error():
  472. flexmock(module.validate).should_receive('guard_configuration_contains_repository')
  473. flexmock(module).should_receive('run_configuration').and_return([])
  474. flexmock(module.borg_umount).should_receive('unmount_archive').and_raise(OSError)
  475. flexmock(module).should_receive('make_error_log_records').and_return(
  476. [logging.makeLogRecord(dict(levelno=logging.CRITICAL, levelname='CRITICAL', msg='Error'))]
  477. )
  478. arguments = {'umount': flexmock(mount_point='/mnt')}
  479. logs = tuple(
  480. module.collect_configuration_run_summary_logs({'test.yaml': {}}, arguments=arguments)
  481. )
  482. assert {log.levelno for log in logs} == {logging.INFO, logging.CRITICAL}
  483. def test_collect_configuration_run_summary_logs_outputs_merged_json_results():
  484. flexmock(module).should_receive('run_configuration').and_return(['foo', 'bar']).and_return(
  485. ['baz']
  486. )
  487. stdout = flexmock()
  488. stdout.should_receive('write').with_args('["foo", "bar", "baz"]').once()
  489. flexmock(module.sys).stdout = stdout
  490. arguments = {}
  491. tuple(
  492. module.collect_configuration_run_summary_logs(
  493. {'test.yaml': {}, 'test2.yaml': {}}, arguments=arguments
  494. )
  495. )