test_check.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import check as module
  5. from ..test_verbosity import insert_logging_mock
  6. def insert_execute_command_mock(
  7. command, output_file=None, working_directory=None, borg_exit_codes=None
  8. ):
  9. flexmock(module.environment).should_receive('make_environment')
  10. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  11. working_directory,
  12. )
  13. flexmock(module).should_receive('execute_command').with_args(
  14. command,
  15. output_file=output_file,
  16. environment=None,
  17. working_directory=working_directory,
  18. borg_local_path=command[0],
  19. borg_exit_codes=borg_exit_codes,
  20. ).once()
  21. def insert_execute_command_never():
  22. flexmock(module).should_receive('execute_command').never()
  23. def test_make_archive_filter_flags_with_default_checks_and_prefix_returns_default_flags():
  24. flexmock(module.feature).should_receive('available').and_return(True)
  25. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  26. flags = module.make_archive_filter_flags(
  27. '1.2.3',
  28. {'prefix': 'foo'},
  29. ('repository', 'archives'),
  30. check_arguments=flexmock(match_archives=None),
  31. )
  32. assert flags == ('--match-archives', 'sh:foo*')
  33. def test_make_archive_filter_flags_with_all_checks_and_prefix_returns_default_flags():
  34. flexmock(module.feature).should_receive('available').and_return(True)
  35. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  36. flags = module.make_archive_filter_flags(
  37. '1.2.3',
  38. {'prefix': 'foo'},
  39. ('repository', 'archives', 'extract'),
  40. check_arguments=flexmock(match_archives=None),
  41. )
  42. assert flags == ('--match-archives', 'sh:foo*')
  43. def test_make_archive_filter_flags_with_all_checks_and_prefix_without_borg_features_returns_glob_archives_flags():
  44. flexmock(module.feature).should_receive('available').and_return(False)
  45. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  46. flags = module.make_archive_filter_flags(
  47. '1.2.3',
  48. {'prefix': 'foo'},
  49. ('repository', 'archives', 'extract'),
  50. check_arguments=flexmock(match_archives=None),
  51. )
  52. assert flags == ('--glob-archives', 'foo*')
  53. def test_make_archive_filter_flags_with_archives_check_and_last_includes_last_flag():
  54. flexmock(module.feature).should_receive('available').and_return(True)
  55. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  56. flags = module.make_archive_filter_flags(
  57. '1.2.3',
  58. {'check_last': 3},
  59. ('archives',),
  60. check_arguments=flexmock(match_archives=None),
  61. )
  62. assert flags == ('--last', '3')
  63. def test_make_archive_filter_flags_with_data_check_and_last_includes_last_flag():
  64. flexmock(module.feature).should_receive('available').and_return(True)
  65. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  66. flags = module.make_archive_filter_flags(
  67. '1.2.3',
  68. {'check_last': 3},
  69. ('data',),
  70. check_arguments=flexmock(match_archives=None),
  71. )
  72. assert flags == ('--last', '3')
  73. def test_make_archive_filter_flags_with_repository_check_and_last_omits_last_flag():
  74. flexmock(module.feature).should_receive('available').and_return(True)
  75. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  76. flags = module.make_archive_filter_flags(
  77. '1.2.3',
  78. {'check_last': 3},
  79. ('repository',),
  80. check_arguments=flexmock(match_archives=None),
  81. )
  82. assert flags == ()
  83. def test_make_archive_filter_flags_with_default_checks_and_last_includes_last_flag():
  84. flexmock(module.feature).should_receive('available').and_return(True)
  85. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  86. flags = module.make_archive_filter_flags(
  87. '1.2.3',
  88. {'check_last': 3},
  89. ('repository', 'archives'),
  90. check_arguments=flexmock(match_archives=None),
  91. )
  92. assert flags == ('--last', '3')
  93. def test_make_archive_filter_flags_with_archives_check_and_prefix_includes_match_archives_flag():
  94. flexmock(module.feature).should_receive('available').and_return(True)
  95. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  96. flags = module.make_archive_filter_flags(
  97. '1.2.3',
  98. {'prefix': 'foo-'},
  99. ('archives',),
  100. check_arguments=flexmock(match_archives=None),
  101. )
  102. assert flags == ('--match-archives', 'sh:foo-*')
  103. def test_make_archive_filter_flags_with_data_check_and_prefix_includes_match_archives_flag():
  104. flexmock(module.feature).should_receive('available').and_return(True)
  105. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  106. flags = module.make_archive_filter_flags(
  107. '1.2.3',
  108. {'prefix': 'foo-'},
  109. ('data',),
  110. check_arguments=flexmock(match_archives=None),
  111. )
  112. assert flags == ('--match-archives', 'sh:foo-*')
  113. def test_make_archive_filter_flags_with_archives_check_and_empty_prefix_uses_archive_name_format_instead():
  114. flexmock(module.feature).should_receive('available').and_return(True)
  115. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  116. None, 'bar-{now}', '1.2.3' # noqa: FS003
  117. ).and_return(('--match-archives', 'sh:bar-*'))
  118. flags = module.make_archive_filter_flags(
  119. '1.2.3',
  120. {'archive_name_format': 'bar-{now}', 'prefix': ''}, # noqa: FS003
  121. ('archives',),
  122. check_arguments=flexmock(match_archives=None),
  123. )
  124. assert flags == ('--match-archives', 'sh:bar-*')
  125. def test_make_archive_filter_flags_with_archives_check_and_none_prefix_omits_match_archives_flag():
  126. flexmock(module.feature).should_receive('available').and_return(True)
  127. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  128. flags = module.make_archive_filter_flags(
  129. '1.2.3',
  130. {},
  131. ('archives',),
  132. check_arguments=flexmock(match_archives=None),
  133. )
  134. assert flags == ()
  135. def test_make_archive_filter_flags_with_repository_check_and_prefix_omits_match_archives_flag():
  136. flexmock(module.feature).should_receive('available').and_return(True)
  137. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  138. flags = module.make_archive_filter_flags(
  139. '1.2.3',
  140. {'prefix': 'foo-'},
  141. ('repository',),
  142. check_arguments=flexmock(match_archives=None),
  143. )
  144. assert flags == ()
  145. def test_make_archive_filter_flags_with_default_checks_and_prefix_includes_match_archives_flag():
  146. flexmock(module.feature).should_receive('available').and_return(True)
  147. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  148. flags = module.make_archive_filter_flags(
  149. '1.2.3',
  150. {'prefix': 'foo-'},
  151. ('repository', 'archives'),
  152. check_arguments=flexmock(match_archives=None),
  153. )
  154. assert flags == ('--match-archives', 'sh:foo-*')
  155. def test_make_check_name_flags_with_repository_check_returns_flag():
  156. flags = module.make_check_name_flags({'repository'}, ())
  157. assert flags == ('--repository-only',)
  158. def test_make_check_name_flags_with_archives_check_returns_flag():
  159. flags = module.make_check_name_flags({'archives'}, ())
  160. assert flags == ('--archives-only',)
  161. def test_make_check_name_flags_with_archives_check_and_archive_filter_flags_includes_those_flags():
  162. flags = module.make_check_name_flags({'archives'}, ('--match-archives', 'sh:foo-*'))
  163. assert flags == ('--archives-only', '--match-archives', 'sh:foo-*')
  164. def test_make_check_name_flags_without_archives_check_and_with_archive_filter_flags_includes_those_flags():
  165. flags = module.make_check_name_flags({'repository'}, ('--match-archives', 'sh:foo-*'))
  166. assert flags == ('--repository-only',)
  167. def test_make_check_name_flags_with_archives_and_data_check_returns_verify_data_flag():
  168. flexmock(module.feature).should_receive('available').and_return(True)
  169. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  170. flags = module.make_check_name_flags({'archives', 'data'}, ())
  171. assert flags == (
  172. '--archives-only',
  173. '--verify-data',
  174. )
  175. def test_make_check_name_flags_with_repository_and_data_check_returns_verify_data_flag():
  176. flexmock(module.feature).should_receive('available').and_return(True)
  177. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  178. flags = module.make_check_name_flags({'archives', 'data', 'repository'}, ())
  179. assert flags == ('--verify-data',)
  180. def test_make_check_name_flags_with_extract_omits_extract_flag():
  181. flexmock(module.feature).should_receive('available').and_return(True)
  182. flexmock(module.flags).should_receive('make_match_archives_flags').and_return(())
  183. flags = module.make_check_name_flags({'extract'}, ())
  184. assert flags == ()
  185. def test_get_repository_id_with_valid_json_does_not_raise():
  186. config = {}
  187. flexmock(module.repo_info).should_receive('display_repository_info').and_return(
  188. '{"repository": {"id": "repo"}}'
  189. )
  190. assert module.get_repository_id(
  191. repository_path='repo',
  192. config=config,
  193. local_borg_version='1.2.3',
  194. global_arguments=flexmock(),
  195. local_path='borg',
  196. remote_path=None,
  197. )
  198. def test_get_repository_id_with_json_error_raises():
  199. config = {}
  200. flexmock(module.repo_info).should_receive('display_repository_info').and_return(
  201. '{"unexpected": {"id": "repo"}}'
  202. )
  203. with pytest.raises(ValueError):
  204. module.get_repository_id(
  205. repository_path='repo',
  206. config=config,
  207. local_borg_version='1.2.3',
  208. global_arguments=flexmock(),
  209. local_path='borg',
  210. remote_path=None,
  211. )
  212. def test_get_repository_id_with_missing_json_keys_raises():
  213. config = {}
  214. flexmock(module.repo_info).should_receive('display_repository_info').and_return('{invalid JSON')
  215. with pytest.raises(ValueError):
  216. module.get_repository_id(
  217. repository_path='repo',
  218. config=config,
  219. local_borg_version='1.2.3',
  220. global_arguments=flexmock(),
  221. local_path='borg',
  222. remote_path=None,
  223. )
  224. def test_check_archives_with_progress_passes_through_to_borg():
  225. config = {'progress': True}
  226. flexmock(module).should_receive('make_check_name_flags').with_args(
  227. {'repository'}, ()
  228. ).and_return(())
  229. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  230. flexmock(module.environment).should_receive('make_environment')
  231. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  232. flexmock(module).should_receive('execute_command').with_args(
  233. ('borg', 'check', '--progress', 'repo'),
  234. output_file=module.DO_NOT_CAPTURE,
  235. environment=None,
  236. working_directory=None,
  237. borg_local_path='borg',
  238. borg_exit_codes=None,
  239. ).once()
  240. module.check_archives(
  241. repository_path='repo',
  242. config=config,
  243. local_borg_version='1.2.3',
  244. check_arguments=flexmock(
  245. progress=None,
  246. repair=None,
  247. only_checks=None,
  248. force=None,
  249. match_archives=None,
  250. max_duration=None,
  251. ),
  252. global_arguments=flexmock(),
  253. checks={'repository'},
  254. archive_filter_flags=(),
  255. )
  256. def test_check_archives_with_repair_passes_through_to_borg():
  257. config = {}
  258. flexmock(module).should_receive('make_check_name_flags').with_args(
  259. {'repository'}, ()
  260. ).and_return(())
  261. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  262. flexmock(module.environment).should_receive('make_environment')
  263. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  264. flexmock(module).should_receive('execute_command').with_args(
  265. ('borg', 'check', '--repair', 'repo'),
  266. output_file=module.DO_NOT_CAPTURE,
  267. environment=None,
  268. working_directory=None,
  269. borg_local_path='borg',
  270. borg_exit_codes=None,
  271. ).once()
  272. module.check_archives(
  273. repository_path='repo',
  274. config=config,
  275. local_borg_version='1.2.3',
  276. check_arguments=flexmock(
  277. progress=None,
  278. repair=True,
  279. only_checks=None,
  280. force=None,
  281. match_archives=None,
  282. max_duration=None,
  283. ),
  284. global_arguments=flexmock(),
  285. checks={'repository'},
  286. archive_filter_flags=(),
  287. )
  288. def test_check_archives_with_max_duration_flag_passes_through_to_borg():
  289. config = {}
  290. flexmock(module).should_receive('make_check_name_flags').with_args(
  291. {'repository'}, ()
  292. ).and_return(())
  293. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  294. flexmock(module.environment).should_receive('make_environment')
  295. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  296. flexmock(module).should_receive('execute_command').with_args(
  297. ('borg', 'check', '--max-duration', '33', 'repo'),
  298. output_file=None,
  299. environment=None,
  300. working_directory=None,
  301. borg_local_path='borg',
  302. borg_exit_codes=None,
  303. ).once()
  304. module.check_archives(
  305. repository_path='repo',
  306. config=config,
  307. local_borg_version='1.2.3',
  308. check_arguments=flexmock(
  309. progress=None,
  310. repair=None,
  311. only_checks=None,
  312. force=None,
  313. match_archives=None,
  314. max_duration=33,
  315. ),
  316. global_arguments=flexmock(),
  317. checks={'repository'},
  318. archive_filter_flags=(),
  319. )
  320. def test_check_archives_with_max_duration_option_passes_through_to_borg():
  321. config = {'checks': [{'name': 'repository', 'max_duration': 33}]}
  322. flexmock(module).should_receive('make_check_name_flags').with_args(
  323. {'repository'}, ()
  324. ).and_return(())
  325. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  326. flexmock(module.environment).should_receive('make_environment')
  327. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  328. flexmock(module).should_receive('execute_command').with_args(
  329. ('borg', 'check', '--max-duration', '33', 'repo'),
  330. output_file=None,
  331. environment=None,
  332. working_directory=None,
  333. borg_local_path='borg',
  334. borg_exit_codes=None,
  335. ).once()
  336. module.check_archives(
  337. repository_path='repo',
  338. config=config,
  339. local_borg_version='1.2.3',
  340. check_arguments=flexmock(
  341. progress=None,
  342. repair=None,
  343. only_checks=None,
  344. force=None,
  345. match_archives=None,
  346. max_duration=None,
  347. ),
  348. global_arguments=flexmock(),
  349. checks={'repository'},
  350. archive_filter_flags=(),
  351. )
  352. def test_check_archives_with_max_duration_option_and_archives_check_runs_repository_check_separately():
  353. config = {'checks': [{'name': 'repository', 'max_duration': 33}, {'name': 'archives'}]}
  354. flexmock(module).should_receive('make_check_name_flags').with_args({'archives'}, ()).and_return(
  355. ('--archives-only',)
  356. )
  357. flexmock(module).should_receive('make_check_name_flags').with_args(
  358. {'repository'}, ()
  359. ).and_return(('--repository-only',))
  360. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  361. insert_execute_command_mock(('borg', 'check', '--archives-only', 'repo'))
  362. insert_execute_command_mock(
  363. ('borg', 'check', '--max-duration', '33', '--repository-only', 'repo')
  364. )
  365. module.check_archives(
  366. repository_path='repo',
  367. config=config,
  368. local_borg_version='1.2.3',
  369. check_arguments=flexmock(
  370. progress=None,
  371. repair=None,
  372. only_checks=None,
  373. force=None,
  374. match_archives=None,
  375. max_duration=None,
  376. ),
  377. global_arguments=flexmock(),
  378. checks={'repository', 'archives'},
  379. archive_filter_flags=(),
  380. )
  381. def test_check_archives_with_max_duration_flag_and_archives_check_runs_repository_check_separately():
  382. config = {'checks': [{'name': 'repository'}, {'name': 'archives'}]}
  383. flexmock(module).should_receive('make_check_name_flags').with_args({'archives'}, ()).and_return(
  384. ('--archives-only',)
  385. )
  386. flexmock(module).should_receive('make_check_name_flags').with_args(
  387. {'repository'}, ()
  388. ).and_return(('--repository-only',))
  389. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  390. insert_execute_command_mock(('borg', 'check', '--archives-only', 'repo'))
  391. insert_execute_command_mock(
  392. ('borg', 'check', '--max-duration', '33', '--repository-only', 'repo')
  393. )
  394. module.check_archives(
  395. repository_path='repo',
  396. config=config,
  397. local_borg_version='1.2.3',
  398. check_arguments=flexmock(
  399. progress=None,
  400. repair=None,
  401. only_checks=None,
  402. force=None,
  403. match_archives=None,
  404. max_duration=33,
  405. ),
  406. global_arguments=flexmock(),
  407. checks={'repository', 'archives'},
  408. archive_filter_flags=(),
  409. )
  410. def test_check_archives_with_max_duration_option_and_data_check_runs_repository_check_separately():
  411. config = {'checks': [{'name': 'repository', 'max_duration': 33}, {'name': 'data'}]}
  412. flexmock(module).should_receive('make_check_name_flags').with_args(
  413. {'data', 'archives'}, ()
  414. ).and_return(('--archives-only', '--verify-data'))
  415. flexmock(module).should_receive('make_check_name_flags').with_args(
  416. {'repository'}, ()
  417. ).and_return(('--repository-only',))
  418. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  419. insert_execute_command_mock(('borg', 'check', '--archives-only', '--verify-data', 'repo'))
  420. insert_execute_command_mock(
  421. ('borg', 'check', '--max-duration', '33', '--repository-only', 'repo')
  422. )
  423. module.check_archives(
  424. repository_path='repo',
  425. config=config,
  426. local_borg_version='1.2.3',
  427. check_arguments=flexmock(
  428. progress=None,
  429. repair=None,
  430. only_checks=None,
  431. force=None,
  432. match_archives=None,
  433. max_duration=None,
  434. ),
  435. global_arguments=flexmock(),
  436. checks={'repository', 'data'},
  437. archive_filter_flags=(),
  438. )
  439. def test_check_archives_with_max_duration_flag_and_data_check_runs_repository_check_separately():
  440. config = {'checks': [{'name': 'repository'}, {'name': 'data'}]}
  441. flexmock(module).should_receive('make_check_name_flags').with_args(
  442. {'data', 'archives'}, ()
  443. ).and_return(('--archives-only', '--verify-data'))
  444. flexmock(module).should_receive('make_check_name_flags').with_args(
  445. {'repository'}, ()
  446. ).and_return(('--repository-only',))
  447. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  448. insert_execute_command_mock(('borg', 'check', '--archives-only', '--verify-data', 'repo'))
  449. insert_execute_command_mock(
  450. ('borg', 'check', '--max-duration', '33', '--repository-only', 'repo')
  451. )
  452. module.check_archives(
  453. repository_path='repo',
  454. config=config,
  455. local_borg_version='1.2.3',
  456. check_arguments=flexmock(
  457. progress=None,
  458. repair=None,
  459. only_checks=None,
  460. force=None,
  461. match_archives=None,
  462. max_duration=33,
  463. ),
  464. global_arguments=flexmock(),
  465. checks={'repository', 'data'},
  466. archive_filter_flags=(),
  467. )
  468. def test_check_archives_with_max_duration_flag_overrides_max_duration_option():
  469. config = {'checks': [{'name': 'repository', 'max_duration': 33}]}
  470. flexmock(module).should_receive('make_check_name_flags').with_args(
  471. {'repository'}, ()
  472. ).and_return(())
  473. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  474. flexmock(module.environment).should_receive('make_environment')
  475. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  476. flexmock(module).should_receive('execute_command').with_args(
  477. ('borg', 'check', '--max-duration', '44', 'repo'),
  478. output_file=None,
  479. environment=None,
  480. working_directory=None,
  481. borg_local_path='borg',
  482. borg_exit_codes=None,
  483. ).once()
  484. module.check_archives(
  485. repository_path='repo',
  486. config=config,
  487. local_borg_version='1.2.3',
  488. check_arguments=flexmock(
  489. progress=None,
  490. repair=None,
  491. only_checks=None,
  492. force=None,
  493. match_archives=None,
  494. max_duration=44,
  495. ),
  496. global_arguments=flexmock(),
  497. checks={'repository'},
  498. archive_filter_flags=(),
  499. )
  500. @pytest.mark.parametrize(
  501. 'checks',
  502. (
  503. ('repository',),
  504. ('archives',),
  505. ('repository', 'archives'),
  506. ('repository', 'archives', 'other'),
  507. ),
  508. )
  509. def test_check_archives_calls_borg_with_parameters(checks):
  510. config = {}
  511. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  512. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  513. insert_execute_command_mock(('borg', 'check', 'repo'))
  514. module.check_archives(
  515. repository_path='repo',
  516. config=config,
  517. local_borg_version='1.2.3',
  518. check_arguments=flexmock(
  519. progress=None,
  520. repair=None,
  521. only_checks=None,
  522. force=None,
  523. match_archives=None,
  524. max_duration=None,
  525. ),
  526. global_arguments=flexmock(),
  527. checks=checks,
  528. archive_filter_flags=(),
  529. )
  530. def test_check_archives_with_data_check_implies_archives_check_calls_borg_with_parameters():
  531. config = {}
  532. flexmock(module).should_receive('make_check_name_flags').with_args(
  533. {'data', 'archives'}, ()
  534. ).and_return(())
  535. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  536. insert_execute_command_mock(('borg', 'check', 'repo'))
  537. module.check_archives(
  538. repository_path='repo',
  539. config=config,
  540. local_borg_version='1.2.3',
  541. check_arguments=flexmock(
  542. progress=None,
  543. repair=None,
  544. only_checks=None,
  545. force=None,
  546. match_archives=None,
  547. max_duration=None,
  548. ),
  549. global_arguments=flexmock(),
  550. checks={'data'},
  551. archive_filter_flags=(),
  552. )
  553. def test_check_archives_with_log_info_passes_through_to_borg():
  554. config = {}
  555. flexmock(module).should_receive('make_check_name_flags').with_args(
  556. {'repository'}, ()
  557. ).and_return(())
  558. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  559. insert_logging_mock(logging.INFO)
  560. insert_execute_command_mock(('borg', 'check', '--info', 'repo'))
  561. module.check_archives(
  562. repository_path='repo',
  563. config=config,
  564. local_borg_version='1.2.3',
  565. check_arguments=flexmock(
  566. progress=None,
  567. repair=None,
  568. only_checks=None,
  569. force=None,
  570. match_archives=None,
  571. max_duration=None,
  572. ),
  573. global_arguments=flexmock(),
  574. checks={'repository'},
  575. archive_filter_flags=(),
  576. )
  577. def test_check_archives_with_log_debug_passes_through_to_borg():
  578. config = {}
  579. flexmock(module).should_receive('make_check_name_flags').with_args(
  580. {'repository'}, ()
  581. ).and_return(())
  582. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  583. insert_logging_mock(logging.DEBUG)
  584. insert_execute_command_mock(('borg', 'check', '--debug', '--show-rc', 'repo'))
  585. module.check_archives(
  586. repository_path='repo',
  587. config=config,
  588. local_borg_version='1.2.3',
  589. check_arguments=flexmock(
  590. progress=None,
  591. repair=None,
  592. only_checks=None,
  593. force=None,
  594. match_archives=None,
  595. max_duration=None,
  596. ),
  597. global_arguments=flexmock(),
  598. checks={'repository'},
  599. archive_filter_flags=(),
  600. )
  601. def test_check_archives_with_local_path_calls_borg_via_local_path():
  602. checks = {'repository'}
  603. config = {}
  604. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  605. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  606. insert_execute_command_mock(('borg1', 'check', 'repo'))
  607. module.check_archives(
  608. repository_path='repo',
  609. config=config,
  610. local_borg_version='1.2.3',
  611. check_arguments=flexmock(
  612. progress=None,
  613. repair=None,
  614. only_checks=None,
  615. force=None,
  616. match_archives=None,
  617. max_duration=None,
  618. ),
  619. global_arguments=flexmock(),
  620. checks=checks,
  621. archive_filter_flags=(),
  622. local_path='borg1',
  623. )
  624. def test_check_archives_with_exit_codes_calls_borg_using_them():
  625. checks = {'repository'}
  626. borg_exit_codes = flexmock()
  627. config = {'borg_exit_codes': borg_exit_codes}
  628. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  629. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  630. insert_execute_command_mock(('borg', 'check', 'repo'), borg_exit_codes=borg_exit_codes)
  631. module.check_archives(
  632. repository_path='repo',
  633. config=config,
  634. local_borg_version='1.2.3',
  635. check_arguments=flexmock(
  636. progress=None,
  637. repair=None,
  638. only_checks=None,
  639. force=None,
  640. match_archives=None,
  641. max_duration=None,
  642. ),
  643. global_arguments=flexmock(),
  644. checks=checks,
  645. archive_filter_flags=(),
  646. )
  647. def test_check_archives_with_remote_path_passes_through_to_borg():
  648. checks = {'repository'}
  649. config = {}
  650. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  651. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  652. insert_execute_command_mock(('borg', 'check', '--remote-path', 'borg1', 'repo'))
  653. module.check_archives(
  654. repository_path='repo',
  655. config=config,
  656. local_borg_version='1.2.3',
  657. check_arguments=flexmock(
  658. progress=None,
  659. repair=None,
  660. only_checks=None,
  661. force=None,
  662. match_archives=None,
  663. max_duration=None,
  664. ),
  665. global_arguments=flexmock(),
  666. checks=checks,
  667. archive_filter_flags=(),
  668. remote_path='borg1',
  669. )
  670. def test_check_archives_with_umask_passes_through_to_borg():
  671. checks = {'repository'}
  672. config = {'umask': '077'}
  673. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  674. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  675. insert_execute_command_mock(('borg', 'check', '--umask', '077', 'repo'))
  676. module.check_archives(
  677. repository_path='repo',
  678. config=config,
  679. local_borg_version='1.2.3',
  680. check_arguments=flexmock(
  681. progress=None,
  682. repair=None,
  683. only_checks=None,
  684. force=None,
  685. match_archives=None,
  686. max_duration=None,
  687. ),
  688. global_arguments=flexmock(),
  689. checks=checks,
  690. archive_filter_flags=(),
  691. )
  692. def test_check_archives_with_log_json_passes_through_to_borg():
  693. checks = {'repository'}
  694. config = {'log_json': True}
  695. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  696. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  697. insert_execute_command_mock(('borg', 'check', '--log-json', 'repo'))
  698. module.check_archives(
  699. repository_path='repo',
  700. config=config,
  701. local_borg_version='1.2.3',
  702. check_arguments=flexmock(
  703. progress=None,
  704. repair=None,
  705. only_checks=None,
  706. force=None,
  707. match_archives=None,
  708. max_duration=None,
  709. ),
  710. global_arguments=flexmock(),
  711. checks=checks,
  712. archive_filter_flags=(),
  713. )
  714. def test_check_archives_with_lock_wait_passes_through_to_borg():
  715. checks = {'repository'}
  716. config = {'lock_wait': 5}
  717. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  718. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  719. insert_execute_command_mock(('borg', 'check', '--lock-wait', '5', 'repo'))
  720. module.check_archives(
  721. repository_path='repo',
  722. config=config,
  723. local_borg_version='1.2.3',
  724. check_arguments=flexmock(
  725. progress=None,
  726. repair=None,
  727. only_checks=None,
  728. force=None,
  729. match_archives=None,
  730. max_duration=None,
  731. ),
  732. global_arguments=flexmock(),
  733. checks=checks,
  734. archive_filter_flags=(),
  735. )
  736. def test_check_archives_with_retention_prefix():
  737. checks = {'repository'}
  738. prefix = 'foo-'
  739. config = {'prefix': prefix}
  740. flexmock(module).should_receive('make_check_name_flags').with_args(checks, ()).and_return(())
  741. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  742. insert_execute_command_mock(('borg', 'check', 'repo'))
  743. module.check_archives(
  744. repository_path='repo',
  745. config=config,
  746. local_borg_version='1.2.3',
  747. check_arguments=flexmock(
  748. progress=None,
  749. repair=None,
  750. only_checks=None,
  751. force=None,
  752. match_archives=None,
  753. max_duration=None,
  754. ),
  755. global_arguments=flexmock(),
  756. checks=checks,
  757. archive_filter_flags=(),
  758. )
  759. def test_check_archives_with_extra_borg_options_passes_through_to_borg():
  760. config = {'extra_borg_options': {'check': '--extra --options'}}
  761. flexmock(module).should_receive('make_check_name_flags').with_args(
  762. {'repository'}, ()
  763. ).and_return(())
  764. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  765. insert_execute_command_mock(('borg', 'check', '--extra', '--options', 'repo'))
  766. module.check_archives(
  767. repository_path='repo',
  768. config=config,
  769. local_borg_version='1.2.3',
  770. check_arguments=flexmock(
  771. progress=None,
  772. repair=None,
  773. only_checks=None,
  774. force=None,
  775. match_archives=None,
  776. max_duration=None,
  777. ),
  778. global_arguments=flexmock(),
  779. checks={'repository'},
  780. archive_filter_flags=(),
  781. )
  782. def test_check_archives_with_match_archives_passes_through_to_borg():
  783. config = {'checks': [{'name': 'archives'}]}
  784. flexmock(module).should_receive('make_check_name_flags').with_args(
  785. {'archives'}, object
  786. ).and_return(('--match-archives', 'foo-*'))
  787. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  788. flexmock(module.environment).should_receive('make_environment')
  789. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  790. flexmock(module).should_receive('execute_command').with_args(
  791. ('borg', 'check', '--match-archives', 'foo-*', 'repo'),
  792. output_file=None,
  793. environment=None,
  794. working_directory=None,
  795. borg_local_path='borg',
  796. borg_exit_codes=None,
  797. ).once()
  798. module.check_archives(
  799. repository_path='repo',
  800. config=config,
  801. local_borg_version='1.2.3',
  802. check_arguments=flexmock(
  803. progress=None,
  804. repair=None,
  805. only_checks=None,
  806. force=None,
  807. match_archives='foo-*',
  808. max_duration=None,
  809. ),
  810. global_arguments=flexmock(),
  811. checks={'archives'},
  812. archive_filter_flags=('--match-archives', 'foo-*'),
  813. )
  814. def test_check_archives_calls_borg_with_working_directory():
  815. config = {'working_directory': '/working/dir'}
  816. flexmock(module).should_receive('make_check_name_flags').with_args(
  817. {'repository'}, ()
  818. ).and_return(())
  819. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  820. flexmock(module.environment).should_receive('make_environment')
  821. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  822. insert_execute_command_mock(('borg', 'check', 'repo'), working_directory='/working/dir')
  823. module.check_archives(
  824. repository_path='repo',
  825. config=config,
  826. local_borg_version='1.2.3',
  827. check_arguments=flexmock(
  828. progress=False,
  829. repair=None,
  830. only_checks=None,
  831. force=None,
  832. match_archives=None,
  833. max_duration=None,
  834. ),
  835. global_arguments=flexmock(),
  836. checks={'repository'},
  837. archive_filter_flags=(),
  838. )