test_borgmatic.py 43 KB

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