test_check.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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',
  54. (
  55. 'sometime',
  56. 'x days',
  57. '3 decades',
  58. ),
  59. )
  60. def test_parse_frequency_raises_on_parse_error(frequency):
  61. with pytest.raises(ValueError):
  62. module.parse_frequency(frequency)
  63. def test_filter_checks_on_frequency_without_config_uses_default_checks():
  64. flexmock(module).should_receive('parse_frequency').and_return(
  65. module.datetime.timedelta(weeks=4)
  66. )
  67. flexmock(module).should_receive('make_check_time_path')
  68. flexmock(module).should_receive('read_check_time').and_return(None)
  69. assert module.filter_checks_on_frequency(
  70. location_config={},
  71. consistency_config={},
  72. borg_repository_id='repo',
  73. checks=('repository', 'archives'),
  74. force=False,
  75. ) == ('repository', 'archives')
  76. def test_filter_checks_on_frequency_retains_unconfigured_check():
  77. assert module.filter_checks_on_frequency(
  78. location_config={},
  79. consistency_config={},
  80. borg_repository_id='repo',
  81. checks=('data',),
  82. force=False,
  83. ) == ('data',)
  84. def test_filter_checks_on_frequency_retains_check_without_frequency():
  85. flexmock(module).should_receive('parse_frequency').and_return(None)
  86. assert module.filter_checks_on_frequency(
  87. location_config={},
  88. consistency_config={'checks': [{'name': 'archives'}]},
  89. borg_repository_id='repo',
  90. checks=('archives',),
  91. force=False,
  92. ) == ('archives',)
  93. def test_filter_checks_on_frequency_retains_check_with_elapsed_frequency():
  94. flexmock(module).should_receive('parse_frequency').and_return(
  95. module.datetime.timedelta(hours=1)
  96. )
  97. flexmock(module).should_receive('make_check_time_path')
  98. flexmock(module).should_receive('read_check_time').and_return(
  99. module.datetime.datetime(year=module.datetime.MINYEAR, month=1, day=1)
  100. )
  101. assert module.filter_checks_on_frequency(
  102. location_config={},
  103. consistency_config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  104. borg_repository_id='repo',
  105. checks=('archives',),
  106. force=False,
  107. ) == ('archives',)
  108. def test_filter_checks_on_frequency_retains_check_with_missing_check_time_file():
  109. flexmock(module).should_receive('parse_frequency').and_return(
  110. module.datetime.timedelta(hours=1)
  111. )
  112. flexmock(module).should_receive('make_check_time_path')
  113. flexmock(module).should_receive('read_check_time').and_return(None)
  114. assert module.filter_checks_on_frequency(
  115. location_config={},
  116. consistency_config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  117. borg_repository_id='repo',
  118. checks=('archives',),
  119. force=False,
  120. ) == ('archives',)
  121. def test_filter_checks_on_frequency_skips_check_with_unelapsed_frequency():
  122. flexmock(module).should_receive('parse_frequency').and_return(
  123. module.datetime.timedelta(hours=1)
  124. )
  125. flexmock(module).should_receive('make_check_time_path')
  126. flexmock(module).should_receive('read_check_time').and_return(module.datetime.datetime.now())
  127. assert (
  128. module.filter_checks_on_frequency(
  129. location_config={},
  130. consistency_config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  131. borg_repository_id='repo',
  132. checks=('archives',),
  133. force=False,
  134. )
  135. == ()
  136. )
  137. def test_filter_checks_on_frequency_restains_check_with_unelapsed_frequency_and_force():
  138. assert module.filter_checks_on_frequency(
  139. location_config={},
  140. consistency_config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  141. borg_repository_id='repo',
  142. checks=('archives',),
  143. force=True,
  144. ) == ('archives',)
  145. def test_make_check_flags_with_repository_check_returns_flag():
  146. flexmock(module.feature).should_receive('available').and_return(True)
  147. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  148. flags = module.make_check_flags('1.2.3', {}, ('repository',))
  149. assert flags == ('--repository-only',)
  150. def test_make_check_flags_with_archives_check_returns_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_check_flags('1.2.3', {}, ('archives',))
  154. assert flags == ('--archives-only',)
  155. def test_make_check_flags_with_data_check_returns_flag_and_implies_archives():
  156. flexmock(module.feature).should_receive('available').and_return(True)
  157. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  158. flags = module.make_check_flags('1.2.3', {}, ('data',))
  159. assert flags == (
  160. '--archives-only',
  161. '--verify-data',
  162. )
  163. def test_make_check_flags_with_extract_omits_extract_flag():
  164. flexmock(module.feature).should_receive('available').and_return(True)
  165. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  166. flags = module.make_check_flags('1.2.3', {}, ('extract',))
  167. assert flags == ()
  168. def test_make_check_flags_with_repository_and_data_checks_does_not_return_repository_only():
  169. flexmock(module.feature).should_receive('available').and_return(True)
  170. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  171. flags = module.make_check_flags(
  172. '1.2.3',
  173. {},
  174. (
  175. 'repository',
  176. 'data',
  177. ),
  178. )
  179. assert flags == ('--verify-data',)
  180. def test_make_check_flags_with_default_checks_and_prefix_returns_default_flags():
  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_flags(
  184. '1.2.3',
  185. {},
  186. ('repository', 'archives'),
  187. prefix='foo',
  188. )
  189. assert flags == ('--match-archives', 'sh:foo*')
  190. def test_make_check_flags_with_all_checks_and_prefix_returns_default_flags():
  191. flexmock(module.feature).should_receive('available').and_return(True)
  192. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  193. flags = module.make_check_flags(
  194. '1.2.3',
  195. {},
  196. ('repository', 'archives', 'extract'),
  197. prefix='foo',
  198. )
  199. assert flags == ('--match-archives', 'sh:foo*')
  200. def test_make_check_flags_with_all_checks_and_prefix_without_borg_features_returns_glob_archives_flags():
  201. flexmock(module.feature).should_receive('available').and_return(False)
  202. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  203. flags = module.make_check_flags(
  204. '1.2.3',
  205. {},
  206. ('repository', 'archives', 'extract'),
  207. prefix='foo',
  208. )
  209. assert flags == ('--glob-archives', 'foo*')
  210. def test_make_check_flags_with_archives_check_and_last_includes_last_flag():
  211. flexmock(module.feature).should_receive('available').and_return(True)
  212. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  213. flags = module.make_check_flags('1.2.3', {}, ('archives',), check_last=3)
  214. assert flags == ('--archives-only', '--last', '3')
  215. def test_make_check_flags_with_data_check_and_last_includes_last_flag():
  216. flexmock(module.feature).should_receive('available').and_return(True)
  217. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  218. flags = module.make_check_flags('1.2.3', {}, ('data',), check_last=3)
  219. assert flags == ('--archives-only', '--last', '3', '--verify-data')
  220. def test_make_check_flags_with_repository_check_and_last_omits_last_flag():
  221. flexmock(module.feature).should_receive('available').and_return(True)
  222. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  223. flags = module.make_check_flags('1.2.3', {}, ('repository',), check_last=3)
  224. assert flags == ('--repository-only',)
  225. def test_make_check_flags_with_default_checks_and_last_includes_last_flag():
  226. flexmock(module.feature).should_receive('available').and_return(True)
  227. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  228. flags = module.make_check_flags('1.2.3', {}, ('repository', 'archives'), check_last=3)
  229. assert flags == ('--last', '3')
  230. def test_make_check_flags_with_archives_check_and_prefix_includes_match_archives_flag():
  231. flexmock(module.feature).should_receive('available').and_return(True)
  232. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  233. flags = module.make_check_flags('1.2.3', {}, ('archives',), prefix='foo-')
  234. assert flags == ('--archives-only', '--match-archives', 'sh:foo-*')
  235. def test_make_check_flags_with_data_check_and_prefix_includes_match_archives_flag():
  236. flexmock(module.feature).should_receive('available').and_return(True)
  237. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  238. flags = module.make_check_flags('1.2.3', {}, ('data',), prefix='foo-')
  239. assert flags == ('--archives-only', '--match-archives', 'sh:foo-*', '--verify-data')
  240. def test_make_check_flags_with_archives_check_and_empty_prefix_uses_archive_name_format_instead():
  241. flexmock(module.feature).should_receive('available').and_return(True)
  242. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  243. None, 'bar-{now}', '1.2.3' # noqa: FS003
  244. ).and_return(('--match-archives', 'sh:bar-*'))
  245. flags = module.make_check_flags(
  246. '1.2.3', {'archive_name_format': 'bar-{now}'}, ('archives',), prefix='' # noqa: FS003
  247. )
  248. assert flags == ('--archives-only', '--match-archives', 'sh:bar-*')
  249. def test_make_check_flags_with_archives_check_and_none_prefix_omits_match_archives_flag():
  250. flexmock(module.feature).should_receive('available').and_return(True)
  251. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  252. flags = module.make_check_flags('1.2.3', {}, ('archives',), prefix=None)
  253. assert flags == ('--archives-only',)
  254. def test_make_check_flags_with_repository_check_and_prefix_omits_match_archives_flag():
  255. flexmock(module.feature).should_receive('available').and_return(True)
  256. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  257. flags = module.make_check_flags('1.2.3', {}, ('repository',), prefix='foo-')
  258. assert flags == ('--repository-only',)
  259. def test_make_check_flags_with_default_checks_and_prefix_includes_match_archives_flag():
  260. flexmock(module.feature).should_receive('available').and_return(True)
  261. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  262. flags = module.make_check_flags('1.2.3', {}, ('repository', 'archives'), prefix='foo-')
  263. assert flags == ('--match-archives', 'sh:foo-*')
  264. def test_read_check_time_does_not_raise():
  265. flexmock(module.os).should_receive('stat').and_return(flexmock(st_mtime=123))
  266. assert module.read_check_time('/path')
  267. def test_read_check_time_on_missing_file_does_not_raise():
  268. flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
  269. assert module.read_check_time('/path') is None
  270. def test_check_archives_with_progress_calls_borg_with_progress_parameter():
  271. checks = ('repository',)
  272. consistency_config = {'check_last': None}
  273. flexmock(module).should_receive('parse_checks')
  274. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  275. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  276. '{"repository": {"id": "repo"}}'
  277. )
  278. flexmock(module).should_receive('make_check_flags').and_return(())
  279. flexmock(module).should_receive('execute_command').never()
  280. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  281. flexmock(module.environment).should_receive('make_environment')
  282. flexmock(module).should_receive('execute_command').with_args(
  283. ('borg', 'check', '--progress', 'repo'),
  284. output_file=module.DO_NOT_CAPTURE,
  285. extra_environment=None,
  286. ).once()
  287. flexmock(module).should_receive('make_check_time_path')
  288. flexmock(module).should_receive('write_check_time')
  289. module.check_archives(
  290. repository_path='repo',
  291. location_config={},
  292. storage_config={},
  293. consistency_config=consistency_config,
  294. local_borg_version='1.2.3',
  295. progress=True,
  296. )
  297. def test_check_archives_with_repair_calls_borg_with_repair_parameter():
  298. checks = ('repository',)
  299. consistency_config = {'check_last': None}
  300. flexmock(module).should_receive('parse_checks')
  301. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  302. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  303. '{"repository": {"id": "repo"}}'
  304. )
  305. flexmock(module).should_receive('make_check_flags').and_return(())
  306. flexmock(module).should_receive('execute_command').never()
  307. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  308. flexmock(module.environment).should_receive('make_environment')
  309. flexmock(module).should_receive('execute_command').with_args(
  310. ('borg', 'check', '--repair', 'repo'),
  311. output_file=module.DO_NOT_CAPTURE,
  312. extra_environment=None,
  313. ).once()
  314. flexmock(module).should_receive('make_check_time_path')
  315. flexmock(module).should_receive('write_check_time')
  316. module.check_archives(
  317. repository_path='repo',
  318. location_config={},
  319. storage_config={},
  320. consistency_config=consistency_config,
  321. local_borg_version='1.2.3',
  322. repair=True,
  323. )
  324. @pytest.mark.parametrize(
  325. 'checks',
  326. (
  327. ('repository',),
  328. ('archives',),
  329. ('repository', 'archives'),
  330. ('repository', 'archives', 'other'),
  331. ),
  332. )
  333. def test_check_archives_calls_borg_with_parameters(checks):
  334. check_last = flexmock()
  335. consistency_config = {'check_last': check_last}
  336. flexmock(module).should_receive('parse_checks')
  337. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  338. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  339. '{"repository": {"id": "repo"}}'
  340. )
  341. flexmock(module).should_receive('make_check_flags').with_args(
  342. '1.2.3',
  343. {},
  344. checks,
  345. check_last,
  346. prefix=None,
  347. ).and_return(())
  348. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  349. insert_execute_command_mock(('borg', 'check', 'repo'))
  350. flexmock(module).should_receive('make_check_time_path')
  351. flexmock(module).should_receive('write_check_time')
  352. module.check_archives(
  353. repository_path='repo',
  354. location_config={},
  355. storage_config={},
  356. consistency_config=consistency_config,
  357. local_borg_version='1.2.3',
  358. )
  359. def test_check_archives_with_json_error_raises():
  360. checks = ('archives',)
  361. check_last = flexmock()
  362. consistency_config = {'check_last': check_last}
  363. flexmock(module).should_receive('parse_checks')
  364. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  365. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  366. '{"unexpected": {"id": "repo"}}'
  367. )
  368. with pytest.raises(ValueError):
  369. module.check_archives(
  370. repository_path='repo',
  371. location_config={},
  372. storage_config={},
  373. consistency_config=consistency_config,
  374. local_borg_version='1.2.3',
  375. )
  376. def test_check_archives_with_missing_json_keys_raises():
  377. checks = ('archives',)
  378. check_last = flexmock()
  379. consistency_config = {'check_last': check_last}
  380. flexmock(module).should_receive('parse_checks')
  381. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  382. flexmock(module.rinfo).should_receive('display_repository_info').and_return('{invalid JSON')
  383. with pytest.raises(ValueError):
  384. module.check_archives(
  385. repository_path='repo',
  386. location_config={},
  387. storage_config={},
  388. consistency_config=consistency_config,
  389. local_borg_version='1.2.3',
  390. )
  391. def test_check_archives_with_extract_check_calls_extract_only():
  392. checks = ('extract',)
  393. check_last = flexmock()
  394. consistency_config = {'check_last': check_last}
  395. flexmock(module).should_receive('parse_checks')
  396. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  397. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  398. '{"repository": {"id": "repo"}}'
  399. )
  400. flexmock(module).should_receive('make_check_flags').never()
  401. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  402. flexmock(module.extract).should_receive('extract_last_archive_dry_run').once()
  403. flexmock(module).should_receive('write_check_time')
  404. insert_execute_command_never()
  405. module.check_archives(
  406. repository_path='repo',
  407. location_config={},
  408. storage_config={},
  409. consistency_config=consistency_config,
  410. local_borg_version='1.2.3',
  411. )
  412. def test_check_archives_with_log_info_calls_borg_with_info_parameter():
  413. checks = ('repository',)
  414. consistency_config = {'check_last': None}
  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').and_return(())
  421. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  422. insert_logging_mock(logging.INFO)
  423. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  424. flexmock(module).should_receive('make_check_time_path')
  425. flexmock(module).should_receive('write_check_time')
  426. module.check_archives(
  427. repository_path='repo',
  428. location_config={},
  429. storage_config={},
  430. consistency_config=consistency_config,
  431. local_borg_version='1.2.3',
  432. )
  433. def test_check_archives_with_log_debug_calls_borg_with_debug_parameter():
  434. checks = ('repository',)
  435. consistency_config = {'check_last': None}
  436. flexmock(module).should_receive('parse_checks')
  437. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  438. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  439. '{"repository": {"id": "repo"}}'
  440. )
  441. flexmock(module).should_receive('make_check_flags').and_return(())
  442. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  443. insert_logging_mock(logging.DEBUG)
  444. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', 'repo'))
  445. flexmock(module).should_receive('make_check_time_path')
  446. flexmock(module).should_receive('write_check_time')
  447. module.check_archives(
  448. repository_path='repo',
  449. location_config={},
  450. storage_config={},
  451. consistency_config=consistency_config,
  452. local_borg_version='1.2.3',
  453. )
  454. def test_check_archives_without_any_checks_bails():
  455. consistency_config = {'check_last': None}
  456. flexmock(module).should_receive('parse_checks')
  457. flexmock(module).should_receive('filter_checks_on_frequency').and_return(())
  458. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  459. '{"repository": {"id": "repo"}}'
  460. )
  461. insert_execute_command_never()
  462. module.check_archives(
  463. repository_path='repo',
  464. location_config={},
  465. storage_config={},
  466. consistency_config=consistency_config,
  467. local_borg_version='1.2.3',
  468. )
  469. def test_check_archives_with_local_path_calls_borg_via_local_path():
  470. checks = ('repository',)
  471. check_last = flexmock()
  472. consistency_config = {'check_last': check_last}
  473. flexmock(module).should_receive('parse_checks')
  474. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  475. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  476. '{"repository": {"id": "repo"}}'
  477. )
  478. flexmock(module).should_receive('make_check_flags').with_args(
  479. '1.2.3',
  480. {},
  481. checks,
  482. check_last,
  483. prefix=None,
  484. ).and_return(())
  485. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  486. insert_execute_command_mock(('borg1', 'check', 'repo'))
  487. flexmock(module).should_receive('make_check_time_path')
  488. flexmock(module).should_receive('write_check_time')
  489. module.check_archives(
  490. repository_path='repo',
  491. location_config={},
  492. storage_config={},
  493. consistency_config=consistency_config,
  494. local_borg_version='1.2.3',
  495. local_path='borg1',
  496. )
  497. def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  498. checks = ('repository',)
  499. check_last = flexmock()
  500. consistency_config = {'check_last': check_last}
  501. flexmock(module).should_receive('parse_checks')
  502. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  503. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  504. '{"repository": {"id": "repo"}}'
  505. )
  506. flexmock(module).should_receive('make_check_flags').with_args(
  507. '1.2.3',
  508. {},
  509. checks,
  510. check_last,
  511. prefix=None,
  512. ).and_return(())
  513. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  514. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  515. flexmock(module).should_receive('make_check_time_path')
  516. flexmock(module).should_receive('write_check_time')
  517. module.check_archives(
  518. repository_path='repo',
  519. location_config={},
  520. storage_config={},
  521. consistency_config=consistency_config,
  522. local_borg_version='1.2.3',
  523. remote_path='borg1',
  524. )
  525. def test_check_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  526. checks = ('repository',)
  527. check_last = flexmock()
  528. storage_config = {'lock_wait': 5}
  529. consistency_config = {'check_last': check_last}
  530. flexmock(module).should_receive('parse_checks')
  531. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  532. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  533. '{"repository": {"id": "repo"}}'
  534. )
  535. flexmock(module).should_receive('make_check_flags').with_args(
  536. '1.2.3',
  537. storage_config,
  538. checks,
  539. check_last,
  540. None,
  541. ).and_return(())
  542. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  543. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  544. flexmock(module).should_receive('make_check_time_path')
  545. flexmock(module).should_receive('write_check_time')
  546. module.check_archives(
  547. repository_path='repo',
  548. location_config={},
  549. storage_config=storage_config,
  550. consistency_config=consistency_config,
  551. local_borg_version='1.2.3',
  552. )
  553. def test_check_archives_with_retention_prefix():
  554. checks = ('repository',)
  555. check_last = flexmock()
  556. prefix = 'foo-'
  557. consistency_config = {'check_last': check_last, 'prefix': prefix}
  558. flexmock(module).should_receive('parse_checks')
  559. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  560. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  561. '{"repository": {"id": "repo"}}'
  562. )
  563. flexmock(module).should_receive('make_check_flags').with_args(
  564. '1.2.3', {}, checks, check_last, prefix
  565. ).and_return(())
  566. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  567. insert_execute_command_mock(('borg', 'check', 'repo'))
  568. flexmock(module).should_receive('make_check_time_path')
  569. flexmock(module).should_receive('write_check_time')
  570. module.check_archives(
  571. repository_path='repo',
  572. location_config={},
  573. storage_config={},
  574. consistency_config=consistency_config,
  575. local_borg_version='1.2.3',
  576. )
  577. def test_check_archives_with_extra_borg_options_calls_borg_with_extra_options():
  578. checks = ('repository',)
  579. consistency_config = {'check_last': None}
  580. flexmock(module).should_receive('parse_checks')
  581. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  582. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  583. '{"repository": {"id": "repo"}}'
  584. )
  585. flexmock(module).should_receive('make_check_flags').and_return(())
  586. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  587. insert_execute_command_mock(('borg', 'check', '--extra', '--options', 'repo'))
  588. flexmock(module).should_receive('make_check_time_path')
  589. flexmock(module).should_receive('write_check_time')
  590. module.check_archives(
  591. repository_path='repo',
  592. location_config={},
  593. storage_config={'extra_borg_options': {'check': '--extra --options'}},
  594. consistency_config=consistency_config,
  595. local_borg_version='1.2.3',
  596. )