test_check.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. flags = module.make_check_flags(('repository',))
  142. assert flags == ('--repository-only',)
  143. def test_make_check_flags_with_archives_check_returns_flag():
  144. flags = module.make_check_flags(('archives',))
  145. assert flags == ('--archives-only',)
  146. def test_make_check_flags_with_data_check_returns_flag_and_implies_archives():
  147. flags = module.make_check_flags(('data',))
  148. assert flags == ('--archives-only', '--verify-data',)
  149. def test_make_check_flags_with_extract_omits_extract_flag():
  150. flags = module.make_check_flags(('extract',))
  151. assert flags == ()
  152. def test_make_check_flags_with_repository_and_data_checks_does_not_return_repository_only():
  153. flags = module.make_check_flags(('repository', 'data',))
  154. assert flags == ('--verify-data',)
  155. def test_make_check_flags_with_default_checks_and_default_prefix_returns_default_flags():
  156. flags = module.make_check_flags(('repository', 'archives'), prefix=module.DEFAULT_PREFIX)
  157. assert flags == ('--prefix', module.DEFAULT_PREFIX)
  158. def test_make_check_flags_with_all_checks_and_default_prefix_returns_default_flags():
  159. flags = module.make_check_flags(
  160. ('repository', 'archives', 'extract'), prefix=module.DEFAULT_PREFIX
  161. )
  162. assert flags == ('--prefix', module.DEFAULT_PREFIX)
  163. def test_make_check_flags_with_archives_check_and_last_includes_last_flag():
  164. flags = module.make_check_flags(('archives',), check_last=3)
  165. assert flags == ('--archives-only', '--last', '3')
  166. def test_make_check_flags_with_repository_check_and_last_omits_last_flag():
  167. flags = module.make_check_flags(('repository',), check_last=3)
  168. assert flags == ('--repository-only',)
  169. def test_make_check_flags_with_default_checks_and_last_includes_last_flag():
  170. flags = module.make_check_flags(('repository', 'archives'), check_last=3)
  171. assert flags == ('--last', '3')
  172. def test_make_check_flags_with_archives_check_and_prefix_includes_prefix_flag():
  173. flags = module.make_check_flags(('archives',), prefix='foo-')
  174. assert flags == ('--archives-only', '--prefix', 'foo-')
  175. def test_make_check_flags_with_archives_check_and_empty_prefix_omits_prefix_flag():
  176. flags = module.make_check_flags(('archives',), prefix='')
  177. assert flags == ('--archives-only',)
  178. def test_make_check_flags_with_archives_check_and_none_prefix_omits_prefix_flag():
  179. flags = module.make_check_flags(('archives',), prefix=None)
  180. assert flags == ('--archives-only',)
  181. def test_make_check_flags_with_repository_check_and_prefix_omits_prefix_flag():
  182. flags = module.make_check_flags(('repository',), prefix='foo-')
  183. assert flags == ('--repository-only',)
  184. def test_make_check_flags_with_default_checks_and_prefix_includes_prefix_flag():
  185. flags = module.make_check_flags(('repository', 'archives'), prefix='foo-')
  186. assert flags == ('--prefix', 'foo-')
  187. def test_read_check_time_does_not_raise():
  188. flexmock(module.os).should_receive('stat').and_return(flexmock(st_mtime=123))
  189. assert module.read_check_time('/path')
  190. def test_read_check_time_on_missing_file_does_not_raise():
  191. flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
  192. assert module.read_check_time('/path') is None
  193. def test_check_archives_with_progress_calls_borg_with_progress_parameter():
  194. checks = ('repository',)
  195. consistency_config = {'check_last': None}
  196. flexmock(module).should_receive('parse_checks')
  197. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  198. flexmock(module.info).should_receive('display_archives_info').and_return(
  199. '{"repository": {"id": "repo"}}'
  200. )
  201. flexmock(module).should_receive('make_check_flags').and_return(())
  202. flexmock(module).should_receive('execute_command').never()
  203. flexmock(module.environment).should_receive('make_environment')
  204. flexmock(module).should_receive('execute_command').with_args(
  205. ('borg', 'check', '--progress', 'repo'),
  206. output_file=module.DO_NOT_CAPTURE,
  207. extra_environment=None,
  208. ).once()
  209. flexmock(module).should_receive('make_check_time_path')
  210. flexmock(module).should_receive('write_check_time')
  211. module.check_archives(
  212. repository='repo',
  213. location_config={},
  214. storage_config={},
  215. consistency_config=consistency_config,
  216. progress=True,
  217. )
  218. def test_check_archives_with_repair_calls_borg_with_repair_parameter():
  219. checks = ('repository',)
  220. consistency_config = {'check_last': None}
  221. flexmock(module).should_receive('parse_checks')
  222. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  223. flexmock(module.info).should_receive('display_archives_info').and_return(
  224. '{"repository": {"id": "repo"}}'
  225. )
  226. flexmock(module).should_receive('make_check_flags').and_return(())
  227. flexmock(module).should_receive('execute_command').never()
  228. flexmock(module.environment).should_receive('make_environment')
  229. flexmock(module).should_receive('execute_command').with_args(
  230. ('borg', 'check', '--repair', 'repo'),
  231. output_file=module.DO_NOT_CAPTURE,
  232. extra_environment=None,
  233. ).once()
  234. flexmock(module).should_receive('make_check_time_path')
  235. flexmock(module).should_receive('write_check_time')
  236. module.check_archives(
  237. repository='repo',
  238. location_config={},
  239. storage_config={},
  240. consistency_config=consistency_config,
  241. repair=True,
  242. )
  243. @pytest.mark.parametrize(
  244. 'checks',
  245. (
  246. ('repository',),
  247. ('archives',),
  248. ('repository', 'archives'),
  249. ('repository', 'archives', 'other'),
  250. ),
  251. )
  252. def test_check_archives_calls_borg_with_parameters(checks):
  253. check_last = flexmock()
  254. consistency_config = {'check_last': check_last}
  255. flexmock(module).should_receive('parse_checks')
  256. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  257. flexmock(module.info).should_receive('display_archives_info').and_return(
  258. '{"repository": {"id": "repo"}}'
  259. )
  260. flexmock(module).should_receive('make_check_flags').with_args(
  261. checks, check_last, module.DEFAULT_PREFIX
  262. ).and_return(())
  263. insert_execute_command_mock(('borg', 'check', 'repo'))
  264. flexmock(module).should_receive('make_check_time_path')
  265. flexmock(module).should_receive('write_check_time')
  266. module.check_archives(
  267. repository='repo',
  268. location_config={},
  269. storage_config={},
  270. consistency_config=consistency_config,
  271. )
  272. def test_check_archives_with_json_error_raises():
  273. checks = ('archives',)
  274. check_last = flexmock()
  275. consistency_config = {'check_last': check_last}
  276. flexmock(module).should_receive('parse_checks')
  277. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  278. flexmock(module.info).should_receive('display_archives_info').and_return(
  279. '{"unexpected": {"id": "repo"}}'
  280. )
  281. with pytest.raises(ValueError):
  282. module.check_archives(
  283. repository='repo',
  284. location_config={},
  285. storage_config={},
  286. consistency_config=consistency_config,
  287. )
  288. def test_check_archives_with_missing_json_keys_raises():
  289. checks = ('archives',)
  290. check_last = flexmock()
  291. consistency_config = {'check_last': check_last}
  292. flexmock(module).should_receive('parse_checks')
  293. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  294. flexmock(module.info).should_receive('display_archives_info').and_return('{invalid JSON')
  295. with pytest.raises(ValueError):
  296. module.check_archives(
  297. repository='repo',
  298. location_config={},
  299. storage_config={},
  300. consistency_config=consistency_config,
  301. )
  302. def test_check_archives_with_extract_check_calls_extract_only():
  303. checks = ('extract',)
  304. check_last = flexmock()
  305. consistency_config = {'check_last': check_last}
  306. flexmock(module).should_receive('parse_checks')
  307. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  308. flexmock(module.info).should_receive('display_archives_info').and_return(
  309. '{"repository": {"id": "repo"}}'
  310. )
  311. flexmock(module).should_receive('make_check_flags').never()
  312. flexmock(module.extract).should_receive('extract_last_archive_dry_run').once()
  313. flexmock(module).should_receive('write_check_time')
  314. insert_execute_command_never()
  315. module.check_archives(
  316. repository='repo',
  317. location_config={},
  318. storage_config={},
  319. consistency_config=consistency_config,
  320. )
  321. def test_check_archives_with_log_info_calls_borg_with_info_parameter():
  322. checks = ('repository',)
  323. consistency_config = {'check_last': None}
  324. flexmock(module).should_receive('parse_checks')
  325. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  326. flexmock(module.info).should_receive('display_archives_info').and_return(
  327. '{"repository": {"id": "repo"}}'
  328. )
  329. flexmock(module).should_receive('make_check_flags').and_return(())
  330. insert_logging_mock(logging.INFO)
  331. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  332. flexmock(module).should_receive('make_check_time_path')
  333. flexmock(module).should_receive('write_check_time')
  334. module.check_archives(
  335. repository='repo',
  336. location_config={},
  337. storage_config={},
  338. consistency_config=consistency_config,
  339. )
  340. def test_check_archives_with_log_debug_calls_borg_with_debug_parameter():
  341. checks = ('repository',)
  342. consistency_config = {'check_last': None}
  343. flexmock(module).should_receive('parse_checks')
  344. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  345. flexmock(module.info).should_receive('display_archives_info').and_return(
  346. '{"repository": {"id": "repo"}}'
  347. )
  348. flexmock(module).should_receive('make_check_flags').and_return(())
  349. insert_logging_mock(logging.DEBUG)
  350. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', 'repo'))
  351. flexmock(module).should_receive('make_check_time_path')
  352. flexmock(module).should_receive('write_check_time')
  353. module.check_archives(
  354. repository='repo',
  355. location_config={},
  356. storage_config={},
  357. consistency_config=consistency_config,
  358. )
  359. def test_check_archives_without_any_checks_bails():
  360. consistency_config = {'check_last': None}
  361. flexmock(module).should_receive('parse_checks')
  362. flexmock(module).should_receive('filter_checks_on_frequency').and_return(())
  363. flexmock(module.info).should_receive('display_archives_info').and_return(
  364. '{"repository": {"id": "repo"}}'
  365. )
  366. insert_execute_command_never()
  367. module.check_archives(
  368. repository='repo',
  369. location_config={},
  370. storage_config={},
  371. consistency_config=consistency_config,
  372. )
  373. def test_check_archives_with_local_path_calls_borg_via_local_path():
  374. checks = ('repository',)
  375. check_last = flexmock()
  376. consistency_config = {'check_last': check_last}
  377. flexmock(module).should_receive('parse_checks')
  378. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  379. flexmock(module.info).should_receive('display_archives_info').and_return(
  380. '{"repository": {"id": "repo"}}'
  381. )
  382. flexmock(module).should_receive('make_check_flags').with_args(
  383. checks, check_last, module.DEFAULT_PREFIX
  384. ).and_return(())
  385. insert_execute_command_mock(('borg1', 'check', 'repo'))
  386. flexmock(module).should_receive('make_check_time_path')
  387. flexmock(module).should_receive('write_check_time')
  388. module.check_archives(
  389. repository='repo',
  390. location_config={},
  391. storage_config={},
  392. consistency_config=consistency_config,
  393. local_path='borg1',
  394. )
  395. def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  396. checks = ('repository',)
  397. check_last = flexmock()
  398. consistency_config = {'check_last': check_last}
  399. flexmock(module).should_receive('parse_checks')
  400. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  401. flexmock(module.info).should_receive('display_archives_info').and_return(
  402. '{"repository": {"id": "repo"}}'
  403. )
  404. flexmock(module).should_receive('make_check_flags').with_args(
  405. checks, check_last, module.DEFAULT_PREFIX
  406. ).and_return(())
  407. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  408. flexmock(module).should_receive('make_check_time_path')
  409. flexmock(module).should_receive('write_check_time')
  410. module.check_archives(
  411. repository='repo',
  412. location_config={},
  413. storage_config={},
  414. consistency_config=consistency_config,
  415. remote_path='borg1',
  416. )
  417. def test_check_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  418. checks = ('repository',)
  419. check_last = flexmock()
  420. consistency_config = {'check_last': check_last}
  421. flexmock(module).should_receive('parse_checks')
  422. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  423. flexmock(module.info).should_receive('display_archives_info').and_return(
  424. '{"repository": {"id": "repo"}}'
  425. )
  426. flexmock(module).should_receive('make_check_flags').with_args(
  427. checks, check_last, module.DEFAULT_PREFIX
  428. ).and_return(())
  429. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  430. flexmock(module).should_receive('make_check_time_path')
  431. flexmock(module).should_receive('write_check_time')
  432. module.check_archives(
  433. repository='repo',
  434. location_config={},
  435. storage_config={'lock_wait': 5},
  436. consistency_config=consistency_config,
  437. )
  438. def test_check_archives_with_retention_prefix():
  439. checks = ('repository',)
  440. check_last = flexmock()
  441. prefix = 'foo-'
  442. consistency_config = {'check_last': check_last, 'prefix': prefix}
  443. flexmock(module).should_receive('parse_checks')
  444. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  445. flexmock(module.info).should_receive('display_archives_info').and_return(
  446. '{"repository": {"id": "repo"}}'
  447. )
  448. flexmock(module).should_receive('make_check_flags').with_args(
  449. checks, check_last, prefix
  450. ).and_return(())
  451. insert_execute_command_mock(('borg', 'check', 'repo'))
  452. flexmock(module).should_receive('make_check_time_path')
  453. flexmock(module).should_receive('write_check_time')
  454. module.check_archives(
  455. repository='repo',
  456. location_config={},
  457. storage_config={},
  458. consistency_config=consistency_config,
  459. )
  460. def test_check_archives_with_extra_borg_options_calls_borg_with_extra_options():
  461. checks = ('repository',)
  462. consistency_config = {'check_last': None}
  463. flexmock(module).should_receive('parse_checks')
  464. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  465. flexmock(module.info).should_receive('display_archives_info').and_return(
  466. '{"repository": {"id": "repo"}}'
  467. )
  468. flexmock(module).should_receive('make_check_flags').and_return(())
  469. insert_execute_command_mock(('borg', 'check', '--extra', '--options', 'repo'))
  470. flexmock(module).should_receive('make_check_time_path')
  471. flexmock(module).should_receive('write_check_time')
  472. module.check_archives(
  473. repository='repo',
  474. location_config={},
  475. storage_config={'extra_borg_options': {'check': '--extra --options'}},
  476. consistency_config=consistency_config,
  477. )