test_check.py 63 KB

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