test_check.py 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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('probe_for_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. archives_check_id='1234',
  76. ) == ('repository', 'archives')
  77. def test_filter_checks_on_frequency_retains_unconfigured_check():
  78. assert module.filter_checks_on_frequency(
  79. location_config={},
  80. consistency_config={},
  81. borg_repository_id='repo',
  82. checks=('data',),
  83. force=False,
  84. ) == ('data',)
  85. def test_filter_checks_on_frequency_retains_check_without_frequency():
  86. flexmock(module).should_receive('parse_frequency').and_return(None)
  87. assert module.filter_checks_on_frequency(
  88. location_config={},
  89. consistency_config={'checks': [{'name': 'archives'}]},
  90. borg_repository_id='repo',
  91. checks=('archives',),
  92. force=False,
  93. archives_check_id='1234',
  94. ) == ('archives',)
  95. def test_filter_checks_on_frequency_retains_check_with_elapsed_frequency():
  96. flexmock(module).should_receive('parse_frequency').and_return(
  97. module.datetime.timedelta(hours=1)
  98. )
  99. flexmock(module).should_receive('make_check_time_path')
  100. flexmock(module).should_receive('probe_for_check_time').and_return(
  101. module.datetime.datetime(year=module.datetime.MINYEAR, month=1, day=1)
  102. )
  103. assert module.filter_checks_on_frequency(
  104. location_config={},
  105. consistency_config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  106. borg_repository_id='repo',
  107. checks=('archives',),
  108. force=False,
  109. archives_check_id='1234',
  110. ) == ('archives',)
  111. def test_filter_checks_on_frequency_retains_check_with_missing_check_time_file():
  112. flexmock(module).should_receive('parse_frequency').and_return(
  113. module.datetime.timedelta(hours=1)
  114. )
  115. flexmock(module).should_receive('make_check_time_path')
  116. flexmock(module).should_receive('probe_for_check_time').and_return(None)
  117. assert module.filter_checks_on_frequency(
  118. location_config={},
  119. consistency_config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  120. borg_repository_id='repo',
  121. checks=('archives',),
  122. force=False,
  123. archives_check_id='1234',
  124. ) == ('archives',)
  125. def test_filter_checks_on_frequency_skips_check_with_unelapsed_frequency():
  126. flexmock(module).should_receive('parse_frequency').and_return(
  127. module.datetime.timedelta(hours=1)
  128. )
  129. flexmock(module).should_receive('make_check_time_path')
  130. flexmock(module).should_receive('probe_for_check_time').and_return(
  131. module.datetime.datetime.now()
  132. )
  133. assert (
  134. module.filter_checks_on_frequency(
  135. location_config={},
  136. consistency_config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  137. borg_repository_id='repo',
  138. checks=('archives',),
  139. force=False,
  140. archives_check_id='1234',
  141. )
  142. == ()
  143. )
  144. def test_filter_checks_on_frequency_restains_check_with_unelapsed_frequency_and_force():
  145. assert module.filter_checks_on_frequency(
  146. location_config={},
  147. consistency_config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  148. borg_repository_id='repo',
  149. checks=('archives',),
  150. force=True,
  151. archives_check_id='1234',
  152. ) == ('archives',)
  153. def test_make_archive_filter_flags_with_default_checks_and_prefix_returns_default_flags():
  154. flexmock(module.feature).should_receive('available').and_return(True)
  155. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  156. flags = module.make_archive_filter_flags(
  157. '1.2.3',
  158. {},
  159. ('repository', 'archives'),
  160. prefix='foo',
  161. )
  162. assert flags == ('--match-archives', 'sh:foo*')
  163. def test_make_archive_filter_flags_with_all_checks_and_prefix_returns_default_flags():
  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_archive_filter_flags(
  167. '1.2.3',
  168. {},
  169. ('repository', 'archives', 'extract'),
  170. prefix='foo',
  171. )
  172. assert flags == ('--match-archives', 'sh:foo*')
  173. def test_make_archive_filter_flags_with_all_checks_and_prefix_without_borg_features_returns_glob_archives_flags():
  174. flexmock(module.feature).should_receive('available').and_return(False)
  175. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  176. flags = module.make_archive_filter_flags(
  177. '1.2.3',
  178. {},
  179. ('repository', 'archives', 'extract'),
  180. prefix='foo',
  181. )
  182. assert flags == ('--glob-archives', 'foo*')
  183. def test_make_archive_filter_flags_with_archives_check_and_last_includes_last_flag():
  184. flexmock(module.feature).should_receive('available').and_return(True)
  185. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  186. flags = module.make_archive_filter_flags('1.2.3', {}, ('archives',), check_last=3)
  187. assert flags == ('--last', '3')
  188. def test_make_archive_filter_flags_with_data_check_and_last_includes_last_flag():
  189. flexmock(module.feature).should_receive('available').and_return(True)
  190. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  191. flags = module.make_archive_filter_flags('1.2.3', {}, ('data',), check_last=3)
  192. assert flags == ('--last', '3')
  193. def test_make_archive_filter_flags_with_repository_check_and_last_omits_last_flag():
  194. flexmock(module.feature).should_receive('available').and_return(True)
  195. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  196. flags = module.make_archive_filter_flags('1.2.3', {}, ('repository',), check_last=3)
  197. assert flags == ()
  198. def test_make_archive_filter_flags_with_default_checks_and_last_includes_last_flag():
  199. flexmock(module.feature).should_receive('available').and_return(True)
  200. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  201. flags = module.make_archive_filter_flags('1.2.3', {}, ('repository', 'archives'), check_last=3)
  202. assert flags == ('--last', '3')
  203. def test_make_archive_filter_flags_with_archives_check_and_prefix_includes_match_archives_flag():
  204. flexmock(module.feature).should_receive('available').and_return(True)
  205. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  206. flags = module.make_archive_filter_flags('1.2.3', {}, ('archives',), prefix='foo-')
  207. assert flags == ('--match-archives', 'sh:foo-*')
  208. def test_make_archive_filter_flags_with_data_check_and_prefix_includes_match_archives_flag():
  209. flexmock(module.feature).should_receive('available').and_return(True)
  210. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  211. flags = module.make_archive_filter_flags('1.2.3', {}, ('data',), prefix='foo-')
  212. assert flags == ('--match-archives', 'sh:foo-*')
  213. def test_make_archive_filter_flags_with_archives_check_and_empty_prefix_uses_archive_name_format_instead():
  214. flexmock(module.feature).should_receive('available').and_return(True)
  215. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  216. None, 'bar-{now}', '1.2.3' # noqa: FS003
  217. ).and_return(('--match-archives', 'sh:bar-*'))
  218. flags = module.make_archive_filter_flags(
  219. '1.2.3', {'archive_name_format': 'bar-{now}'}, ('archives',), prefix='' # noqa: FS003
  220. )
  221. assert flags == ('--match-archives', 'sh:bar-*')
  222. def test_make_archive_filter_flags_with_archives_check_and_none_prefix_omits_match_archives_flag():
  223. flexmock(module.feature).should_receive('available').and_return(True)
  224. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  225. flags = module.make_archive_filter_flags('1.2.3', {}, ('archives',), prefix=None)
  226. assert flags == ()
  227. def test_make_archive_filter_flags_with_repository_check_and_prefix_omits_match_archives_flag():
  228. flexmock(module.feature).should_receive('available').and_return(True)
  229. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  230. flags = module.make_archive_filter_flags('1.2.3', {}, ('repository',), prefix='foo-')
  231. assert flags == ()
  232. def test_make_archive_filter_flags_with_default_checks_and_prefix_includes_match_archives_flag():
  233. flexmock(module.feature).should_receive('available').and_return(True)
  234. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  235. flags = module.make_archive_filter_flags('1.2.3', {}, ('repository', 'archives'), prefix='foo-')
  236. assert flags == ('--match-archives', 'sh:foo-*')
  237. def test_make_archives_check_id_with_flags_returns_a_value_and_does_not_raise():
  238. assert module.make_archives_check_id(('--match-archives', 'sh:foo-*'))
  239. def test_make_archives_check_id_with_empty_flags_returns_none():
  240. assert module.make_archives_check_id(()) is None
  241. def test_make_check_flags_with_repository_check_returns_flag():
  242. flags = module.make_check_flags(('repository',), ())
  243. assert flags == ('--repository-only',)
  244. def test_make_check_flags_with_archives_check_returns_flag():
  245. flags = module.make_check_flags(('archives',), ())
  246. assert flags == ('--archives-only',)
  247. def test_make_check_flags_with_archives_check_and_archive_filter_flags_includes_those_flags():
  248. flags = module.make_check_flags(('archives',), ('--match-archives', 'sh:foo-*'))
  249. assert flags == ('--archives-only', '--match-archives', 'sh:foo-*')
  250. def test_make_check_flags_without_archives_check_and_with_archive_filter_flags_includes_those_flags():
  251. flags = module.make_check_flags(('repository',), ('--match-archives', 'sh:foo-*'))
  252. assert flags == ('--repository-only',)
  253. def test_make_check_flags_with_data_check_returns_flag_and_implies_archives():
  254. flexmock(module.feature).should_receive('available').and_return(True)
  255. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  256. flags = module.make_check_flags(('data',), ())
  257. assert flags == (
  258. '--archives-only',
  259. '--verify-data',
  260. )
  261. def test_make_check_flags_with_extract_omits_extract_flag():
  262. flexmock(module.feature).should_receive('available').and_return(True)
  263. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  264. flags = module.make_check_flags(('extract',), ())
  265. assert flags == ()
  266. def test_make_check_flags_with_repository_and_data_checks_does_not_return_repository_only():
  267. flexmock(module.feature).should_receive('available').and_return(True)
  268. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  269. flags = module.make_check_flags(
  270. (
  271. 'repository',
  272. 'data',
  273. ),
  274. (),
  275. )
  276. assert flags == ('--verify-data',)
  277. def test_make_check_time_path_with_borgmatic_source_directory_includes_it():
  278. flexmock(module.os.path).should_receive('expanduser').with_args('~/.borgmatic').and_return(
  279. '/home/user/.borgmatic'
  280. )
  281. assert (
  282. module.make_check_time_path(
  283. {'borgmatic_source_directory': '~/.borgmatic'}, '1234', 'archives', '5678'
  284. )
  285. == '/home/user/.borgmatic/checks/1234/archives/5678'
  286. )
  287. def test_make_check_time_path_without_borgmatic_source_directory_uses_default():
  288. flexmock(module.os.path).should_receive('expanduser').with_args(
  289. module.state.DEFAULT_BORGMATIC_SOURCE_DIRECTORY
  290. ).and_return('/home/user/.borgmatic')
  291. assert (
  292. module.make_check_time_path({}, '1234', 'archives', '5678')
  293. == '/home/user/.borgmatic/checks/1234/archives/5678'
  294. )
  295. def test_make_check_time_path_with_archives_check_and_no_archives_check_id_defaults_to_all():
  296. flexmock(module.os.path).should_receive('expanduser').with_args('~/.borgmatic').and_return(
  297. '/home/user/.borgmatic'
  298. )
  299. assert (
  300. module.make_check_time_path(
  301. {'borgmatic_source_directory': '~/.borgmatic'},
  302. '1234',
  303. 'archives',
  304. )
  305. == '/home/user/.borgmatic/checks/1234/archives/all'
  306. )
  307. def test_make_check_time_path_with_repositories_check_ignores_archives_check_id():
  308. flexmock(module.os.path).should_receive('expanduser').with_args('~/.borgmatic').and_return(
  309. '/home/user/.borgmatic'
  310. )
  311. assert (
  312. module.make_check_time_path(
  313. {'borgmatic_source_directory': '~/.borgmatic'}, '1234', 'repository', '5678'
  314. )
  315. == '/home/user/.borgmatic/checks/1234/repository'
  316. )
  317. def test_read_check_time_does_not_raise():
  318. flexmock(module.os).should_receive('stat').and_return(flexmock(st_mtime=123))
  319. assert module.read_check_time('/path')
  320. def test_read_check_time_on_missing_file_does_not_raise():
  321. flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
  322. assert module.read_check_time('/path') is None
  323. def test_probe_for_check_time_uses_maximum_of_multiple_check_times():
  324. flexmock(module).should_receive('make_check_time_path').and_return(
  325. '~/.borgmatic/checks/1234/archives/5678'
  326. ).and_return('~/.borgmatic/checks/1234/archives/all')
  327. flexmock(module).should_receive('read_check_time').and_return(1).and_return(2)
  328. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 2
  329. def test_probe_for_check_time_deduplicates_identical_check_time_paths():
  330. flexmock(module).should_receive('make_check_time_path').and_return(
  331. '~/.borgmatic/checks/1234/archives/5678'
  332. ).and_return('~/.borgmatic/checks/1234/archives/5678')
  333. flexmock(module).should_receive('read_check_time').and_return(1).once()
  334. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 1
  335. def test_probe_for_check_time_skips_none_check_time():
  336. flexmock(module).should_receive('make_check_time_path').and_return(
  337. '~/.borgmatic/checks/1234/archives/5678'
  338. ).and_return('~/.borgmatic/checks/1234/archives/all')
  339. flexmock(module).should_receive('read_check_time').and_return(None).and_return(2)
  340. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 2
  341. def test_probe_for_check_time_uses_single_check_time():
  342. flexmock(module).should_receive('make_check_time_path').and_return(
  343. '~/.borgmatic/checks/1234/archives/5678'
  344. ).and_return('~/.borgmatic/checks/1234/archives/all')
  345. flexmock(module).should_receive('read_check_time').and_return(1).and_return(None)
  346. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 1
  347. def test_probe_for_check_time_returns_none_when_no_check_time_found():
  348. flexmock(module).should_receive('make_check_time_path').and_return(
  349. '~/.borgmatic/checks/1234/archives/5678'
  350. ).and_return('~/.borgmatic/checks/1234/archives/all')
  351. flexmock(module).should_receive('read_check_time').and_return(None).and_return(None)
  352. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) is None
  353. def test_upgrade_check_times_renames_old_check_paths_to_all():
  354. base_path = '~/.borgmatic/checks/1234'
  355. flexmock(module).should_receive('make_check_time_path').with_args(
  356. object, object, 'archives', 'all'
  357. ).and_return(f'{base_path}/archives/all')
  358. flexmock(module).should_receive('make_check_time_path').with_args(
  359. object, object, 'data', 'all'
  360. ).and_return(f'{base_path}/data/all')
  361. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/archives').and_return(
  362. True
  363. )
  364. flexmock(module.os.path).should_receive('isfile').with_args(
  365. f'{base_path}/archives.temp'
  366. ).and_return(False)
  367. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/data').and_return(
  368. False
  369. )
  370. flexmock(module.os.path).should_receive('isfile').with_args(
  371. f'{base_path}/data.temp'
  372. ).and_return(False)
  373. flexmock(module.os).should_receive('rename').with_args(
  374. f'{base_path}/archives', f'{base_path}/archives.temp'
  375. ).once()
  376. flexmock(module.os).should_receive('mkdir').with_args(f'{base_path}/archives').once()
  377. flexmock(module.os).should_receive('rename').with_args(
  378. f'{base_path}/archives.temp', f'{base_path}/archives/all'
  379. ).once()
  380. module.upgrade_check_times(flexmock(), flexmock())
  381. def test_upgrade_check_times_renames_data_check_paths_when_archives_paths_are_already_upgraded():
  382. base_path = '~/.borgmatic/checks/1234'
  383. flexmock(module).should_receive('make_check_time_path').with_args(
  384. object, object, 'archives', 'all'
  385. ).and_return(f'{base_path}/archives/all')
  386. flexmock(module).should_receive('make_check_time_path').with_args(
  387. object, object, 'data', 'all'
  388. ).and_return(f'{base_path}/data/all')
  389. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/archives').and_return(
  390. False
  391. )
  392. flexmock(module.os.path).should_receive('isfile').with_args(
  393. f'{base_path}/archives.temp'
  394. ).and_return(False)
  395. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/data').and_return(
  396. True
  397. )
  398. flexmock(module.os).should_receive('rename').with_args(
  399. f'{base_path}/data', f'{base_path}/data.temp'
  400. ).once()
  401. flexmock(module.os).should_receive('mkdir').with_args(f'{base_path}/data').once()
  402. flexmock(module.os).should_receive('rename').with_args(
  403. f'{base_path}/data.temp', f'{base_path}/data/all'
  404. ).once()
  405. module.upgrade_check_times(flexmock(), flexmock())
  406. def test_upgrade_check_times_skips_missing_check_paths():
  407. flexmock(module).should_receive('make_check_time_path').and_return(
  408. '~/.borgmatic/checks/1234/archives/all'
  409. )
  410. flexmock(module.os.path).should_receive('isfile').and_return(False)
  411. flexmock(module.os).should_receive('rename').never()
  412. flexmock(module.os).should_receive('mkdir').never()
  413. module.upgrade_check_times(flexmock(), flexmock())
  414. def test_upgrade_check_times_renames_stale_temporary_check_path():
  415. base_path = '~/.borgmatic/checks/1234'
  416. flexmock(module).should_receive('make_check_time_path').with_args(
  417. object, object, 'archives', 'all'
  418. ).and_return(f'{base_path}/archives/all')
  419. flexmock(module).should_receive('make_check_time_path').with_args(
  420. object, object, 'data', 'all'
  421. ).and_return(f'{base_path}/data/all')
  422. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/archives').and_return(
  423. False
  424. )
  425. flexmock(module.os.path).should_receive('isfile').with_args(
  426. f'{base_path}/archives.temp'
  427. ).and_return(True)
  428. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/data').and_return(
  429. False
  430. )
  431. flexmock(module.os.path).should_receive('isfile').with_args(
  432. f'{base_path}/data.temp'
  433. ).and_return(False)
  434. flexmock(module.os).should_receive('rename').with_args(
  435. f'{base_path}/archives', f'{base_path}/archives.temp'
  436. ).and_raise(FileNotFoundError)
  437. flexmock(module.os).should_receive('mkdir').with_args(f'{base_path}/archives').once()
  438. flexmock(module.os).should_receive('rename').with_args(
  439. f'{base_path}/archives.temp', f'{base_path}/archives/all'
  440. ).once()
  441. module.upgrade_check_times(flexmock(), flexmock())
  442. def test_check_archives_with_progress_calls_borg_with_progress_parameter():
  443. checks = ('repository',)
  444. consistency_config = {'check_last': None}
  445. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  446. '{"repository": {"id": "repo"}}'
  447. )
  448. flexmock(module).should_receive('upgrade_check_times')
  449. flexmock(module).should_receive('parse_checks')
  450. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  451. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  452. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  453. flexmock(module).should_receive('make_check_flags').and_return(())
  454. flexmock(module).should_receive('execute_command').never()
  455. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  456. flexmock(module.environment).should_receive('make_environment')
  457. flexmock(module).should_receive('execute_command').with_args(
  458. ('borg', 'check', '--progress', 'repo'),
  459. output_file=module.DO_NOT_CAPTURE,
  460. extra_environment=None,
  461. ).once()
  462. flexmock(module).should_receive('make_check_time_path')
  463. flexmock(module).should_receive('write_check_time')
  464. module.check_archives(
  465. repository_path='repo',
  466. location_config={},
  467. storage_config={},
  468. consistency_config=consistency_config,
  469. local_borg_version='1.2.3',
  470. global_arguments=flexmock(log_json=False),
  471. progress=True,
  472. )
  473. def test_check_archives_with_repair_calls_borg_with_repair_parameter():
  474. checks = ('repository',)
  475. consistency_config = {'check_last': None}
  476. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  477. '{"repository": {"id": "repo"}}'
  478. )
  479. flexmock(module).should_receive('upgrade_check_times')
  480. flexmock(module).should_receive('parse_checks')
  481. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  482. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  483. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  484. flexmock(module).should_receive('make_check_flags').and_return(())
  485. flexmock(module).should_receive('execute_command').never()
  486. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  487. flexmock(module.environment).should_receive('make_environment')
  488. flexmock(module).should_receive('execute_command').with_args(
  489. ('borg', 'check', '--repair', 'repo'),
  490. output_file=module.DO_NOT_CAPTURE,
  491. extra_environment=None,
  492. ).once()
  493. flexmock(module).should_receive('make_check_time_path')
  494. flexmock(module).should_receive('write_check_time')
  495. module.check_archives(
  496. repository_path='repo',
  497. location_config={},
  498. storage_config={},
  499. consistency_config=consistency_config,
  500. local_borg_version='1.2.3',
  501. global_arguments=flexmock(log_json=False),
  502. repair=True,
  503. )
  504. @pytest.mark.parametrize(
  505. 'checks',
  506. (
  507. ('repository',),
  508. ('archives',),
  509. ('repository', 'archives'),
  510. ('repository', 'archives', 'other'),
  511. ),
  512. )
  513. def test_check_archives_calls_borg_with_parameters(checks):
  514. check_last = flexmock()
  515. consistency_config = {'check_last': check_last}
  516. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  517. '{"repository": {"id": "repo"}}'
  518. )
  519. flexmock(module).should_receive('upgrade_check_times')
  520. flexmock(module).should_receive('parse_checks')
  521. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  522. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  523. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  524. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  525. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  526. insert_execute_command_mock(('borg', 'check', 'repo'))
  527. flexmock(module).should_receive('make_check_time_path')
  528. flexmock(module).should_receive('write_check_time')
  529. module.check_archives(
  530. repository_path='repo',
  531. location_config={},
  532. storage_config={},
  533. consistency_config=consistency_config,
  534. local_borg_version='1.2.3',
  535. global_arguments=flexmock(log_json=False),
  536. )
  537. def test_check_archives_with_json_error_raises():
  538. checks = ('archives',)
  539. check_last = flexmock()
  540. consistency_config = {'check_last': check_last}
  541. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  542. '{"unexpected": {"id": "repo"}}'
  543. )
  544. flexmock(module).should_receive('upgrade_check_times')
  545. flexmock(module).should_receive('parse_checks')
  546. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  547. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  548. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  549. with pytest.raises(ValueError):
  550. module.check_archives(
  551. repository_path='repo',
  552. location_config={},
  553. storage_config={},
  554. consistency_config=consistency_config,
  555. local_borg_version='1.2.3',
  556. global_arguments=flexmock(log_json=False),
  557. )
  558. def test_check_archives_with_missing_json_keys_raises():
  559. checks = ('archives',)
  560. check_last = flexmock()
  561. consistency_config = {'check_last': check_last}
  562. flexmock(module.rinfo).should_receive('display_repository_info').and_return('{invalid JSON')
  563. flexmock(module).should_receive('upgrade_check_times')
  564. flexmock(module).should_receive('parse_checks')
  565. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  566. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  567. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  568. with pytest.raises(ValueError):
  569. module.check_archives(
  570. repository_path='repo',
  571. location_config={},
  572. storage_config={},
  573. consistency_config=consistency_config,
  574. local_borg_version='1.2.3',
  575. global_arguments=flexmock(log_json=False),
  576. )
  577. def test_check_archives_with_extract_check_calls_extract_only():
  578. checks = ('extract',)
  579. check_last = flexmock()
  580. consistency_config = {'check_last': check_last}
  581. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  582. '{"repository": {"id": "repo"}}'
  583. )
  584. flexmock(module).should_receive('upgrade_check_times')
  585. flexmock(module).should_receive('parse_checks')
  586. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  587. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  588. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  589. flexmock(module).should_receive('make_check_flags').never()
  590. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  591. flexmock(module.extract).should_receive('extract_last_archive_dry_run').once()
  592. flexmock(module).should_receive('write_check_time')
  593. insert_execute_command_never()
  594. module.check_archives(
  595. repository_path='repo',
  596. location_config={},
  597. storage_config={},
  598. consistency_config=consistency_config,
  599. local_borg_version='1.2.3',
  600. global_arguments=flexmock(log_json=False),
  601. )
  602. def test_check_archives_with_log_info_calls_borg_with_info_parameter():
  603. checks = ('repository',)
  604. consistency_config = {'check_last': None}
  605. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  606. '{"repository": {"id": "repo"}}'
  607. )
  608. flexmock(module).should_receive('upgrade_check_times')
  609. flexmock(module).should_receive('parse_checks')
  610. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  611. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  612. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  613. flexmock(module).should_receive('make_check_flags').and_return(())
  614. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  615. insert_logging_mock(logging.INFO)
  616. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  617. flexmock(module).should_receive('make_check_time_path')
  618. flexmock(module).should_receive('write_check_time')
  619. module.check_archives(
  620. repository_path='repo',
  621. location_config={},
  622. storage_config={},
  623. consistency_config=consistency_config,
  624. local_borg_version='1.2.3',
  625. global_arguments=flexmock(log_json=False),
  626. )
  627. def test_check_archives_with_log_debug_calls_borg_with_debug_parameter():
  628. checks = ('repository',)
  629. consistency_config = {'check_last': None}
  630. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  631. '{"repository": {"id": "repo"}}'
  632. )
  633. flexmock(module).should_receive('upgrade_check_times')
  634. flexmock(module).should_receive('parse_checks')
  635. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  636. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  637. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  638. flexmock(module).should_receive('make_check_flags').and_return(())
  639. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  640. insert_logging_mock(logging.DEBUG)
  641. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', 'repo'))
  642. flexmock(module).should_receive('make_check_time_path')
  643. flexmock(module).should_receive('write_check_time')
  644. module.check_archives(
  645. repository_path='repo',
  646. location_config={},
  647. storage_config={},
  648. consistency_config=consistency_config,
  649. local_borg_version='1.2.3',
  650. global_arguments=flexmock(log_json=False),
  651. )
  652. def test_check_archives_without_any_checks_bails():
  653. consistency_config = {'check_last': None}
  654. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  655. '{"repository": {"id": "repo"}}'
  656. )
  657. flexmock(module).should_receive('upgrade_check_times')
  658. flexmock(module).should_receive('parse_checks')
  659. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  660. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  661. flexmock(module).should_receive('filter_checks_on_frequency').and_return(())
  662. insert_execute_command_never()
  663. module.check_archives(
  664. repository_path='repo',
  665. location_config={},
  666. storage_config={},
  667. consistency_config=consistency_config,
  668. local_borg_version='1.2.3',
  669. global_arguments=flexmock(log_json=False),
  670. )
  671. def test_check_archives_with_local_path_calls_borg_via_local_path():
  672. checks = ('repository',)
  673. check_last = flexmock()
  674. consistency_config = {'check_last': check_last}
  675. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  676. '{"repository": {"id": "repo"}}'
  677. )
  678. flexmock(module).should_receive('upgrade_check_times')
  679. flexmock(module).should_receive('parse_checks')
  680. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  681. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  682. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  683. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  684. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  685. insert_execute_command_mock(('borg1', 'check', 'repo'))
  686. flexmock(module).should_receive('make_check_time_path')
  687. flexmock(module).should_receive('write_check_time')
  688. module.check_archives(
  689. repository_path='repo',
  690. location_config={},
  691. storage_config={},
  692. consistency_config=consistency_config,
  693. local_borg_version='1.2.3',
  694. global_arguments=flexmock(log_json=False),
  695. local_path='borg1',
  696. )
  697. def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  698. checks = ('repository',)
  699. check_last = flexmock()
  700. consistency_config = {'check_last': check_last}
  701. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  702. '{"repository": {"id": "repo"}}'
  703. )
  704. flexmock(module).should_receive('upgrade_check_times')
  705. flexmock(module).should_receive('parse_checks')
  706. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  707. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  708. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  709. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  710. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  711. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  712. flexmock(module).should_receive('make_check_time_path')
  713. flexmock(module).should_receive('write_check_time')
  714. module.check_archives(
  715. repository_path='repo',
  716. location_config={},
  717. storage_config={},
  718. consistency_config=consistency_config,
  719. local_borg_version='1.2.3',
  720. global_arguments=flexmock(log_json=False),
  721. remote_path='borg1',
  722. )
  723. def test_check_archives_with_log_json_calls_borg_with_log_json_parameters():
  724. checks = ('repository',)
  725. check_last = flexmock()
  726. storage_config = {}
  727. consistency_config = {'check_last': check_last}
  728. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  729. '{"repository": {"id": "repo"}}'
  730. )
  731. flexmock(module).should_receive('upgrade_check_times')
  732. flexmock(module).should_receive('parse_checks')
  733. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  734. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  735. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  736. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  737. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  738. insert_execute_command_mock(('borg', 'check', '--log-json', 'repo'))
  739. flexmock(module).should_receive('make_check_time_path')
  740. flexmock(module).should_receive('write_check_time')
  741. module.check_archives(
  742. repository_path='repo',
  743. location_config={},
  744. storage_config=storage_config,
  745. consistency_config=consistency_config,
  746. local_borg_version='1.2.3',
  747. global_arguments=flexmock(log_json=True),
  748. )
  749. def test_check_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  750. checks = ('repository',)
  751. check_last = flexmock()
  752. storage_config = {'lock_wait': 5}
  753. consistency_config = {'check_last': check_last}
  754. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  755. '{"repository": {"id": "repo"}}'
  756. )
  757. flexmock(module).should_receive('upgrade_check_times')
  758. flexmock(module).should_receive('parse_checks')
  759. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  760. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  761. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  762. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  763. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  764. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  765. flexmock(module).should_receive('make_check_time_path')
  766. flexmock(module).should_receive('write_check_time')
  767. module.check_archives(
  768. repository_path='repo',
  769. location_config={},
  770. storage_config=storage_config,
  771. consistency_config=consistency_config,
  772. local_borg_version='1.2.3',
  773. global_arguments=flexmock(log_json=False),
  774. )
  775. def test_check_archives_with_retention_prefix():
  776. checks = ('repository',)
  777. check_last = flexmock()
  778. prefix = 'foo-'
  779. consistency_config = {'check_last': check_last, 'prefix': prefix}
  780. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  781. '{"repository": {"id": "repo"}}'
  782. )
  783. flexmock(module).should_receive('upgrade_check_times')
  784. flexmock(module).should_receive('parse_checks')
  785. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  786. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  787. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  788. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  789. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  790. insert_execute_command_mock(('borg', 'check', 'repo'))
  791. flexmock(module).should_receive('make_check_time_path')
  792. flexmock(module).should_receive('write_check_time')
  793. module.check_archives(
  794. repository_path='repo',
  795. location_config={},
  796. storage_config={},
  797. consistency_config=consistency_config,
  798. local_borg_version='1.2.3',
  799. global_arguments=flexmock(log_json=False),
  800. )
  801. def test_check_archives_with_extra_borg_options_calls_borg_with_extra_options():
  802. checks = ('repository',)
  803. consistency_config = {'check_last': None}
  804. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  805. '{"repository": {"id": "repo"}}'
  806. )
  807. flexmock(module).should_receive('upgrade_check_times')
  808. flexmock(module).should_receive('parse_checks')
  809. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  810. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  811. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  812. flexmock(module).should_receive('make_check_flags').and_return(())
  813. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  814. insert_execute_command_mock(('borg', 'check', '--extra', '--options', 'repo'))
  815. flexmock(module).should_receive('make_check_time_path')
  816. flexmock(module).should_receive('write_check_time')
  817. module.check_archives(
  818. repository_path='repo',
  819. location_config={},
  820. storage_config={'extra_borg_options': {'check': '--extra --options'}},
  821. consistency_config=consistency_config,
  822. local_borg_version='1.2.3',
  823. global_arguments=flexmock(log_json=False),
  824. )