test_check.py 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  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('sample').replace_with(
  793. 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('sample').replace_with(
  831. 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('sample').replace_with(
  869. 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('sample').replace_with(
  907. 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(('/usr/local/bin/xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  917. 'hash1 /foo\nhash2 /bar'
  918. )
  919. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  920. ['hash1 foo', 'nothash2 bar']
  921. )
  922. assert module.compare_spot_check_hashes(
  923. repository={'path': 'repo'},
  924. archive='archive',
  925. config={
  926. 'checks': [
  927. {
  928. 'name': 'spot',
  929. 'data_sample_percentage': 50,
  930. 'xxh64sum_command': '/usr/local/bin/xxh64sum',
  931. },
  932. ]
  933. },
  934. local_borg_version=flexmock(),
  935. global_arguments=flexmock(),
  936. local_path=flexmock(),
  937. remote_path=flexmock(),
  938. source_paths=('/foo', '/bar', '/baz', '/quux'),
  939. ) == ('/bar',)
  940. def test_compare_spot_check_hashes_considers_path_missing_from_archive_as_not_matching():
  941. flexmock(module.random).should_receive('sample').replace_with(
  942. lambda population, count: population[:count]
  943. )
  944. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  945. None,
  946. )
  947. flexmock(module.os.path).should_receive('exists').and_return(True)
  948. flexmock(module.os.path).should_receive('islink').and_return(False)
  949. flexmock(module.borgmatic.execute).should_receive(
  950. 'execute_command_and_capture_output'
  951. ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  952. 'hash1 /foo\nhash2 /bar'
  953. )
  954. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  955. ['hash1 foo']
  956. )
  957. assert module.compare_spot_check_hashes(
  958. repository={'path': 'repo'},
  959. archive='archive',
  960. config={
  961. 'checks': [
  962. {
  963. 'name': 'spot',
  964. 'data_sample_percentage': 50,
  965. },
  966. ]
  967. },
  968. local_borg_version=flexmock(),
  969. global_arguments=flexmock(),
  970. local_path=flexmock(),
  971. remote_path=flexmock(),
  972. source_paths=('/foo', '/bar', '/baz', '/quux'),
  973. ) == ('/bar',)
  974. def test_compare_spot_check_hashes_considers_symlink_path_as_not_matching():
  975. flexmock(module.random).should_receive('sample').replace_with(
  976. lambda population, count: population[:count]
  977. )
  978. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  979. None,
  980. )
  981. flexmock(module.os.path).should_receive('exists').and_return(True)
  982. flexmock(module.os.path).should_receive('islink').with_args('/foo').and_return(False)
  983. flexmock(module.os.path).should_receive('islink').with_args('/bar').and_return(True)
  984. flexmock(module.borgmatic.execute).should_receive(
  985. 'execute_command_and_capture_output'
  986. ).with_args(('xxh64sum', '/foo'), working_directory=None).and_return('hash1 /foo')
  987. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  988. ['hash1 foo', 'hash2 bar']
  989. )
  990. assert module.compare_spot_check_hashes(
  991. repository={'path': 'repo'},
  992. archive='archive',
  993. config={
  994. 'checks': [
  995. {
  996. 'name': 'spot',
  997. 'data_sample_percentage': 50,
  998. },
  999. ]
  1000. },
  1001. local_borg_version=flexmock(),
  1002. global_arguments=flexmock(),
  1003. local_path=flexmock(),
  1004. remote_path=flexmock(),
  1005. source_paths=('/foo', '/bar', '/baz', '/quux'),
  1006. ) == ('/bar',)
  1007. def test_compare_spot_check_hashes_considers_non_existent_path_as_not_matching():
  1008. flexmock(module.random).should_receive('sample').replace_with(
  1009. lambda population, count: population[:count]
  1010. )
  1011. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1012. None,
  1013. )
  1014. flexmock(module.os.path).should_receive('exists').with_args('/foo').and_return(True)
  1015. flexmock(module.os.path).should_receive('exists').with_args('/bar').and_return(False)
  1016. flexmock(module.os.path).should_receive('islink').and_return(False)
  1017. flexmock(module.borgmatic.execute).should_receive(
  1018. 'execute_command_and_capture_output'
  1019. ).with_args(('xxh64sum', '/foo'), working_directory=None).and_return('hash1 /foo')
  1020. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  1021. ['hash1 foo', 'hash2 bar']
  1022. )
  1023. assert module.compare_spot_check_hashes(
  1024. repository={'path': 'repo'},
  1025. archive='archive',
  1026. config={
  1027. 'checks': [
  1028. {
  1029. 'name': 'spot',
  1030. 'data_sample_percentage': 50,
  1031. },
  1032. ]
  1033. },
  1034. local_borg_version=flexmock(),
  1035. global_arguments=flexmock(),
  1036. local_path=flexmock(),
  1037. remote_path=flexmock(),
  1038. source_paths=('/foo', '/bar', '/baz', '/quux'),
  1039. ) == ('/bar',)
  1040. def test_compare_spot_check_hashes_with_too_many_paths_feeds_them_to_commands_in_chunks():
  1041. flexmock(module).SAMPLE_PATHS_SUBSET_COUNT = 2
  1042. flexmock(module.random).should_receive('sample').replace_with(
  1043. lambda population, count: population[:count]
  1044. )
  1045. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1046. None,
  1047. )
  1048. flexmock(module.os.path).should_receive('exists').and_return(True)
  1049. flexmock(module.os.path).should_receive('islink').and_return(False)
  1050. flexmock(module.borgmatic.execute).should_receive(
  1051. 'execute_command_and_capture_output'
  1052. ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  1053. 'hash1 /foo\nhash2 /bar'
  1054. )
  1055. flexmock(module.borgmatic.execute).should_receive(
  1056. 'execute_command_and_capture_output'
  1057. ).with_args(('xxh64sum', '/baz', '/quux'), working_directory=None).and_return(
  1058. 'hash3 /baz\nhash4 /quux'
  1059. )
  1060. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  1061. ['hash1 foo', 'hash2 bar']
  1062. ).and_return(['hash3 baz', 'nothash4 quux'])
  1063. assert module.compare_spot_check_hashes(
  1064. repository={'path': 'repo'},
  1065. archive='archive',
  1066. config={
  1067. 'checks': [
  1068. {
  1069. 'name': 'archives',
  1070. 'frequency': '2 weeks',
  1071. },
  1072. {
  1073. 'name': 'spot',
  1074. 'data_sample_percentage': 100,
  1075. },
  1076. ]
  1077. },
  1078. local_borg_version=flexmock(),
  1079. global_arguments=flexmock(),
  1080. local_path=flexmock(),
  1081. remote_path=flexmock(),
  1082. source_paths=('/foo', '/bar', '/baz', '/quux'),
  1083. ) == ('/quux',)
  1084. def test_compare_spot_check_hashes_uses_working_directory_to_access_source_paths():
  1085. flexmock(module.random).should_receive('sample').replace_with(
  1086. lambda population, count: population[:count]
  1087. )
  1088. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1089. '/working/dir',
  1090. )
  1091. flexmock(module.os.path).should_receive('exists').with_args('/working/dir/foo').and_return(True)
  1092. flexmock(module.os.path).should_receive('exists').with_args('/working/dir/bar').and_return(True)
  1093. flexmock(module.os.path).should_receive('islink').and_return(False)
  1094. flexmock(module.borgmatic.execute).should_receive(
  1095. 'execute_command_and_capture_output'
  1096. ).with_args(('xxh64sum', 'foo', 'bar'), working_directory='/working/dir').and_return(
  1097. 'hash1 foo\nhash2 bar'
  1098. )
  1099. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  1100. ['hash1 foo', 'nothash2 bar']
  1101. )
  1102. assert module.compare_spot_check_hashes(
  1103. repository={'path': 'repo'},
  1104. archive='archive',
  1105. config={
  1106. 'checks': [
  1107. {
  1108. 'name': 'archives',
  1109. 'frequency': '2 weeks',
  1110. },
  1111. {
  1112. 'name': 'spot',
  1113. 'data_sample_percentage': 50,
  1114. },
  1115. ],
  1116. 'working_directory': '/working/dir',
  1117. },
  1118. local_borg_version=flexmock(),
  1119. global_arguments=flexmock(),
  1120. local_path=flexmock(),
  1121. remote_path=flexmock(),
  1122. source_paths=('foo', 'bar', 'baz', 'quux'),
  1123. ) == ('bar',)
  1124. def test_spot_check_without_spot_configuration_errors():
  1125. with pytest.raises(ValueError):
  1126. module.spot_check(
  1127. repository={'path': 'repo'},
  1128. config={
  1129. 'checks': [
  1130. {
  1131. 'name': 'archives',
  1132. },
  1133. ]
  1134. },
  1135. local_borg_version=flexmock(),
  1136. global_arguments=flexmock(),
  1137. local_path=flexmock(),
  1138. remote_path=flexmock(),
  1139. borgmatic_runtime_directory='/run/borgmatic',
  1140. )
  1141. def test_spot_check_without_any_configuration_errors():
  1142. with pytest.raises(ValueError):
  1143. module.spot_check(
  1144. repository={'path': 'repo'},
  1145. config={},
  1146. local_borg_version=flexmock(),
  1147. global_arguments=flexmock(),
  1148. local_path=flexmock(),
  1149. remote_path=flexmock(),
  1150. borgmatic_runtime_directory='/run/borgmatic',
  1151. )
  1152. def test_spot_check_data_tolerance_percentage_greater_than_data_sample_percentage_errors():
  1153. with pytest.raises(ValueError):
  1154. module.spot_check(
  1155. repository={'path': 'repo'},
  1156. config={
  1157. 'checks': [
  1158. {
  1159. 'name': 'spot',
  1160. 'data_tolerance_percentage': 7,
  1161. 'data_sample_percentage': 5,
  1162. },
  1163. ]
  1164. },
  1165. local_borg_version=flexmock(),
  1166. global_arguments=flexmock(),
  1167. local_path=flexmock(),
  1168. remote_path=flexmock(),
  1169. borgmatic_runtime_directory='/run/borgmatic',
  1170. )
  1171. def test_spot_check_with_count_delta_greater_than_count_tolerance_percentage_errors():
  1172. flexmock(module).should_receive('collect_spot_check_source_paths').and_return(
  1173. ('/foo', '/bar', '/baz', '/quux')
  1174. )
  1175. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1176. 'archive'
  1177. )
  1178. flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(
  1179. ('/foo', '/bar')
  1180. ).once()
  1181. with pytest.raises(ValueError):
  1182. module.spot_check(
  1183. repository={'path': 'repo'},
  1184. config={
  1185. 'checks': [
  1186. {
  1187. 'name': 'spot',
  1188. 'count_tolerance_percentage': 1,
  1189. 'data_tolerance_percentage': 4,
  1190. 'data_sample_percentage': 5,
  1191. },
  1192. ]
  1193. },
  1194. local_borg_version=flexmock(),
  1195. global_arguments=flexmock(),
  1196. local_path=flexmock(),
  1197. remote_path=flexmock(),
  1198. borgmatic_runtime_directory='/run/borgmatic',
  1199. )
  1200. def test_spot_check_with_failing_percentage_greater_than_data_tolerance_percentage_errors():
  1201. flexmock(module).should_receive('collect_spot_check_source_paths').and_return(
  1202. ('/foo', '/bar', '/baz', '/quux')
  1203. )
  1204. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1205. 'archive'
  1206. )
  1207. flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(('/foo', '/bar'))
  1208. flexmock(module).should_receive('compare_spot_check_hashes').and_return(
  1209. ('/bar', '/baz', '/quux')
  1210. ).once()
  1211. with pytest.raises(ValueError):
  1212. module.spot_check(
  1213. repository={'path': 'repo'},
  1214. config={
  1215. 'checks': [
  1216. {
  1217. 'name': 'spot',
  1218. 'count_tolerance_percentage': 55,
  1219. 'data_tolerance_percentage': 4,
  1220. 'data_sample_percentage': 5,
  1221. },
  1222. ]
  1223. },
  1224. local_borg_version=flexmock(),
  1225. global_arguments=flexmock(),
  1226. local_path=flexmock(),
  1227. remote_path=flexmock(),
  1228. borgmatic_runtime_directory='/run/borgmatic',
  1229. )
  1230. def test_spot_check_with_high_enough_tolerances_does_not_raise():
  1231. flexmock(module).should_receive('collect_spot_check_source_paths').and_return(
  1232. ('/foo', '/bar', '/baz', '/quux')
  1233. )
  1234. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1235. 'archive'
  1236. )
  1237. flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(('/foo', '/bar'))
  1238. flexmock(module).should_receive('compare_spot_check_hashes').and_return(
  1239. ('/bar', '/baz', '/quux')
  1240. ).once()
  1241. module.spot_check(
  1242. repository={'path': 'repo'},
  1243. config={
  1244. 'checks': [
  1245. {
  1246. 'name': 'spot',
  1247. 'count_tolerance_percentage': 55,
  1248. 'data_tolerance_percentage': 80,
  1249. 'data_sample_percentage': 80,
  1250. },
  1251. ]
  1252. },
  1253. local_borg_version=flexmock(),
  1254. global_arguments=flexmock(),
  1255. local_path=flexmock(),
  1256. remote_path=flexmock(),
  1257. borgmatic_runtime_directory='/run/borgmatic',
  1258. )
  1259. def test_spot_check_without_any_source_paths_errors():
  1260. flexmock(module).should_receive('collect_spot_check_source_paths').and_return(())
  1261. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1262. 'archive'
  1263. )
  1264. flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(('/foo', '/bar'))
  1265. flexmock(module).should_receive('compare_spot_check_hashes').never()
  1266. with pytest.raises(ValueError):
  1267. module.spot_check(
  1268. repository={'path': 'repo'},
  1269. config={
  1270. 'checks': [
  1271. {
  1272. 'name': 'spot',
  1273. 'count_tolerance_percentage': 10,
  1274. 'data_tolerance_percentage': 40,
  1275. 'data_sample_percentage': 50,
  1276. },
  1277. ]
  1278. },
  1279. local_borg_version=flexmock(),
  1280. global_arguments=flexmock(),
  1281. local_path=flexmock(),
  1282. remote_path=flexmock(),
  1283. borgmatic_runtime_directory='/run/borgmatic',
  1284. )
  1285. def test_run_check_checks_archives_for_configured_repository():
  1286. flexmock(module.logger).answer = lambda message: None
  1287. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1288. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1289. flexmock(module).should_receive('upgrade_check_times')
  1290. flexmock(module).should_receive('parse_checks')
  1291. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1292. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1293. flexmock(module).should_receive('filter_checks_on_frequency').and_return(
  1294. {'repository', 'archives'}
  1295. )
  1296. flexmock(module.borgmatic.borg.check).should_receive('check_archives').once()
  1297. flexmock(module).should_receive('make_check_time_path')
  1298. flexmock(module).should_receive('write_check_time')
  1299. flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').never()
  1300. check_arguments = flexmock(
  1301. repository=None,
  1302. progress=flexmock(),
  1303. repair=flexmock(),
  1304. only_checks=flexmock(),
  1305. force=flexmock(),
  1306. )
  1307. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1308. module.run_check(
  1309. config_filename='test.yaml',
  1310. repository={'path': 'repo'},
  1311. config={'repositories': ['repo']},
  1312. local_borg_version=None,
  1313. check_arguments=check_arguments,
  1314. global_arguments=global_arguments,
  1315. local_path=None,
  1316. remote_path=None,
  1317. )
  1318. def test_run_check_runs_configured_extract_check():
  1319. flexmock(module.logger).answer = lambda message: None
  1320. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1321. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1322. flexmock(module).should_receive('upgrade_check_times')
  1323. flexmock(module).should_receive('parse_checks')
  1324. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1325. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1326. flexmock(module).should_receive('filter_checks_on_frequency').and_return({'extract'})
  1327. flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
  1328. flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').once()
  1329. flexmock(module).should_receive('make_check_time_path')
  1330. flexmock(module).should_receive('write_check_time')
  1331. check_arguments = flexmock(
  1332. repository=None,
  1333. progress=flexmock(),
  1334. repair=flexmock(),
  1335. only_checks=flexmock(),
  1336. force=flexmock(),
  1337. )
  1338. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1339. module.run_check(
  1340. config_filename='test.yaml',
  1341. repository={'path': 'repo'},
  1342. config={'repositories': ['repo']},
  1343. local_borg_version=None,
  1344. check_arguments=check_arguments,
  1345. global_arguments=global_arguments,
  1346. local_path=None,
  1347. remote_path=None,
  1348. )
  1349. def test_run_check_runs_configured_spot_check():
  1350. flexmock(module.logger).answer = lambda message: None
  1351. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1352. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1353. flexmock(module).should_receive('upgrade_check_times')
  1354. flexmock(module).should_receive('parse_checks')
  1355. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1356. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1357. flexmock(module).should_receive('filter_checks_on_frequency').and_return({'spot'})
  1358. flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
  1359. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1360. flexmock()
  1361. )
  1362. flexmock(module.borgmatic.actions.check).should_receive('spot_check').once()
  1363. flexmock(module).should_receive('make_check_time_path')
  1364. flexmock(module).should_receive('write_check_time')
  1365. check_arguments = flexmock(
  1366. repository=None,
  1367. progress=flexmock(),
  1368. repair=flexmock(),
  1369. only_checks=flexmock(),
  1370. force=flexmock(),
  1371. )
  1372. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1373. module.run_check(
  1374. config_filename='test.yaml',
  1375. repository={'path': 'repo'},
  1376. config={'repositories': ['repo']},
  1377. local_borg_version=None,
  1378. check_arguments=check_arguments,
  1379. global_arguments=global_arguments,
  1380. local_path=None,
  1381. remote_path=None,
  1382. )
  1383. def test_run_check_without_checks_runs_nothing_except_hooks():
  1384. flexmock(module.logger).answer = lambda message: None
  1385. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1386. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1387. flexmock(module).should_receive('upgrade_check_times')
  1388. flexmock(module).should_receive('parse_checks')
  1389. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1390. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1391. flexmock(module).should_receive('filter_checks_on_frequency').and_return({})
  1392. flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
  1393. flexmock(module).should_receive('make_check_time_path')
  1394. flexmock(module).should_receive('write_check_time').never()
  1395. flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').never()
  1396. check_arguments = flexmock(
  1397. repository=None,
  1398. progress=flexmock(),
  1399. repair=flexmock(),
  1400. only_checks=flexmock(),
  1401. force=flexmock(),
  1402. )
  1403. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1404. module.run_check(
  1405. config_filename='test.yaml',
  1406. repository={'path': 'repo'},
  1407. config={'repositories': ['repo']},
  1408. local_borg_version=None,
  1409. check_arguments=check_arguments,
  1410. global_arguments=global_arguments,
  1411. local_path=None,
  1412. remote_path=None,
  1413. )
  1414. def test_run_check_checks_archives_in_selected_repository():
  1415. flexmock(module.logger).answer = lambda message: None
  1416. flexmock(module.borgmatic.config.validate).should_receive(
  1417. 'repositories_match'
  1418. ).once().and_return(True)
  1419. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1420. flexmock(module).should_receive('upgrade_check_times')
  1421. flexmock(module).should_receive('parse_checks')
  1422. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1423. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1424. flexmock(module).should_receive('filter_checks_on_frequency').and_return(
  1425. {'repository', 'archives'}
  1426. )
  1427. flexmock(module.borgmatic.borg.check).should_receive('check_archives').once()
  1428. flexmock(module).should_receive('make_check_time_path')
  1429. flexmock(module).should_receive('write_check_time')
  1430. flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').never()
  1431. check_arguments = flexmock(
  1432. repository=flexmock(),
  1433. progress=flexmock(),
  1434. repair=flexmock(),
  1435. only_checks=flexmock(),
  1436. force=flexmock(),
  1437. )
  1438. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1439. module.run_check(
  1440. config_filename='test.yaml',
  1441. repository={'path': 'repo'},
  1442. config={'repositories': ['repo']},
  1443. local_borg_version=None,
  1444. check_arguments=check_arguments,
  1445. global_arguments=global_arguments,
  1446. local_path=None,
  1447. remote_path=None,
  1448. )
  1449. def test_run_check_bails_if_repository_does_not_match():
  1450. flexmock(module.logger).answer = lambda message: None
  1451. flexmock(module.borgmatic.config.validate).should_receive(
  1452. 'repositories_match'
  1453. ).once().and_return(False)
  1454. flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
  1455. check_arguments = flexmock(
  1456. repository=flexmock(),
  1457. progress=flexmock(),
  1458. repair=flexmock(),
  1459. only_checks=flexmock(),
  1460. force=flexmock(),
  1461. )
  1462. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1463. module.run_check(
  1464. config_filename='test.yaml',
  1465. repository={'path': 'repo'},
  1466. config={'repositories': ['repo']},
  1467. local_borg_version=None,
  1468. check_arguments=check_arguments,
  1469. global_arguments=global_arguments,
  1470. local_path=None,
  1471. remote_path=None,
  1472. )