test_check.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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_archive_filtler_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_with_data_check_returns_flag_and_implies_archives():
  251. flexmock(module.feature).should_receive('available').and_return(True)
  252. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  253. flags = module.make_check_flags(('data',), ())
  254. assert flags == (
  255. '--archives-only',
  256. '--verify-data',
  257. )
  258. def test_make_check_flags_with_extract_omits_extract_flag():
  259. flexmock(module.feature).should_receive('available').and_return(True)
  260. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  261. flags = module.make_check_flags(('extract',), ())
  262. assert flags == ()
  263. def test_make_check_flags_with_repository_and_data_checks_does_not_return_repository_only():
  264. flexmock(module.feature).should_receive('available').and_return(True)
  265. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  266. flags = module.make_check_flags(
  267. (
  268. 'repository',
  269. 'data',
  270. ),
  271. (),
  272. )
  273. assert flags == ('--verify-data',)
  274. def test_make_check_time_path_with_borgmatic_source_directory_includes_it():
  275. flexmock(module.os.path).should_receive('expanduser').with_args('~/.borgmatic').and_return(
  276. '/home/user/.borgmatic'
  277. )
  278. assert (
  279. module.make_check_time_path(
  280. {'borgmatic_source_directory': '~/.borgmatic'}, '1234', 'archives', '5678'
  281. )
  282. == '/home/user/.borgmatic/checks/1234/archives/5678'
  283. )
  284. def test_make_check_time_path_without_borgmatic_source_directory_uses_default():
  285. flexmock(module.os.path).should_receive('expanduser').with_args(
  286. module.state.DEFAULT_BORGMATIC_SOURCE_DIRECTORY
  287. ).and_return('/home/user/.borgmatic')
  288. assert (
  289. module.make_check_time_path({}, '1234', 'archives', '5678')
  290. == '/home/user/.borgmatic/checks/1234/archives/5678'
  291. )
  292. def test_make_check_time_path_with_archives_check_and_no_archives_check_id_defaults_to_all():
  293. flexmock(module.os.path).should_receive('expanduser').with_args('~/.borgmatic').and_return(
  294. '/home/user/.borgmatic'
  295. )
  296. assert (
  297. module.make_check_time_path(
  298. {'borgmatic_source_directory': '~/.borgmatic'},
  299. '1234',
  300. 'archives',
  301. )
  302. == '/home/user/.borgmatic/checks/1234/archives/all'
  303. )
  304. def test_make_check_time_path_with_repositories_check_ignores_archives_check_id():
  305. flexmock(module.os.path).should_receive('expanduser').with_args('~/.borgmatic').and_return(
  306. '/home/user/.borgmatic'
  307. )
  308. assert (
  309. module.make_check_time_path(
  310. {'borgmatic_source_directory': '~/.borgmatic'}, '1234', 'repository', '5678'
  311. )
  312. == '/home/user/.borgmatic/checks/1234/repository'
  313. )
  314. def test_read_check_time_does_not_raise():
  315. flexmock(module.os).should_receive('stat').and_return(flexmock(st_mtime=123))
  316. assert module.read_check_time('/path')
  317. def test_read_check_time_on_missing_file_does_not_raise():
  318. flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
  319. assert module.read_check_time('/path') is None
  320. def test_probe_for_check_time_uses_maximum_of_multiple_check_times():
  321. flexmock(module).should_receive('make_check_time_path').and_return(
  322. '~/.borgmatic/checks/1234/archives/5678'
  323. ).and_return('~/.borgmatic/checks/1234/archives/all')
  324. flexmock(module).should_receive('read_check_time').and_return(1).and_return(2)
  325. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 2
  326. def test_probe_for_check_time_deduplicates_identical_check_time_paths():
  327. flexmock(module).should_receive('make_check_time_path').and_return(
  328. '~/.borgmatic/checks/1234/archives/5678'
  329. ).and_return('~/.borgmatic/checks/1234/archives/5678')
  330. flexmock(module).should_receive('read_check_time').and_return(1).once()
  331. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 1
  332. def test_probe_for_check_time_skips_none_check_time():
  333. flexmock(module).should_receive('make_check_time_path').and_return(
  334. '~/.borgmatic/checks/1234/archives/5678'
  335. ).and_return('~/.borgmatic/checks/1234/archives/all')
  336. flexmock(module).should_receive('read_check_time').and_return(None).and_return(2)
  337. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 2
  338. def test_probe_for_check_time_uses_single_check_time():
  339. flexmock(module).should_receive('make_check_time_path').and_return(
  340. '~/.borgmatic/checks/1234/archives/5678'
  341. ).and_return('~/.borgmatic/checks/1234/archives/all')
  342. flexmock(module).should_receive('read_check_time').and_return(1).and_return(None)
  343. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 1
  344. def test_probe_for_check_time_returns_none_when_no_check_time_found():
  345. flexmock(module).should_receive('make_check_time_path').and_return(
  346. '~/.borgmatic/checks/1234/archives/5678'
  347. ).and_return('~/.borgmatic/checks/1234/archives/all')
  348. flexmock(module).should_receive('read_check_time').and_return(None).and_return(None)
  349. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) is None
  350. def test_upgrade_check_times_renames_old_check_paths_to_all():
  351. base_path = '~/.borgmatic/checks/1234'
  352. flexmock(module).should_receive('make_check_time_path').with_args(
  353. object, object, 'archives', 'all'
  354. ).and_return(f'{base_path}/archives/all')
  355. flexmock(module).should_receive('make_check_time_path').with_args(
  356. object, object, 'data', 'all'
  357. ).and_return(f'{base_path}/data/all')
  358. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/archives').and_return(
  359. True
  360. )
  361. flexmock(module.os.path).should_receive('isfile').with_args(
  362. f'{base_path}/archives.temp'
  363. ).and_return(False)
  364. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/data').and_return(
  365. False
  366. )
  367. flexmock(module.os.path).should_receive('isfile').with_args(
  368. f'{base_path}/data.temp'
  369. ).and_return(False)
  370. flexmock(module.os).should_receive('rename').with_args(
  371. f'{base_path}/archives', f'{base_path}/archives.temp'
  372. ).once()
  373. flexmock(module.os).should_receive('mkdir').with_args(f'{base_path}/archives').once()
  374. flexmock(module.os).should_receive('rename').with_args(
  375. f'{base_path}/archives.temp', f'{base_path}/archives/all'
  376. ).once()
  377. module.upgrade_check_times(flexmock(), flexmock())
  378. def test_upgrade_check_times_skips_missing_check_paths():
  379. flexmock(module).should_receive('make_check_time_path').and_return(
  380. '~/.borgmatic/checks/1234/archives/all'
  381. )
  382. flexmock(module.os.path).should_receive('isfile').and_return(False)
  383. flexmock(module.os).should_receive('rename').never()
  384. flexmock(module.os).should_receive('mkdir').never()
  385. module.upgrade_check_times(flexmock(), flexmock())
  386. def test_upgrade_check_times_renames_stale_temporary_check_path():
  387. base_path = '~/.borgmatic/checks/1234'
  388. flexmock(module).should_receive('make_check_time_path').with_args(
  389. object, object, 'archives', 'all'
  390. ).and_return(f'{base_path}/archives/all')
  391. flexmock(module).should_receive('make_check_time_path').with_args(
  392. object, object, 'data', 'all'
  393. ).and_return(f'{base_path}/data/all')
  394. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/archives').and_return(
  395. False
  396. )
  397. flexmock(module.os.path).should_receive('isfile').with_args(
  398. f'{base_path}/archives.temp'
  399. ).and_return(True)
  400. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/data').and_return(
  401. False
  402. )
  403. flexmock(module.os.path).should_receive('isfile').with_args(
  404. f'{base_path}/data.temp'
  405. ).and_return(False)
  406. flexmock(module.os).should_receive('rename').with_args(
  407. f'{base_path}/archives', f'{base_path}/archives.temp'
  408. ).and_raise(FileNotFoundError)
  409. flexmock(module.os).should_receive('mkdir').with_args(f'{base_path}/archives').once()
  410. flexmock(module.os).should_receive('rename').with_args(
  411. f'{base_path}/archives.temp', f'{base_path}/archives/all'
  412. ).once()
  413. module.upgrade_check_times(flexmock(), flexmock())
  414. def test_check_archives_with_progress_calls_borg_with_progress_parameter():
  415. checks = ('repository',)
  416. consistency_config = {'check_last': None}
  417. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  418. '{"repository": {"id": "repo"}}'
  419. )
  420. flexmock(module).should_receive('upgrade_check_times')
  421. flexmock(module).should_receive('parse_checks')
  422. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  423. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  424. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  425. flexmock(module).should_receive('make_check_flags').and_return(())
  426. flexmock(module).should_receive('execute_command').never()
  427. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  428. flexmock(module.environment).should_receive('make_environment')
  429. flexmock(module).should_receive('execute_command').with_args(
  430. ('borg', 'check', '--progress', 'repo'),
  431. output_file=module.DO_NOT_CAPTURE,
  432. extra_environment=None,
  433. ).once()
  434. flexmock(module).should_receive('make_check_time_path')
  435. flexmock(module).should_receive('write_check_time')
  436. module.check_archives(
  437. repository_path='repo',
  438. location_config={},
  439. storage_config={},
  440. consistency_config=consistency_config,
  441. local_borg_version='1.2.3',
  442. global_arguments=flexmock(log_json=False),
  443. progress=True,
  444. )
  445. def test_check_archives_with_repair_calls_borg_with_repair_parameter():
  446. checks = ('repository',)
  447. consistency_config = {'check_last': None}
  448. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  449. '{"repository": {"id": "repo"}}'
  450. )
  451. flexmock(module).should_receive('upgrade_check_times')
  452. flexmock(module).should_receive('parse_checks')
  453. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  454. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  455. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  456. flexmock(module).should_receive('make_check_flags').and_return(())
  457. flexmock(module).should_receive('execute_command').never()
  458. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  459. flexmock(module.environment).should_receive('make_environment')
  460. flexmock(module).should_receive('execute_command').with_args(
  461. ('borg', 'check', '--repair', 'repo'),
  462. output_file=module.DO_NOT_CAPTURE,
  463. extra_environment=None,
  464. ).once()
  465. flexmock(module).should_receive('make_check_time_path')
  466. flexmock(module).should_receive('write_check_time')
  467. module.check_archives(
  468. repository_path='repo',
  469. location_config={},
  470. storage_config={},
  471. consistency_config=consistency_config,
  472. local_borg_version='1.2.3',
  473. global_arguments=flexmock(log_json=False),
  474. repair=True,
  475. )
  476. @pytest.mark.parametrize(
  477. 'checks',
  478. (
  479. ('repository',),
  480. ('archives',),
  481. ('repository', 'archives'),
  482. ('repository', 'archives', 'other'),
  483. ),
  484. )
  485. def test_check_archives_calls_borg_with_parameters(checks):
  486. check_last = flexmock()
  487. consistency_config = {'check_last': check_last}
  488. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  489. '{"repository": {"id": "repo"}}'
  490. )
  491. flexmock(module).should_receive('upgrade_check_times')
  492. flexmock(module).should_receive('parse_checks')
  493. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  494. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  495. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  496. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  497. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  498. insert_execute_command_mock(('borg', 'check', 'repo'))
  499. flexmock(module).should_receive('make_check_time_path')
  500. flexmock(module).should_receive('write_check_time')
  501. module.check_archives(
  502. repository_path='repo',
  503. location_config={},
  504. storage_config={},
  505. consistency_config=consistency_config,
  506. local_borg_version='1.2.3',
  507. global_arguments=flexmock(log_json=False),
  508. )
  509. def test_check_archives_with_json_error_raises():
  510. checks = ('archives',)
  511. check_last = flexmock()
  512. consistency_config = {'check_last': check_last}
  513. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  514. '{"unexpected": {"id": "repo"}}'
  515. )
  516. flexmock(module).should_receive('upgrade_check_times')
  517. flexmock(module).should_receive('parse_checks')
  518. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  519. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  520. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  521. with pytest.raises(ValueError):
  522. module.check_archives(
  523. repository_path='repo',
  524. location_config={},
  525. storage_config={},
  526. consistency_config=consistency_config,
  527. local_borg_version='1.2.3',
  528. global_arguments=flexmock(log_json=False),
  529. )
  530. def test_check_archives_with_missing_json_keys_raises():
  531. checks = ('archives',)
  532. check_last = flexmock()
  533. consistency_config = {'check_last': check_last}
  534. flexmock(module.rinfo).should_receive('display_repository_info').and_return('{invalid JSON')
  535. flexmock(module).should_receive('upgrade_check_times')
  536. flexmock(module).should_receive('parse_checks')
  537. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  538. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  539. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  540. with pytest.raises(ValueError):
  541. module.check_archives(
  542. repository_path='repo',
  543. location_config={},
  544. storage_config={},
  545. consistency_config=consistency_config,
  546. local_borg_version='1.2.3',
  547. global_arguments=flexmock(log_json=False),
  548. )
  549. def test_check_archives_with_extract_check_calls_extract_only():
  550. checks = ('extract',)
  551. check_last = flexmock()
  552. consistency_config = {'check_last': check_last}
  553. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  554. '{"repository": {"id": "repo"}}'
  555. )
  556. flexmock(module).should_receive('upgrade_check_times')
  557. flexmock(module).should_receive('parse_checks')
  558. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  559. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  560. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  561. flexmock(module).should_receive('make_check_flags').never()
  562. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  563. flexmock(module.extract).should_receive('extract_last_archive_dry_run').once()
  564. flexmock(module).should_receive('write_check_time')
  565. insert_execute_command_never()
  566. module.check_archives(
  567. repository_path='repo',
  568. location_config={},
  569. storage_config={},
  570. consistency_config=consistency_config,
  571. local_borg_version='1.2.3',
  572. global_arguments=flexmock(log_json=False),
  573. )
  574. def test_check_archives_with_log_info_calls_borg_with_info_parameter():
  575. checks = ('repository',)
  576. consistency_config = {'check_last': None}
  577. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  578. '{"repository": {"id": "repo"}}'
  579. )
  580. flexmock(module).should_receive('upgrade_check_times')
  581. flexmock(module).should_receive('parse_checks')
  582. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  583. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  584. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  585. flexmock(module).should_receive('make_check_flags').and_return(())
  586. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  587. insert_logging_mock(logging.INFO)
  588. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  589. flexmock(module).should_receive('make_check_time_path')
  590. flexmock(module).should_receive('write_check_time')
  591. module.check_archives(
  592. repository_path='repo',
  593. location_config={},
  594. storage_config={},
  595. consistency_config=consistency_config,
  596. local_borg_version='1.2.3',
  597. global_arguments=flexmock(log_json=False),
  598. )
  599. def test_check_archives_with_log_debug_calls_borg_with_debug_parameter():
  600. checks = ('repository',)
  601. consistency_config = {'check_last': None}
  602. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  603. '{"repository": {"id": "repo"}}'
  604. )
  605. flexmock(module).should_receive('upgrade_check_times')
  606. flexmock(module).should_receive('parse_checks')
  607. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  608. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  609. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  610. flexmock(module).should_receive('make_check_flags').and_return(())
  611. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  612. insert_logging_mock(logging.DEBUG)
  613. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', 'repo'))
  614. flexmock(module).should_receive('make_check_time_path')
  615. flexmock(module).should_receive('write_check_time')
  616. module.check_archives(
  617. repository_path='repo',
  618. location_config={},
  619. storage_config={},
  620. consistency_config=consistency_config,
  621. local_borg_version='1.2.3',
  622. global_arguments=flexmock(log_json=False),
  623. )
  624. def test_check_archives_without_any_checks_bails():
  625. consistency_config = {'check_last': None}
  626. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  627. '{"repository": {"id": "repo"}}'
  628. )
  629. flexmock(module).should_receive('upgrade_check_times')
  630. flexmock(module).should_receive('parse_checks')
  631. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  632. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  633. flexmock(module).should_receive('filter_checks_on_frequency').and_return(())
  634. insert_execute_command_never()
  635. module.check_archives(
  636. repository_path='repo',
  637. location_config={},
  638. storage_config={},
  639. consistency_config=consistency_config,
  640. local_borg_version='1.2.3',
  641. global_arguments=flexmock(log_json=False),
  642. )
  643. def test_check_archives_with_local_path_calls_borg_via_local_path():
  644. checks = ('repository',)
  645. check_last = flexmock()
  646. consistency_config = {'check_last': check_last}
  647. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  648. '{"repository": {"id": "repo"}}'
  649. )
  650. flexmock(module).should_receive('upgrade_check_times')
  651. flexmock(module).should_receive('parse_checks')
  652. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  653. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  654. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  655. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  656. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  657. insert_execute_command_mock(('borg1', 'check', 'repo'))
  658. flexmock(module).should_receive('make_check_time_path')
  659. flexmock(module).should_receive('write_check_time')
  660. module.check_archives(
  661. repository_path='repo',
  662. location_config={},
  663. storage_config={},
  664. consistency_config=consistency_config,
  665. local_borg_version='1.2.3',
  666. global_arguments=flexmock(log_json=False),
  667. local_path='borg1',
  668. )
  669. def test_check_archives_with_remote_path_calls_borg_with_remote_path_parameters():
  670. checks = ('repository',)
  671. check_last = flexmock()
  672. consistency_config = {'check_last': check_last}
  673. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  674. '{"repository": {"id": "repo"}}'
  675. )
  676. flexmock(module).should_receive('upgrade_check_times')
  677. flexmock(module).should_receive('parse_checks')
  678. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  679. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  680. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  681. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  682. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  683. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  684. flexmock(module).should_receive('make_check_time_path')
  685. flexmock(module).should_receive('write_check_time')
  686. module.check_archives(
  687. repository_path='repo',
  688. location_config={},
  689. storage_config={},
  690. consistency_config=consistency_config,
  691. local_borg_version='1.2.3',
  692. global_arguments=flexmock(log_json=False),
  693. remote_path='borg1',
  694. )
  695. def test_check_archives_with_log_json_calls_borg_with_log_json_parameters():
  696. checks = ('repository',)
  697. check_last = flexmock()
  698. storage_config = {}
  699. consistency_config = {'check_last': check_last}
  700. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  701. '{"repository": {"id": "repo"}}'
  702. )
  703. flexmock(module).should_receive('upgrade_check_times')
  704. flexmock(module).should_receive('parse_checks')
  705. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  706. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  707. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  708. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  709. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  710. insert_execute_command_mock(('borg', 'check', '--log-json', 'repo'))
  711. flexmock(module).should_receive('make_check_time_path')
  712. flexmock(module).should_receive('write_check_time')
  713. module.check_archives(
  714. repository_path='repo',
  715. location_config={},
  716. storage_config=storage_config,
  717. consistency_config=consistency_config,
  718. local_borg_version='1.2.3',
  719. global_arguments=flexmock(log_json=True),
  720. )
  721. def test_check_archives_with_lock_wait_calls_borg_with_lock_wait_parameters():
  722. checks = ('repository',)
  723. check_last = flexmock()
  724. storage_config = {'lock_wait': 5}
  725. consistency_config = {'check_last': check_last}
  726. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  727. '{"repository": {"id": "repo"}}'
  728. )
  729. flexmock(module).should_receive('upgrade_check_times')
  730. flexmock(module).should_receive('parse_checks')
  731. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  732. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  733. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  734. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  735. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  736. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  737. flexmock(module).should_receive('make_check_time_path')
  738. flexmock(module).should_receive('write_check_time')
  739. module.check_archives(
  740. repository_path='repo',
  741. location_config={},
  742. storage_config=storage_config,
  743. consistency_config=consistency_config,
  744. local_borg_version='1.2.3',
  745. global_arguments=flexmock(log_json=False),
  746. )
  747. def test_check_archives_with_retention_prefix():
  748. checks = ('repository',)
  749. check_last = flexmock()
  750. prefix = 'foo-'
  751. consistency_config = {'check_last': check_last, 'prefix': prefix}
  752. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  753. '{"repository": {"id": "repo"}}'
  754. )
  755. flexmock(module).should_receive('upgrade_check_times')
  756. flexmock(module).should_receive('parse_checks')
  757. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  758. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  759. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  760. flexmock(module).should_receive('make_check_flags').with_args(checks, ()).and_return(())
  761. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  762. insert_execute_command_mock(('borg', 'check', 'repo'))
  763. flexmock(module).should_receive('make_check_time_path')
  764. flexmock(module).should_receive('write_check_time')
  765. module.check_archives(
  766. repository_path='repo',
  767. location_config={},
  768. storage_config={},
  769. consistency_config=consistency_config,
  770. local_borg_version='1.2.3',
  771. global_arguments=flexmock(log_json=False),
  772. )
  773. def test_check_archives_with_extra_borg_options_calls_borg_with_extra_options():
  774. checks = ('repository',)
  775. consistency_config = {'check_last': None}
  776. flexmock(module.rinfo).should_receive('display_repository_info').and_return(
  777. '{"repository": {"id": "repo"}}'
  778. )
  779. flexmock(module).should_receive('upgrade_check_times')
  780. flexmock(module).should_receive('parse_checks')
  781. flexmock(module).should_receive('make_archive_filter_flags').and_return(())
  782. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  783. flexmock(module).should_receive('filter_checks_on_frequency').and_return(checks)
  784. flexmock(module).should_receive('make_check_flags').and_return(())
  785. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  786. insert_execute_command_mock(('borg', 'check', '--extra', '--options', 'repo'))
  787. flexmock(module).should_receive('make_check_time_path')
  788. flexmock(module).should_receive('write_check_time')
  789. module.check_archives(
  790. repository_path='repo',
  791. location_config={},
  792. storage_config={'extra_borg_options': {'check': '--extra --options'}},
  793. consistency_config=consistency_config,
  794. local_borg_version='1.2.3',
  795. global_arguments=flexmock(log_json=False),
  796. )