test_check.py 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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.create).should_receive('collect_patterns').and_return(
  446. flexmock()
  447. )
  448. flexmock(module.borgmatic.actions.create).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_passes_through_stream_processes_false():
  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.create).should_receive('collect_patterns').and_return(
  490. flexmock()
  491. )
  492. flexmock(module.borgmatic.actions.create).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=object,
  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': '/'},
  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_without_working_directory_parses_borg_output():
  527. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return(
  528. {'hook1': False, 'hook2': True}
  529. )
  530. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  531. flexmock()
  532. )
  533. flexmock(module.borgmatic.actions.create).should_receive('collect_patterns').and_return(
  534. flexmock()
  535. )
  536. flexmock(module.borgmatic.actions.create).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=True,
  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={},
  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_skips_directories():
  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.create).should_receive('collect_patterns').and_return(
  578. flexmock()
  579. )
  580. flexmock(module.borgmatic.actions.create).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/dir\n? /nope',
  603. )
  604. flexmock(module.os.path).should_receive('isfile').with_args('/etc/path').and_return(False)
  605. flexmock(module.os.path).should_receive('isfile').with_args('/etc/dir').and_return(False)
  606. assert (
  607. module.collect_spot_check_source_paths(
  608. repository={'path': 'repo'},
  609. config={'working_directory': '/'},
  610. local_borg_version=flexmock(),
  611. global_arguments=flexmock(),
  612. local_path=flexmock(),
  613. remote_path=flexmock(),
  614. borgmatic_runtime_directory='/run/borgmatic',
  615. )
  616. == ()
  617. )
  618. def test_collect_spot_check_archive_paths_excludes_directories_and_pipes():
  619. flexmock(module.borgmatic.config.paths).should_receive(
  620. 'get_borgmatic_source_directory'
  621. ).and_return('/home/user/.borgmatic')
  622. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  623. (
  624. 'f etc/path',
  625. 'p var/pipe',
  626. 'f etc/other',
  627. 'd etc/dir',
  628. )
  629. )
  630. assert module.collect_spot_check_archive_paths(
  631. repository={'path': 'repo'},
  632. archive='archive',
  633. config={},
  634. local_borg_version=flexmock(),
  635. global_arguments=flexmock(),
  636. local_path=flexmock(),
  637. remote_path=flexmock(),
  638. borgmatic_runtime_directory='/run/user/1001/borgmatic',
  639. ) == ('etc/path', 'etc/other')
  640. def test_collect_spot_check_archive_paths_excludes_file_in_borgmatic_runtime_directory_as_stored_with_prefix_truncation():
  641. flexmock(module.borgmatic.config.paths).should_receive(
  642. 'get_borgmatic_source_directory'
  643. ).and_return('/root/.borgmatic')
  644. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  645. (
  646. 'f etc/path',
  647. 'f borgmatic/some/thing',
  648. )
  649. )
  650. assert module.collect_spot_check_archive_paths(
  651. repository={'path': 'repo'},
  652. archive='archive',
  653. config={},
  654. local_borg_version=flexmock(),
  655. global_arguments=flexmock(),
  656. local_path=flexmock(),
  657. remote_path=flexmock(),
  658. borgmatic_runtime_directory='/run/user/0/borgmatic',
  659. ) == ('etc/path',)
  660. def test_collect_spot_check_archive_paths_excludes_file_in_borgmatic_source_directory():
  661. flexmock(module.borgmatic.config.paths).should_receive(
  662. 'get_borgmatic_source_directory'
  663. ).and_return('/root/.borgmatic')
  664. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  665. (
  666. 'f etc/path',
  667. 'f root/.borgmatic/some/thing',
  668. )
  669. )
  670. assert module.collect_spot_check_archive_paths(
  671. repository={'path': 'repo'},
  672. archive='archive',
  673. config={},
  674. local_borg_version=flexmock(),
  675. global_arguments=flexmock(),
  676. local_path=flexmock(),
  677. remote_path=flexmock(),
  678. borgmatic_runtime_directory='/run/user/0/borgmatic',
  679. ) == ('etc/path',)
  680. def test_collect_spot_check_archive_paths_excludes_file_in_borgmatic_runtime_directory():
  681. flexmock(module.borgmatic.config.paths).should_receive(
  682. 'get_borgmatic_source_directory'
  683. ).and_return('/root.borgmatic')
  684. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  685. (
  686. 'f etc/path',
  687. 'f run/user/0/borgmatic/some/thing',
  688. )
  689. )
  690. assert module.collect_spot_check_archive_paths(
  691. repository={'path': 'repo'},
  692. archive='archive',
  693. config={},
  694. local_borg_version=flexmock(),
  695. global_arguments=flexmock(),
  696. local_path=flexmock(),
  697. remote_path=flexmock(),
  698. borgmatic_runtime_directory='/run/user/0/borgmatic',
  699. ) == ('etc/path',)
  700. def test_collect_spot_check_source_paths_uses_working_directory():
  701. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return(
  702. {'hook1': False, 'hook2': True}
  703. )
  704. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  705. flexmock()
  706. )
  707. flexmock(module.borgmatic.actions.create).should_receive('collect_patterns').and_return(
  708. flexmock()
  709. )
  710. flexmock(module.borgmatic.actions.create).should_receive('process_patterns').and_return(
  711. [Pattern('foo'), Pattern('bar')]
  712. )
  713. flexmock(module.borgmatic.borg.create).should_receive('make_base_create_command').with_args(
  714. dry_run=True,
  715. repository_path='repo',
  716. config={'working_directory': '/working/dir', 'list_details': True},
  717. patterns=[Pattern('foo'), Pattern('bar')],
  718. local_borg_version=object,
  719. global_arguments=object,
  720. borgmatic_runtime_directory='/run/borgmatic',
  721. local_path=object,
  722. remote_path=object,
  723. stream_processes=True,
  724. ).and_return((('borg', 'create'), ('repo::archive',), flexmock()))
  725. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  726. flexmock()
  727. )
  728. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  729. '/working/dir'
  730. )
  731. flexmock(module.borgmatic.execute).should_receive(
  732. 'execute_command_and_capture_output'
  733. ).and_return(
  734. 'warning: stuff\n- foo\n+ bar\n? /nope',
  735. )
  736. flexmock(module.os.path).should_receive('isfile').with_args('/working/dir/foo').and_return(True)
  737. flexmock(module.os.path).should_receive('isfile').with_args('/working/dir/bar').and_return(True)
  738. assert module.collect_spot_check_source_paths(
  739. repository={'path': 'repo'},
  740. config={'working_directory': '/working/dir'},
  741. local_borg_version=flexmock(),
  742. global_arguments=flexmock(),
  743. local_path=flexmock(),
  744. remote_path=flexmock(),
  745. borgmatic_runtime_directory='/run/borgmatic',
  746. ) == ('foo', 'bar')
  747. def test_compare_spot_check_hashes_returns_paths_having_failing_hashes():
  748. flexmock(module.random).should_receive('sample').replace_with(
  749. lambda population, count: population[:count]
  750. )
  751. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  752. None,
  753. )
  754. flexmock(module.os.path).should_receive('exists').and_return(True)
  755. flexmock(module.os.path).should_receive('islink').and_return(False)
  756. flexmock(module.borgmatic.execute).should_receive(
  757. 'execute_command_and_capture_output'
  758. ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  759. 'hash1 /foo\nhash2 /bar'
  760. )
  761. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  762. ['hash1 foo', 'nothash2 bar']
  763. )
  764. assert module.compare_spot_check_hashes(
  765. repository={'path': 'repo'},
  766. archive='archive',
  767. config={
  768. 'checks': [
  769. {
  770. 'name': 'archives',
  771. 'frequency': '2 weeks',
  772. },
  773. {
  774. 'name': 'spot',
  775. 'data_sample_percentage': 50,
  776. },
  777. ]
  778. },
  779. local_borg_version=flexmock(),
  780. global_arguments=flexmock(),
  781. local_path=flexmock(),
  782. remote_path=flexmock(),
  783. source_paths=('/foo', '/bar', '/baz', '/quux'),
  784. ) == ('/bar',)
  785. def test_compare_spot_check_hashes_returns_relative_paths_having_failing_hashes():
  786. flexmock(module.random).should_receive('sample').replace_with(
  787. lambda population, count: population[:count]
  788. )
  789. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  790. None,
  791. )
  792. flexmock(module.os.path).should_receive('exists').and_return(True)
  793. flexmock(module.os.path).should_receive('islink').and_return(False)
  794. flexmock(module.borgmatic.execute).should_receive(
  795. 'execute_command_and_capture_output'
  796. ).with_args(('xxh64sum', 'foo', 'bar'), working_directory=None).and_return(
  797. 'hash1 foo\nhash2 bar'
  798. )
  799. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  800. ['hash1 foo', 'nothash2 bar']
  801. )
  802. assert module.compare_spot_check_hashes(
  803. repository={'path': 'repo'},
  804. archive='archive',
  805. config={
  806. 'checks': [
  807. {
  808. 'name': 'archives',
  809. 'frequency': '2 weeks',
  810. },
  811. {
  812. 'name': 'spot',
  813. 'data_sample_percentage': 50,
  814. },
  815. ]
  816. },
  817. local_borg_version=flexmock(),
  818. global_arguments=flexmock(),
  819. local_path=flexmock(),
  820. remote_path=flexmock(),
  821. source_paths=('foo', 'bar', 'baz', 'quux'),
  822. ) == ('bar',)
  823. def test_compare_spot_check_hashes_handles_data_sample_percentage_above_100():
  824. flexmock(module.random).should_receive('sample').replace_with(
  825. lambda population, count: population[:count]
  826. )
  827. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  828. None,
  829. )
  830. flexmock(module.os.path).should_receive('exists').and_return(True)
  831. flexmock(module.os.path).should_receive('islink').and_return(False)
  832. flexmock(module.borgmatic.execute).should_receive(
  833. 'execute_command_and_capture_output'
  834. ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  835. 'hash1 /foo\nhash2 /bar'
  836. )
  837. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  838. ['nothash1 foo', 'nothash2 bar']
  839. )
  840. assert module.compare_spot_check_hashes(
  841. repository={'path': 'repo'},
  842. archive='archive',
  843. config={
  844. 'checks': [
  845. {
  846. 'name': 'archives',
  847. 'frequency': '2 weeks',
  848. },
  849. {
  850. 'name': 'spot',
  851. 'data_sample_percentage': 1000,
  852. },
  853. ]
  854. },
  855. local_borg_version=flexmock(),
  856. global_arguments=flexmock(),
  857. local_path=flexmock(),
  858. remote_path=flexmock(),
  859. source_paths=('/foo', '/bar'),
  860. ) == ('/foo', '/bar')
  861. def test_compare_spot_check_hashes_uses_xxh64sum_command_option():
  862. flexmock(module.random).should_receive('sample').replace_with(
  863. lambda population, count: population[:count]
  864. )
  865. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  866. None,
  867. )
  868. flexmock(module.os.path).should_receive('exists').and_return(True)
  869. flexmock(module.os.path).should_receive('islink').and_return(False)
  870. flexmock(module.borgmatic.execute).should_receive(
  871. 'execute_command_and_capture_output'
  872. ).with_args(('/usr/local/bin/xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  873. 'hash1 /foo\nhash2 /bar'
  874. )
  875. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  876. ['hash1 foo', 'nothash2 bar']
  877. )
  878. assert module.compare_spot_check_hashes(
  879. repository={'path': 'repo'},
  880. archive='archive',
  881. config={
  882. 'checks': [
  883. {
  884. 'name': 'spot',
  885. 'data_sample_percentage': 50,
  886. 'xxh64sum_command': '/usr/local/bin/xxh64sum',
  887. },
  888. ]
  889. },
  890. local_borg_version=flexmock(),
  891. global_arguments=flexmock(),
  892. local_path=flexmock(),
  893. remote_path=flexmock(),
  894. source_paths=('/foo', '/bar', '/baz', '/quux'),
  895. ) == ('/bar',)
  896. def test_compare_spot_check_hashes_considers_path_missing_from_archive_as_not_matching():
  897. flexmock(module.random).should_receive('sample').replace_with(
  898. lambda population, count: population[:count]
  899. )
  900. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  901. None,
  902. )
  903. flexmock(module.os.path).should_receive('exists').and_return(True)
  904. flexmock(module.os.path).should_receive('islink').and_return(False)
  905. flexmock(module.borgmatic.execute).should_receive(
  906. 'execute_command_and_capture_output'
  907. ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  908. 'hash1 /foo\nhash2 /bar'
  909. )
  910. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  911. ['hash1 foo']
  912. )
  913. assert module.compare_spot_check_hashes(
  914. repository={'path': 'repo'},
  915. archive='archive',
  916. config={
  917. 'checks': [
  918. {
  919. 'name': 'spot',
  920. 'data_sample_percentage': 50,
  921. },
  922. ]
  923. },
  924. local_borg_version=flexmock(),
  925. global_arguments=flexmock(),
  926. local_path=flexmock(),
  927. remote_path=flexmock(),
  928. source_paths=('/foo', '/bar', '/baz', '/quux'),
  929. ) == ('/bar',)
  930. def test_compare_spot_check_hashes_considers_symlink_path_as_not_matching():
  931. flexmock(module.random).should_receive('sample').replace_with(
  932. lambda population, count: population[:count]
  933. )
  934. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  935. None,
  936. )
  937. flexmock(module.os.path).should_receive('exists').and_return(True)
  938. flexmock(module.os.path).should_receive('islink').with_args('/foo').and_return(False)
  939. flexmock(module.os.path).should_receive('islink').with_args('/bar').and_return(True)
  940. flexmock(module.borgmatic.execute).should_receive(
  941. 'execute_command_and_capture_output'
  942. ).with_args(('xxh64sum', '/foo'), working_directory=None).and_return('hash1 /foo')
  943. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  944. ['hash1 foo', 'hash2 bar']
  945. )
  946. assert module.compare_spot_check_hashes(
  947. repository={'path': 'repo'},
  948. archive='archive',
  949. config={
  950. 'checks': [
  951. {
  952. 'name': 'spot',
  953. 'data_sample_percentage': 50,
  954. },
  955. ]
  956. },
  957. local_borg_version=flexmock(),
  958. global_arguments=flexmock(),
  959. local_path=flexmock(),
  960. remote_path=flexmock(),
  961. source_paths=('/foo', '/bar', '/baz', '/quux'),
  962. ) == ('/bar',)
  963. def test_compare_spot_check_hashes_considers_non_existent_path_as_not_matching():
  964. flexmock(module.random).should_receive('sample').replace_with(
  965. lambda population, count: population[:count]
  966. )
  967. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  968. None,
  969. )
  970. flexmock(module.os.path).should_receive('exists').with_args('/foo').and_return(True)
  971. flexmock(module.os.path).should_receive('exists').with_args('/bar').and_return(False)
  972. flexmock(module.os.path).should_receive('islink').and_return(False)
  973. flexmock(module.borgmatic.execute).should_receive(
  974. 'execute_command_and_capture_output'
  975. ).with_args(('xxh64sum', '/foo'), working_directory=None).and_return('hash1 /foo')
  976. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  977. ['hash1 foo', 'hash2 bar']
  978. )
  979. assert module.compare_spot_check_hashes(
  980. repository={'path': 'repo'},
  981. archive='archive',
  982. config={
  983. 'checks': [
  984. {
  985. 'name': 'spot',
  986. 'data_sample_percentage': 50,
  987. },
  988. ]
  989. },
  990. local_borg_version=flexmock(),
  991. global_arguments=flexmock(),
  992. local_path=flexmock(),
  993. remote_path=flexmock(),
  994. source_paths=('/foo', '/bar', '/baz', '/quux'),
  995. ) == ('/bar',)
  996. def test_compare_spot_check_hashes_with_too_many_paths_feeds_them_to_commands_in_chunks():
  997. flexmock(module).SAMPLE_PATHS_SUBSET_COUNT = 2
  998. flexmock(module.random).should_receive('sample').replace_with(
  999. lambda population, count: population[:count]
  1000. )
  1001. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1002. None,
  1003. )
  1004. flexmock(module.os.path).should_receive('exists').and_return(True)
  1005. flexmock(module.os.path).should_receive('islink').and_return(False)
  1006. flexmock(module.borgmatic.execute).should_receive(
  1007. 'execute_command_and_capture_output'
  1008. ).with_args(('xxh64sum', '/foo', '/bar'), working_directory=None).and_return(
  1009. 'hash1 /foo\nhash2 /bar'
  1010. )
  1011. flexmock(module.borgmatic.execute).should_receive(
  1012. 'execute_command_and_capture_output'
  1013. ).with_args(('xxh64sum', '/baz', '/quux'), working_directory=None).and_return(
  1014. 'hash3 /baz\nhash4 /quux'
  1015. )
  1016. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  1017. ['hash1 foo', 'hash2 bar']
  1018. ).and_return(['hash3 baz', 'nothash4 quux'])
  1019. assert module.compare_spot_check_hashes(
  1020. repository={'path': 'repo'},
  1021. archive='archive',
  1022. config={
  1023. 'checks': [
  1024. {
  1025. 'name': 'archives',
  1026. 'frequency': '2 weeks',
  1027. },
  1028. {
  1029. 'name': 'spot',
  1030. 'data_sample_percentage': 100,
  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. ) == ('/quux',)
  1040. def test_compare_spot_check_hashes_uses_working_directory_to_access_source_paths():
  1041. flexmock(module.random).should_receive('sample').replace_with(
  1042. lambda population, count: population[:count]
  1043. )
  1044. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  1045. '/working/dir',
  1046. )
  1047. flexmock(module.os.path).should_receive('exists').with_args('/working/dir/foo').and_return(True)
  1048. flexmock(module.os.path).should_receive('exists').with_args('/working/dir/bar').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='/working/dir').and_return(
  1053. 'hash1 foo\nhash2 bar'
  1054. )
  1055. flexmock(module.borgmatic.borg.list).should_receive('capture_archive_listing').and_return(
  1056. ['hash1 foo', 'nothash2 bar']
  1057. )
  1058. assert module.compare_spot_check_hashes(
  1059. repository={'path': 'repo'},
  1060. archive='archive',
  1061. config={
  1062. 'checks': [
  1063. {
  1064. 'name': 'archives',
  1065. 'frequency': '2 weeks',
  1066. },
  1067. {
  1068. 'name': 'spot',
  1069. 'data_sample_percentage': 50,
  1070. },
  1071. ],
  1072. 'working_directory': '/working/dir',
  1073. },
  1074. local_borg_version=flexmock(),
  1075. global_arguments=flexmock(),
  1076. local_path=flexmock(),
  1077. remote_path=flexmock(),
  1078. source_paths=('foo', 'bar', 'baz', 'quux'),
  1079. ) == ('bar',)
  1080. def test_spot_check_without_spot_configuration_errors():
  1081. with pytest.raises(ValueError):
  1082. module.spot_check(
  1083. repository={'path': 'repo'},
  1084. config={
  1085. 'checks': [
  1086. {
  1087. 'name': 'archives',
  1088. },
  1089. ]
  1090. },
  1091. local_borg_version=flexmock(),
  1092. global_arguments=flexmock(),
  1093. local_path=flexmock(),
  1094. remote_path=flexmock(),
  1095. borgmatic_runtime_directory='/run/borgmatic',
  1096. )
  1097. def test_spot_check_without_any_configuration_errors():
  1098. with pytest.raises(ValueError):
  1099. module.spot_check(
  1100. repository={'path': 'repo'},
  1101. config={},
  1102. local_borg_version=flexmock(),
  1103. global_arguments=flexmock(),
  1104. local_path=flexmock(),
  1105. remote_path=flexmock(),
  1106. borgmatic_runtime_directory='/run/borgmatic',
  1107. )
  1108. def test_spot_check_data_tolerance_percentage_greater_than_data_sample_percentage_errors():
  1109. with pytest.raises(ValueError):
  1110. module.spot_check(
  1111. repository={'path': 'repo'},
  1112. config={
  1113. 'checks': [
  1114. {
  1115. 'name': 'spot',
  1116. 'data_tolerance_percentage': 7,
  1117. 'data_sample_percentage': 5,
  1118. },
  1119. ]
  1120. },
  1121. local_borg_version=flexmock(),
  1122. global_arguments=flexmock(),
  1123. local_path=flexmock(),
  1124. remote_path=flexmock(),
  1125. borgmatic_runtime_directory='/run/borgmatic',
  1126. )
  1127. def test_spot_check_with_count_delta_greater_than_count_tolerance_percentage_errors():
  1128. flexmock(module).should_receive('collect_spot_check_source_paths').and_return(
  1129. ('/foo', '/bar', '/baz', '/quux')
  1130. )
  1131. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1132. 'archive'
  1133. )
  1134. flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(
  1135. ('/foo', '/bar')
  1136. ).once()
  1137. with pytest.raises(ValueError):
  1138. module.spot_check(
  1139. repository={'path': 'repo'},
  1140. config={
  1141. 'checks': [
  1142. {
  1143. 'name': 'spot',
  1144. 'count_tolerance_percentage': 1,
  1145. 'data_tolerance_percentage': 4,
  1146. 'data_sample_percentage': 5,
  1147. },
  1148. ]
  1149. },
  1150. local_borg_version=flexmock(),
  1151. global_arguments=flexmock(),
  1152. local_path=flexmock(),
  1153. remote_path=flexmock(),
  1154. borgmatic_runtime_directory='/run/borgmatic',
  1155. )
  1156. def test_spot_check_with_failing_percentage_greater_than_data_tolerance_percentage_errors():
  1157. flexmock(module).should_receive('collect_spot_check_source_paths').and_return(
  1158. ('/foo', '/bar', '/baz', '/quux')
  1159. )
  1160. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1161. 'archive'
  1162. )
  1163. flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(('/foo', '/bar'))
  1164. flexmock(module).should_receive('compare_spot_check_hashes').and_return(
  1165. ('/bar', '/baz', '/quux')
  1166. ).once()
  1167. with pytest.raises(ValueError):
  1168. module.spot_check(
  1169. repository={'path': 'repo'},
  1170. config={
  1171. 'checks': [
  1172. {
  1173. 'name': 'spot',
  1174. 'count_tolerance_percentage': 55,
  1175. 'data_tolerance_percentage': 4,
  1176. 'data_sample_percentage': 5,
  1177. },
  1178. ]
  1179. },
  1180. local_borg_version=flexmock(),
  1181. global_arguments=flexmock(),
  1182. local_path=flexmock(),
  1183. remote_path=flexmock(),
  1184. borgmatic_runtime_directory='/run/borgmatic',
  1185. )
  1186. def test_spot_check_with_high_enough_tolerances_does_not_raise():
  1187. flexmock(module).should_receive('collect_spot_check_source_paths').and_return(
  1188. ('/foo', '/bar', '/baz', '/quux')
  1189. )
  1190. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1191. 'archive'
  1192. )
  1193. flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(('/foo', '/bar'))
  1194. flexmock(module).should_receive('compare_spot_check_hashes').and_return(
  1195. ('/bar', '/baz', '/quux')
  1196. ).once()
  1197. module.spot_check(
  1198. repository={'path': 'repo'},
  1199. config={
  1200. 'checks': [
  1201. {
  1202. 'name': 'spot',
  1203. 'count_tolerance_percentage': 55,
  1204. 'data_tolerance_percentage': 80,
  1205. 'data_sample_percentage': 80,
  1206. },
  1207. ]
  1208. },
  1209. local_borg_version=flexmock(),
  1210. global_arguments=flexmock(),
  1211. local_path=flexmock(),
  1212. remote_path=flexmock(),
  1213. borgmatic_runtime_directory='/run/borgmatic',
  1214. )
  1215. def test_spot_check_without_any_source_paths_errors():
  1216. flexmock(module).should_receive('collect_spot_check_source_paths').and_return(())
  1217. flexmock(module.borgmatic.borg.repo_list).should_receive('resolve_archive_name').and_return(
  1218. 'archive'
  1219. )
  1220. flexmock(module).should_receive('collect_spot_check_archive_paths').and_return(('/foo', '/bar'))
  1221. flexmock(module).should_receive('compare_spot_check_hashes').never()
  1222. with pytest.raises(ValueError):
  1223. module.spot_check(
  1224. repository={'path': 'repo'},
  1225. config={
  1226. 'checks': [
  1227. {
  1228. 'name': 'spot',
  1229. 'count_tolerance_percentage': 10,
  1230. 'data_tolerance_percentage': 40,
  1231. 'data_sample_percentage': 50,
  1232. },
  1233. ]
  1234. },
  1235. local_borg_version=flexmock(),
  1236. global_arguments=flexmock(),
  1237. local_path=flexmock(),
  1238. remote_path=flexmock(),
  1239. borgmatic_runtime_directory='/run/borgmatic',
  1240. )
  1241. def test_run_check_checks_archives_for_configured_repository():
  1242. flexmock(module.logger).answer = lambda message: None
  1243. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1244. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1245. flexmock(module).should_receive('upgrade_check_times')
  1246. flexmock(module).should_receive('parse_checks')
  1247. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1248. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1249. flexmock(module).should_receive('filter_checks_on_frequency').and_return(
  1250. {'repository', 'archives'}
  1251. )
  1252. flexmock(module.borgmatic.borg.check).should_receive('check_archives').once()
  1253. flexmock(module).should_receive('make_check_time_path')
  1254. flexmock(module).should_receive('write_check_time')
  1255. flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').never()
  1256. check_arguments = flexmock(
  1257. repository=None,
  1258. progress=flexmock(),
  1259. repair=flexmock(),
  1260. only_checks=flexmock(),
  1261. force=flexmock(),
  1262. )
  1263. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1264. module.run_check(
  1265. config_filename='test.yaml',
  1266. repository={'path': 'repo'},
  1267. config={'repositories': ['repo']},
  1268. local_borg_version=None,
  1269. check_arguments=check_arguments,
  1270. global_arguments=global_arguments,
  1271. local_path=None,
  1272. remote_path=None,
  1273. )
  1274. def test_run_check_runs_configured_extract_check():
  1275. flexmock(module.logger).answer = lambda message: None
  1276. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1277. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1278. flexmock(module).should_receive('upgrade_check_times')
  1279. flexmock(module).should_receive('parse_checks')
  1280. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1281. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1282. flexmock(module).should_receive('filter_checks_on_frequency').and_return({'extract'})
  1283. flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
  1284. flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').once()
  1285. flexmock(module).should_receive('make_check_time_path')
  1286. flexmock(module).should_receive('write_check_time')
  1287. check_arguments = flexmock(
  1288. repository=None,
  1289. progress=flexmock(),
  1290. repair=flexmock(),
  1291. only_checks=flexmock(),
  1292. force=flexmock(),
  1293. )
  1294. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1295. module.run_check(
  1296. config_filename='test.yaml',
  1297. repository={'path': 'repo'},
  1298. config={'repositories': ['repo']},
  1299. local_borg_version=None,
  1300. check_arguments=check_arguments,
  1301. global_arguments=global_arguments,
  1302. local_path=None,
  1303. remote_path=None,
  1304. )
  1305. def test_run_check_runs_configured_spot_check():
  1306. flexmock(module.logger).answer = lambda message: None
  1307. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1308. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1309. flexmock(module).should_receive('upgrade_check_times')
  1310. flexmock(module).should_receive('parse_checks')
  1311. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1312. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1313. flexmock(module).should_receive('filter_checks_on_frequency').and_return({'spot'})
  1314. flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
  1315. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  1316. flexmock()
  1317. )
  1318. flexmock(module.borgmatic.actions.check).should_receive('spot_check').once()
  1319. flexmock(module).should_receive('make_check_time_path')
  1320. flexmock(module).should_receive('write_check_time')
  1321. check_arguments = flexmock(
  1322. repository=None,
  1323. progress=flexmock(),
  1324. repair=flexmock(),
  1325. only_checks=flexmock(),
  1326. force=flexmock(),
  1327. )
  1328. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1329. module.run_check(
  1330. config_filename='test.yaml',
  1331. repository={'path': 'repo'},
  1332. config={'repositories': ['repo']},
  1333. local_borg_version=None,
  1334. check_arguments=check_arguments,
  1335. global_arguments=global_arguments,
  1336. local_path=None,
  1337. remote_path=None,
  1338. )
  1339. def test_run_check_without_checks_runs_nothing_except_hooks():
  1340. flexmock(module.logger).answer = lambda message: None
  1341. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  1342. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1343. flexmock(module).should_receive('upgrade_check_times')
  1344. flexmock(module).should_receive('parse_checks')
  1345. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1346. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1347. flexmock(module).should_receive('filter_checks_on_frequency').and_return({})
  1348. flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
  1349. flexmock(module).should_receive('make_check_time_path')
  1350. flexmock(module).should_receive('write_check_time').never()
  1351. flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').never()
  1352. check_arguments = flexmock(
  1353. repository=None,
  1354. progress=flexmock(),
  1355. repair=flexmock(),
  1356. only_checks=flexmock(),
  1357. force=flexmock(),
  1358. )
  1359. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1360. module.run_check(
  1361. config_filename='test.yaml',
  1362. repository={'path': 'repo'},
  1363. config={'repositories': ['repo']},
  1364. local_borg_version=None,
  1365. check_arguments=check_arguments,
  1366. global_arguments=global_arguments,
  1367. local_path=None,
  1368. remote_path=None,
  1369. )
  1370. def test_run_check_checks_archives_in_selected_repository():
  1371. flexmock(module.logger).answer = lambda message: None
  1372. flexmock(module.borgmatic.config.validate).should_receive(
  1373. 'repositories_match'
  1374. ).once().and_return(True)
  1375. flexmock(module.borgmatic.borg.check).should_receive('get_repository_id').and_return(flexmock())
  1376. flexmock(module).should_receive('upgrade_check_times')
  1377. flexmock(module).should_receive('parse_checks')
  1378. flexmock(module.borgmatic.borg.check).should_receive('make_archive_filter_flags').and_return(())
  1379. flexmock(module).should_receive('make_archives_check_id').and_return(None)
  1380. flexmock(module).should_receive('filter_checks_on_frequency').and_return(
  1381. {'repository', 'archives'}
  1382. )
  1383. flexmock(module.borgmatic.borg.check).should_receive('check_archives').once()
  1384. flexmock(module).should_receive('make_check_time_path')
  1385. flexmock(module).should_receive('write_check_time')
  1386. flexmock(module.borgmatic.borg.extract).should_receive('extract_last_archive_dry_run').never()
  1387. check_arguments = flexmock(
  1388. repository=flexmock(),
  1389. progress=flexmock(),
  1390. repair=flexmock(),
  1391. only_checks=flexmock(),
  1392. force=flexmock(),
  1393. )
  1394. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1395. module.run_check(
  1396. config_filename='test.yaml',
  1397. repository={'path': 'repo'},
  1398. config={'repositories': ['repo']},
  1399. local_borg_version=None,
  1400. check_arguments=check_arguments,
  1401. global_arguments=global_arguments,
  1402. local_path=None,
  1403. remote_path=None,
  1404. )
  1405. def test_run_check_bails_if_repository_does_not_match():
  1406. flexmock(module.logger).answer = lambda message: None
  1407. flexmock(module.borgmatic.config.validate).should_receive(
  1408. 'repositories_match'
  1409. ).once().and_return(False)
  1410. flexmock(module.borgmatic.borg.check).should_receive('check_archives').never()
  1411. check_arguments = flexmock(
  1412. repository=flexmock(),
  1413. progress=flexmock(),
  1414. repair=flexmock(),
  1415. only_checks=flexmock(),
  1416. force=flexmock(),
  1417. )
  1418. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  1419. module.run_check(
  1420. config_filename='test.yaml',
  1421. repository={'path': 'repo'},
  1422. config={'repositories': ['repo']},
  1423. local_borg_version=None,
  1424. check_arguments=check_arguments,
  1425. global_arguments=global_arguments,
  1426. local_path=None,
  1427. remote_path=None,
  1428. )