test_check.py 27 KB

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