test_info.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. import logging
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.borg import info as module
  5. from ..test_verbosity import insert_logging_mock
  6. def test_make_info_command_constructs_borg_info_command():
  7. flexmock(module.flags).should_receive('make_flags').and_return(())
  8. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  9. None,
  10. None,
  11. '2.3.4',
  12. ).and_return(())
  13. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  14. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  15. command = module.make_info_command(
  16. repository_path='repo',
  17. config={},
  18. local_borg_version='2.3.4',
  19. global_arguments=flexmock(),
  20. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  21. local_path='borg',
  22. remote_path=None,
  23. )
  24. assert command == ('borg', 'info', '--repo', 'repo')
  25. def test_make_info_command_with_log_info_passes_through_to_command():
  26. flexmock(module.flags).should_receive('make_flags').and_return(())
  27. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  28. None,
  29. None,
  30. '2.3.4',
  31. ).and_return(())
  32. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  33. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  34. insert_logging_mock(logging.INFO)
  35. command = module.make_info_command(
  36. repository_path='repo',
  37. config={},
  38. local_borg_version='2.3.4',
  39. global_arguments=flexmock(),
  40. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  41. local_path='borg',
  42. remote_path=None,
  43. )
  44. assert command == ('borg', 'info', '--info', '--repo', 'repo')
  45. def test_make_info_command_with_log_info_and_json_omits_borg_logging_flags():
  46. flexmock(module.flags).should_receive('make_flags').and_return(())
  47. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  48. None,
  49. None,
  50. '2.3.4',
  51. ).and_return(())
  52. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  53. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  54. insert_logging_mock(logging.INFO)
  55. command = module.make_info_command(
  56. repository_path='repo',
  57. config={},
  58. local_borg_version='2.3.4',
  59. global_arguments=flexmock(),
  60. info_arguments=flexmock(archive=None, json=True, prefix=None, match_archives=None),
  61. local_path='borg',
  62. remote_path=None,
  63. )
  64. assert command == ('borg', 'info', '--json', '--repo', 'repo')
  65. def test_make_info_command_with_log_debug_passes_through_to_command():
  66. flexmock(module.flags).should_receive('make_flags').and_return(())
  67. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  68. None,
  69. None,
  70. '2.3.4',
  71. ).and_return(())
  72. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  73. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  74. insert_logging_mock(logging.DEBUG)
  75. command = module.make_info_command(
  76. repository_path='repo',
  77. config={},
  78. local_borg_version='2.3.4',
  79. global_arguments=flexmock(),
  80. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  81. local_path='borg',
  82. remote_path=None,
  83. )
  84. assert command == ('borg', 'info', '--debug', '--show-rc', '--repo', 'repo')
  85. def test_make_info_command_with_log_debug_and_json_omits_borg_logging_flags():
  86. flexmock(module.flags).should_receive('make_flags').and_return(())
  87. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  88. None,
  89. None,
  90. '2.3.4',
  91. ).and_return(())
  92. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  93. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  94. command = module.make_info_command(
  95. repository_path='repo',
  96. config={},
  97. local_borg_version='2.3.4',
  98. global_arguments=flexmock(),
  99. info_arguments=flexmock(archive=None, json=True, prefix=None, match_archives=None),
  100. local_path='borg',
  101. remote_path=None,
  102. )
  103. assert command == ('borg', 'info', '--json', '--repo', 'repo')
  104. def test_make_info_command_with_json_passes_through_to_command():
  105. flexmock(module.flags).should_receive('make_flags').and_return(())
  106. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  107. None,
  108. None,
  109. '2.3.4',
  110. ).and_return(())
  111. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(('--json',))
  112. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  113. command = module.make_info_command(
  114. repository_path='repo',
  115. config={},
  116. local_borg_version='2.3.4',
  117. global_arguments=flexmock(),
  118. info_arguments=flexmock(archive=None, json=True, prefix=None, match_archives=None),
  119. local_path='borg',
  120. remote_path=None,
  121. )
  122. assert command == ('borg', 'info', '--json', '--repo', 'repo')
  123. def test_make_info_command_with_archive_uses_match_archives_flags():
  124. flexmock(module.flags).should_receive('make_flags').and_return(())
  125. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  126. 'archive',
  127. None,
  128. '2.3.4',
  129. ).and_return(('--match-archives', 'archive'))
  130. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  131. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  132. command = module.make_info_command(
  133. repository_path='repo',
  134. config={},
  135. local_borg_version='2.3.4',
  136. global_arguments=flexmock(),
  137. info_arguments=flexmock(archive='archive', json=False, prefix=None, match_archives=None),
  138. local_path='borg',
  139. remote_path=None,
  140. )
  141. assert command == ('borg', 'info', '--match-archives', 'archive', '--repo', 'repo')
  142. def test_make_info_command_with_local_path_passes_through_to_command():
  143. flexmock(module.flags).should_receive('make_flags').and_return(())
  144. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  145. None,
  146. None,
  147. '2.3.4',
  148. ).and_return(())
  149. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  150. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  151. command = module.make_info_command(
  152. repository_path='repo',
  153. config={},
  154. local_borg_version='2.3.4',
  155. global_arguments=flexmock(),
  156. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  157. local_path='borg1',
  158. remote_path=None,
  159. )
  160. assert command == ('borg1', 'info', '--repo', 'repo')
  161. def test_make_info_command_with_remote_path_passes_through_to_command():
  162. flexmock(module.flags).should_receive('make_flags').and_return(())
  163. flexmock(module.flags).should_receive('make_flags').with_args(
  164. 'remote-path',
  165. 'borg1',
  166. ).and_return(('--remote-path', 'borg1'))
  167. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  168. None,
  169. None,
  170. '2.3.4',
  171. ).and_return(())
  172. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  173. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  174. command = module.make_info_command(
  175. repository_path='repo',
  176. config={},
  177. local_borg_version='2.3.4',
  178. global_arguments=flexmock(),
  179. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  180. local_path='borg',
  181. remote_path='borg1',
  182. )
  183. assert command == ('borg', 'info', '--remote-path', 'borg1', '--repo', 'repo')
  184. def test_make_info_command_with_umask_passes_through_to_command():
  185. flexmock(module.flags).should_receive('make_flags').replace_with(
  186. lambda name, value: (f'--{name}', value) if value else (),
  187. )
  188. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  189. None,
  190. None,
  191. '2.3.4',
  192. ).and_return(())
  193. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  194. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  195. command = module.make_info_command(
  196. repository_path='repo',
  197. config={'umask': '077'},
  198. local_borg_version='2.3.4',
  199. global_arguments=flexmock(),
  200. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  201. local_path='borg',
  202. remote_path=None,
  203. )
  204. assert command == ('borg', 'info', '--umask', '077', '--repo', 'repo')
  205. def test_make_info_command_with_log_json_passes_through_to_command():
  206. flexmock(module.flags).should_receive('make_flags').and_return(())
  207. flexmock(module.flags).should_receive('make_flags').with_args('log-json', True).and_return(
  208. ('--log-json',),
  209. )
  210. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  211. None,
  212. None,
  213. '2.3.4',
  214. ).and_return(())
  215. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  216. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  217. command = module.make_info_command(
  218. repository_path='repo',
  219. config={'log_json': True},
  220. local_borg_version='2.3.4',
  221. global_arguments=flexmock(),
  222. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  223. local_path='borg',
  224. remote_path=None,
  225. )
  226. assert command == ('borg', 'info', '--log-json', '--repo', 'repo')
  227. def test_make_info_command_with_lock_wait_passes_through_to_command():
  228. flexmock(module.flags).should_receive('make_flags').and_return(())
  229. flexmock(module.flags).should_receive('make_flags').with_args('lock-wait', 5).and_return(
  230. ('--lock-wait', '5'),
  231. )
  232. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  233. None,
  234. None,
  235. '2.3.4',
  236. ).and_return(())
  237. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  238. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  239. config = {'lock_wait': 5}
  240. command = module.make_info_command(
  241. repository_path='repo',
  242. config=config,
  243. local_borg_version='2.3.4',
  244. global_arguments=flexmock(),
  245. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  246. local_path='borg',
  247. remote_path=None,
  248. )
  249. assert command == ('borg', 'info', '--lock-wait', '5', '--repo', 'repo')
  250. def test_make_info_command_transforms_prefix_into_match_archives_flags():
  251. flexmock(module.flags).should_receive('make_flags').and_return(())
  252. flexmock(module.flags).should_receive('make_flags').with_args(
  253. 'match-archives',
  254. 'sh:foo*',
  255. ).and_return(('--match-archives', 'sh:foo*'))
  256. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  257. None,
  258. None,
  259. '2.3.4',
  260. ).and_return(())
  261. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  262. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  263. command = module.make_info_command(
  264. repository_path='repo',
  265. config={},
  266. local_borg_version='2.3.4',
  267. global_arguments=flexmock(),
  268. info_arguments=flexmock(archive=None, json=False, prefix='foo'),
  269. local_path='borg',
  270. remote_path=None,
  271. )
  272. assert command == ('borg', 'info', '--match-archives', 'sh:foo*', '--repo', 'repo')
  273. def test_make_info_command_prefers_prefix_over_archive_name_format():
  274. flexmock(module.flags).should_receive('make_flags').and_return(())
  275. flexmock(module.flags).should_receive('make_flags').with_args(
  276. 'match-archives',
  277. 'sh:foo*',
  278. ).and_return(('--match-archives', 'sh:foo*'))
  279. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  280. None,
  281. None,
  282. '2.3.4',
  283. ).and_return(())
  284. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  285. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  286. command = module.make_info_command(
  287. repository_path='repo',
  288. config={'archive_name_format': 'bar-{now}'},
  289. local_borg_version='2.3.4',
  290. global_arguments=flexmock(),
  291. info_arguments=flexmock(archive=None, json=False, prefix='foo'),
  292. local_path='borg',
  293. remote_path=None,
  294. )
  295. assert command == ('borg', 'info', '--match-archives', 'sh:foo*', '--repo', 'repo')
  296. def test_make_info_command_transforms_archive_name_format_into_match_archives_flags():
  297. flexmock(module.flags).should_receive('make_flags').and_return(())
  298. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  299. None,
  300. 'bar-{now}',
  301. '2.3.4',
  302. ).and_return(('--match-archives', 'sh:bar-*'))
  303. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  304. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  305. command = module.make_info_command(
  306. repository_path='repo',
  307. config={'archive_name_format': 'bar-{now}'},
  308. local_borg_version='2.3.4',
  309. global_arguments=flexmock(),
  310. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  311. local_path='borg',
  312. remote_path=None,
  313. )
  314. assert command == ('borg', 'info', '--match-archives', 'sh:bar-*', '--repo', 'repo')
  315. def test_make_info_command_with_match_archives_option_passes_through_to_command():
  316. flexmock(module.flags).should_receive('make_flags').and_return(())
  317. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  318. 'sh:foo-*',
  319. 'bar-{now}',
  320. '2.3.4',
  321. ).and_return(('--match-archives', 'sh:foo-*'))
  322. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  323. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  324. flexmock(module.environment).should_receive('make_environment')
  325. command = module.make_info_command(
  326. repository_path='repo',
  327. config={
  328. 'archive_name_format': 'bar-{now}',
  329. 'match_archives': 'sh:foo-*',
  330. },
  331. local_borg_version='2.3.4',
  332. global_arguments=flexmock(),
  333. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  334. local_path='borg',
  335. remote_path=None,
  336. )
  337. assert command == ('borg', 'info', '--match-archives', 'sh:foo-*', '--repo', 'repo')
  338. def test_make_info_command_with_match_archives_flag_passes_through_to_command():
  339. flexmock(module.flags).should_receive('make_flags').and_return(())
  340. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  341. 'sh:foo-*',
  342. 'bar-{now}',
  343. '2.3.4',
  344. ).and_return(('--match-archives', 'sh:foo-*'))
  345. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  346. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  347. flexmock(module.environment).should_receive('make_environment')
  348. command = module.make_info_command(
  349. repository_path='repo',
  350. config={'archive_name_format': 'bar-{now}', 'match_archives': 'sh:foo-*'},
  351. local_borg_version='2.3.4',
  352. global_arguments=flexmock(),
  353. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives='sh:foo-*'),
  354. local_path='borg',
  355. remote_path=None,
  356. )
  357. assert command == ('borg', 'info', '--match-archives', 'sh:foo-*', '--repo', 'repo')
  358. @pytest.mark.parametrize('argument_name', ('sort_by', 'first', 'last'))
  359. def test_make_info_command_passes_arguments_through_to_command(argument_name):
  360. flag_name = f"--{argument_name.replace('_', ' ')}"
  361. flexmock(module.flags).should_receive('make_flags').and_return(())
  362. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  363. None,
  364. None,
  365. '2.3.4',
  366. ).and_return(())
  367. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  368. (flag_name, 'value'),
  369. )
  370. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  371. flexmock(module.environment).should_receive('make_environment')
  372. command = module.make_info_command(
  373. repository_path='repo',
  374. config={},
  375. local_borg_version='2.3.4',
  376. global_arguments=flexmock(),
  377. info_arguments=flexmock(
  378. archive=None,
  379. json=False,
  380. prefix=None,
  381. match_archives=None,
  382. **{argument_name: 'value'},
  383. ),
  384. local_path='borg',
  385. remote_path=None,
  386. )
  387. assert command == ('borg', 'info', flag_name, 'value', '--repo', 'repo')
  388. def test_make_info_command_with_date_based_matching_passes_through_to_command():
  389. flexmock(module.flags).should_receive('make_flags').and_return(())
  390. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  391. None,
  392. None,
  393. '2.3.4',
  394. ).and_return(())
  395. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  396. ('--newer', '1d', '--newest', '1y', '--older', '1m', '--oldest', '1w'),
  397. )
  398. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  399. info_arguments = flexmock(
  400. archive=None,
  401. json=False,
  402. prefix=None,
  403. match_archives=None,
  404. newer='1d',
  405. newest='1y',
  406. older='1m',
  407. oldest='1w',
  408. )
  409. command = module.make_info_command(
  410. repository_path='repo',
  411. config={},
  412. local_borg_version='2.3.4',
  413. global_arguments=flexmock(),
  414. info_arguments=info_arguments,
  415. local_path='borg',
  416. remote_path=None,
  417. )
  418. assert command == (
  419. 'borg',
  420. 'info',
  421. '--newer',
  422. '1d',
  423. '--newest',
  424. '1y',
  425. '--older',
  426. '1m',
  427. '--oldest',
  428. '1w',
  429. '--repo',
  430. 'repo',
  431. )
  432. def test_display_archives_info_calls_two_commands():
  433. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  434. flexmock(module).should_receive('make_info_command')
  435. flexmock(module.environment).should_receive('make_environment')
  436. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  437. flexmock(module).should_receive('execute_command_and_capture_output').once()
  438. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags')
  439. flexmock(module).should_receive('execute_command').once()
  440. module.display_archives_info(
  441. repository_path='repo',
  442. config={},
  443. local_borg_version='2.3.4',
  444. global_arguments=flexmock(),
  445. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  446. )
  447. def test_display_archives_info_with_json_calls_json_command_only():
  448. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  449. flexmock(module).should_receive('make_info_command')
  450. flexmock(module.environment).should_receive('make_environment')
  451. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  452. json_output = flexmock()
  453. flexmock(module).should_receive('execute_command_and_capture_output').and_return(json_output)
  454. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags').never()
  455. flexmock(module).should_receive('execute_command').never()
  456. assert (
  457. module.display_archives_info(
  458. repository_path='repo',
  459. config={},
  460. local_borg_version='2.3.4',
  461. global_arguments=flexmock(),
  462. info_arguments=flexmock(archive=None, json=True, prefix=None, match_archives=None),
  463. )
  464. == json_output
  465. )
  466. def test_display_archives_info_calls_borg_with_working_directory():
  467. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  468. flexmock(module).should_receive('make_info_command')
  469. flexmock(module.environment).should_receive('make_environment')
  470. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  471. '/working/dir',
  472. )
  473. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  474. full_command=object,
  475. environment=object,
  476. working_directory='/working/dir',
  477. borg_local_path=object,
  478. borg_exit_codes=object,
  479. ).once()
  480. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags')
  481. flexmock(module).should_receive('execute_command').with_args(
  482. full_command=object,
  483. output_log_level=object,
  484. environment=object,
  485. working_directory='/working/dir',
  486. borg_local_path=object,
  487. borg_exit_codes=object,
  488. ).once()
  489. module.display_archives_info(
  490. repository_path='repo',
  491. config={'working_directory': '/working/dir'},
  492. local_borg_version='2.3.4',
  493. global_arguments=flexmock(),
  494. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  495. )