test_check.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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.options).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.options).should_receive('get_working_directory').and_return(
  245. None
  246. )
  247. flexmock(module).should_receive('execute_command').with_args(
  248. ('borg', 'check', '--progress', 'repo'),
  249. output_file=module.DO_NOT_CAPTURE,
  250. extra_environment=None,
  251. working_directory=None,
  252. borg_local_path='borg',
  253. borg_exit_codes=None,
  254. ).once()
  255. module.check_archives(
  256. repository_path='repo',
  257. config=config,
  258. local_borg_version='1.2.3',
  259. check_arguments=flexmock(
  260. progress=True,
  261. repair=None,
  262. only_checks=None,
  263. force=None,
  264. match_archives=None,
  265. max_duration=None,
  266. ),
  267. global_arguments=flexmock(log_json=False),
  268. checks={'repository'},
  269. archive_filter_flags=(),
  270. )
  271. def test_check_archives_with_repair_passes_through_to_borg():
  272. config = {}
  273. flexmock(module).should_receive('make_check_name_flags').and_return(())
  274. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  275. flexmock(module.environment).should_receive('make_environment')
  276. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  277. None
  278. )
  279. flexmock(module).should_receive('execute_command').with_args(
  280. ('borg', 'check', '--repair', 'repo'),
  281. output_file=module.DO_NOT_CAPTURE,
  282. extra_environment=None,
  283. working_directory=None,
  284. borg_local_path='borg',
  285. borg_exit_codes=None,
  286. ).once()
  287. module.check_archives(
  288. repository_path='repo',
  289. config=config,
  290. local_borg_version='1.2.3',
  291. check_arguments=flexmock(
  292. progress=None,
  293. repair=True,
  294. only_checks=None,
  295. force=None,
  296. match_archives=None,
  297. max_duration=None,
  298. ),
  299. global_arguments=flexmock(log_json=False),
  300. checks={'repository'},
  301. archive_filter_flags=(),
  302. )
  303. def test_check_archives_with_max_duration_flag_passes_through_to_borg():
  304. config = {}
  305. flexmock(module).should_receive('make_check_name_flags').and_return(())
  306. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  307. flexmock(module.environment).should_receive('make_environment')
  308. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  309. None
  310. )
  311. flexmock(module).should_receive('execute_command').with_args(
  312. ('borg', 'check', '--max-duration', '33', 'repo'),
  313. extra_environment=None,
  314. working_directory=None,
  315. borg_local_path='borg',
  316. borg_exit_codes=None,
  317. ).once()
  318. module.check_archives(
  319. repository_path='repo',
  320. config=config,
  321. local_borg_version='1.2.3',
  322. check_arguments=flexmock(
  323. progress=None,
  324. repair=None,
  325. only_checks=None,
  326. force=None,
  327. match_archives=None,
  328. max_duration=33,
  329. ),
  330. global_arguments=flexmock(log_json=False),
  331. checks={'repository'},
  332. archive_filter_flags=(),
  333. )
  334. def test_check_archives_with_max_duration_flag_and_archives_check_errors():
  335. config = {}
  336. flexmock(module).should_receive('execute_command').never()
  337. with pytest.raises(ValueError):
  338. module.check_archives(
  339. repository_path='repo',
  340. config=config,
  341. local_borg_version='1.2.3',
  342. check_arguments=flexmock(
  343. progress=None,
  344. repair=None,
  345. only_checks=None,
  346. force=None,
  347. match_archives=None,
  348. max_duration=33,
  349. ),
  350. global_arguments=flexmock(log_json=False),
  351. checks={'repository', 'archives'},
  352. archive_filter_flags=(),
  353. )
  354. def test_check_archives_with_max_duration_option_passes_through_to_borg():
  355. config = {'checks': [{'name': 'repository', 'max_duration': 33}]}
  356. flexmock(module).should_receive('make_check_name_flags').and_return(())
  357. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  358. flexmock(module.environment).should_receive('make_environment')
  359. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  360. None
  361. )
  362. flexmock(module).should_receive('execute_command').with_args(
  363. ('borg', 'check', '--max-duration', '33', 'repo'),
  364. extra_environment=None,
  365. working_directory=None,
  366. borg_local_path='borg',
  367. borg_exit_codes=None,
  368. ).once()
  369. module.check_archives(
  370. repository_path='repo',
  371. config=config,
  372. local_borg_version='1.2.3',
  373. check_arguments=flexmock(
  374. progress=None,
  375. repair=None,
  376. only_checks=None,
  377. force=None,
  378. match_archives=None,
  379. max_duration=None,
  380. ),
  381. global_arguments=flexmock(log_json=False),
  382. checks={'repository'},
  383. archive_filter_flags=(),
  384. )
  385. def test_check_archives_with_max_duration_option_and_archives_check_errors():
  386. config = {'checks': [{'name': 'repository', 'max_duration': 33}]}
  387. flexmock(module).should_receive('execute_command').never()
  388. with pytest.raises(ValueError):
  389. module.check_archives(
  390. repository_path='repo',
  391. config=config,
  392. local_borg_version='1.2.3',
  393. check_arguments=flexmock(
  394. progress=None,
  395. repair=None,
  396. only_checks=None,
  397. force=None,
  398. match_archives=None,
  399. max_duration=None,
  400. ),
  401. global_arguments=flexmock(log_json=False),
  402. checks={'repository', 'archives'},
  403. archive_filter_flags=(),
  404. )
  405. def test_check_archives_with_max_duration_flag_overrides_max_duration_option():
  406. config = {'checks': [{'name': 'repository', 'max_duration': 33}]}
  407. flexmock(module).should_receive('make_check_name_flags').and_return(())
  408. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  409. flexmock(module.environment).should_receive('make_environment')
  410. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  411. None
  412. )
  413. flexmock(module).should_receive('execute_command').with_args(
  414. ('borg', 'check', '--max-duration', '44', 'repo'),
  415. extra_environment=None,
  416. working_directory=None,
  417. borg_local_path='borg',
  418. borg_exit_codes=None,
  419. ).once()
  420. module.check_archives(
  421. repository_path='repo',
  422. config=config,
  423. local_borg_version='1.2.3',
  424. check_arguments=flexmock(
  425. progress=None,
  426. repair=None,
  427. only_checks=None,
  428. force=None,
  429. match_archives=None,
  430. max_duration=44,
  431. ),
  432. global_arguments=flexmock(log_json=False),
  433. checks={'repository'},
  434. archive_filter_flags=(),
  435. )
  436. @pytest.mark.parametrize(
  437. 'checks',
  438. (
  439. ('repository',),
  440. ('archives',),
  441. ('repository', 'archives'),
  442. ('repository', 'archives', 'other'),
  443. ),
  444. )
  445. def test_check_archives_calls_borg_with_parameters(checks):
  446. config = {}
  447. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  448. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  449. insert_execute_command_mock(('borg', 'check', 'repo'))
  450. module.check_archives(
  451. repository_path='repo',
  452. config=config,
  453. local_borg_version='1.2.3',
  454. check_arguments=flexmock(
  455. progress=None,
  456. repair=None,
  457. only_checks=None,
  458. force=None,
  459. match_archives=None,
  460. max_duration=None,
  461. ),
  462. global_arguments=flexmock(log_json=False),
  463. checks=checks,
  464. archive_filter_flags=(),
  465. )
  466. def test_check_archives_with_log_info_passes_through_to_borg():
  467. config = {}
  468. flexmock(module).should_receive('make_check_name_flags').and_return(())
  469. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  470. insert_logging_mock(logging.INFO)
  471. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  472. module.check_archives(
  473. repository_path='repo',
  474. config=config,
  475. local_borg_version='1.2.3',
  476. check_arguments=flexmock(
  477. progress=None,
  478. repair=None,
  479. only_checks=None,
  480. force=None,
  481. match_archives=None,
  482. max_duration=None,
  483. ),
  484. global_arguments=flexmock(log_json=False),
  485. checks={'repository'},
  486. archive_filter_flags=(),
  487. )
  488. def test_check_archives_with_log_debug_passes_through_to_borg():
  489. config = {}
  490. flexmock(module).should_receive('make_check_name_flags').and_return(())
  491. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  492. insert_logging_mock(logging.DEBUG)
  493. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', 'repo'))
  494. module.check_archives(
  495. repository_path='repo',
  496. config=config,
  497. local_borg_version='1.2.3',
  498. check_arguments=flexmock(
  499. progress=None,
  500. repair=None,
  501. only_checks=None,
  502. force=None,
  503. match_archives=None,
  504. max_duration=None,
  505. ),
  506. global_arguments=flexmock(log_json=False),
  507. checks={'repository'},
  508. archive_filter_flags=(),
  509. )
  510. def test_check_archives_with_local_path_calls_borg_via_local_path():
  511. checks = {'repository'}
  512. config = {}
  513. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  514. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  515. insert_execute_command_mock(('borg1', 'check', 'repo'))
  516. module.check_archives(
  517. repository_path='repo',
  518. config=config,
  519. local_borg_version='1.2.3',
  520. check_arguments=flexmock(
  521. progress=None,
  522. repair=None,
  523. only_checks=None,
  524. force=None,
  525. match_archives=None,
  526. max_duration=None,
  527. ),
  528. global_arguments=flexmock(log_json=False),
  529. checks=checks,
  530. archive_filter_flags=(),
  531. local_path='borg1',
  532. )
  533. def test_check_archives_with_exit_codes_calls_borg_using_them():
  534. checks = {'repository'}
  535. borg_exit_codes = flexmock()
  536. config = {'borg_exit_codes': borg_exit_codes}
  537. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  538. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  539. insert_execute_command_mock(('borg', 'check', 'repo'), borg_exit_codes=borg_exit_codes)
  540. module.check_archives(
  541. repository_path='repo',
  542. config=config,
  543. local_borg_version='1.2.3',
  544. check_arguments=flexmock(
  545. progress=None,
  546. repair=None,
  547. only_checks=None,
  548. force=None,
  549. match_archives=None,
  550. max_duration=None,
  551. ),
  552. global_arguments=flexmock(log_json=False),
  553. checks=checks,
  554. archive_filter_flags=(),
  555. )
  556. def test_check_archives_with_remote_path_passes_through_to_borg():
  557. checks = {'repository'}
  558. config = {}
  559. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  560. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  561. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  562. module.check_archives(
  563. repository_path='repo',
  564. config=config,
  565. local_borg_version='1.2.3',
  566. check_arguments=flexmock(
  567. progress=None,
  568. repair=None,
  569. only_checks=None,
  570. force=None,
  571. match_archives=None,
  572. max_duration=None,
  573. ),
  574. global_arguments=flexmock(log_json=False),
  575. checks=checks,
  576. archive_filter_flags=(),
  577. remote_path='borg1',
  578. )
  579. def test_check_archives_with_log_json_passes_through_to_borg():
  580. checks = {'repository'}
  581. config = {}
  582. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  583. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  584. insert_execute_command_mock(('borg', 'check', '--log-json', 'repo'))
  585. module.check_archives(
  586. repository_path='repo',
  587. config=config,
  588. local_borg_version='1.2.3',
  589. check_arguments=flexmock(
  590. progress=None,
  591. repair=None,
  592. only_checks=None,
  593. force=None,
  594. match_archives=None,
  595. max_duration=None,
  596. ),
  597. global_arguments=flexmock(log_json=True),
  598. checks=checks,
  599. archive_filter_flags=(),
  600. )
  601. def test_check_archives_with_lock_wait_passes_through_to_borg():
  602. checks = {'repository'}
  603. config = {'lock_wait': 5}
  604. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  605. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  606. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  607. module.check_archives(
  608. repository_path='repo',
  609. config=config,
  610. local_borg_version='1.2.3',
  611. check_arguments=flexmock(
  612. progress=None,
  613. repair=None,
  614. only_checks=None,
  615. force=None,
  616. match_archives=None,
  617. max_duration=None,
  618. ),
  619. global_arguments=flexmock(log_json=False),
  620. checks=checks,
  621. archive_filter_flags=(),
  622. )
  623. def test_check_archives_with_retention_prefix():
  624. checks = {'repository'}
  625. prefix = 'foo-'
  626. config = {'prefix': prefix}
  627. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  628. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  629. insert_execute_command_mock(('borg', 'check', 'repo'))
  630. module.check_archives(
  631. repository_path='repo',
  632. config=config,
  633. local_borg_version='1.2.3',
  634. check_arguments=flexmock(
  635. progress=None,
  636. repair=None,
  637. only_checks=None,
  638. force=None,
  639. match_archives=None,
  640. max_duration=None,
  641. ),
  642. global_arguments=flexmock(log_json=False),
  643. checks=checks,
  644. archive_filter_flags=(),
  645. )
  646. def test_check_archives_with_extra_borg_options_passes_through_to_borg():
  647. config = {'extra_borg_options': {'check': '--extra --options'}}
  648. flexmock(module).should_receive('make_check_name_flags').and_return(())
  649. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  650. insert_execute_command_mock(('borg', 'check', '--extra', '--options', 'repo'))
  651. module.check_archives(
  652. repository_path='repo',
  653. config=config,
  654. local_borg_version='1.2.3',
  655. check_arguments=flexmock(
  656. progress=None,
  657. repair=None,
  658. only_checks=None,
  659. force=None,
  660. match_archives=None,
  661. max_duration=None,
  662. ),
  663. global_arguments=flexmock(log_json=False),
  664. checks={'repository'},
  665. archive_filter_flags=(),
  666. )
  667. def test_check_archives_with_match_archives_passes_through_to_borg():
  668. config = {}
  669. flexmock(module).should_receive('make_check_name_flags').and_return(
  670. ('--match-archives', 'foo-*')
  671. )
  672. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  673. flexmock(module.environment).should_receive('make_environment')
  674. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  675. None
  676. )
  677. flexmock(module).should_receive('execute_command').with_args(
  678. ('borg', 'check', '--match-archives', 'foo-*', 'repo'),
  679. extra_environment=None,
  680. working_directory=None,
  681. borg_local_path='borg',
  682. borg_exit_codes=None,
  683. ).once()
  684. module.check_archives(
  685. repository_path='repo',
  686. config=config,
  687. local_borg_version='1.2.3',
  688. check_arguments=flexmock(
  689. progress=None,
  690. repair=None,
  691. only_checks=None,
  692. force=None,
  693. match_archives='foo-*',
  694. max_duration=None,
  695. ),
  696. global_arguments=flexmock(log_json=False),
  697. checks={'archives'},
  698. archive_filter_flags=('--match-archives', 'foo-*'),
  699. )
  700. def test_check_archives_calls_borg_with_working_directory():
  701. config = {'working_directory': '/working/dir'}
  702. flexmock(module).should_receive('make_check_name_flags').and_return(())
  703. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  704. flexmock(module.environment).should_receive('make_environment')
  705. flexmock(module.borgmatic.config.options).should_receive('get_working_directory').and_return(
  706. None
  707. )
  708. insert_execute_command_mock(('borg', 'check', 'repo'), working_directory='/working/dir')
  709. module.check_archives(
  710. repository_path='repo',
  711. config=config,
  712. local_borg_version='1.2.3',
  713. check_arguments=flexmock(
  714. progress=False,
  715. repair=None,
  716. only_checks=None,
  717. force=None,
  718. match_archives=None,
  719. max_duration=None,
  720. ),
  721. global_arguments=flexmock(log_json=False),
  722. checks={'repository'},
  723. archive_filter_flags=(),
  724. )