test_check.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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_data_check_and_last_includes_last_flag():
  183. flexmock(module.feature).should_receive('available').and_return(True)
  184. flags = module.make_check_flags('1.2.3', ('data',), check_last=3)
  185. assert flags == ('--archives-only', '--last', '3', '--verify-data')
  186. def test_make_check_flags_with_repository_check_and_last_omits_last_flag():
  187. flexmock(module.feature).should_receive('available').and_return(True)
  188. flags = module.make_check_flags('1.2.3', ('repository',), check_last=3)
  189. assert flags == ('--repository-only',)
  190. def test_make_check_flags_with_default_checks_and_last_includes_last_flag():
  191. flexmock(module.feature).should_receive('available').and_return(True)
  192. flags = module.make_check_flags('1.2.3', ('repository', 'archives'), check_last=3)
  193. assert flags == ('--last', '3')
  194. def test_make_check_flags_with_archives_check_and_prefix_includes_match_archives_flag():
  195. flexmock(module.feature).should_receive('available').and_return(True)
  196. flags = module.make_check_flags('1.2.3', ('archives',), prefix='foo-')
  197. assert flags == ('--archives-only', '--match-archives', 'sh:foo-*')
  198. def test_make_check_flags_with_data_check_and_prefix_includes_match_archives_flag():
  199. flexmock(module.feature).should_receive('available').and_return(True)
  200. flags = module.make_check_flags('1.2.3', ('data',), prefix='foo-')
  201. assert flags == ('--archives-only', '--match-archives', 'sh:foo-*', '--verify-data')
  202. def test_make_check_flags_with_archives_check_and_empty_prefix_omits_match_archives_flag():
  203. flexmock(module.feature).should_receive('available').and_return(True)
  204. flags = module.make_check_flags('1.2.3', ('archives',), prefix='')
  205. assert flags == ('--archives-only',)
  206. def test_make_check_flags_with_archives_check_and_none_prefix_omits_match_archives_flag():
  207. flexmock(module.feature).should_receive('available').and_return(True)
  208. flags = module.make_check_flags('1.2.3', ('archives',), prefix=None)
  209. assert flags == ('--archives-only',)
  210. def test_make_check_flags_with_repository_check_and_prefix_omits_match_archives_flag():
  211. flexmock(module.feature).should_receive('available').and_return(True)
  212. flags = module.make_check_flags('1.2.3', ('repository',), prefix='foo-')
  213. assert flags == ('--repository-only',)
  214. def test_make_check_flags_with_default_checks_and_prefix_includes_match_archives_flag():
  215. flexmock(module.feature).should_receive('available').and_return(True)
  216. flags = module.make_check_flags('1.2.3', ('repository', 'archives'), prefix='foo-')
  217. assert flags == ('--match-archives', 'sh:foo-*')
  218. def test_read_check_time_does_not_raise():
  219. flexmock(module.os).should_receive('stat').and_return(flexmock(st_mtime=123))
  220. assert module.read_check_time('/path')
  221. def test_read_check_time_on_missing_file_does_not_raise():
  222. flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
  223. assert module.read_check_time('/path') is None
  224. def test_check_archives_with_progress_calls_borg_with_progress_parameter():
  225. checks = ('repository',)
  226. consistency_config = {'check_last': None}
  227. flexmock(module).should_receive('parse_checks')
  228. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  229. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  230. '{"repository": {"id": "repo"}}'
  231. )
  232. flexmock(module).should_receive('make_check_flags').and_return(())
  233. flexmock(module).should_receive('execute_command').never()
  234. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  235. flexmock(module.environment).should_receive('make_environment')
  236. flexmock(module).should_receive('execute_command').with_args(
  237. ('borg', 'check', '--progress', 'repo'),
  238. output_file=module.DO_NOT_CAPTURE,
  239. extra_environment=None,
  240. ).once()
  241. flexmock(module).should_receive('make_check_time_path')
  242. flexmock(module).should_receive('write_check_time')
  243. module.check_archives(
  244. repository='repo',
  245. location_config={},
  246. storage_config={},
  247. consistency_config=consistency_config,
  248. local_borg_version='1.2.3',
  249. progress=True,
  250. )
  251. def test_check_archives_with_repair_calls_borg_with_repair_parameter():
  252. checks = ('repository',)
  253. consistency_config = {'check_last': None}
  254. flexmock(module).should_receive('parse_checks')
  255. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  256. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  257. '{"repository": {"id": "repo"}}'
  258. )
  259. flexmock(module).should_receive('make_check_flags').and_return(())
  260. flexmock(module).should_receive('execute_command').never()
  261. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  262. flexmock(module.environment).should_receive('make_environment')
  263. flexmock(module).should_receive('execute_command').with_args(
  264. ('borg', 'check', '--repair', 'repo'),
  265. output_file=module.DO_NOT_CAPTURE,
  266. extra_environment=None,
  267. ).once()
  268. flexmock(module).should_receive('make_check_time_path')
  269. flexmock(module).should_receive('write_check_time')
  270. module.check_archives(
  271. repository='repo',
  272. location_config={},
  273. storage_config={},
  274. consistency_config=consistency_config,
  275. local_borg_version='1.2.3',
  276. repair=True,
  277. )
  278. @pytest.mark.parametrize(
  279. 'checks',
  280. (
  281. ('repository',),
  282. ('archives',),
  283. ('repository', 'archives'),
  284. ('repository', 'archives', 'other'),
  285. ),
  286. )
  287. def test_check_archives_calls_borg_with_parameters(checks):
  288. check_last = flexmock()
  289. consistency_config = {'check_last': check_last}
  290. flexmock(module).should_receive('parse_checks')
  291. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  292. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  293. '{"repository": {"id": "repo"}}'
  294. )
  295. flexmock(module).should_receive('make_check_flags').with_args(
  296. '1.2.3', checks, check_last, module.DEFAULT_PREFIX
  297. ).and_return(())
  298. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  299. insert_execute_command_mock(('borg', 'check', 'repo'))
  300. flexmock(module).should_receive('make_check_time_path')
  301. flexmock(module).should_receive('write_check_time')
  302. module.check_archives(
  303. repository='repo',
  304. location_config={},
  305. storage_config={},
  306. consistency_config=consistency_config,
  307. local_borg_version='1.2.3',
  308. )
  309. def test_check_archives_with_json_error_raises():
  310. checks = ('archives',)
  311. check_last = flexmock()
  312. consistency_config = {'check_last': check_last}
  313. flexmock(module).should_receive('parse_checks')
  314. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  315. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  316. '{"unexpected": {"id": "repo"}}'
  317. )
  318. with pytest.raises(ValueError):
  319. module.check_archives(
  320. repository='repo',
  321. location_config={},
  322. storage_config={},
  323. consistency_config=consistency_config,
  324. local_borg_version='1.2.3',
  325. )
  326. def test_check_archives_with_missing_json_keys_raises():
  327. checks = ('archives',)
  328. check_last = flexmock()
  329. consistency_config = {'check_last': check_last}
  330. flexmock(module).should_receive('parse_checks')
  331. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  332. flexmock(module.rinfo).should_receive('display_repository_info').and_return('{invalid JSON')
  333. with pytest.raises(ValueError):
  334. module.check_archives(
  335. repository='repo',
  336. location_config={},
  337. storage_config={},
  338. consistency_config=consistency_config,
  339. local_borg_version='1.2.3',
  340. )
  341. def test_check_archives_with_extract_check_calls_extract_only():
  342. checks = ('extract',)
  343. check_last = flexmock()
  344. consistency_config = {'check_last': check_last}
  345. flexmock(module).should_receive('parse_checks')
  346. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  347. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  348. '{"repository": {"id": "repo"}}'
  349. )
  350. flexmock(module).should_receive('make_check_flags').never()
  351. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  352. flexmock(module.extract).should_receive('extract_last_archive_dry_run').once()
  353. flexmock(module).should_receive('write_check_time')
  354. insert_execute_command_never()
  355. module.check_archives(
  356. repository='repo',
  357. location_config={},
  358. storage_config={},
  359. consistency_config=consistency_config,
  360. local_borg_version='1.2.3',
  361. )
  362. def test_check_archives_with_log_info_calls_borg_with_info_parameter():
  363. checks = ('repository',)
  364. consistency_config = {'check_last': None}
  365. flexmock(module).should_receive('parse_checks')
  366. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  367. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  368. '{"repository": {"id": "repo"}}'
  369. )
  370. flexmock(module).should_receive('make_check_flags').and_return(())
  371. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  372. insert_logging_mock(logging.INFO)
  373. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  374. flexmock(module).should_receive('make_check_time_path')
  375. flexmock(module).should_receive('write_check_time')
  376. module.check_archives(
  377. repository='repo',
  378. location_config={},
  379. storage_config={},
  380. consistency_config=consistency_config,
  381. local_borg_version='1.2.3',
  382. )
  383. def test_check_archives_with_log_debug_calls_borg_with_debug_parameter():
  384. checks = ('repository',)
  385. consistency_config = {'check_last': None}
  386. flexmock(module).should_receive('parse_checks')
  387. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  388. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  389. '{"repository": {"id": "repo"}}'
  390. )
  391. flexmock(module).should_receive('make_check_flags').and_return(())
  392. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  393. insert_logging_mock(logging.DEBUG)
  394. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', 'repo'))
  395. flexmock(module).should_receive('make_check_time_path')
  396. flexmock(module).should_receive('write_check_time')
  397. module.check_archives(
  398. repository='repo',
  399. location_config={},
  400. storage_config={},
  401. consistency_config=consistency_config,
  402. local_borg_version='1.2.3',
  403. )
  404. def test_check_archives_without_any_checks_bails():
  405. consistency_config = {'check_last': None}
  406. flexmock(module).should_receive('parse_checks')
  407. flexmock(module).should_receive('filter_checks_on_frequency').and_return(())
  408. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  409. '{"repository": {"id": "repo"}}'
  410. )
  411. insert_execute_command_never()
  412. module.check_archives(
  413. repository='repo',
  414. location_config={},
  415. storage_config={},
  416. consistency_config=consistency_config,
  417. local_borg_version='1.2.3',
  418. )
  419. def test_check_archives_with_local_path_calls_borg_via_local_path():
  420. checks = ('repository',)
  421. check_last = flexmock()
  422. consistency_config = {'check_last': check_last}
  423. flexmock(module).should_receive('parse_checks')
  424. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  425. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  426. '{"repository": {"id": "repo"}}'
  427. )
  428. flexmock(module).should_receive('make_check_flags').with_args(
  429. '1.2.3', checks, check_last, module.DEFAULT_PREFIX
  430. ).and_return(())
  431. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  432. insert_execute_command_mock(('borg1', 'check', 'repo'))
  433. flexmock(module).should_receive('make_check_time_path')
  434. flexmock(module).should_receive('write_check_time')
  435. module.check_archives(
  436. repository='repo',
  437. location_config={},
  438. storage_config={},
  439. consistency_config=consistency_config,
  440. local_borg_version='1.2.3',
  441. local_path='borg1',
  442. )
  443. def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  444. checks = ('repository',)
  445. check_last = flexmock()
  446. consistency_config = {'check_last': check_last}
  447. flexmock(module).should_receive('parse_checks')
  448. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  449. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  450. '{"repository": {"id": "repo"}}'
  451. )
  452. flexmock(module).should_receive('make_check_flags').with_args(
  453. '1.2.3', checks, check_last, module.DEFAULT_PREFIX
  454. ).and_return(())
  455. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  456. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  457. flexmock(module).should_receive('make_check_time_path')
  458. flexmock(module).should_receive('write_check_time')
  459. module.check_archives(
  460. repository='repo',
  461. location_config={},
  462. storage_config={},
  463. consistency_config=consistency_config,
  464. local_borg_version='1.2.3',
  465. remote_path='borg1',
  466. )
  467. def test_check_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  468. checks = ('repository',)
  469. check_last = flexmock()
  470. consistency_config = {'check_last': check_last}
  471. flexmock(module).should_receive('parse_checks')
  472. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  473. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  474. '{"repository": {"id": "repo"}}'
  475. )
  476. flexmock(module).should_receive('make_check_flags').with_args(
  477. '1.2.3', checks, check_last, module.DEFAULT_PREFIX
  478. ).and_return(())
  479. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  480. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  481. flexmock(module).should_receive('make_check_time_path')
  482. flexmock(module).should_receive('write_check_time')
  483. module.check_archives(
  484. repository='repo',
  485. location_config={},
  486. storage_config={'lock_wait': 5},
  487. consistency_config=consistency_config,
  488. local_borg_version='1.2.3',
  489. )
  490. def test_check_archives_with_retention_prefix():
  491. checks = ('repository',)
  492. check_last = flexmock()
  493. prefix = 'foo-'
  494. consistency_config = {'check_last': check_last, 'prefix': prefix}
  495. flexmock(module).should_receive('parse_checks')
  496. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  497. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  498. '{"repository": {"id": "repo"}}'
  499. )
  500. flexmock(module).should_receive('make_check_flags').with_args(
  501. '1.2.3', checks, check_last, prefix
  502. ).and_return(())
  503. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  504. insert_execute_command_mock(('borg', 'check', 'repo'))
  505. flexmock(module).should_receive('make_check_time_path')
  506. flexmock(module).should_receive('write_check_time')
  507. module.check_archives(
  508. repository='repo',
  509. location_config={},
  510. storage_config={},
  511. consistency_config=consistency_config,
  512. local_borg_version='1.2.3',
  513. )
  514. def test_check_archives_with_extra_borg_options_calls_borg_with_extra_options():
  515. checks = ('repository',)
  516. consistency_config = {'check_last': None}
  517. flexmock(module).should_receive('parse_checks')
  518. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  519. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  520. '{"repository": {"id": "repo"}}'
  521. )
  522. flexmock(module).should_receive('make_check_flags').and_return(())
  523. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  524. insert_execute_command_mock(('borg', 'check', '--extra', '--options', 'repo'))
  525. flexmock(module).should_receive('make_check_time_path')
  526. flexmock(module).should_receive('write_check_time')
  527. module.check_archives(
  528. repository='repo',
  529. location_config={},
  530. storage_config={'extra_borg_options': {'check': '--extra --options'}},
  531. consistency_config=consistency_config,
  532. local_borg_version='1.2.3',
  533. )