test_info.py 21 KB

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