test_check.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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_skips_missing_check_paths():
  382. flexmock(module).should_receive('make_check_time_path').and_return(
  383. '~/.borgmatic/checks/1234/archives/all'
  384. )
  385. flexmock(module.os.path).should_receive('isfile').and_return(False)
  386. flexmock(module.os).should_receive('rename').never()
  387. flexmock(module.os).should_receive('mkdir').never()
  388. module.upgrade_check_times(flexmock(), flexmock())
  389. def test_upgrade_check_times_renames_stale_temporary_check_path():
  390. base_path = '~/.borgmatic/checks/1234'
  391. flexmock(module).should_receive('make_check_time_path').with_args(
  392. object, object, 'archives', 'all'
  393. ).and_return(f'{base_path}/archives/all')
  394. flexmock(module).should_receive('make_check_time_path').with_args(
  395. object, object, 'data', 'all'
  396. ).and_return(f'{base_path}/data/all')
  397. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/archives').and_return(
  398. False
  399. )
  400. flexmock(module.os.path).should_receive('isfile').with_args(
  401. f'{base_path}/archives.temp'
  402. ).and_return(True)
  403. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/data').and_return(
  404. False
  405. )
  406. flexmock(module.os.path).should_receive('isfile').with_args(
  407. f'{base_path}/data.temp'
  408. ).and_return(False)
  409. flexmock(module.os).should_receive('rename').with_args(
  410. f'{base_path}/archives', f'{base_path}/archives.temp'
  411. ).and_raise(FileNotFoundError)
  412. flexmock(module.os).should_receive('mkdir').with_args(f'{base_path}/archives').once()
  413. flexmock(module.os).should_receive('rename').with_args(
  414. f'{base_path}/archives.temp', f'{base_path}/archives/all'
  415. ).once()
  416. module.upgrade_check_times(flexmock(), flexmock())
  417. def test_check_archives_with_progress_calls_borg_with_progress_parameter():
  418. checks = ('repository',)
  419. consistency_config = {'check_last': None}
  420. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  421. '{"repository": {"id": "repo"}}'
  422. )
  423. flexmock(module).should_receive('upgrade_check_times')
  424. flexmock(module).should_receive('parse_checks')
  425. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  426. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  427. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  428. flexmock(module).should_receive('make_check_flags').and_return(())
  429. flexmock(module).should_receive('execute_command').never()
  430. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  431. flexmock(module.environment).should_receive('make_environment')
  432. flexmock(module).should_receive('execute_command').with_args(
  433. ('borg', 'check', '--progress', 'repo'),
  434. output_file=module.DO_NOT_CAPTURE,
  435. extra_environment=None,
  436. ).once()
  437. flexmock(module).should_receive('make_check_time_path')
  438. flexmock(module).should_receive('write_check_time')
  439. module.check_archives(
  440. repository_path='repo',
  441. location_config={},
  442. storage_config={},
  443. consistency_config=consistency_config,
  444. local_borg_version='1.2.3',
  445. global_arguments=flexmock(log_json=False),
  446. progress=True,
  447. )
  448. def test_check_archives_with_repair_calls_borg_with_repair_parameter():
  449. checks = ('repository',)
  450. consistency_config = {'check_last': None}
  451. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  452. '{"repository": {"id": "repo"}}'
  453. )
  454. flexmock(module).should_receive('upgrade_check_times')
  455. flexmock(module).should_receive('parse_checks')
  456. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  457. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  458. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  459. flexmock(module).should_receive('make_check_flags').and_return(())
  460. flexmock(module).should_receive('execute_command').never()
  461. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  462. flexmock(module.environment).should_receive('make_environment')
  463. flexmock(module).should_receive('execute_command').with_args(
  464. ('borg', 'check', '--repair', 'repo'),
  465. output_file=module.DO_NOT_CAPTURE,
  466. extra_environment=None,
  467. ).once()
  468. flexmock(module).should_receive('make_check_time_path')
  469. flexmock(module).should_receive('write_check_time')
  470. module.check_archives(
  471. repository_path='repo',
  472. location_config={},
  473. storage_config={},
  474. consistency_config=consistency_config,
  475. local_borg_version='1.2.3',
  476. global_arguments=flexmock(log_json=False),
  477. repair=True,
  478. )
  479. @pytest.mark.parametrize(
  480. 'checks',
  481. (
  482. ('repository',),
  483. ('archives',),
  484. ('repository', 'archives'),
  485. ('repository', 'archives', 'other'),
  486. ),
  487. )
  488. def test_check_archives_calls_borg_with_parameters(checks):
  489. check_last = flexmock()
  490. consistency_config = {'check_last': check_last}
  491. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  492. '{"repository": {"id": "repo"}}'
  493. )
  494. flexmock(module).should_receive('upgrade_check_times')
  495. flexmock(module).should_receive('parse_checks')
  496. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  497. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  498. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  499. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  500. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  501. insert_execute_command_mock(('borg', 'check', 'repo'))
  502. flexmock(module).should_receive('make_check_time_path')
  503. flexmock(module).should_receive('write_check_time')
  504. module.check_archives(
  505. repository_path='repo',
  506. location_config={},
  507. storage_config={},
  508. consistency_config=consistency_config,
  509. local_borg_version='1.2.3',
  510. global_arguments=flexmock(log_json=False),
  511. )
  512. def test_check_archives_with_json_error_raises():
  513. checks = ('archives',)
  514. check_last = flexmock()
  515. consistency_config = {'check_last': check_last}
  516. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  517. '{"unexpected": {"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. with pytest.raises(ValueError):
  525. module.check_archives(
  526. repository_path='repo',
  527. location_config={},
  528. storage_config={},
  529. consistency_config=consistency_config,
  530. local_borg_version='1.2.3',
  531. global_arguments=flexmock(log_json=False),
  532. )
  533. def test_check_archives_with_missing_json_keys_raises():
  534. checks = ('archives',)
  535. check_last = flexmock()
  536. consistency_config = {'check_last': check_last}
  537. flexmock(module.rinfo).should_receive('display_repository_info').and_return('{invalid JSON')
  538. flexmock(module).should_receive('upgrade_check_times')
  539. flexmock(module).should_receive('parse_checks')
  540. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  541. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  542. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  543. with pytest.raises(ValueError):
  544. module.check_archives(
  545. repository_path='repo',
  546. location_config={},
  547. storage_config={},
  548. consistency_config=consistency_config,
  549. local_borg_version='1.2.3',
  550. global_arguments=flexmock(log_json=False),
  551. )
  552. def test_check_archives_with_extract_check_calls_extract_only():
  553. checks = ('extract',)
  554. check_last = flexmock()
  555. consistency_config = {'check_last': check_last}
  556. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  557. '{"repository": {"id": "repo"}}'
  558. )
  559. flexmock(module).should_receive('upgrade_check_times')
  560. flexmock(module).should_receive('parse_checks')
  561. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  562. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  563. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  564. flexmock(module).should_receive('make_check_flags').never()
  565. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  566. flexmock(module.extract).should_receive('extract_last_archive_dry_run').once()
  567. flexmock(module).should_receive('write_check_time')
  568. insert_execute_command_never()
  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_log_info_calls_borg_with_info_parameter():
  578. checks = ('repository',)
  579. consistency_config = {'check_last': None}
  580. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  581. '{"repository": {"id": "repo"}}'
  582. )
  583. flexmock(module).should_receive('upgrade_check_times')
  584. flexmock(module).should_receive('parse_checks')
  585. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  586. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  587. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  588. flexmock(module).should_receive('make_check_flags').and_return(())
  589. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  590. insert_logging_mock(logging.INFO)
  591. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  592. flexmock(module).should_receive('make_check_time_path')
  593. flexmock(module).should_receive('write_check_time')
  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_debug_calls_borg_with_debug_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.DEBUG)
  616. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', '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_without_any_checks_bails():
  628. consistency_config = {'check_last': None}
  629. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  630. '{"repository": {"id": "repo"}}'
  631. )
  632. flexmock(module).should_receive('upgrade_check_times')
  633. flexmock(module).should_receive('parse_checks')
  634. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  635. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  636. flexmock(module).should_receive('filter_checks_on_frequency').and_return(())
  637. insert_execute_command_never()
  638. module.check_archives(
  639. repository_path='repo',
  640. location_config={},
  641. storage_config={},
  642. consistency_config=consistency_config,
  643. local_borg_version='1.2.3',
  644. global_arguments=flexmock(log_json=False),
  645. )
  646. def test_check_archives_with_local_path_calls_borg_via_local_path():
  647. checks = ('repository',)
  648. check_last = flexmock()
  649. consistency_config = {'check_last': check_last}
  650. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  651. '{"repository": {"id": "repo"}}'
  652. )
  653. flexmock(module).should_receive('upgrade_check_times')
  654. flexmock(module).should_receive('parse_checks')
  655. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  656. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  657. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  658. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  659. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  660. insert_execute_command_mock(('borg1', 'check', 'repo'))
  661. flexmock(module).should_receive('make_check_time_path')
  662. flexmock(module).should_receive('write_check_time')
  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. local_path='borg1',
  671. )
  672. def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  673. checks = ('repository',)
  674. check_last = flexmock()
  675. consistency_config = {'check_last': check_last}
  676. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  677. '{"repository": {"id": "repo"}}'
  678. )
  679. flexmock(module).should_receive('upgrade_check_times')
  680. flexmock(module).should_receive('parse_checks')
  681. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  682. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  683. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  684. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  685. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  686. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  687. flexmock(module).should_receive('make_check_time_path')
  688. flexmock(module).should_receive('write_check_time')
  689. module.check_archives(
  690. repository_path='repo',
  691. location_config={},
  692. storage_config={},
  693. consistency_config=consistency_config,
  694. local_borg_version='1.2.3',
  695. global_arguments=flexmock(log_json=False),
  696. remote_path='borg1',
  697. )
  698. def test_check_archives_with_log_json_calls_borg_with_log_json_parameters():
  699. checks = ('repository',)
  700. check_last = flexmock()
  701. storage_config = {}
  702. consistency_config = {'check_last': check_last}
  703. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  704. '{"repository": {"id": "repo"}}'
  705. )
  706. flexmock(module).should_receive('upgrade_check_times')
  707. flexmock(module).should_receive('parse_checks')
  708. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  709. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  710. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  711. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  712. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  713. insert_execute_command_mock(('borg', 'check', '--log-json', 'repo'))
  714. flexmock(module).should_receive('make_check_time_path')
  715. flexmock(module).should_receive('write_check_time')
  716. module.check_archives(
  717. repository_path='repo',
  718. location_config={},
  719. storage_config=storage_config,
  720. consistency_config=consistency_config,
  721. local_borg_version='1.2.3',
  722. global_arguments=flexmock(log_json=True),
  723. )
  724. def test_check_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  725. checks = ('repository',)
  726. check_last = flexmock()
  727. storage_config = {'lock_wait': 5}
  728. consistency_config = {'check_last': check_last}
  729. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  730. '{"repository": {"id": "repo"}}'
  731. )
  732. flexmock(module).should_receive('upgrade_check_times')
  733. flexmock(module).should_receive('parse_checks')
  734. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  735. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  736. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  737. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  738. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  739. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  740. flexmock(module).should_receive('make_check_time_path')
  741. flexmock(module).should_receive('write_check_time')
  742. module.check_archives(
  743. repository_path='repo',
  744. location_config={},
  745. storage_config=storage_config,
  746. consistency_config=consistency_config,
  747. local_borg_version='1.2.3',
  748. global_arguments=flexmock(log_json=False),
  749. )
  750. def test_check_archives_with_retention_prefix():
  751. checks = ('repository',)
  752. check_last = flexmock()
  753. prefix = 'foo-'
  754. consistency_config = {'check_last': check_last, 'prefix': prefix}
  755. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  756. '{"repository": {"id": "repo"}}'
  757. )
  758. flexmock(module).should_receive('upgrade_check_times')
  759. flexmock(module).should_receive('parse_checks')
  760. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  761. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  762. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  763. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  764. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  765. insert_execute_command_mock(('borg', 'check', 'repo'))
  766. flexmock(module).should_receive('make_check_time_path')
  767. flexmock(module).should_receive('write_check_time')
  768. module.check_archives(
  769. repository_path='repo',
  770. location_config={},
  771. storage_config={},
  772. consistency_config=consistency_config,
  773. local_borg_version='1.2.3',
  774. global_arguments=flexmock(log_json=False),
  775. )
  776. def test_check_archives_with_extra_borg_options_calls_borg_with_extra_options():
  777. checks = ('repository',)
  778. consistency_config = {'check_last': None}
  779. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  780. '{"repository": {"id": "repo"}}'
  781. )
  782. flexmock(module).should_receive('upgrade_check_times')
  783. flexmock(module).should_receive('parse_checks')
  784. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  785. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  786. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  787. flexmock(module).should_receive('make_check_flags').and_return(())
  788. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  789. insert_execute_command_mock(('borg', 'check', '--extra', '--options', 'repo'))
  790. flexmock(module).should_receive('make_check_time_path')
  791. flexmock(module).should_receive('write_check_time')
  792. module.check_archives(
  793. repository_path='repo',
  794. location_config={},
  795. storage_config={'extra_borg_options': {'check': '--extra --options'}},
  796. consistency_config=consistency_config,
  797. local_borg_version='1.2.3',
  798. global_arguments=flexmock(log_json=False),
  799. )