test_delete.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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_details=False, force=0, match_archives=None, archive=None),
  19. global_arguments=flexmock(dry_run=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_details=False, force=0, match_archives=None, archive=None),
  37. global_arguments=flexmock(dry_run=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_details=False, force=0, match_archives=None, archive=None),
  57. global_arguments=flexmock(dry_run=True),
  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_details=False, force=0, match_archives=None, archive=None),
  77. global_arguments=flexmock(dry_run=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_details=False, force=0, match_archives=None, archive=None),
  96. global_arguments=flexmock(dry_run=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={'log_json': True},
  114. local_borg_version='1.2.3',
  115. delete_arguments=flexmock(list_details=False, force=0, match_archives=None, archive=None),
  116. global_arguments=flexmock(dry_run=False),
  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_details=False, force=0, match_archives=None, archive=None),
  136. global_arguments=flexmock(dry_run=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_with_list_config_calls_borg_with_list_flag():
  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': True},
  154. local_borg_version='1.2.3',
  155. delete_arguments=flexmock(list_details=None, force=0, match_archives=None, archive=None),
  156. global_arguments=flexmock(dry_run=False),
  157. local_path='borg',
  158. remote_path=None,
  159. )
  160. assert command == ('borg', 'delete', '--list', 'repo')
  161. def test_make_delete_command_includes_force():
  162. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  163. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  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_delete_command(
  169. repository={'path': 'repo'},
  170. config={},
  171. local_borg_version='1.2.3',
  172. delete_arguments=flexmock(list_details=False, force=1, match_archives=None, archive=None),
  173. global_arguments=flexmock(dry_run=False),
  174. local_path='borg',
  175. remote_path=None,
  176. )
  177. assert command == ('borg', 'delete', '--force', 'repo')
  178. def test_make_delete_command_includes_force_twice():
  179. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  180. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_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_delete_command(
  186. repository={'path': 'repo'},
  187. config={},
  188. local_borg_version='1.2.3',
  189. delete_arguments=flexmock(list_details=False, force=2, match_archives=None, archive=None),
  190. global_arguments=flexmock(dry_run=False),
  191. local_path='borg',
  192. remote_path=None,
  193. )
  194. assert command == ('borg', 'delete', '--force', '--force', 'repo')
  195. def test_make_delete_command_includes_archive():
  196. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  197. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(
  198. ('--match-archives', 'archive')
  199. )
  200. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  201. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  202. ('repo',)
  203. )
  204. command = module.make_delete_command(
  205. repository={'path': 'repo'},
  206. config={},
  207. local_borg_version='1.2.3',
  208. delete_arguments=flexmock(
  209. list_details=False, force=0, match_archives=None, archive='archive'
  210. ),
  211. global_arguments=flexmock(dry_run=False),
  212. local_path='borg',
  213. remote_path=None,
  214. )
  215. assert command == ('borg', 'delete', '--match-archives', 'archive', 'repo')
  216. def test_make_delete_command_includes_match_archives():
  217. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  218. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(
  219. ('--match-archives', 'sh:foo*')
  220. )
  221. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  222. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  223. ('repo',)
  224. )
  225. command = module.make_delete_command(
  226. repository={'path': 'repo'},
  227. config={},
  228. local_borg_version='1.2.3',
  229. delete_arguments=flexmock(
  230. list_details=False, force=0, match_archives='sh:foo*', archive='archive'
  231. ),
  232. global_arguments=flexmock(dry_run=False),
  233. local_path='borg',
  234. remote_path=None,
  235. )
  236. assert command == ('borg', 'delete', '--match-archives', 'sh:foo*', 'repo')
  237. LOGGING_ANSWER = flexmock()
  238. def test_delete_archives_with_archive_calls_borg_delete():
  239. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  240. flexmock(module.logging).ANSWER = LOGGING_ANSWER
  241. flexmock(module.borgmatic.borg.repo_delete).should_receive('delete_repository').never()
  242. flexmock(module).should_receive('make_delete_command').and_return(flexmock())
  243. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  244. flexmock()
  245. )
  246. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  247. flexmock(module.borgmatic.execute).should_receive('execute_command').once()
  248. module.delete_archives(
  249. repository={'path': 'repo'},
  250. config={},
  251. local_borg_version=flexmock(),
  252. delete_arguments=flexmock(archive='archive'),
  253. global_arguments=flexmock(),
  254. )
  255. def test_delete_archives_with_match_archives_calls_borg_delete():
  256. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  257. flexmock(module.logging).ANSWER = LOGGING_ANSWER
  258. flexmock(module.borgmatic.borg.repo_delete).should_receive('delete_repository').never()
  259. flexmock(module).should_receive('make_delete_command').and_return(flexmock())
  260. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  261. flexmock()
  262. )
  263. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  264. flexmock(module.borgmatic.execute).should_receive('execute_command').once()
  265. module.delete_archives(
  266. repository={'path': 'repo'},
  267. config={},
  268. local_borg_version=flexmock(),
  269. delete_arguments=flexmock(match_archives='sh:foo*'),
  270. global_arguments=flexmock(),
  271. )
  272. @pytest.mark.parametrize('argument_name', module.ARCHIVE_RELATED_ARGUMENT_NAMES[2:])
  273. def test_delete_archives_with_archive_related_argument_calls_borg_delete(argument_name):
  274. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  275. flexmock(module.logging).ANSWER = LOGGING_ANSWER
  276. flexmock(module.borgmatic.borg.repo_delete).should_receive('delete_repository').never()
  277. flexmock(module).should_receive('make_delete_command').and_return(flexmock())
  278. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  279. flexmock()
  280. )
  281. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  282. flexmock(module.borgmatic.execute).should_receive('execute_command').once()
  283. module.delete_archives(
  284. repository={'path': 'repo'},
  285. config={},
  286. local_borg_version=flexmock(),
  287. delete_arguments=flexmock(archive='archive', **{argument_name: 'value'}),
  288. global_arguments=flexmock(),
  289. )
  290. def test_delete_archives_without_archive_related_argument_calls_borg_repo_delete():
  291. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  292. flexmock(module.logging).ANSWER = LOGGING_ANSWER
  293. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  294. flexmock(module.borgmatic.borg.repo_delete).should_receive('delete_repository').once()
  295. flexmock(module).should_receive('make_delete_command').never()
  296. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').never()
  297. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  298. flexmock(module.borgmatic.execute).should_receive('execute_command').never()
  299. module.delete_archives(
  300. repository={'path': 'repo'},
  301. config={},
  302. local_borg_version=flexmock(),
  303. delete_arguments=flexmock(
  304. list_details=True, force=False, cache_only=False, keep_security_info=False
  305. ),
  306. global_arguments=flexmock(),
  307. )
  308. def test_delete_archives_calls_borg_delete_with_working_directory():
  309. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  310. flexmock(module.logging).ANSWER = LOGGING_ANSWER
  311. flexmock(module.borgmatic.borg.repo_delete).should_receive('delete_repository').never()
  312. command = flexmock()
  313. flexmock(module).should_receive('make_delete_command').and_return(command)
  314. environment = flexmock()
  315. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  316. environment
  317. )
  318. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  319. '/working/dir'
  320. )
  321. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  322. command,
  323. output_log_level=logging.ANSWER,
  324. environment=environment,
  325. working_directory='/working/dir',
  326. borg_local_path='borg',
  327. borg_exit_codes=None,
  328. ).once()
  329. module.delete_archives(
  330. repository={'path': 'repo'},
  331. config={'working_directory': '/working/dir'},
  332. local_borg_version=flexmock(),
  333. delete_arguments=flexmock(archive='archive'),
  334. global_arguments=flexmock(),
  335. )