test_check.py 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.actions import check as module
  4. def test_parse_checks_returns_them_as_tuple():
  5. checks = module.parse_checks({'checks': [{'name': 'foo'}, {'name': 'bar'}]})
  6. assert checks == ('foo', 'bar')
  7. def test_parse_checks_with_missing_value_returns_defaults():
  8. checks = module.parse_checks({})
  9. assert checks == ('repository', 'archives')
  10. def test_parse_checks_with_empty_list_returns_defaults():
  11. checks = module.parse_checks({'checks': []})
  12. assert checks == ('repository', 'archives')
  13. def test_parse_checks_with_none_value_returns_defaults():
  14. checks = module.parse_checks({'checks': None})
  15. assert checks == ('repository', 'archives')
  16. def test_parse_checks_with_disabled_returns_no_checks():
  17. checks = module.parse_checks({'checks': [{'name': 'foo'}, {'name': 'disabled'}]})
  18. assert checks == ()
  19. def test_parse_checks_prefers_override_checks_to_configured_checks():
  20. checks = module.parse_checks(
  21. {'checks': [{'name': 'archives'}]}, only_checks=['repository', 'extract']
  22. )
  23. assert checks == ('repository', 'extract')
  24. @pytest.mark.parametrize(
  25. 'frequency,expected_result',
  26. (
  27. (None, None),
  28. ('always', None),
  29. ('1 hour', module.datetime.timedelta(hours=1)),
  30. ('2 hours', module.datetime.timedelta(hours=2)),
  31. ('1 day', module.datetime.timedelta(days=1)),
  32. ('2 days', module.datetime.timedelta(days=2)),
  33. ('1 week', module.datetime.timedelta(weeks=1)),
  34. ('2 weeks', module.datetime.timedelta(weeks=2)),
  35. ('1 month', module.datetime.timedelta(days=30)),
  36. ('2 months', module.datetime.timedelta(days=60)),
  37. ('1 year', module.datetime.timedelta(days=365)),
  38. ('2 years', module.datetime.timedelta(days=365 * 2)),
  39. ),
  40. )
  41. def test_parse_frequency_parses_into_timedeltas(frequency, expected_result):
  42. assert module.parse_frequency(frequency) == expected_result
  43. @pytest.mark.parametrize(
  44. 'frequency',
  45. (
  46. 'sometime',
  47. 'x days',
  48. '3 decades',
  49. ),
  50. )
  51. def test_parse_frequency_raises_on_parse_error(frequency):
  52. with pytest.raises(ValueError):
  53. module.parse_frequency(frequency)
  54. def test_filter_checks_on_frequency_without_config_uses_default_checks():
  55. flexmock(module).should_receive('parse_frequency').and_return(
  56. module.datetime.timedelta(weeks=4)
  57. )
  58. flexmock(module).should_receive('make_check_time_path')
  59. flexmock(module).should_receive('probe_for_check_time').and_return(None)
  60. assert module.filter_checks_on_frequency(
  61. config={},
  62. borg_repository_id='repo',
  63. checks=('repository', 'archives'),
  64. force=False,
  65. archives_check_id='1234',
  66. ) == ('repository', 'archives')
  67. def test_filter_checks_on_frequency_retains_unconfigured_check():
  68. assert module.filter_checks_on_frequency(
  69. config={},
  70. borg_repository_id='repo',
  71. checks=('data',),
  72. force=False,
  73. ) == ('data',)
  74. def test_filter_checks_on_frequency_retains_check_without_frequency():
  75. flexmock(module).should_receive('parse_frequency').and_return(None)
  76. assert module.filter_checks_on_frequency(
  77. config={'checks': [{'name': 'archives'}]},
  78. borg_repository_id='repo',
  79. checks=('archives',),
  80. force=False,
  81. archives_check_id='1234',
  82. ) == ('archives',)
  83. def test_filter_checks_on_frequency_retains_check_with_empty_only_run_on():
  84. flexmock(module).should_receive('parse_frequency').and_return(None)
  85. assert module.filter_checks_on_frequency(
  86. config={'checks': [{'name': 'archives', 'only_run_on': []}]},
  87. borg_repository_id='repo',
  88. checks=('archives',),
  89. force=False,
  90. archives_check_id='1234',
  91. datetime_now=flexmock(weekday=lambda: 0),
  92. ) == ('archives',)
  93. def test_filter_checks_on_frequency_retains_check_with_only_run_on_matching_today():
  94. flexmock(module).should_receive('parse_frequency').and_return(None)
  95. assert module.filter_checks_on_frequency(
  96. config={'checks': [{'name': 'archives', 'only_run_on': [module.calendar.day_name[0]]}]},
  97. borg_repository_id='repo',
  98. checks=('archives',),
  99. force=False,
  100. archives_check_id='1234',
  101. datetime_now=flexmock(weekday=lambda: 0),
  102. ) == ('archives',)
  103. def test_filter_checks_on_frequency_retains_check_with_only_run_on_matching_today_via_weekday_value():
  104. flexmock(module).should_receive('parse_frequency').and_return(None)
  105. assert module.filter_checks_on_frequency(
  106. config={'checks': [{'name': 'archives', 'only_run_on': ['weekday']}]},
  107. borg_repository_id='repo',
  108. checks=('archives',),
  109. force=False,
  110. archives_check_id='1234',
  111. datetime_now=flexmock(weekday=lambda: 0),
  112. ) == ('archives',)
  113. def test_filter_checks_on_frequency_retains_check_with_only_run_on_matching_today_via_weekend_value():
  114. flexmock(module).should_receive('parse_frequency').and_return(None)
  115. assert module.filter_checks_on_frequency(
  116. config={'checks': [{'name': 'archives', 'only_run_on': ['weekend']}]},
  117. borg_repository_id='repo',
  118. checks=('archives',),
  119. force=False,
  120. archives_check_id='1234',
  121. datetime_now=flexmock(weekday=lambda: 6),
  122. ) == ('archives',)
  123. def test_filter_checks_on_frequency_skips_check_with_only_run_on_not_matching_today():
  124. flexmock(module).should_receive('parse_frequency').and_return(None)
  125. assert (
  126. module.filter_checks_on_frequency(
  127. config={'checks': [{'name': 'archives', 'only_run_on': [module.calendar.day_name[5]]}]},
  128. borg_repository_id='repo',
  129. checks=('archives',),
  130. force=False,
  131. archives_check_id='1234',
  132. datetime_now=flexmock(weekday=lambda: 0),
  133. )
  134. == ()
  135. )
  136. def test_filter_checks_on_frequency_retains_check_with_elapsed_frequency():
  137. flexmock(module).should_receive('parse_frequency').and_return(
  138. module.datetime.timedelta(hours=1)
  139. )
  140. flexmock(module).should_receive('make_check_time_path')
  141. flexmock(module).should_receive('probe_for_check_time').and_return(
  142. module.datetime.datetime(year=module.datetime.MINYEAR, month=1, day=1)
  143. )
  144. assert module.filter_checks_on_frequency(
  145. config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  146. borg_repository_id='repo',
  147. checks=('archives',),
  148. force=False,
  149. archives_check_id='1234',
  150. ) == ('archives',)
  151. def test_filter_checks_on_frequency_retains_check_with_missing_check_time_file():
  152. flexmock(module).should_receive('parse_frequency').and_return(
  153. module.datetime.timedelta(hours=1)
  154. )
  155. flexmock(module).should_receive('make_check_time_path')
  156. flexmock(module).should_receive('probe_for_check_time').and_return(None)
  157. assert module.filter_checks_on_frequency(
  158. config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  159. borg_repository_id='repo',
  160. checks=('archives',),
  161. force=False,
  162. archives_check_id='1234',
  163. ) == ('archives',)
  164. def test_filter_checks_on_frequency_skips_check_with_unelapsed_frequency():
  165. flexmock(module).should_receive('parse_frequency').and_return(
  166. module.datetime.timedelta(hours=1)
  167. )
  168. flexmock(module).should_receive('make_check_time_path')
  169. flexmock(module).should_receive('probe_for_check_time').and_return(
  170. module.datetime.datetime.now()
  171. )
  172. assert (
  173. module.filter_checks_on_frequency(
  174. config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  175. borg_repository_id='repo',
  176. checks=('archives',),
  177. force=False,
  178. archives_check_id='1234',
  179. )
  180. == ()
  181. )
  182. def test_filter_checks_on_frequency_retains_check_with_unelapsed_frequency_and_force():
  183. assert module.filter_checks_on_frequency(
  184. config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  185. borg_repository_id='repo',
  186. checks=('archives',),
  187. force=True,
  188. archives_check_id='1234',
  189. ) == ('archives',)
  190. def test_filter_checks_on_frequency_passes_through_empty_checks():
  191. assert (
  192. module.filter_checks_on_frequency(
  193. config={'checks': [{'name': 'archives', 'frequency': '1 hour'}]},
  194. borg_repository_id='repo',
  195. checks=(),
  196. force=False,
  197. archives_check_id='1234',
  198. )
  199. == ()
  200. )
  201. def test_make_archives_check_id_with_flags_returns_a_value_and_does_not_raise():
  202. assert module.make_archives_check_id(('--match-archives', 'sh:foo-*'))
  203. def test_make_archives_check_id_with_empty_flags_returns_none():
  204. assert module.make_archives_check_id(()) is None
  205. def test_make_check_time_path_with_borgmatic_source_directory_includes_it():
  206. flexmock(module.borgmatic.config.paths).should_receive(
  207. 'get_borgmatic_state_directory'
  208. ).and_return(
  209. '/home/user/.local/state/borgmatic',
  210. )
  211. assert (
  212. module.make_check_time_path({}, '1234', 'archives', '5678')
  213. == '/home/user/.local/state/borgmatic/checks/1234/archives/5678'
  214. )
  215. def test_make_check_time_path_with_archives_check_and_no_archives_check_id_defaults_to_all():
  216. flexmock(module.borgmatic.config.paths).should_receive(
  217. 'get_borgmatic_state_directory'
  218. ).and_return(
  219. '/home/user/.local/state/borgmatic',
  220. )
  221. assert (
  222. module.make_check_time_path(
  223. {},
  224. '1234',
  225. 'archives',
  226. )
  227. == '/home/user/.local/state/borgmatic/checks/1234/archives/all'
  228. )
  229. def test_make_check_time_path_with_repositories_check_ignores_archives_check_id():
  230. flexmock(module.borgmatic.config.paths).should_receive(
  231. 'get_borgmatic_state_directory'
  232. ).and_return(
  233. '/home/user/.local/state/borgmatic',
  234. )
  235. assert (
  236. module.make_check_time_path({}, '1234', 'repository', '5678')
  237. == '/home/user/.local/state/borgmatic/checks/1234/repository'
  238. )
  239. def test_read_check_time_does_not_raise():
  240. flexmock(module.os).should_receive('stat').and_return(flexmock(st_mtime=123))
  241. assert module.read_check_time('/path')
  242. def test_read_check_time_on_missing_file_does_not_raise():
  243. flexmock(module.os).should_receive('stat').and_raise(FileNotFoundError)
  244. assert module.read_check_time('/path') is None
  245. def test_probe_for_check_time_uses_maximum_of_multiple_check_times():
  246. flexmock(module).should_receive('make_check_time_path').and_return(
  247. '~/.borgmatic/checks/1234/archives/5678'
  248. ).and_return('~/.borgmatic/checks/1234/archives/all')
  249. flexmock(module).should_receive('read_check_time').and_return(1).and_return(2)
  250. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 2
  251. def test_probe_for_check_time_deduplicates_identical_check_time_paths():
  252. flexmock(module).should_receive('make_check_time_path').and_return(
  253. '~/.borgmatic/checks/1234/archives/5678'
  254. ).and_return('~/.borgmatic/checks/1234/archives/5678')
  255. flexmock(module).should_receive('read_check_time').and_return(1).once()
  256. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 1
  257. def test_probe_for_check_time_skips_none_check_time():
  258. flexmock(module).should_receive('make_check_time_path').and_return(
  259. '~/.borgmatic/checks/1234/archives/5678'
  260. ).and_return('~/.borgmatic/checks/1234/archives/all')
  261. flexmock(module).should_receive('read_check_time').and_return(None).and_return(2)
  262. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 2
  263. def test_probe_for_check_time_uses_single_check_time():
  264. flexmock(module).should_receive('make_check_time_path').and_return(
  265. '~/.borgmatic/checks/1234/archives/5678'
  266. ).and_return('~/.borgmatic/checks/1234/archives/all')
  267. flexmock(module).should_receive('read_check_time').and_return(1).and_return(None)
  268. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) == 1
  269. def test_probe_for_check_time_returns_none_when_no_check_time_found():
  270. flexmock(module).should_receive('make_check_time_path').and_return(
  271. '~/.borgmatic/checks/1234/archives/5678'
  272. ).and_return('~/.borgmatic/checks/1234/archives/all')
  273. flexmock(module).should_receive('read_check_time').and_return(None).and_return(None)
  274. assert module.probe_for_check_time(flexmock(), flexmock(), flexmock(), flexmock()) is None
  275. def test_upgrade_check_times_moves_checks_from_borgmatic_source_directory_to_state_directory():
  276. flexmock(module.borgmatic.config.paths).should_receive(
  277. 'get_borgmatic_source_directory'
  278. ).and_return('/home/user/.borgmatic')
  279. flexmock(module.borgmatic.config.paths).should_receive(
  280. 'get_borgmatic_state_directory'
  281. ).and_return('/home/user/.local/state/borgmatic')
  282. flexmock(module.os.path).should_receive('exists').with_args(
  283. '/home/user/.borgmatic/checks'
  284. ).and_return(True)
  285. flexmock(module.os.path).should_receive('exists').with_args(
  286. '/home/user/.local/state/borgmatic/checks'
  287. ).and_return(False)
  288. flexmock(module.os).should_receive('makedirs')
  289. flexmock(module.shutil).should_receive('move').with_args(
  290. '/home/user/.borgmatic/checks', '/home/user/.local/state/borgmatic/checks'
  291. ).once()
  292. flexmock(module).should_receive('make_check_time_path').and_return(
  293. '/home/user/.local/state/borgmatic/checks/1234/archives/all'
  294. )
  295. flexmock(module.os.path).should_receive('isfile').and_return(False)
  296. flexmock(module.os).should_receive('mkdir').never()
  297. module.upgrade_check_times(flexmock(), flexmock())
  298. def test_upgrade_check_times_with_checks_already_in_borgmatic_state_directory_does_not_move_anything():
  299. flexmock(module.borgmatic.config.paths).should_receive(
  300. 'get_borgmatic_source_directory'
  301. ).and_return('/home/user/.borgmatic')
  302. flexmock(module.borgmatic.config.paths).should_receive(
  303. 'get_borgmatic_state_directory'
  304. ).and_return('/home/user/.local/state/borgmatic')
  305. flexmock(module.os.path).should_receive('exists').with_args(
  306. '/home/user/.borgmatic/checks'
  307. ).and_return(True)
  308. flexmock(module.os.path).should_receive('exists').with_args(
  309. '/home/user/.local/state/borgmatic/checks'
  310. ).and_return(True)
  311. flexmock(module.os).should_receive('makedirs').never()
  312. flexmock(module.shutil).should_receive('move').never()
  313. flexmock(module).should_receive('make_check_time_path').and_return(
  314. '/home/user/.local/state/borgmatic/checks/1234/archives/all'
  315. )
  316. flexmock(module.os.path).should_receive('isfile').and_return(False)
  317. flexmock(module.shutil).should_receive('move').never()
  318. flexmock(module.os).should_receive('mkdir').never()
  319. module.upgrade_check_times(flexmock(), flexmock())
  320. def test_upgrade_check_times_renames_old_check_paths_to_all():
  321. flexmock(module.borgmatic.config.paths).should_receive(
  322. 'get_borgmatic_source_directory'
  323. ).and_return('/home/user/.borgmatic')
  324. flexmock(module.borgmatic.config.paths).should_receive(
  325. 'get_borgmatic_state_directory'
  326. ).and_return('/home/user/.local/state/borgmatic')
  327. flexmock(module.os.path).should_receive('exists').and_return(False)
  328. base_path = '/home/user/.local/state/borgmatic/checks/1234'
  329. flexmock(module).should_receive('make_check_time_path').with_args(
  330. object, object, 'archives', 'all'
  331. ).and_return(f'{base_path}/archives/all')
  332. flexmock(module).should_receive('make_check_time_path').with_args(
  333. object, object, 'data', 'all'
  334. ).and_return(f'{base_path}/data/all')
  335. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/archives').and_return(
  336. True
  337. )
  338. flexmock(module.os.path).should_receive('isfile').with_args(
  339. f'{base_path}/archives.temp'
  340. ).and_return(False)
  341. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/data').and_return(
  342. False
  343. )
  344. flexmock(module.os.path).should_receive('isfile').with_args(
  345. f'{base_path}/data.temp'
  346. ).and_return(False)
  347. flexmock(module.shutil).should_receive('move').with_args(
  348. f'{base_path}/archives', f'{base_path}/archives.temp'
  349. ).once()
  350. flexmock(module.os).should_receive('mkdir').with_args(f'{base_path}/archives').once()
  351. flexmock(module.shutil).should_receive('move').with_args(
  352. f'{base_path}/archives.temp', f'{base_path}/archives/all'
  353. ).once()
  354. module.upgrade_check_times(flexmock(), flexmock())
  355. def test_upgrade_check_times_renames_data_check_paths_when_archives_paths_are_already_upgraded():
  356. flexmock(module.borgmatic.config.paths).should_receive(
  357. 'get_borgmatic_source_directory'
  358. ).and_return('/home/user/.borgmatic')
  359. flexmock(module.borgmatic.config.paths).should_receive(
  360. 'get_borgmatic_state_directory'
  361. ).and_return('/home/user/.local/state/borgmatic')
  362. flexmock(module.os.path).should_receive('exists').and_return(False)
  363. base_path = '/home/user/.local/state/borgmatic/checks/1234'
  364. flexmock(module).should_receive('make_check_time_path').with_args(
  365. object, object, 'archives', 'all'
  366. ).and_return(f'{base_path}/archives/all')
  367. flexmock(module).should_receive('make_check_time_path').with_args(
  368. object, object, 'data', 'all'
  369. ).and_return(f'{base_path}/data/all')
  370. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/archives').and_return(
  371. False
  372. )
  373. flexmock(module.os.path).should_receive('isfile').with_args(
  374. f'{base_path}/archives.temp'
  375. ).and_return(False)
  376. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/data').and_return(
  377. True
  378. )
  379. flexmock(module.shutil).should_receive('move').with_args(
  380. f'{base_path}/data', f'{base_path}/data.temp'
  381. ).once()
  382. flexmock(module.os).should_receive('mkdir').with_args(f'{base_path}/data').once()
  383. flexmock(module.shutil).should_receive('move').with_args(
  384. f'{base_path}/data.temp', f'{base_path}/data/all'
  385. ).once()
  386. module.upgrade_check_times(flexmock(), flexmock())
  387. def test_upgrade_check_times_skips_already_upgraded_check_paths():
  388. flexmock(module.borgmatic.config.paths).should_receive(
  389. 'get_borgmatic_source_directory'
  390. ).and_return('/home/user/.borgmatic')
  391. flexmock(module.borgmatic.config.paths).should_receive(
  392. 'get_borgmatic_state_directory'
  393. ).and_return('/home/user/.local/state/borgmatic')
  394. flexmock(module.os.path).should_receive('exists').and_return(False)
  395. flexmock(module).should_receive('make_check_time_path').and_return(
  396. '/home/user/.local/state/borgmatic/checks/1234/archives/all'
  397. )
  398. flexmock(module.os.path).should_receive('isfile').and_return(False)
  399. flexmock(module.shutil).should_receive('move').never()
  400. flexmock(module.os).should_receive('mkdir').never()
  401. module.upgrade_check_times(flexmock(), flexmock())
  402. def test_upgrade_check_times_renames_stale_temporary_check_path():
  403. flexmock(module.borgmatic.config.paths).should_receive(
  404. 'get_borgmatic_source_directory'
  405. ).and_return('/home/user/.borgmatic')
  406. flexmock(module.borgmatic.config.paths).should_receive(
  407. 'get_borgmatic_state_directory'
  408. ).and_return('/home/user/.local/state/borgmatic')
  409. flexmock(module.os.path).should_receive('exists').and_return(False)
  410. base_path = '/home/borgmatic/.local/state/checks/1234'
  411. flexmock(module).should_receive('make_check_time_path').with_args(
  412. object, object, 'archives', 'all'
  413. ).and_return(f'{base_path}/archives/all')
  414. flexmock(module).should_receive('make_check_time_path').with_args(
  415. object, object, 'data', 'all'
  416. ).and_return(f'{base_path}/data/all')
  417. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/archives').and_return(
  418. False
  419. )
  420. flexmock(module.os.path).should_receive('isfile').with_args(
  421. f'{base_path}/archives.temp'
  422. ).and_return(True)
  423. flexmock(module.os.path).should_receive('isfile').with_args(f'{base_path}/data').and_return(
  424. False
  425. )
  426. flexmock(module.os.path).should_receive('isfile').with_args(
  427. f'{base_path}/data.temp'
  428. ).and_return(False)
  429. flexmock(module.shutil).should_receive('move').with_args(
  430. f'{base_path}/archives', f'{base_path}/archives.temp'
  431. ).and_raise(FileNotFoundError)
  432. flexmock(module.os).should_receive('mkdir').with_args(f'{base_path}/archives').once()
  433. flexmock(module.shutil).should_receive('move').with_args(
  434. f'{base_path}/archives.temp', f'{base_path}/archives/all'
  435. ).once()
  436. module.upgrade_check_times(flexmock(), flexmock())
  437. def test_collect_spot_check_source_paths_parses_borg_output():
  438. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return(
  439. {'hook1': False, 'hook2': True}
  440. )
  441. flexmock(module.borgmatic.borg.create).should_receive('make_base_create_command').with_args(
  442. dry_run=True,
  443. repository_path='repo',
  444. config=object,
  445. config_paths=(),
  446. local_borg_version=object,
  447. global_arguments=object,
  448. borgmatic_runtime_directories=(),
  449. local_path=object,
  450. remote_path=object,
  451. list_files=True,
  452. stream_processes=True,
  453. ).and_return((('borg', 'create'), ('repo::archive',), flexmock(), flexmock()))
  454. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  455. flexmock()
  456. )
  457. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  458. flexmock(module.borgmatic.execute).should_receive(
  459. 'execute_command_and_capture_output'
  460. ).and_return(
  461. 'warning: stuff\n- /etc/path\n+ /etc/other\n? /nope',
  462. )
  463. flexmock(module.os.path).should_receive('isfile').and_return(True)
  464. assert module.collect_spot_check_source_paths(
  465. repository={'path': 'repo'},
  466. config={'working_directory': '/'},
  467. local_borg_version=flexmock(),
  468. global_arguments=flexmock(),
  469. local_path=flexmock(),
  470. remote_path=flexmock(),
  471. ) == ('/etc/path', '/etc/other')
  472. def test_collect_spot_check_source_paths_passes_through_stream_processes_false():
  473. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return(
  474. {'hook1': False, 'hook2': False}
  475. )
  476. flexmock(module.borgmatic.borg.create).should_receive('make_base_create_command').with_args(
  477. dry_run=True,
  478. repository_path='repo',
  479. config=object,
  480. config_paths=(),
  481. local_borg_version=object,
  482. global_arguments=object,
  483. borgmatic_runtime_directories=(),
  484. local_path=object,
  485. remote_path=object,
  486. list_files=True,
  487. stream_processes=False,
  488. ).and_return((('borg', 'create'), ('repo::archive',), flexmock(), flexmock()))
  489. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  490. flexmock()
  491. )
  492. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  493. flexmock(module.borgmatic.execute).should_receive(
  494. 'execute_command_and_capture_output'
  495. ).and_return(
  496. 'warning: stuff\n- /etc/path\n+ /etc/other\n? /nope',
  497. )
  498. flexmock(module.os.path).should_receive('isfile').and_return(True)
  499. assert module.collect_spot_check_source_paths(
  500. repository={'path': 'repo'},
  501. config={'working_directory': '/'},
  502. local_borg_version=flexmock(),
  503. global_arguments=flexmock(),
  504. local_path=flexmock(),
  505. remote_path=flexmock(),
  506. ) == ('/etc/path', '/etc/other')
  507. def test_collect_spot_check_source_paths_without_working_directory_parses_borg_output():
  508. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return(
  509. {'hook1': False, 'hook2': True}
  510. )
  511. flexmock(module.borgmatic.borg.create).should_receive('make_base_create_command').with_args(
  512. dry_run=True,
  513. repository_path='repo',
  514. config=object,
  515. config_paths=(),
  516. local_borg_version=object,
  517. global_arguments=object,
  518. borgmatic_runtime_directories=(),
  519. local_path=object,
  520. remote_path=object,
  521. list_files=True,
  522. stream_processes=True,
  523. ).and_return((('borg', 'create'), ('repo::archive',), flexmock(), flexmock()))
  524. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  525. flexmock()
  526. )
  527. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  528. flexmock(module.borgmatic.execute).should_receive(
  529. 'execute_command_and_capture_output'
  530. ).and_return(
  531. 'warning: stuff\n- /etc/path\n+ /etc/other\n? /nope',
  532. )
  533. flexmock(module.os.path).should_receive('isfile').and_return(True)
  534. assert module.collect_spot_check_source_paths(
  535. repository={'path': 'repo'},
  536. config={},
  537. local_borg_version=flexmock(),
  538. global_arguments=flexmock(),
  539. local_path=flexmock(),
  540. remote_path=flexmock(),
  541. ) == ('/etc/path', '/etc/other')
  542. def test_collect_spot_check_source_paths_skips_directories():
  543. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return(
  544. {'hook1': False, 'hook2': True}
  545. )
  546. flexmock(module.borgmatic.borg.create).should_receive('make_base_create_command').with_args(
  547. dry_run=True,
  548. repository_path='repo',
  549. config=object,
  550. config_paths=(),
  551. local_borg_version=object,
  552. global_arguments=object,
  553. borgmatic_runtime_directories=(),
  554. local_path=object,
  555. remote_path=object,
  556. list_files=True,
  557. stream_processes=True,
  558. ).and_return((('borg', 'create'), ('repo::archive',), flexmock(), flexmock()))
  559. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  560. flexmock()
  561. )
  562. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  563. flexmock(module.borgmatic.execute).should_receive(
  564. 'execute_command_and_capture_output'
  565. ).and_return(
  566. 'warning: stuff\n- /etc/path\n+ /etc/dir\n? /nope',
  567. )
  568. flexmock(module.os.path).should_receive('isfile').with_args('/etc/path').and_return(False)
  569. flexmock(module.os.path).should_receive('isfile').with_args('/etc/dir').and_return(False)
  570. assert (
  571. module.collect_spot_check_source_paths(
  572. repository={'path': 'repo'},
  573. config={'working_directory': '/'},
  574. local_borg_version=flexmock(),
  575. global_arguments=flexmock(),
  576. local_path=flexmock(),
  577. remote_path=flexmock(),
  578. )
  579. == ()
  580. )
  581. def test_collect_spot_check_archive_paths_excludes_directories():
  582. flexmock(module.borgmatic.config.paths).should_receive(
  583. 'get_borgmatic_source_directory'
  584. ).and_return('/home/user/.borgmatic')
  585. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  586. (
  587. 'f /etc/path',
  588. 'f /etc/other',
  589. 'd /etc/dir',
  590. )
  591. )
  592. assert module.collect_spot_check_archive_paths(
  593. repository={'path': 'repo'},
  594. archive='archive',
  595. config={},
  596. local_borg_version=flexmock(),
  597. global_arguments=flexmock(),
  598. local_path=flexmock(),
  599. remote_path=flexmock(),
  600. borgmatic_runtime_directory='/run/user/1001/borgmatic',
  601. ) == ('/etc/path', '/etc/other')
  602. def test_collect_spot_check_archive_paths_excludes_file_in_borgmatic_runtime_directory_as_stored_with_prefix_truncation():
  603. flexmock(module.borgmatic.config.paths).should_receive(
  604. 'get_borgmatic_source_directory'
  605. ).and_return('/root/.borgmatic')
  606. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  607. (
  608. 'f /etc/path',
  609. 'f /borgmatic/some/thing',
  610. )
  611. )
  612. assert module.collect_spot_check_archive_paths(
  613. repository={'path': 'repo'},
  614. archive='archive',
  615. config={},
  616. local_borg_version=flexmock(),
  617. global_arguments=flexmock(),
  618. local_path=flexmock(),
  619. remote_path=flexmock(),
  620. borgmatic_runtime_directory='/run/user/0/borgmatic',
  621. ) == ('/etc/path',)
  622. def test_collect_spot_check_archive_paths_excludes_file_in_borgmatic_source_directory():
  623. flexmock(module.borgmatic.config.paths).should_receive(
  624. 'get_borgmatic_source_directory'
  625. ).and_return('/root/.borgmatic')
  626. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  627. (
  628. 'f /etc/path',
  629. 'f /root/.borgmatic/some/thing',
  630. )
  631. )
  632. assert module.collect_spot_check_archive_paths(
  633. repository={'path': 'repo'},
  634. archive='archive',
  635. config={},
  636. local_borg_version=flexmock(),
  637. global_arguments=flexmock(),
  638. local_path=flexmock(),
  639. remote_path=flexmock(),
  640. borgmatic_runtime_directory='/run/user/0/borgmatic',
  641. ) == ('/etc/path',)
  642. def test_collect_spot_check_archive_paths_excludes_file_in_borgmatic_runtime_directory():
  643. flexmock(module.borgmatic.config.paths).should_receive(
  644. 'get_borgmatic_source_directory'
  645. ).and_return('/root.borgmatic')
  646. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  647. (
  648. 'f /etc/path',
  649. 'f /run/user/0/borgmatic/some/thing',
  650. )
  651. )
  652. assert module.collect_spot_check_archive_paths(
  653. repository={'path': 'repo'},
  654. archive='archive',
  655. config={},
  656. local_borg_version=flexmock(),
  657. global_arguments=flexmock(),
  658. local_path=flexmock(),
  659. remote_path=flexmock(),
  660. borgmatic_runtime_directory='/run/user/0/borgmatic',
  661. ) == ('/etc/path',)
  662. def test_collect_spot_check_source_paths_uses_working_directory():
  663. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return(
  664. {'hook1': False, 'hook2': True}
  665. )
  666. flexmock(module.borgmatic.borg.create).should_receive('make_base_create_command').with_args(
  667. dry_run=True,
  668. repository_path='repo',
  669. config=object,
  670. config_paths=(),
  671. local_borg_version=object,
  672. global_arguments=object,
  673. borgmatic_runtime_directories=(),
  674. local_path=object,
  675. remote_path=object,
  676. list_files=True,
  677. stream_processes=True,
  678. ).and_return((('borg', 'create'), ('repo::archive',), flexmock(), flexmock()))
  679. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  680. flexmock()
  681. )
  682. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  683. '/working/dir'
  684. )
  685. flexmock(module.borgmatic.execute).should_receive(
  686. 'execute_command_and_capture_output'
  687. ).and_return(
  688. 'warning: stuff\n- foo\n+ bar\n? /nope',
  689. )
  690. flexmock(module.os.path).should_receive('isfile').with_args('/working/dir/foo').and_return(True)
  691. flexmock(module.os.path).should_receive('isfile').with_args('/working/dir/bar').and_return(True)
  692. assert module.collect_spot_check_source_paths(
  693. repository={'path': 'repo'},
  694. config={'working_directory': '/working/dir'},
  695. local_borg_version=flexmock(),
  696. global_arguments=flexmock(),
  697. local_path=flexmock(),
  698. remote_path=flexmock(),
  699. ) == ('foo', 'bar')
  700. def test_compare_spot_check_hashes_returns_paths_having_failing_hashes():
  701. flexmock(module.random).should_receive('sample').replace_with(
  702. lambda population, count: population[:count]
  703. )
  704. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  705. None,
  706. )
  707. flexmock(module.os.path).should_receive('exists').and_return(True)
  708. flexmock(module.borgmatic.execute).should_receive(
  709. 'execute_command_and_capture_output'
  710. ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  711. 'hash1 /foo\nhash2 /bar'
  712. )
  713. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  714. ['hash1 /foo', 'nothash2 /bar']
  715. )
  716. assert module.compare_spot_check_hashes(
  717. repository={'path': 'repo'},
  718. archive='archive',
  719. config={
  720. 'checks': [
  721. {
  722. 'name': 'archives',
  723. 'frequency': '2 weeks',
  724. },
  725. {
  726. 'name': 'spot',
  727. 'data_sample_percentage': 50,
  728. },
  729. ]
  730. },
  731. local_borg_version=flexmock(),
  732. global_arguments=flexmock(),
  733. local_path=flexmock(),
  734. remote_path=flexmock(),
  735. log_label='repo',
  736. source_paths=('/foo', '/bar', '/baz', '/quux'),
  737. ) == ('/bar',)
  738. def test_compare_spot_check_hashes_handles_data_sample_percentage_above_100():
  739. flexmock(module.random).should_receive('sample').replace_with(
  740. lambda population, count: population[:count]
  741. )
  742. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  743. None,
  744. )
  745. flexmock(module.os.path).should_receive('exists').and_return(True)
  746. flexmock(module.borgmatic.execute).should_receive(
  747. 'execute_command_and_capture_output'
  748. ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  749. 'hash1 /foo\nhash2 /bar'
  750. )
  751. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  752. ['nothash1 /foo', 'nothash2 /bar']
  753. )
  754. assert module.compare_spot_check_hashes(
  755. repository={'path': 'repo'},
  756. archive='archive',
  757. config={
  758. 'checks': [
  759. {
  760. 'name': 'archives',
  761. 'frequency': '2 weeks',
  762. },
  763. {
  764. 'name': 'spot',
  765. 'data_sample_percentage': 1000,
  766. },
  767. ]
  768. },
  769. local_borg_version=flexmock(),
  770. global_arguments=flexmock(),
  771. local_path=flexmock(),
  772. remote_path=flexmock(),
  773. log_label='repo',
  774. source_paths=('/foo', '/bar'),
  775. ) == ('/foo', '/bar')
  776. def test_compare_spot_check_hashes_uses_xxh64sum_command_option():
  777. flexmock(module.random).should_receive('sample').replace_with(
  778. lambda population, count: population[:count]
  779. )
  780. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  781. None,
  782. )
  783. flexmock(module.os.path).should_receive('exists').and_return(True)
  784. flexmock(module.borgmatic.execute).should_receive(
  785. 'execute_command_and_capture_output'
  786. ).with_args(('/usr/local/bin/xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  787. 'hash1 /foo\nhash2 /bar'
  788. )
  789. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  790. ['hash1 /foo', 'nothash2 /bar']
  791. )
  792. assert module.compare_spot_check_hashes(
  793. repository={'path': 'repo'},
  794. archive='archive',
  795. config={
  796. 'checks': [
  797. {
  798. 'name': 'spot',
  799. 'data_sample_percentage': 50,
  800. 'xxh64sum_command': '/usr/local/bin/xxh64sum',
  801. },
  802. ]
  803. },
  804. local_borg_version=flexmock(),
  805. global_arguments=flexmock(),
  806. local_path=flexmock(),
  807. remote_path=flexmock(),
  808. log_label='repo',
  809. source_paths=('/foo', '/bar', '/baz', '/quux'),
  810. ) == ('/bar',)
  811. def test_compare_spot_check_hashes_considers_path_missing_from_archive_as_not_matching():
  812. flexmock(module.random).should_receive('sample').replace_with(
  813. lambda population, count: population[:count]
  814. )
  815. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  816. None,
  817. )
  818. flexmock(module.os.path).should_receive('exists').and_return(True)
  819. flexmock(module.borgmatic.execute).should_receive(
  820. 'execute_command_and_capture_output'
  821. ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  822. 'hash1 /foo\nhash2 /bar'
  823. )
  824. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  825. ['hash1 /foo']
  826. )
  827. assert module.compare_spot_check_hashes(
  828. repository={'path': 'repo'},
  829. archive='archive',
  830. config={
  831. 'checks': [
  832. {
  833. 'name': 'spot',
  834. 'data_sample_percentage': 50,
  835. },
  836. ]
  837. },
  838. local_borg_version=flexmock(),
  839. global_arguments=flexmock(),
  840. local_path=flexmock(),
  841. remote_path=flexmock(),
  842. log_label='repo',
  843. source_paths=('/foo', '/bar', '/baz', '/quux'),
  844. ) == ('/bar',)
  845. def test_compare_spot_check_hashes_considers_non_existent_path_as_not_matching():
  846. flexmock(module.random).should_receive('sample').replace_with(
  847. lambda population, count: population[:count]
  848. )
  849. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  850. None,
  851. )
  852. flexmock(module.os.path).should_receive('exists').with_args('/foo').and_return(True)
  853. flexmock(module.os.path).should_receive('exists').with_args('/bar').and_return(False)
  854. flexmock(module.borgmatic.execute).should_receive(
  855. 'execute_command_and_capture_output'
  856. ).with_args(('xxh64sum', '/foo'), working_directory=None).and_return('hash1 /foo')
  857. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  858. ['hash1 /foo', 'hash2 /bar']
  859. )
  860. assert module.compare_spot_check_hashes(
  861. repository={'path': 'repo'},
  862. archive='archive',
  863. config={
  864. 'checks': [
  865. {
  866. 'name': 'spot',
  867. 'data_sample_percentage': 50,
  868. },
  869. ]
  870. },
  871. local_borg_version=flexmock(),
  872. global_arguments=flexmock(),
  873. local_path=flexmock(),
  874. remote_path=flexmock(),
  875. log_label='repo',
  876. source_paths=('/foo', '/bar', '/baz', '/quux'),
  877. ) == ('/bar',)
  878. def test_compare_spot_check_hashes_with_too_many_paths_feeds_them_to_commands_in_chunks():
  879. flexmock(module).SAMPLE_PATHS_SUBSET_COUNT = 2
  880. flexmock(module.random).should_receive('sample').replace_with(
  881. lambda population, count: population[:count]
  882. )
  883. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  884. None,
  885. )
  886. flexmock(module.os.path).should_receive('exists').and_return(True)
  887. flexmock(module.borgmatic.execute).should_receive(
  888. 'execute_command_and_capture_output'
  889. ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  890. 'hash1 /foo\nhash2 /bar'
  891. )
  892. flexmock(module.borgmatic.execute).should_receive(
  893. 'execute_command_and_capture_output'
  894. ).with_args(('xxh64sum', '/baz', '/quux'), working_directory=None).and_return(
  895. 'hash3 /baz\nhash4 /quux'
  896. )
  897. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  898. ['hash1 /foo', 'hash2 /bar']
  899. ).and_return(['hash3 /baz', 'nothash4 /quux'])
  900. assert module.compare_spot_check_hashes(
  901. repository={'path': 'repo'},
  902. archive='archive',
  903. config={
  904. 'checks': [
  905. {
  906. 'name': 'archives',
  907. 'frequency': '2 weeks',
  908. },
  909. {
  910. 'name': 'spot',
  911. 'data_sample_percentage': 100,
  912. },
  913. ]
  914. },
  915. local_borg_version=flexmock(),
  916. global_arguments=flexmock(),
  917. local_path=flexmock(),
  918. remote_path=flexmock(),
  919. log_label='repo',
  920. source_paths=('/foo', '/bar', '/baz', '/quux'),
  921. ) == ('/quux',)
  922. def test_compare_spot_check_hashes_uses_working_directory_to_access_source_paths():
  923. flexmock(module.random).should_receive('sample').replace_with(
  924. lambda population, count: population[:count]
  925. )
  926. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  927. '/working/dir',
  928. )
  929. flexmock(module.os.path).should_receive('exists').with_args('/working/dir/foo').and_return(True)
  930. flexmock(module.os.path).should_receive('exists').with_args('/working/dir/bar').and_return(True)
  931. flexmock(module.borgmatic.execute).should_receive(
  932. 'execute_command_and_capture_output'
  933. ).with_args(('xxh64sum', 'foo', 'bar'), working_directory='/working/dir').and_return(
  934. 'hash1 foo\nhash2 bar'
  935. )
  936. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  937. ['hash1 foo', 'nothash2 bar']
  938. )
  939. assert module.compare_spot_check_hashes(
  940. repository={'path': 'repo'},
  941. archive='archive',
  942. config={
  943. 'checks': [
  944. {
  945. 'name': 'archives',
  946. 'frequency': '2 weeks',
  947. },
  948. {
  949. 'name': 'spot',
  950. 'data_sample_percentage': 50,
  951. },
  952. ],
  953. 'working_directory': '/working/dir',
  954. },
  955. local_borg_version=flexmock(),
  956. global_arguments=flexmock(),
  957. local_path=flexmock(),
  958. remote_path=flexmock(),
  959. log_label='repo',
  960. source_paths=('foo', 'bar', 'baz', 'quux'),
  961. ) == ('bar',)
  962. def test_spot_check_without_spot_configuration_errors():
  963. with pytest.raises(ValueError):
  964. module.spot_check(
  965. repository={'path': 'repo'},
  966. config={
  967. 'checks': [
  968. {
  969. 'name': 'archives',
  970. },
  971. ]
  972. },
  973. local_borg_version=flexmock(),
  974. global_arguments=flexmock(),
  975. local_path=flexmock(),
  976. remote_path=flexmock(),
  977. borgmatic_runtime_directory='/run/borgmatic',
  978. )
  979. def test_spot_check_without_any_configuration_errors():
  980. with pytest.raises(ValueError):
  981. module.spot_check(
  982. repository={'path': 'repo'},
  983. config={},
  984. local_borg_version=flexmock(),
  985. global_arguments=flexmock(),
  986. local_path=flexmock(),
  987. remote_path=flexmock(),
  988. borgmatic_runtime_directory='/run/borgmatic',
  989. )
  990. def test_spot_check_data_tolerance_percenatge_greater_than_data_sample_percentage_errors():
  991. with pytest.raises(ValueError):
  992. module.spot_check(
  993. repository={'path': 'repo'},
  994. config={
  995. 'checks': [
  996. {
  997. 'name': 'spot',
  998. 'data_tolerance_percentage': 7,
  999. 'data_sample_percentage': 5,
  1000. },
  1001. ]
  1002. },
  1003. local_borg_version=flexmock(),
  1004. global_arguments=flexmock(),
  1005. local_path=flexmock(),
  1006. remote_path=flexmock(),
  1007. borgmatic_runtime_directory='/run/borgmatic',
  1008. )
  1009. def test_spot_check_with_count_delta_greater_than_count_tolerance_percentage_errors():
  1010. flexmock(module).should_receive('collect_spot_check_source_paths').and_return(
  1011. ('/foo', '/bar', '/baz', '/quux')
  1012. )
  1013. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1014. 'archive'
  1015. )
  1016. flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(
  1017. ('/foo', '/bar')
  1018. ).once()
  1019. with pytest.raises(ValueError):
  1020. module.spot_check(
  1021. repository={'path': 'repo'},
  1022. config={
  1023. 'checks': [
  1024. {
  1025. 'name': 'spot',
  1026. 'count_tolerance_percentage': 1,
  1027. 'data_tolerance_percentage': 4,
  1028. 'data_sample_percentage': 5,
  1029. },
  1030. ]
  1031. },
  1032. local_borg_version=flexmock(),
  1033. global_arguments=flexmock(),
  1034. local_path=flexmock(),
  1035. remote_path=flexmock(),
  1036. borgmatic_runtime_directory='/run/borgmatic',
  1037. )
  1038. def test_spot_check_with_failing_percentage_greater_than_data_tolerance_percentage_errors():
  1039. flexmock(module).should_receive('collect_spot_check_source_paths').and_return(
  1040. ('/foo', '/bar', '/baz', '/quux')
  1041. )
  1042. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1043. 'archive'
  1044. )
  1045. flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(('/foo', '/bar'))
  1046. flexmock(module).should_receive('compare_spot_check_hashes').and_return(
  1047. ('/bar', '/baz', '/quux')
  1048. ).once()
  1049. with pytest.raises(ValueError):
  1050. module.spot_check(
  1051. repository={'path': 'repo'},
  1052. config={
  1053. 'checks': [
  1054. {
  1055. 'name': 'spot',
  1056. 'count_tolerance_percentage': 55,
  1057. 'data_tolerance_percentage': 4,
  1058. 'data_sample_percentage': 5,
  1059. },
  1060. ]
  1061. },
  1062. local_borg_version=flexmock(),
  1063. global_arguments=flexmock(),
  1064. local_path=flexmock(),
  1065. remote_path=flexmock(),
  1066. borgmatic_runtime_directory='/run/borgmatic',
  1067. )
  1068. def test_spot_check_with_high_enough_tolerances_does_not_raise():
  1069. flexmock(module).should_receive('collect_spot_check_source_paths').and_return(
  1070. ('/foo', '/bar', '/baz', '/quux')
  1071. )
  1072. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1073. 'archive'
  1074. )
  1075. flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(('/foo', '/bar'))
  1076. flexmock(module).should_receive('compare_spot_check_hashes').and_return(
  1077. ('/bar', '/baz', '/quux')
  1078. ).once()
  1079. module.spot_check(
  1080. repository={'path': 'repo'},
  1081. config={
  1082. 'checks': [
  1083. {
  1084. 'name': 'spot',
  1085. 'count_tolerance_percentage': 55,
  1086. 'data_tolerance_percentage': 80,
  1087. 'data_sample_percentage': 80,
  1088. },
  1089. ]
  1090. },
  1091. local_borg_version=flexmock(),
  1092. global_arguments=flexmock(),
  1093. local_path=flexmock(),
  1094. remote_path=flexmock(),
  1095. borgmatic_runtime_directory='/run/borgmatic',
  1096. )
  1097. def test_run_check_checks_archives_for_configured_repository():
  1098. flexmock(module.logger).answer = lambda message: None
  1099. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1100. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1101. flexmock(module).should_receive('upgrade_check_times')
  1102. flexmock(module).should_receive('parse_checks')
  1103. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1104. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1105. flexmock(module).should_receive('filter_checks_on_frequency').and_return(
  1106. {'repository', 'archives'}
  1107. )
  1108. flexmock(module.borgmatic.borg.check).should_receive('check_archives').once()
  1109. flexmock(module).should_receive('make_check_time_path')
  1110. flexmock(module).should_receive('write_check_time')
  1111. flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').never()
  1112. flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2)
  1113. check_arguments = flexmock(
  1114. repository=None,
  1115. progress=flexmock(),
  1116. repair=flexmock(),
  1117. only_checks=flexmock(),
  1118. force=flexmock(),
  1119. )
  1120. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1121. module.run_check(
  1122. config_filename='test.yaml',
  1123. repository={'path': 'repo'},
  1124. config={'repositories': ['repo']},
  1125. hook_context={},
  1126. local_borg_version=None,
  1127. check_arguments=check_arguments,
  1128. global_arguments=global_arguments,
  1129. local_path=None,
  1130. remote_path=None,
  1131. )
  1132. def test_run_check_runs_configured_extract_check():
  1133. flexmock(module.logger).answer = lambda message: None
  1134. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1135. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1136. flexmock(module).should_receive('upgrade_check_times')
  1137. flexmock(module).should_receive('parse_checks')
  1138. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1139. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1140. flexmock(module).should_receive('filter_checks_on_frequency').and_return({'extract'})
  1141. flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
  1142. flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').once()
  1143. flexmock(module).should_receive('make_check_time_path')
  1144. flexmock(module).should_receive('write_check_time')
  1145. flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2)
  1146. check_arguments = flexmock(
  1147. repository=None,
  1148. progress=flexmock(),
  1149. repair=flexmock(),
  1150. only_checks=flexmock(),
  1151. force=flexmock(),
  1152. )
  1153. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1154. module.run_check(
  1155. config_filename='test.yaml',
  1156. repository={'path': 'repo'},
  1157. config={'repositories': ['repo']},
  1158. hook_context={},
  1159. local_borg_version=None,
  1160. check_arguments=check_arguments,
  1161. global_arguments=global_arguments,
  1162. local_path=None,
  1163. remote_path=None,
  1164. )
  1165. def test_run_check_runs_configured_spot_check():
  1166. flexmock(module.logger).answer = lambda message: None
  1167. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1168. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1169. flexmock(module).should_receive('upgrade_check_times')
  1170. flexmock(module).should_receive('parse_checks')
  1171. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1172. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1173. flexmock(module).should_receive('filter_checks_on_frequency').and_return({'spot'})
  1174. flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
  1175. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1176. flexmock()
  1177. )
  1178. flexmock(module.borgmatic.actions.check).should_receive('spot_check').once()
  1179. flexmock(module).should_receive('make_check_time_path')
  1180. flexmock(module).should_receive('write_check_time')
  1181. flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2)
  1182. check_arguments = flexmock(
  1183. repository=None,
  1184. progress=flexmock(),
  1185. repair=flexmock(),
  1186. only_checks=flexmock(),
  1187. force=flexmock(),
  1188. )
  1189. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1190. module.run_check(
  1191. config_filename='test.yaml',
  1192. repository={'path': 'repo'},
  1193. config={'repositories': ['repo']},
  1194. hook_context={},
  1195. local_borg_version=None,
  1196. check_arguments=check_arguments,
  1197. global_arguments=global_arguments,
  1198. local_path=None,
  1199. remote_path=None,
  1200. )
  1201. def test_run_check_without_checks_runs_nothing_except_hooks():
  1202. flexmock(module.logger).answer = lambda message: None
  1203. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1204. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1205. flexmock(module).should_receive('upgrade_check_times')
  1206. flexmock(module).should_receive('parse_checks')
  1207. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1208. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1209. flexmock(module).should_receive('filter_checks_on_frequency').and_return({})
  1210. flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
  1211. flexmock(module).should_receive('make_check_time_path')
  1212. flexmock(module).should_receive('write_check_time').never()
  1213. flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').never()
  1214. flexmock(module.borgmatic.hooks.command).should_receive('execute_hook').times(2)
  1215. check_arguments = flexmock(
  1216. repository=None,
  1217. progress=flexmock(),
  1218. repair=flexmock(),
  1219. only_checks=flexmock(),
  1220. force=flexmock(),
  1221. )
  1222. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1223. module.run_check(
  1224. config_filename='test.yaml',
  1225. repository={'path': 'repo'},
  1226. config={'repositories': ['repo']},
  1227. hook_context={},
  1228. local_borg_version=None,
  1229. check_arguments=check_arguments,
  1230. global_arguments=global_arguments,
  1231. local_path=None,
  1232. remote_path=None,
  1233. )
  1234. def test_run_check_checks_archives_in_selected_repository():
  1235. flexmock(module.logger).answer = lambda message: None
  1236. flexmock(module.borgmatic.config.validate).should_receive(
  1237. 'repositories_match'
  1238. ).once().and_return(True)
  1239. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1240. flexmock(module).should_receive('upgrade_check_times')
  1241. flexmock(module).should_receive('parse_checks')
  1242. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1243. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1244. flexmock(module).should_receive('filter_checks_on_frequency').and_return(
  1245. {'repository', 'archives'}
  1246. )
  1247. flexmock(module.borgmatic.borg.check).should_receive('check_archives').once()
  1248. flexmock(module).should_receive('make_check_time_path')
  1249. flexmock(module).should_receive('write_check_time')
  1250. flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').never()
  1251. check_arguments = flexmock(
  1252. repository=flexmock(),
  1253. progress=flexmock(),
  1254. repair=flexmock(),
  1255. only_checks=flexmock(),
  1256. force=flexmock(),
  1257. )
  1258. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1259. module.run_check(
  1260. config_filename='test.yaml',
  1261. repository={'path': 'repo'},
  1262. config={'repositories': ['repo']},
  1263. hook_context={},
  1264. local_borg_version=None,
  1265. check_arguments=check_arguments,
  1266. global_arguments=global_arguments,
  1267. local_path=None,
  1268. remote_path=None,
  1269. )
  1270. def test_run_check_bails_if_repository_does_not_match():
  1271. flexmock(module.logger).answer = lambda message: None
  1272. flexmock(module.borgmatic.config.validate).should_receive(
  1273. 'repositories_match'
  1274. ).once().and_return(False)
  1275. flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
  1276. check_arguments = flexmock(
  1277. repository=flexmock(),
  1278. progress=flexmock(),
  1279. repair=flexmock(),
  1280. only_checks=flexmock(),
  1281. force=flexmock(),
  1282. )
  1283. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1284. module.run_check(
  1285. config_filename='test.yaml',
  1286. repository={'path': 'repo'},
  1287. config={'repositories': ['repo']},
  1288. hook_context={},
  1289. local_borg_version=None,
  1290. check_arguments=check_arguments,
  1291. global_arguments=global_arguments,
  1292. local_path=None,
  1293. remote_path=None,
  1294. )