test_repo_delete.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import repo_delete as module
  4. from ..test_verbosity import insert_logging_mock
  5. def test_make_repo_delete_command_with_feature_available_runs_borg_repo_delete():
  6. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  7. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  8. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  9. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  10. ('repo',),
  11. )
  12. command = module.make_repo_delete_command(
  13. repository={'path': 'repo'},
  14. config={},
  15. local_borg_version='1.2.3',
  16. repo_delete_arguments=flexmock(list_details=False, force=0),
  17. global_arguments=flexmock(dry_run=False),
  18. local_path='borg',
  19. remote_path=None,
  20. )
  21. assert command == ('borg', 'repo-delete', 'repo')
  22. def test_make_repo_delete_command_without_feature_available_runs_borg_delete():
  23. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(False)
  24. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  25. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  26. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  27. ('repo',),
  28. )
  29. command = module.make_repo_delete_command(
  30. repository={'path': 'repo'},
  31. config={},
  32. local_borg_version='1.2.3',
  33. repo_delete_arguments=flexmock(list_details=False, force=0),
  34. global_arguments=flexmock(dry_run=False),
  35. local_path='borg',
  36. remote_path=None,
  37. )
  38. assert command == ('borg', 'delete', 'repo')
  39. def test_make_repo_delete_command_includes_log_info():
  40. insert_logging_mock(logging.INFO)
  41. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  42. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  43. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  44. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  45. ('repo',),
  46. )
  47. command = module.make_repo_delete_command(
  48. repository={'path': 'repo'},
  49. config={},
  50. local_borg_version='1.2.3',
  51. repo_delete_arguments=flexmock(list_details=False, force=0),
  52. global_arguments=flexmock(dry_run=False),
  53. local_path='borg',
  54. remote_path=None,
  55. )
  56. assert command == ('borg', 'repo-delete', '--info', 'repo')
  57. def test_make_repo_delete_command_includes_log_debug():
  58. insert_logging_mock(logging.DEBUG)
  59. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  60. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  61. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  62. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  63. ('repo',),
  64. )
  65. command = module.make_repo_delete_command(
  66. repository={'path': 'repo'},
  67. config={},
  68. local_borg_version='1.2.3',
  69. repo_delete_arguments=flexmock(list_details=False, force=0),
  70. global_arguments=flexmock(dry_run=False),
  71. local_path='borg',
  72. remote_path=None,
  73. )
  74. assert command == ('borg', 'repo-delete', '--debug', '--show-rc', 'repo')
  75. def test_make_repo_delete_command_includes_dry_run():
  76. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  77. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  78. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  79. 'dry-run',
  80. True,
  81. ).and_return(('--dry-run',))
  82. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  83. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  84. ('repo',),
  85. )
  86. command = module.make_repo_delete_command(
  87. repository={'path': 'repo'},
  88. config={},
  89. local_borg_version='1.2.3',
  90. repo_delete_arguments=flexmock(list_details=False, force=0),
  91. global_arguments=flexmock(dry_run=True),
  92. local_path='borg',
  93. remote_path=None,
  94. )
  95. assert command == ('borg', 'repo-delete', '--dry-run', 'repo')
  96. def test_make_repo_delete_command_includes_remote_path():
  97. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  98. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  99. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  100. 'remote-path',
  101. 'borg1',
  102. ).and_return(('--remote-path', 'borg1'))
  103. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  104. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  105. ('repo',),
  106. )
  107. command = module.make_repo_delete_command(
  108. repository={'path': 'repo'},
  109. config={},
  110. local_borg_version='1.2.3',
  111. repo_delete_arguments=flexmock(list_details=False, force=0),
  112. global_arguments=flexmock(dry_run=False),
  113. local_path='borg',
  114. remote_path='borg1',
  115. )
  116. assert command == ('borg', 'repo-delete', '--remote-path', 'borg1', 'repo')
  117. def test_make_repo_delete_command_includes_umask():
  118. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  119. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').replace_with(
  120. lambda name, value: (f'--{name}', value) if value else (),
  121. )
  122. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  123. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  124. ('repo',),
  125. )
  126. command = module.make_repo_delete_command(
  127. repository={'path': 'repo'},
  128. config={'umask': '077'},
  129. local_borg_version='1.2.3',
  130. repo_delete_arguments=flexmock(list_details=False, force=0),
  131. global_arguments=flexmock(dry_run=False),
  132. local_path='borg',
  133. remote_path=None,
  134. )
  135. assert command == ('borg', 'repo-delete', '--umask', '077', 'repo')
  136. def test_make_repo_delete_command_includes_log_json():
  137. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  138. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  139. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  140. 'log-json',
  141. True,
  142. ).and_return(('--log-json',))
  143. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  144. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  145. ('repo',),
  146. )
  147. command = module.make_repo_delete_command(
  148. repository={'path': 'repo'},
  149. config={'log_json': True},
  150. local_borg_version='1.2.3',
  151. repo_delete_arguments=flexmock(list_details=False, force=0),
  152. global_arguments=flexmock(dry_run=False),
  153. local_path='borg',
  154. remote_path=None,
  155. )
  156. assert command == ('borg', 'repo-delete', '--log-json', 'repo')
  157. def test_make_repo_delete_command_includes_lock_wait():
  158. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  159. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  160. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  161. 'lock-wait',
  162. 5,
  163. ).and_return(('--lock-wait', '5'))
  164. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  165. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  166. ('repo',),
  167. )
  168. command = module.make_repo_delete_command(
  169. repository={'path': 'repo'},
  170. config={'lock_wait': 5},
  171. local_borg_version='1.2.3',
  172. repo_delete_arguments=flexmock(list_details=False, force=0),
  173. global_arguments=flexmock(dry_run=False),
  174. local_path='borg',
  175. remote_path=None,
  176. )
  177. assert command == ('borg', 'repo-delete', '--lock-wait', '5', 'repo')
  178. def test_make_repo_delete_command_without_feature_available_includes_delete_extra_borg_options():
  179. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(False)
  180. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  181. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  182. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  183. ('repo',),
  184. )
  185. command = module.make_repo_delete_command(
  186. repository={'path': 'repo'},
  187. config={'extra_borg_options': {'delete': '--extra "value with space"'}},
  188. local_borg_version='1.2.3',
  189. repo_delete_arguments=flexmock(list_details=False, force=0),
  190. global_arguments=flexmock(dry_run=False),
  191. local_path='borg',
  192. remote_path=None,
  193. )
  194. assert command == ('borg', 'delete', '--extra', 'value with space', 'repo')
  195. def test_make_repo_delete_command_with_feature_available_includes_delete_extra_borg_options():
  196. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  197. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  198. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  199. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  200. ('repo',),
  201. )
  202. command = module.make_repo_delete_command(
  203. repository={'path': 'repo'},
  204. config={'extra_borg_options': {'repo_delete': '--extra "value with space"'}},
  205. local_borg_version='1.2.3',
  206. repo_delete_arguments=flexmock(list_details=False, force=0),
  207. global_arguments=flexmock(dry_run=False),
  208. local_path='borg',
  209. remote_path=None,
  210. )
  211. assert command == ('borg', 'repo-delete', '--extra', 'value with space', 'repo')
  212. def test_make_repo_delete_command_includes_list():
  213. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  214. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  215. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  216. 'list',
  217. True,
  218. ).and_return(('--list',))
  219. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  220. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  221. ('repo',),
  222. )
  223. command = module.make_repo_delete_command(
  224. repository={'path': 'repo'},
  225. config={'list_details': True},
  226. local_borg_version='1.2.3',
  227. repo_delete_arguments=flexmock(list_details=True, force=0),
  228. global_arguments=flexmock(dry_run=False),
  229. local_path='borg',
  230. remote_path=None,
  231. )
  232. assert command == ('borg', 'repo-delete', '--list', 'repo')
  233. def test_make_repo_delete_command_includes_force():
  234. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  235. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  236. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  237. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  238. ('repo',),
  239. )
  240. command = module.make_repo_delete_command(
  241. repository={'path': 'repo'},
  242. config={},
  243. local_borg_version='1.2.3',
  244. repo_delete_arguments=flexmock(list_details=False, force=1),
  245. global_arguments=flexmock(dry_run=False),
  246. local_path='borg',
  247. remote_path=None,
  248. )
  249. assert command == ('borg', 'repo-delete', '--force', 'repo')
  250. def test_make_repo_delete_command_includes_force_twice():
  251. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  252. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  253. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  254. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  255. ('repo',),
  256. )
  257. command = module.make_repo_delete_command(
  258. repository={'path': 'repo'},
  259. config={},
  260. local_borg_version='1.2.3',
  261. repo_delete_arguments=flexmock(list_details=False, force=2),
  262. global_arguments=flexmock(dry_run=False),
  263. local_path='borg',
  264. remote_path=None,
  265. )
  266. assert command == ('borg', 'repo-delete', '--force', '--force', 'repo')
  267. def test_delete_repository_with_defaults_does_not_capture_output():
  268. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  269. command = flexmock()
  270. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  271. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  272. flexmock(),
  273. )
  274. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  275. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  276. command,
  277. output_log_level=module.logging.ANSWER,
  278. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  279. environment=object,
  280. working_directory=None,
  281. borg_local_path='borg',
  282. borg_exit_codes=None,
  283. ).once()
  284. module.delete_repository(
  285. repository={'path': 'repo'},
  286. config={},
  287. local_borg_version=flexmock(),
  288. repo_delete_arguments=flexmock(force=False, cache_only=False),
  289. global_arguments=flexmock(),
  290. local_path='borg',
  291. remote_path=None,
  292. )
  293. def test_delete_repository_with_force_captures_output():
  294. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  295. command = flexmock()
  296. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  297. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  298. flexmock(),
  299. )
  300. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  301. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  302. command,
  303. output_log_level=module.logging.ANSWER,
  304. output_file=None,
  305. environment=object,
  306. working_directory=None,
  307. borg_local_path='borg',
  308. borg_exit_codes=None,
  309. ).once()
  310. module.delete_repository(
  311. repository={'path': 'repo'},
  312. config={},
  313. local_borg_version=flexmock(),
  314. repo_delete_arguments=flexmock(force=True, cache_only=False),
  315. global_arguments=flexmock(),
  316. local_path='borg',
  317. remote_path=None,
  318. )
  319. def test_delete_repository_with_cache_only_captures_output():
  320. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  321. command = flexmock()
  322. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  323. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  324. flexmock(),
  325. )
  326. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  327. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  328. command,
  329. output_log_level=module.logging.ANSWER,
  330. output_file=None,
  331. environment=object,
  332. working_directory=None,
  333. borg_local_path='borg',
  334. borg_exit_codes=None,
  335. ).once()
  336. module.delete_repository(
  337. repository={'path': 'repo'},
  338. config={},
  339. local_borg_version=flexmock(),
  340. repo_delete_arguments=flexmock(force=False, cache_only=True),
  341. global_arguments=flexmock(),
  342. local_path='borg',
  343. remote_path=None,
  344. )
  345. def test_delete_repository_calls_borg_with_working_directory():
  346. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  347. command = flexmock()
  348. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  349. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  350. flexmock(),
  351. )
  352. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  353. '/working/dir',
  354. )
  355. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  356. command,
  357. output_log_level=module.logging.ANSWER,
  358. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  359. environment=object,
  360. working_directory='/working/dir',
  361. borg_local_path='borg',
  362. borg_exit_codes=None,
  363. ).once()
  364. module.delete_repository(
  365. repository={'path': 'repo'},
  366. config={'working_directory': '/working/dir'},
  367. local_borg_version=flexmock(),
  368. repo_delete_arguments=flexmock(force=False, cache_only=False),
  369. global_arguments=flexmock(),
  370. local_path='borg',
  371. remote_path=None,
  372. )