test_info.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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(),
  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(),
  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(),
  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(),
  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(),
  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(),
  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(),
  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(),
  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(),
  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_umask_passes_through_to_command():
  166. flexmock(module.flags).should_receive('make_flags').replace_with(
  167. lambda name, value: (f'--{name}', value) if value else ()
  168. )
  169. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  170. None, None, '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={'umask': '077'},
  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=None,
  182. )
  183. assert command == ('borg', 'info', '--umask', '077', '--repo', 'repo')
  184. def test_make_info_command_with_log_json_passes_through_to_command():
  185. flexmock(module.flags).should_receive('make_flags').and_return(())
  186. flexmock(module.flags).should_receive('make_flags').with_args('log-json', True).and_return(
  187. ('--log-json',)
  188. )
  189. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  190. None, None, '2.3.4'
  191. ).and_return(())
  192. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  193. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  194. command = module.make_info_command(
  195. repository_path='repo',
  196. config={'log_json': True},
  197. local_borg_version='2.3.4',
  198. global_arguments=flexmock(),
  199. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  200. local_path='borg',
  201. remote_path=None,
  202. )
  203. assert command == ('borg', 'info', '--log-json', '--repo', 'repo')
  204. def test_make_info_command_with_lock_wait_passes_through_to_command():
  205. flexmock(module.flags).should_receive('make_flags').and_return(())
  206. flexmock(module.flags).should_receive('make_flags').with_args('lock-wait', 5).and_return(
  207. ('--lock-wait', '5')
  208. )
  209. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  210. None, None, '2.3.4'
  211. ).and_return(())
  212. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  213. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  214. config = {'lock_wait': 5}
  215. command = module.make_info_command(
  216. repository_path='repo',
  217. config=config,
  218. local_borg_version='2.3.4',
  219. global_arguments=flexmock(),
  220. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  221. local_path='borg',
  222. remote_path=None,
  223. )
  224. assert command == ('borg', 'info', '--lock-wait', '5', '--repo', 'repo')
  225. def test_make_info_command_transforms_prefix_into_match_archives_flags():
  226. flexmock(module.flags).should_receive('make_flags').and_return(())
  227. flexmock(module.flags).should_receive('make_flags').with_args(
  228. 'match-archives', 'sh:foo*'
  229. ).and_return(('--match-archives', 'sh:foo*'))
  230. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  231. None, None, '2.3.4'
  232. ).and_return(())
  233. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  234. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  235. command = module.make_info_command(
  236. repository_path='repo',
  237. config={},
  238. local_borg_version='2.3.4',
  239. global_arguments=flexmock(),
  240. info_arguments=flexmock(archive=None, json=False, prefix='foo'),
  241. local_path='borg',
  242. remote_path=None,
  243. )
  244. assert command == ('borg', 'info', '--match-archives', 'sh:foo*', '--repo', 'repo')
  245. def test_make_info_command_prefers_prefix_over_archive_name_format():
  246. flexmock(module.flags).should_receive('make_flags').and_return(())
  247. flexmock(module.flags).should_receive('make_flags').with_args(
  248. 'match-archives', 'sh:foo*'
  249. ).and_return(('--match-archives', 'sh:foo*'))
  250. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  251. None, None, '2.3.4'
  252. ).and_return(())
  253. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  254. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  255. command = module.make_info_command(
  256. repository_path='repo',
  257. config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  258. local_borg_version='2.3.4',
  259. global_arguments=flexmock(),
  260. info_arguments=flexmock(archive=None, json=False, prefix='foo'),
  261. local_path='borg',
  262. remote_path=None,
  263. )
  264. assert command == ('borg', 'info', '--match-archives', 'sh:foo*', '--repo', 'repo')
  265. def test_make_info_command_transforms_archive_name_format_into_match_archives_flags():
  266. flexmock(module.flags).should_receive('make_flags').and_return(())
  267. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  268. None, 'bar-{now}', '2.3.4' # noqa: FS003
  269. ).and_return(('--match-archives', 'sh:bar-*'))
  270. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  271. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  272. command = module.make_info_command(
  273. repository_path='repo',
  274. config={'archive_name_format': 'bar-{now}'}, # noqa: FS003
  275. local_borg_version='2.3.4',
  276. global_arguments=flexmock(),
  277. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  278. local_path='borg',
  279. remote_path=None,
  280. )
  281. assert command == ('borg', 'info', '--match-archives', 'sh:bar-*', '--repo', 'repo')
  282. def test_make_info_command_with_match_archives_option_passes_through_to_command():
  283. flexmock(module.flags).should_receive('make_flags').and_return(())
  284. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  285. 'sh:foo-*', 'bar-{now}', '2.3.4' # noqa: FS003
  286. ).and_return(('--match-archives', 'sh:foo-*'))
  287. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  288. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  289. flexmock(module.environment).should_receive('make_environment')
  290. command = module.make_info_command(
  291. repository_path='repo',
  292. config={
  293. 'archive_name_format': 'bar-{now}', # noqa: FS003
  294. 'match_archives': 'sh:foo-*',
  295. },
  296. local_borg_version='2.3.4',
  297. global_arguments=flexmock(),
  298. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  299. local_path='borg',
  300. remote_path=None,
  301. )
  302. assert command == ('borg', 'info', '--match-archives', 'sh:foo-*', '--repo', 'repo')
  303. def test_make_info_command_with_match_archives_flag_passes_through_to_command():
  304. flexmock(module.flags).should_receive('make_flags').and_return(())
  305. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  306. 'sh:foo-*', 'bar-{now}', '2.3.4' # noqa: FS003
  307. ).and_return(('--match-archives', 'sh:foo-*'))
  308. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(())
  309. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  310. flexmock(module.environment).should_receive('make_environment')
  311. command = module.make_info_command(
  312. repository_path='repo',
  313. config={'archive_name_format': 'bar-{now}', 'match_archives': 'sh:foo-*'}, # noqa: FS003
  314. local_borg_version='2.3.4',
  315. global_arguments=flexmock(),
  316. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives='sh:foo-*'),
  317. local_path='borg',
  318. remote_path=None,
  319. )
  320. assert command == ('borg', 'info', '--match-archives', 'sh:foo-*', '--repo', 'repo')
  321. @pytest.mark.parametrize('argument_name', ('sort_by', 'first', 'last'))
  322. def test_make_info_command_passes_arguments_through_to_command(argument_name):
  323. flag_name = f"--{argument_name.replace('_', ' ')}"
  324. flexmock(module.flags).should_receive('make_flags').and_return(())
  325. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  326. None, None, '2.3.4'
  327. ).and_return(())
  328. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  329. (flag_name, 'value')
  330. )
  331. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  332. flexmock(module.environment).should_receive('make_environment')
  333. command = module.make_info_command(
  334. repository_path='repo',
  335. config={},
  336. local_borg_version='2.3.4',
  337. global_arguments=flexmock(),
  338. info_arguments=flexmock(
  339. archive=None, json=False, prefix=None, match_archives=None, **{argument_name: 'value'}
  340. ),
  341. local_path='borg',
  342. remote_path=None,
  343. )
  344. assert command == ('borg', 'info', flag_name, 'value', '--repo', 'repo')
  345. def test_make_info_command_with_date_based_matching_passes_through_to_command():
  346. flexmock(module.flags).should_receive('make_flags').and_return(())
  347. flexmock(module.flags).should_receive('make_match_archives_flags').with_args(
  348. None, None, '2.3.4'
  349. ).and_return(())
  350. flexmock(module.flags).should_receive('make_flags_from_arguments').and_return(
  351. ('--newer', '1d', '--newest', '1y', '--older', '1m', '--oldest', '1w')
  352. )
  353. flexmock(module.flags).should_receive('make_repository_flags').and_return(('--repo', 'repo'))
  354. info_arguments = flexmock(
  355. archive=None,
  356. json=False,
  357. prefix=None,
  358. match_archives=None,
  359. newer='1d',
  360. newest='1y',
  361. older='1m',
  362. oldest='1w',
  363. )
  364. command = module.make_info_command(
  365. repository_path='repo',
  366. config={},
  367. local_borg_version='2.3.4',
  368. global_arguments=flexmock(),
  369. info_arguments=info_arguments,
  370. local_path='borg',
  371. remote_path=None,
  372. )
  373. assert command == (
  374. 'borg',
  375. 'info',
  376. '--newer',
  377. '1d',
  378. '--newest',
  379. '1y',
  380. '--older',
  381. '1m',
  382. '--oldest',
  383. '1w',
  384. '--repo',
  385. 'repo',
  386. )
  387. def test_display_archives_info_calls_two_commands():
  388. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  389. flexmock(module).should_receive('make_info_command')
  390. flexmock(module.environment).should_receive('make_environment')
  391. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  392. flexmock(module).should_receive('execute_command_and_capture_output').once()
  393. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags')
  394. flexmock(module).should_receive('execute_command').once()
  395. module.display_archives_info(
  396. repository_path='repo',
  397. config={},
  398. local_borg_version='2.3.4',
  399. global_arguments=flexmock(),
  400. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  401. )
  402. def test_display_archives_info_with_json_calls_json_command_only():
  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(None)
  407. json_output = flexmock()
  408. flexmock(module).should_receive('execute_command_and_capture_output').and_return(json_output)
  409. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags').never()
  410. flexmock(module).should_receive('execute_command').never()
  411. assert (
  412. module.display_archives_info(
  413. repository_path='repo',
  414. config={},
  415. local_borg_version='2.3.4',
  416. global_arguments=flexmock(),
  417. info_arguments=flexmock(archive=None, json=True, prefix=None, match_archives=None),
  418. )
  419. == json_output
  420. )
  421. def test_display_archives_info_calls_borg_with_working_directory():
  422. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  423. flexmock(module).should_receive('make_info_command')
  424. flexmock(module.environment).should_receive('make_environment')
  425. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  426. '/working/dir',
  427. )
  428. flexmock(module).should_receive('execute_command_and_capture_output').with_args(
  429. full_command=object,
  430. environment=object,
  431. working_directory='/working/dir',
  432. borg_local_path=object,
  433. borg_exit_codes=object,
  434. ).once()
  435. flexmock(module.flags).should_receive('warn_for_aggressive_archive_flags')
  436. flexmock(module).should_receive('execute_command').with_args(
  437. full_command=object,
  438. output_log_level=object,
  439. environment=object,
  440. working_directory='/working/dir',
  441. borg_local_path=object,
  442. borg_exit_codes=object,
  443. ).once()
  444. module.display_archives_info(
  445. repository_path='repo',
  446. config={'working_directory': '/working/dir'},
  447. local_borg_version='2.3.4',
  448. global_arguments=flexmock(),
  449. info_arguments=flexmock(archive=None, json=False, prefix=None, match_archives=None),
  450. )