test_check.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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):
  7. flexmock(module.environment).should_receive('make_environment')
  8. flexmock(module).should_receive('execute_command').with_args(
  9. command, extra_environment=None
  10. ).once()
  11. def insert_execute_command_never():
  12. flexmock(module).should_receive('execute_command').never()
  13. def test_parse_checks_returns_them_as_tuple():
  14. checks = module.parse_checks({'checks': [{'name': 'foo'}, {'name': 'bar'}]})
  15. assert checks == ('foo', 'bar')
  16. def test_parse_checks_with_missing_value_returns_defaults():
  17. checks = module.parse_checks({})
  18. assert checks == ('repository', 'archives')
  19. def test_parse_checks_with_empty_list_returns_defaults():
  20. checks = module.parse_checks({'checks': []})
  21. assert checks == ('repository', 'archives')
  22. def test_parse_checks_with_none_value_returns_defaults():
  23. checks = module.parse_checks({'checks': None})
  24. assert checks == ('repository', 'archives')
  25. def test_parse_checks_with_disabled_returns_no_checks():
  26. checks = module.parse_checks({'checks': [{'name': 'foo'}, {'name': 'disabled'}]})
  27. assert checks == ()
  28. def test_parse_checks_prefers_override_checks_to_configured_checks():
  29. checks = module.parse_checks(
  30. {'checks': [{'name': 'archives'}]}, only_checks=['repository', 'extract']
  31. )
  32. assert checks == ('repository', 'extract')
  33. @pytest.mark.parametrize(
  34. 'frequency,expected_result',
  35. (
  36. (None, None),
  37. ('always', None),
  38. ('1 hour', module.datetime.timedelta(hours=1)),
  39. ('2 hours', module.datetime.timedelta(hours=2)),
  40. ('1 day', module.datetime.timedelta(days=1)),
  41. ('2 days', module.datetime.timedelta(days=2)),
  42. ('1 week', module.datetime.timedelta(weeks=1)),
  43. ('2 weeks', module.datetime.timedelta(weeks=2)),
  44. ('1 month', module.datetime.timedelta(days=30)),
  45. ('2 months', module.datetime.timedelta(days=60)),
  46. ('1 year', module.datetime.timedelta(days=365)),
  47. ('2 years', module.datetime.timedelta(days=365 * 2)),
  48. ),
  49. )
  50. def test_parse_frequency_parses_into_timedeltas(frequency, expected_result):
  51. assert module.parse_frequency(frequency) == expected_result
  52. @pytest.mark.parametrize(
  53. 'frequency', ('sometime', 'x days', '3 decades',),
  54. )
  55. def test_parse_frequency_raises_on_parse_error(frequency):
  56. with pytest.raises(ValueError):
  57. module.parse_frequency(frequency)
  58. def test_filter_checks_on_frequency_without_config_uses_default_checks():
  59. flexmock(module).should_receive('parse_frequency').and_return(
  60. module.datetime.timedelta(weeks=4)
  61. )
  62. flexmock(module).should_receive('make_check_time_path')
  63. flexmock(module).should_receive('read_check_time').and_return(None)
  64. assert module.filter_checks_on_frequency(
  65. location_config={},
  66. consistency_config={},
  67. borg_repository_id='repo',
  68. checks=('repository', 'archives'),
  69. force=False,
  70. ) == ('repository', 'archives')
  71. def test_filter_checks_on_frequency_retains_unconfigured_check():
  72. assert module.filter_checks_on_frequency(
  73. location_config={},
  74. consistency_config={},
  75. borg_repository_id='repo',
  76. checks=('data',),
  77. force=False,
  78. ) == ('data',)
  79. def test_filter_checks_on_frequency_retains_check_without_frequency():
  80. flexmock(module).should_receive('parse_frequency').and_return(None)
  81. assert module.filter_checks_on_frequency(
  82. location_config={},
  83. consistency_config={'checks': [{'name': 'archives'}]},
  84. borg_repository_id='repo',
  85. checks=('archives',),
  86. force=False,
  87. ) == ('archives',)
  88. def test_filter_checks_on_frequency_retains_check_with_elapsed_frequency():
  89. flexmock(module).should_receive('parse_frequency').and_return(
  90. module.datetime.timedelta(hours=1)
  91. )
  92. flexmock(module).should_receive('make_check_time_path')
  93. flexmock(module).should_receive('read_check_time').and_return(
  94. module.datetime.datetime(year=module.datetime.MINYEAR, month=1, day=1)
  95. )
  96. assert module.filter_checks_on_frequency(
  97. location_config={},
  98. consistency_config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  99. borg_repository_id='repo',
  100. checks=('archives',),
  101. force=False,
  102. ) == ('archives',)
  103. def test_filter_checks_on_frequency_retains_check_with_missing_check_time_file():
  104. flexmock(module).should_receive('parse_frequency').and_return(
  105. module.datetime.timedelta(hours=1)
  106. )
  107. flexmock(module).should_receive('make_check_time_path')
  108. flexmock(module).should_receive('read_check_time').and_return(None)
  109. assert module.filter_checks_on_frequency(
  110. location_config={},
  111. consistency_config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  112. borg_repository_id='repo',
  113. checks=('archives',),
  114. force=False,
  115. ) == ('archives',)
  116. def test_filter_checks_on_frequency_skips_check_with_unelapsed_frequency():
  117. flexmock(module).should_receive('parse_frequency').and_return(
  118. module.datetime.timedelta(hours=1)
  119. )
  120. flexmock(module).should_receive('make_check_time_path')
  121. flexmock(module).should_receive('read_check_time').and_return(module.datetime.datetime.now())
  122. assert (
  123. module.filter_checks_on_frequency(
  124. location_config={},
  125. consistency_config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  126. borg_repository_id='repo',
  127. checks=('archives',),
  128. force=False,
  129. )
  130. == ()
  131. )
  132. def test_filter_checks_on_frequency_restains_check_with_unelapsed_frequency_and_force():
  133. assert module.filter_checks_on_frequency(
  134. location_config={},
  135. consistency_config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  136. borg_repository_id='repo',
  137. checks=('archives',),
  138. force=True,
  139. ) == ('archives',)
  140. def test_make_check_flags_with_repository_check_returns_flag():
  141. flexmock(module.feature).should_receive('available').and_return(True)
  142. flags = module.make_check_flags('1.2.3', ('repository',))
  143. assert flags == ('--repository-only',)
  144. def test_make_check_flags_with_archives_check_returns_flag():
  145. flexmock(module.feature).should_receive('available').and_return(True)
  146. flags = module.make_check_flags('1.2.3', ('archives',))
  147. assert flags == ('--archives-only',)
  148. def test_make_check_flags_with_data_check_returns_flag_and_implies_archives():
  149. flexmock(module.feature).should_receive('available').and_return(True)
  150. flags = module.make_check_flags('1.2.3', ('data',))
  151. assert flags == ('--archives-only', '--verify-data',)
  152. def test_make_check_flags_with_extract_omits_extract_flag():
  153. flexmock(module.feature).should_receive('available').and_return(True)
  154. flags = module.make_check_flags('1.2.3', ('extract',))
  155. assert flags == ()
  156. def test_make_check_flags_with_repository_and_data_checks_does_not_return_repository_only():
  157. flexmock(module.feature).should_receive('available').and_return(True)
  158. flags = module.make_check_flags('1.2.3', ('repository', 'data',))
  159. assert flags == ('--verify-data',)
  160. def test_make_check_flags_with_default_checks_and_default_prefix_returns_default_flags():
  161. flexmock(module.feature).should_receive('available').and_return(True)
  162. flags = module.make_check_flags(
  163. '1.2.3', ('repository', 'archives'), prefix=module.DEFAULT_PREFIX
  164. )
  165. assert flags == ('--match-archives', f'sh:{module.DEFAULT_PREFIX}*')
  166. def test_make_check_flags_with_all_checks_and_default_prefix_returns_default_flags():
  167. flexmock(module.feature).should_receive('available').and_return(True)
  168. flags = module.make_check_flags(
  169. '1.2.3', ('repository', 'archives', 'extract'), prefix=module.DEFAULT_PREFIX
  170. )
  171. assert flags == ('--match-archives', f'sh:{module.DEFAULT_PREFIX}*')
  172. def test_make_check_flags_with_all_checks_and_default_prefix_without_borg_features_returns_glob_archives_flags():
  173. flexmock(module.feature).should_receive('available').and_return(False)
  174. flags = module.make_check_flags(
  175. '1.2.3', ('repository', 'archives', 'extract'), prefix=module.DEFAULT_PREFIX
  176. )
  177. assert flags == ('--glob-archives', f'{module.DEFAULT_PREFIX}*')
  178. def test_make_check_flags_with_archives_check_and_last_includes_last_flag():
  179. flexmock(module.feature).should_receive('available').and_return(True)
  180. flags = module.make_check_flags('1.2.3', ('archives',), check_last=3)
  181. assert flags == ('--archives-only', '--last', '3')
  182. def test_make_check_flags_with_repository_check_and_last_omits_last_flag():
  183. flexmock(module.feature).should_receive('available').and_return(True)
  184. flags = module.make_check_flags('1.2.3', ('repository',), check_last=3)
  185. assert flags == ('--repository-only',)
  186. def test_make_check_flags_with_default_checks_and_last_includes_last_flag():
  187. flexmock(module.feature).should_receive('available').and_return(True)
  188. flags = module.make_check_flags('1.2.3', ('repository', 'archives'), check_last=3)
  189. assert flags == ('--last', '3')
  190. def test_make_check_flags_with_archives_check_and_prefix_includes_match_archives_flag():
  191. flexmock(module.feature).should_receive('available').and_return(True)
  192. flags = module.make_check_flags('1.2.3', ('archives',), prefix='foo-')
  193. assert flags == ('--archives-only', '--match-archives', 'sh:foo-*')
  194. def test_make_check_flags_with_archives_check_and_empty_prefix_omits_match_archives_flag():
  195. flexmock(module.feature).should_receive('available').and_return(True)
  196. flags = module.make_check_flags('1.2.3', ('archives',), prefix='')
  197. assert flags == ('--archives-only',)
  198. def test_make_check_flags_with_archives_check_and_none_prefix_omits_match_archives_flag():
  199. flexmock(module.feature).should_receive('available').and_return(True)
  200. flags = module.make_check_flags('1.2.3', ('archives',), prefix=None)
  201. assert flags == ('--archives-only',)
  202. def test_make_check_flags_with_repository_check_and_prefix_omits_match_archives_flag():
  203. flexmock(module.feature).should_receive('available').and_return(True)
  204. flags = module.make_check_flags('1.2.3', ('repository',), prefix='foo-')
  205. assert flags == ('--repository-only',)
  206. def test_make_check_flags_with_default_checks_and_prefix_includes_match_archives_flag():
  207. flexmock(module.feature).should_receive('available').and_return(True)
  208. flags = module.make_check_flags('1.2.3', ('repository', 'archives'), prefix='foo-')
  209. assert flags == ('--match-archives', 'sh:foo-*')
  210. def test_read_check_time_does_not_raise():
  211. flexmock(module.os).should_receive('stat').and_return(flexmock(st_mtime=123))
  212. assert module.read_check_time('/path')
  213. def test_read_check_time_on_missing_file_does_not_raise():
  214. flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
  215. assert module.read_check_time('/path') is None
  216. def test_check_archives_with_progress_calls_borg_with_progress_parameter():
  217. checks = ('repository',)
  218. consistency_config = {'check_last': None}
  219. flexmock(module).should_receive('parse_checks')
  220. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  221. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  222. '{"repository": {"id": "repo"}}'
  223. )
  224. flexmock(module).should_receive('make_check_flags').and_return(())
  225. flexmock(module).should_receive('execute_command').never()
  226. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  227. flexmock(module.environment).should_receive('make_environment')
  228. flexmock(module).should_receive('execute_command').with_args(
  229. ('borg', 'check', '--progress', 'repo'),
  230. output_file=module.DO_NOT_CAPTURE,
  231. extra_environment=None,
  232. ).once()
  233. flexmock(module).should_receive('make_check_time_path')
  234. flexmock(module).should_receive('write_check_time')
  235. module.check_archives(
  236. repository='repo',
  237. location_config={},
  238. storage_config={},
  239. consistency_config=consistency_config,
  240. local_borg_version='1.2.3',
  241. progress=True,
  242. )
  243. def test_check_archives_with_repair_calls_borg_with_repair_parameter():
  244. checks = ('repository',)
  245. consistency_config = {'check_last': None}
  246. flexmock(module).should_receive('parse_checks')
  247. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  248. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  249. '{"repository": {"id": "repo"}}'
  250. )
  251. flexmock(module).should_receive('make_check_flags').and_return(())
  252. flexmock(module).should_receive('execute_command').never()
  253. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  254. flexmock(module.environment).should_receive('make_environment')
  255. flexmock(module).should_receive('execute_command').with_args(
  256. ('borg', 'check', '--repair', 'repo'),
  257. output_file=module.DO_NOT_CAPTURE,
  258. extra_environment=None,
  259. ).once()
  260. flexmock(module).should_receive('make_check_time_path')
  261. flexmock(module).should_receive('write_check_time')
  262. module.check_archives(
  263. repository='repo',
  264. location_config={},
  265. storage_config={},
  266. consistency_config=consistency_config,
  267. local_borg_version='1.2.3',
  268. repair=True,
  269. )
  270. @pytest.mark.parametrize(
  271. 'checks',
  272. (
  273. ('repository',),
  274. ('archives',),
  275. ('repository', 'archives'),
  276. ('repository', 'archives', 'other'),
  277. ),
  278. )
  279. def test_check_archives_calls_borg_with_parameters(checks):
  280. check_last = flexmock()
  281. consistency_config = {'check_last': check_last}
  282. flexmock(module).should_receive('parse_checks')
  283. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  284. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  285. '{"repository": {"id": "repo"}}'
  286. )
  287. flexmock(module).should_receive('make_check_flags').with_args(
  288. '1.2.3', checks, check_last, module.DEFAULT_PREFIX
  289. ).and_return(())
  290. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  291. insert_execute_command_mock(('borg', 'check', 'repo'))
  292. flexmock(module).should_receive('make_check_time_path')
  293. flexmock(module).should_receive('write_check_time')
  294. module.check_archives(
  295. repository='repo',
  296. location_config={},
  297. storage_config={},
  298. consistency_config=consistency_config,
  299. local_borg_version='1.2.3',
  300. )
  301. def test_check_archives_with_json_error_raises():
  302. checks = ('archives',)
  303. check_last = flexmock()
  304. consistency_config = {'check_last': check_last}
  305. flexmock(module).should_receive('parse_checks')
  306. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  307. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  308. '{"unexpected": {"id": "repo"}}'
  309. )
  310. with pytest.raises(ValueError):
  311. module.check_archives(
  312. repository='repo',
  313. location_config={},
  314. storage_config={},
  315. consistency_config=consistency_config,
  316. local_borg_version='1.2.3',
  317. )
  318. def test_check_archives_with_missing_json_keys_raises():
  319. checks = ('archives',)
  320. check_last = flexmock()
  321. consistency_config = {'check_last': check_last}
  322. flexmock(module).should_receive('parse_checks')
  323. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  324. flexmock(module.rinfo).should_receive('display_repository_info').and_return('{invalid JSON')
  325. with pytest.raises(ValueError):
  326. module.check_archives(
  327. repository='repo',
  328. location_config={},
  329. storage_config={},
  330. consistency_config=consistency_config,
  331. local_borg_version='1.2.3',
  332. )
  333. def test_check_archives_with_extract_check_calls_extract_only():
  334. checks = ('extract',)
  335. check_last = flexmock()
  336. consistency_config = {'check_last': check_last}
  337. flexmock(module).should_receive('parse_checks')
  338. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  339. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  340. '{"repository": {"id": "repo"}}'
  341. )
  342. flexmock(module).should_receive('make_check_flags').never()
  343. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  344. flexmock(module.extract).should_receive('extract_last_archive_dry_run').once()
  345. flexmock(module).should_receive('write_check_time')
  346. insert_execute_command_never()
  347. module.check_archives(
  348. repository='repo',
  349. location_config={},
  350. storage_config={},
  351. consistency_config=consistency_config,
  352. local_borg_version='1.2.3',
  353. )
  354. def test_check_archives_with_log_info_calls_borg_with_info_parameter():
  355. checks = ('repository',)
  356. consistency_config = {'check_last': None}
  357. flexmock(module).should_receive('parse_checks')
  358. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  359. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  360. '{"repository": {"id": "repo"}}'
  361. )
  362. flexmock(module).should_receive('make_check_flags').and_return(())
  363. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  364. insert_logging_mock(logging.INFO)
  365. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  366. flexmock(module).should_receive('make_check_time_path')
  367. flexmock(module).should_receive('write_check_time')
  368. module.check_archives(
  369. repository='repo',
  370. location_config={},
  371. storage_config={},
  372. consistency_config=consistency_config,
  373. local_borg_version='1.2.3',
  374. )
  375. def test_check_archives_with_log_debug_calls_borg_with_debug_parameter():
  376. checks = ('repository',)
  377. consistency_config = {'check_last': None}
  378. flexmock(module).should_receive('parse_checks')
  379. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  380. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  381. '{"repository": {"id": "repo"}}'
  382. )
  383. flexmock(module).should_receive('make_check_flags').and_return(())
  384. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  385. insert_logging_mock(logging.DEBUG)
  386. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', 'repo'))
  387. flexmock(module).should_receive('make_check_time_path')
  388. flexmock(module).should_receive('write_check_time')
  389. module.check_archives(
  390. repository='repo',
  391. location_config={},
  392. storage_config={},
  393. consistency_config=consistency_config,
  394. local_borg_version='1.2.3',
  395. )
  396. def test_check_archives_without_any_checks_bails():
  397. consistency_config = {'check_last': None}
  398. flexmock(module).should_receive('parse_checks')
  399. flexmock(module).should_receive('filter_checks_on_frequency').and_return(())
  400. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  401. '{"repository": {"id": "repo"}}'
  402. )
  403. insert_execute_command_never()
  404. module.check_archives(
  405. repository='repo',
  406. location_config={},
  407. storage_config={},
  408. consistency_config=consistency_config,
  409. local_borg_version='1.2.3',
  410. )
  411. def test_check_archives_with_local_path_calls_borg_via_local_path():
  412. checks = ('repository',)
  413. check_last = flexmock()
  414. consistency_config = {'check_last': check_last}
  415. flexmock(module).should_receive('parse_checks')
  416. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  417. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  418. '{"repository": {"id": "repo"}}'
  419. )
  420. flexmock(module).should_receive('make_check_flags').with_args(
  421. '1.2.3', checks, check_last, module.DEFAULT_PREFIX
  422. ).and_return(())
  423. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  424. insert_execute_command_mock(('borg1', 'check', 'repo'))
  425. flexmock(module).should_receive('make_check_time_path')
  426. flexmock(module).should_receive('write_check_time')
  427. module.check_archives(
  428. repository='repo',
  429. location_config={},
  430. storage_config={},
  431. consistency_config=consistency_config,
  432. local_borg_version='1.2.3',
  433. local_path='borg1',
  434. )
  435. def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  436. checks = ('repository',)
  437. check_last = flexmock()
  438. consistency_config = {'check_last': check_last}
  439. flexmock(module).should_receive('parse_checks')
  440. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  441. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  442. '{"repository": {"id": "repo"}}'
  443. )
  444. flexmock(module).should_receive('make_check_flags').with_args(
  445. '1.2.3', checks, check_last, module.DEFAULT_PREFIX
  446. ).and_return(())
  447. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  448. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  449. flexmock(module).should_receive('make_check_time_path')
  450. flexmock(module).should_receive('write_check_time')
  451. module.check_archives(
  452. repository='repo',
  453. location_config={},
  454. storage_config={},
  455. consistency_config=consistency_config,
  456. local_borg_version='1.2.3',
  457. remote_path='borg1',
  458. )
  459. def test_check_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  460. checks = ('repository',)
  461. check_last = flexmock()
  462. consistency_config = {'check_last': check_last}
  463. flexmock(module).should_receive('parse_checks')
  464. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  465. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  466. '{"repository": {"id": "repo"}}'
  467. )
  468. flexmock(module).should_receive('make_check_flags').with_args(
  469. '1.2.3', checks, check_last, module.DEFAULT_PREFIX
  470. ).and_return(())
  471. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  472. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  473. flexmock(module).should_receive('make_check_time_path')
  474. flexmock(module).should_receive('write_check_time')
  475. module.check_archives(
  476. repository='repo',
  477. location_config={},
  478. storage_config={'lock_wait': 5},
  479. consistency_config=consistency_config,
  480. local_borg_version='1.2.3',
  481. )
  482. def test_check_archives_with_retention_prefix():
  483. checks = ('repository',)
  484. check_last = flexmock()
  485. prefix = 'foo-'
  486. consistency_config = {'check_last': check_last, 'prefix': prefix}
  487. flexmock(module).should_receive('parse_checks')
  488. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  489. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  490. '{"repository": {"id": "repo"}}'
  491. )
  492. flexmock(module).should_receive('make_check_flags').with_args(
  493. '1.2.3', checks, check_last, prefix
  494. ).and_return(())
  495. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  496. insert_execute_command_mock(('borg', 'check', 'repo'))
  497. flexmock(module).should_receive('make_check_time_path')
  498. flexmock(module).should_receive('write_check_time')
  499. module.check_archives(
  500. repository='repo',
  501. location_config={},
  502. storage_config={},
  503. consistency_config=consistency_config,
  504. local_borg_version='1.2.3',
  505. )
  506. def test_check_archives_with_extra_borg_options_calls_borg_with_extra_options():
  507. checks = ('repository',)
  508. consistency_config = {'check_last': None}
  509. flexmock(module).should_receive('parse_checks')
  510. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  511. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  512. '{"repository": {"id": "repo"}}'
  513. )
  514. flexmock(module).should_receive('make_check_flags').and_return(())
  515. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  516. insert_execute_command_mock(('borg', 'check', '--extra', '--options', 'repo'))
  517. flexmock(module).should_receive('make_check_time_path')
  518. flexmock(module).should_receive('write_check_time')
  519. module.check_archives(
  520. repository='repo',
  521. location_config={},
  522. storage_config={'extra_borg_options': {'check': '--extra --options'}},
  523. consistency_config=consistency_config,
  524. local_borg_version='1.2.3',
  525. )