test_delete.py 18 KB

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