test_check.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import check as module
  5. from ..test_verbosity import insert_logging_mock
  6. def insert_execute_command_mock(command, working_directory=None, borg_exit_codes=None):
  7. flexmock(module.environment).should_receive('make_environment')
  8. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  9. working_directory,
  10. )
  11. flexmock(module).should_receive('execute_command').with_args(
  12. command,
  13. extra_environment=None,
  14. working_directory=working_directory,
  15. borg_local_path=command[0],
  16. borg_exit_codes=borg_exit_codes,
  17. ).once()
  18. def insert_execute_command_never():
  19. flexmock(module).should_receive('execute_command').never()
  20. def test_make_archive_filter_flags_with_default_checks_and_prefix_returns_default_flags():
  21. flexmock(module.feature).should_receive('available').and_return(True)
  22. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  23. flags = module.make_archive_filter_flags(
  24. '1.2.3',
  25. {'prefix': 'foo'},
  26. ('repository', 'archives'),
  27. check_arguments=flexmock(match_archives=None),
  28. )
  29. assert flags == ('--match-archives', 'sh:foo*')
  30. def test_make_archive_filter_flags_with_all_checks_and_prefix_returns_default_flags():
  31. flexmock(module.feature).should_receive('available').and_return(True)
  32. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  33. flags = module.make_archive_filter_flags(
  34. '1.2.3',
  35. {'prefix': 'foo'},
  36. ('repository', 'archives', 'extract'),
  37. check_arguments=flexmock(match_archives=None),
  38. )
  39. assert flags == ('--match-archives', 'sh:foo*')
  40. def test_make_archive_filter_flags_with_all_checks_and_prefix_without_borg_features_returns_glob_archives_flags():
  41. flexmock(module.feature).should_receive('available').and_return(False)
  42. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  43. flags = module.make_archive_filter_flags(
  44. '1.2.3',
  45. {'prefix': 'foo'},
  46. ('repository', 'archives', 'extract'),
  47. check_arguments=flexmock(match_archives=None),
  48. )
  49. assert flags == ('--glob-archives', 'foo*')
  50. def test_make_archive_filter_flags_with_archives_check_and_last_includes_last_flag():
  51. flexmock(module.feature).should_receive('available').and_return(True)
  52. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  53. flags = module.make_archive_filter_flags(
  54. '1.2.3',
  55. {'check_last': 3},
  56. ('archives',),
  57. check_arguments=flexmock(match_archives=None),
  58. )
  59. assert flags == ('--last', '3')
  60. def test_make_archive_filter_flags_with_data_check_and_last_includes_last_flag():
  61. flexmock(module.feature).should_receive('available').and_return(True)
  62. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  63. flags = module.make_archive_filter_flags(
  64. '1.2.3',
  65. {'check_last': 3},
  66. ('data',),
  67. check_arguments=flexmock(match_archives=None),
  68. )
  69. assert flags == ('--last', '3')
  70. def test_make_archive_filter_flags_with_repository_check_and_last_omits_last_flag():
  71. flexmock(module.feature).should_receive('available').and_return(True)
  72. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  73. flags = module.make_archive_filter_flags(
  74. '1.2.3',
  75. {'check_last': 3},
  76. ('repository',),
  77. check_arguments=flexmock(match_archives=None),
  78. )
  79. assert flags == ()
  80. def test_make_archive_filter_flags_with_default_checks_and_last_includes_last_flag():
  81. flexmock(module.feature).should_receive('available').and_return(True)
  82. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  83. flags = module.make_archive_filter_flags(
  84. '1.2.3',
  85. {'check_last': 3},
  86. ('repository', 'archives'),
  87. check_arguments=flexmock(match_archives=None),
  88. )
  89. assert flags == ('--last', '3')
  90. def test_make_archive_filter_flags_with_archives_check_and_prefix_includes_match_archives_flag():
  91. flexmock(module.feature).should_receive('available').and_return(True)
  92. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  93. flags = module.make_archive_filter_flags(
  94. '1.2.3',
  95. {'prefix': 'foo-'},
  96. ('archives',),
  97. check_arguments=flexmock(match_archives=None),
  98. )
  99. assert flags == ('--match-archives', 'sh:foo-*')
  100. def test_make_archive_filter_flags_with_data_check_and_prefix_includes_match_archives_flag():
  101. flexmock(module.feature).should_receive('available').and_return(True)
  102. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  103. flags = module.make_archive_filter_flags(
  104. '1.2.3',
  105. {'prefix': 'foo-'},
  106. ('data',),
  107. check_arguments=flexmock(match_archives=None),
  108. )
  109. assert flags == ('--match-archives', 'sh:foo-*')
  110. def test_make_archive_filter_flags_prefers_check_arguments_match_archives_to_config_match_archives():
  111. flexmock(module.feature).should_receive('available').and_return(True)
  112. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  113. 'baz-*', None, '1.2.3'
  114. ).and_return(('--match-archives', 'sh:baz-*'))
  115. flags = module.make_archive_filter_flags(
  116. '1.2.3',
  117. {'match_archives': 'bar-{now}', 'prefix': ''}, # noqa: FS003
  118. ('archives',),
  119. check_arguments=flexmock(match_archives='baz-*'),
  120. )
  121. assert flags == ('--match-archives', 'sh:baz-*')
  122. def test_make_archive_filter_flags_with_archives_check_and_empty_prefix_uses_archive_name_format_instead():
  123. flexmock(module.feature).should_receive('available').and_return(True)
  124. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  125. None, 'bar-{now}', '1.2.3' # noqa: FS003
  126. ).and_return(('--match-archives', 'sh:bar-*'))
  127. flags = module.make_archive_filter_flags(
  128. '1.2.3',
  129. {'archive_name_format': 'bar-{now}', 'prefix': ''}, # noqa: FS003
  130. ('archives',),
  131. check_arguments=flexmock(match_archives=None),
  132. )
  133. assert flags == ('--match-archives', 'sh:bar-*')
  134. def test_make_archive_filter_flags_with_archives_check_and_none_prefix_omits_match_archives_flag():
  135. flexmock(module.feature).should_receive('available').and_return(True)
  136. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  137. flags = module.make_archive_filter_flags(
  138. '1.2.3',
  139. {},
  140. ('archives',),
  141. check_arguments=flexmock(match_archives=None),
  142. )
  143. assert flags == ()
  144. def test_make_archive_filter_flags_with_repository_check_and_prefix_omits_match_archives_flag():
  145. flexmock(module.feature).should_receive('available').and_return(True)
  146. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  147. flags = module.make_archive_filter_flags(
  148. '1.2.3',
  149. {'prefix': 'foo-'},
  150. ('repository',),
  151. check_arguments=flexmock(match_archives=None),
  152. )
  153. assert flags == ()
  154. def test_make_archive_filter_flags_with_default_checks_and_prefix_includes_match_archives_flag():
  155. flexmock(module.feature).should_receive('available').and_return(True)
  156. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  157. flags = module.make_archive_filter_flags(
  158. '1.2.3',
  159. {'prefix': 'foo-'},
  160. ('repository', 'archives'),
  161. check_arguments=flexmock(match_archives=None),
  162. )
  163. assert flags == ('--match-archives', 'sh:foo-*')
  164. def test_make_check_name_flags_with_repository_check_returns_flag():
  165. flags = module.make_check_name_flags({'repository'}, ())
  166. assert flags == ('--repository-only',)
  167. def test_make_check_name_flags_with_archives_check_returns_flag():
  168. flags = module.make_check_name_flags({'archives'}, ())
  169. assert flags == ('--archives-only',)
  170. def test_make_check_name_flags_with_archives_check_and_archive_filter_flags_includes_those_flags():
  171. flags = module.make_check_name_flags({'archives'}, ('--match-archives', 'sh:foo-*'))
  172. assert flags == ('--archives-only', '--match-archives', 'sh:foo-*')
  173. def test_make_check_name_flags_without_archives_check_and_with_archive_filter_flags_includes_those_flags():
  174. flags = module.make_check_name_flags({'repository'}, ('--match-archives', 'sh:foo-*'))
  175. assert flags == ('--repository-only',)
  176. def test_make_check_name_flags_with_data_check_returns_flag_and_implies_archives():
  177. flexmock(module.feature).should_receive('available').and_return(True)
  178. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  179. flags = module.make_check_name_flags({'data'}, ())
  180. assert flags == (
  181. '--archives-only',
  182. '--verify-data',
  183. )
  184. def test_make_check_name_flags_with_extract_omits_extract_flag():
  185. flexmock(module.feature).should_receive('available').and_return(True)
  186. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  187. flags = module.make_check_name_flags({'extract'}, ())
  188. assert flags == ()
  189. def test_make_check_name_flags_with_repository_and_data_checks_does_not_return_repository_only():
  190. flexmock(module.feature).should_receive('available').and_return(True)
  191. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  192. flags = module.make_check_name_flags(
  193. {
  194. 'repository',
  195. 'data',
  196. },
  197. (),
  198. )
  199. assert flags == ('--verify-data',)
  200. def test_get_repository_id_with_valid_json_does_not_raise():
  201. config = {}
  202. flexmock(module.repo_info).should_receive('display_repository_info').and_return(
  203. '{"repository": {"id": "repo"}}'
  204. )
  205. assert module.get_repository_id(
  206. repository_path='repo',
  207. config=config,
  208. local_borg_version='1.2.3',
  209. global_arguments=flexmock(log_json=False),
  210. local_path='borg',
  211. remote_path=None,
  212. )
  213. def test_get_repository_id_with_json_error_raises():
  214. config = {}
  215. flexmock(module.repo_info).should_receive('display_repository_info').and_return(
  216. '{"unexpected": {"id": "repo"}}'
  217. )
  218. with pytest.raises(ValueError):
  219. module.get_repository_id(
  220. repository_path='repo',
  221. config=config,
  222. local_borg_version='1.2.3',
  223. global_arguments=flexmock(log_json=False),
  224. local_path='borg',
  225. remote_path=None,
  226. )
  227. def test_get_repository_id_with_missing_json_keys_raises():
  228. config = {}
  229. flexmock(module.repo_info).should_receive('display_repository_info').and_return('{invalid JSON')
  230. with pytest.raises(ValueError):
  231. module.get_repository_id(
  232. repository_path='repo',
  233. config=config,
  234. local_borg_version='1.2.3',
  235. global_arguments=flexmock(log_json=False),
  236. local_path='borg',
  237. remote_path=None,
  238. )
  239. def test_check_archives_with_progress_passes_through_to_borg():
  240. config = {}
  241. flexmock(module).should_receive('make_check_name_flags').and_return(())
  242. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  243. flexmock(module.environment).should_receive('make_environment')
  244. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  245. flexmock(module).should_receive('execute_command').with_args(
  246. ('borg', 'check', '--progress', 'repo'),
  247. output_file=module.DO_NOT_CAPTURE,
  248. extra_environment=None,
  249. working_directory=None,
  250. borg_local_path='borg',
  251. borg_exit_codes=None,
  252. ).once()
  253. module.check_archives(
  254. repository_path='repo',
  255. config=config,
  256. local_borg_version='1.2.3',
  257. check_arguments=flexmock(
  258. progress=True,
  259. repair=None,
  260. only_checks=None,
  261. force=None,
  262. match_archives=None,
  263. max_duration=None,
  264. ),
  265. global_arguments=flexmock(log_json=False),
  266. checks={'repository'},
  267. archive_filter_flags=(),
  268. )
  269. def test_check_archives_with_repair_passes_through_to_borg():
  270. config = {}
  271. flexmock(module).should_receive('make_check_name_flags').and_return(())
  272. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  273. flexmock(module.environment).should_receive('make_environment')
  274. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  275. flexmock(module).should_receive('execute_command').with_args(
  276. ('borg', 'check', '--repair', 'repo'),
  277. output_file=module.DO_NOT_CAPTURE,
  278. extra_environment=None,
  279. working_directory=None,
  280. borg_local_path='borg',
  281. borg_exit_codes=None,
  282. ).once()
  283. module.check_archives(
  284. repository_path='repo',
  285. config=config,
  286. local_borg_version='1.2.3',
  287. check_arguments=flexmock(
  288. progress=None,
  289. repair=True,
  290. only_checks=None,
  291. force=None,
  292. match_archives=None,
  293. max_duration=None,
  294. ),
  295. global_arguments=flexmock(log_json=False),
  296. checks={'repository'},
  297. archive_filter_flags=(),
  298. )
  299. def test_check_archives_with_max_duration_flag_passes_through_to_borg():
  300. config = {}
  301. flexmock(module).should_receive('make_check_name_flags').and_return(())
  302. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  303. flexmock(module.environment).should_receive('make_environment')
  304. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  305. flexmock(module).should_receive('execute_command').with_args(
  306. ('borg', 'check', '--max-duration', '33', 'repo'),
  307. extra_environment=None,
  308. working_directory=None,
  309. borg_local_path='borg',
  310. borg_exit_codes=None,
  311. ).once()
  312. module.check_archives(
  313. repository_path='repo',
  314. config=config,
  315. local_borg_version='1.2.3',
  316. check_arguments=flexmock(
  317. progress=None,
  318. repair=None,
  319. only_checks=None,
  320. force=None,
  321. match_archives=None,
  322. max_duration=33,
  323. ),
  324. global_arguments=flexmock(log_json=False),
  325. checks={'repository'},
  326. archive_filter_flags=(),
  327. )
  328. def test_check_archives_with_max_duration_flag_and_archives_check_errors():
  329. config = {}
  330. flexmock(module).should_receive('execute_command').never()
  331. with pytest.raises(ValueError):
  332. module.check_archives(
  333. repository_path='repo',
  334. config=config,
  335. local_borg_version='1.2.3',
  336. check_arguments=flexmock(
  337. progress=None,
  338. repair=None,
  339. only_checks=None,
  340. force=None,
  341. match_archives=None,
  342. max_duration=33,
  343. ),
  344. global_arguments=flexmock(log_json=False),
  345. checks={'repository', 'archives'},
  346. archive_filter_flags=(),
  347. )
  348. def test_check_archives_with_max_duration_option_passes_through_to_borg():
  349. config = {'checks': [{'name': 'repository', 'max_duration': 33}]}
  350. flexmock(module).should_receive('make_check_name_flags').and_return(())
  351. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  352. flexmock(module.environment).should_receive('make_environment')
  353. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  354. flexmock(module).should_receive('execute_command').with_args(
  355. ('borg', 'check', '--max-duration', '33', 'repo'),
  356. extra_environment=None,
  357. working_directory=None,
  358. borg_local_path='borg',
  359. borg_exit_codes=None,
  360. ).once()
  361. module.check_archives(
  362. repository_path='repo',
  363. config=config,
  364. local_borg_version='1.2.3',
  365. check_arguments=flexmock(
  366. progress=None,
  367. repair=None,
  368. only_checks=None,
  369. force=None,
  370. match_archives=None,
  371. max_duration=None,
  372. ),
  373. global_arguments=flexmock(log_json=False),
  374. checks={'repository'},
  375. archive_filter_flags=(),
  376. )
  377. def test_check_archives_with_max_duration_option_and_archives_check_errors():
  378. config = {'checks': [{'name': 'repository', 'max_duration': 33}]}
  379. flexmock(module).should_receive('execute_command').never()
  380. with pytest.raises(ValueError):
  381. module.check_archives(
  382. repository_path='repo',
  383. config=config,
  384. local_borg_version='1.2.3',
  385. check_arguments=flexmock(
  386. progress=None,
  387. repair=None,
  388. only_checks=None,
  389. force=None,
  390. match_archives=None,
  391. max_duration=None,
  392. ),
  393. global_arguments=flexmock(log_json=False),
  394. checks={'repository', 'archives'},
  395. archive_filter_flags=(),
  396. )
  397. def test_check_archives_with_max_duration_flag_overrides_max_duration_option():
  398. config = {'checks': [{'name': 'repository', 'max_duration': 33}]}
  399. flexmock(module).should_receive('make_check_name_flags').and_return(())
  400. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  401. flexmock(module.environment).should_receive('make_environment')
  402. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  403. flexmock(module).should_receive('execute_command').with_args(
  404. ('borg', 'check', '--max-duration', '44', 'repo'),
  405. extra_environment=None,
  406. working_directory=None,
  407. borg_local_path='borg',
  408. borg_exit_codes=None,
  409. ).once()
  410. module.check_archives(
  411. repository_path='repo',
  412. config=config,
  413. local_borg_version='1.2.3',
  414. check_arguments=flexmock(
  415. progress=None,
  416. repair=None,
  417. only_checks=None,
  418. force=None,
  419. match_archives=None,
  420. max_duration=44,
  421. ),
  422. global_arguments=flexmock(log_json=False),
  423. checks={'repository'},
  424. archive_filter_flags=(),
  425. )
  426. @pytest.mark.parametrize(
  427. 'checks',
  428. (
  429. ('repository',),
  430. ('archives',),
  431. ('repository', 'archives'),
  432. ('repository', 'archives', 'other'),
  433. ),
  434. )
  435. def test_check_archives_calls_borg_with_parameters(checks):
  436. config = {}
  437. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  438. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  439. insert_execute_command_mock(('borg', 'check', 'repo'))
  440. module.check_archives(
  441. repository_path='repo',
  442. config=config,
  443. local_borg_version='1.2.3',
  444. check_arguments=flexmock(
  445. progress=None,
  446. repair=None,
  447. only_checks=None,
  448. force=None,
  449. match_archives=None,
  450. max_duration=None,
  451. ),
  452. global_arguments=flexmock(log_json=False),
  453. checks=checks,
  454. archive_filter_flags=(),
  455. )
  456. def test_check_archives_with_log_info_passes_through_to_borg():
  457. config = {}
  458. flexmock(module).should_receive('make_check_name_flags').and_return(())
  459. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  460. insert_logging_mock(logging.INFO)
  461. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  462. module.check_archives(
  463. repository_path='repo',
  464. config=config,
  465. local_borg_version='1.2.3',
  466. check_arguments=flexmock(
  467. progress=None,
  468. repair=None,
  469. only_checks=None,
  470. force=None,
  471. match_archives=None,
  472. max_duration=None,
  473. ),
  474. global_arguments=flexmock(log_json=False),
  475. checks={'repository'},
  476. archive_filter_flags=(),
  477. )
  478. def test_check_archives_with_log_debug_passes_through_to_borg():
  479. config = {}
  480. flexmock(module).should_receive('make_check_name_flags').and_return(())
  481. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  482. insert_logging_mock(logging.DEBUG)
  483. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', 'repo'))
  484. module.check_archives(
  485. repository_path='repo',
  486. config=config,
  487. local_borg_version='1.2.3',
  488. check_arguments=flexmock(
  489. progress=None,
  490. repair=None,
  491. only_checks=None,
  492. force=None,
  493. match_archives=None,
  494. max_duration=None,
  495. ),
  496. global_arguments=flexmock(log_json=False),
  497. checks={'repository'},
  498. archive_filter_flags=(),
  499. )
  500. def test_check_archives_with_local_path_calls_borg_via_local_path():
  501. checks = {'repository'}
  502. config = {}
  503. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  504. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  505. insert_execute_command_mock(('borg1', 'check', 'repo'))
  506. module.check_archives(
  507. repository_path='repo',
  508. config=config,
  509. local_borg_version='1.2.3',
  510. check_arguments=flexmock(
  511. progress=None,
  512. repair=None,
  513. only_checks=None,
  514. force=None,
  515. match_archives=None,
  516. max_duration=None,
  517. ),
  518. global_arguments=flexmock(log_json=False),
  519. checks=checks,
  520. archive_filter_flags=(),
  521. local_path='borg1',
  522. )
  523. def test_check_archives_with_exit_codes_calls_borg_using_them():
  524. checks = {'repository'}
  525. borg_exit_codes = flexmock()
  526. config = {'borg_exit_codes': borg_exit_codes}
  527. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  528. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  529. insert_execute_command_mock(('borg', 'check', 'repo'), borg_exit_codes=borg_exit_codes)
  530. module.check_archives(
  531. repository_path='repo',
  532. config=config,
  533. local_borg_version='1.2.3',
  534. check_arguments=flexmock(
  535. progress=None,
  536. repair=None,
  537. only_checks=None,
  538. force=None,
  539. match_archives=None,
  540. max_duration=None,
  541. ),
  542. global_arguments=flexmock(log_json=False),
  543. checks=checks,
  544. archive_filter_flags=(),
  545. )
  546. def test_check_archives_with_remote_path_passes_through_to_borg():
  547. checks = {'repository'}
  548. config = {}
  549. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  550. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  551. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  552. module.check_archives(
  553. repository_path='repo',
  554. config=config,
  555. local_borg_version='1.2.3',
  556. check_arguments=flexmock(
  557. progress=None,
  558. repair=None,
  559. only_checks=None,
  560. force=None,
  561. match_archives=None,
  562. max_duration=None,
  563. ),
  564. global_arguments=flexmock(log_json=False),
  565. checks=checks,
  566. archive_filter_flags=(),
  567. remote_path='borg1',
  568. )
  569. def test_check_archives_with_log_json_passes_through_to_borg():
  570. checks = {'repository'}
  571. config = {}
  572. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  573. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  574. insert_execute_command_mock(('borg', 'check', '--log-json', 'repo'))
  575. module.check_archives(
  576. repository_path='repo',
  577. config=config,
  578. local_borg_version='1.2.3',
  579. check_arguments=flexmock(
  580. progress=None,
  581. repair=None,
  582. only_checks=None,
  583. force=None,
  584. match_archives=None,
  585. max_duration=None,
  586. ),
  587. global_arguments=flexmock(log_json=True),
  588. checks=checks,
  589. archive_filter_flags=(),
  590. )
  591. def test_check_archives_with_lock_wait_passes_through_to_borg():
  592. checks = {'repository'}
  593. config = {'lock_wait': 5}
  594. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  595. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  596. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  597. module.check_archives(
  598. repository_path='repo',
  599. config=config,
  600. local_borg_version='1.2.3',
  601. check_arguments=flexmock(
  602. progress=None,
  603. repair=None,
  604. only_checks=None,
  605. force=None,
  606. match_archives=None,
  607. max_duration=None,
  608. ),
  609. global_arguments=flexmock(log_json=False),
  610. checks=checks,
  611. archive_filter_flags=(),
  612. )
  613. def test_check_archives_with_retention_prefix():
  614. checks = {'repository'}
  615. prefix = 'foo-'
  616. config = {'prefix': prefix}
  617. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  618. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  619. insert_execute_command_mock(('borg', 'check', 'repo'))
  620. module.check_archives(
  621. repository_path='repo',
  622. config=config,
  623. local_borg_version='1.2.3',
  624. check_arguments=flexmock(
  625. progress=None,
  626. repair=None,
  627. only_checks=None,
  628. force=None,
  629. match_archives=None,
  630. max_duration=None,
  631. ),
  632. global_arguments=flexmock(log_json=False),
  633. checks=checks,
  634. archive_filter_flags=(),
  635. )
  636. def test_check_archives_with_extra_borg_options_passes_through_to_borg():
  637. config = {'extra_borg_options': {'check': '--extra --options'}}
  638. flexmock(module).should_receive('make_check_name_flags').and_return(())
  639. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  640. insert_execute_command_mock(('borg', 'check', '--extra', '--options', 'repo'))
  641. module.check_archives(
  642. repository_path='repo',
  643. config=config,
  644. local_borg_version='1.2.3',
  645. check_arguments=flexmock(
  646. progress=None,
  647. repair=None,
  648. only_checks=None,
  649. force=None,
  650. match_archives=None,
  651. max_duration=None,
  652. ),
  653. global_arguments=flexmock(log_json=False),
  654. checks={'repository'},
  655. archive_filter_flags=(),
  656. )
  657. def test_check_archives_with_match_archives_passes_through_to_borg():
  658. config = {}
  659. flexmock(module).should_receive('make_check_name_flags').and_return(
  660. ('--match-archives', 'foo-*')
  661. )
  662. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  663. flexmock(module.environment).should_receive('make_environment')
  664. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  665. flexmock(module).should_receive('execute_command').with_args(
  666. ('borg', 'check', '--match-archives', 'foo-*', 'repo'),
  667. extra_environment=None,
  668. working_directory=None,
  669. borg_local_path='borg',
  670. borg_exit_codes=None,
  671. ).once()
  672. module.check_archives(
  673. repository_path='repo',
  674. config=config,
  675. local_borg_version='1.2.3',
  676. check_arguments=flexmock(
  677. progress=None,
  678. repair=None,
  679. only_checks=None,
  680. force=None,
  681. match_archives='foo-*',
  682. max_duration=None,
  683. ),
  684. global_arguments=flexmock(log_json=False),
  685. checks={'archives'},
  686. archive_filter_flags=('--match-archives', 'foo-*'),
  687. )
  688. def test_check_archives_calls_borg_with_working_directory():
  689. config = {'working_directory': '/working/dir'}
  690. flexmock(module).should_receive('make_check_name_flags').and_return(())
  691. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  692. flexmock(module.environment).should_receive('make_environment')
  693. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  694. insert_execute_command_mock(('borg', 'check', 'repo'), working_directory='/working/dir')
  695. module.check_archives(
  696. repository_path='repo',
  697. config=config,
  698. local_borg_version='1.2.3',
  699. check_arguments=flexmock(
  700. progress=False,
  701. repair=None,
  702. only_checks=None,
  703. force=None,
  704. match_archives=None,
  705. max_duration=None,
  706. ),
  707. global_arguments=flexmock(log_json=False),
  708. checks={'repository'},
  709. archive_filter_flags=(),
  710. )