test_repo_info.py 19 KB

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