test_delete.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_log_json():
  83. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  84. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  85. 'log-json', True
  86. ).and_return(('--log-json',))
  87. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  88. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  89. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  90. ('repo',)
  91. )
  92. command = module.make_delete_command(
  93. repository={'path': 'repo'},
  94. config={},
  95. local_borg_version='1.2.3',
  96. delete_arguments=flexmock(list_archives=False, force=0, match_archives=None, archive=None),
  97. global_arguments=flexmock(dry_run=False, log_json=True),
  98. local_path='borg',
  99. remote_path=None,
  100. )
  101. assert command == ('borg', 'delete', '--log-json', 'repo')
  102. def test_make_delete_command_includes_lock_wait():
  103. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  104. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  105. 'lock-wait', 5
  106. ).and_return(('--lock-wait', '5'))
  107. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  108. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  109. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  110. ('repo',)
  111. )
  112. command = module.make_delete_command(
  113. repository={'path': 'repo'},
  114. config={'lock_wait': 5},
  115. local_borg_version='1.2.3',
  116. delete_arguments=flexmock(list_archives=False, force=0, match_archives=None, archive=None),
  117. global_arguments=flexmock(dry_run=False, log_json=False),
  118. local_path='borg',
  119. remote_path=None,
  120. )
  121. assert command == ('borg', 'delete', '--lock-wait', '5', 'repo')
  122. def test_make_delete_command_includes_list():
  123. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  124. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  125. 'list', True
  126. ).and_return(('--list',))
  127. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  128. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  129. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  130. ('repo',)
  131. )
  132. command = module.make_delete_command(
  133. repository={'path': 'repo'},
  134. config={},
  135. local_borg_version='1.2.3',
  136. delete_arguments=flexmock(list_archives=True, force=0, match_archives=None, archive=None),
  137. global_arguments=flexmock(dry_run=False, log_json=False),
  138. local_path='borg',
  139. remote_path=None,
  140. )
  141. assert command == ('borg', 'delete', '--list', 'repo')
  142. def test_make_delete_command_includes_force():
  143. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  144. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  145. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  146. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  147. ('repo',)
  148. )
  149. command = module.make_delete_command(
  150. repository={'path': 'repo'},
  151. config={},
  152. local_borg_version='1.2.3',
  153. delete_arguments=flexmock(list_archives=False, force=1, match_archives=None, archive=None),
  154. global_arguments=flexmock(dry_run=False, log_json=False),
  155. local_path='borg',
  156. remote_path=None,
  157. )
  158. assert command == ('borg', 'delete', '--force', 'repo')
  159. def test_make_delete_command_includes_force_twice():
  160. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  161. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(())
  162. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  163. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  164. ('repo',)
  165. )
  166. command = module.make_delete_command(
  167. repository={'path': 'repo'},
  168. config={},
  169. local_borg_version='1.2.3',
  170. delete_arguments=flexmock(list_archives=False, force=2, match_archives=None, archive=None),
  171. global_arguments=flexmock(dry_run=False, log_json=False),
  172. local_path='borg',
  173. remote_path=None,
  174. )
  175. assert command == ('borg', 'delete', '--force', '--force', 'repo')
  176. def test_make_delete_command_includes_archive():
  177. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  178. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(
  179. ('--match-archives', 'archive')
  180. )
  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(
  190. list_archives=False, force=0, match_archives=None, archive='archive'
  191. ),
  192. global_arguments=flexmock(dry_run=False, log_json=False),
  193. local_path='borg',
  194. remote_path=None,
  195. )
  196. assert command == ('borg', 'delete', '--match-archives', 'archive', 'repo')
  197. def test_make_delete_command_includes_match_archives():
  198. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  199. flexmock(module.borgmatic.borg.flags).should_receive('make_match_archives_flags').and_return(
  200. ('--match-archives', 'sh:foo*')
  201. )
  202. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  203. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  204. ('repo',)
  205. )
  206. command = module.make_delete_command(
  207. repository={'path': 'repo'},
  208. config={},
  209. local_borg_version='1.2.3',
  210. delete_arguments=flexmock(
  211. list_archives=False, force=0, match_archives='sh:foo*', archive='archive'
  212. ),
  213. global_arguments=flexmock(dry_run=False, log_json=False),
  214. local_path='borg',
  215. remote_path=None,
  216. )
  217. assert command == ('borg', 'delete', '--match-archives', 'sh:foo*', 'repo')
  218. def test_delete_archives_with_archive_calls_borg_delete():
  219. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  220. flexmock(module.borgmatic.borg.repo_delete).should_receive('delete_repository').never()
  221. flexmock(module).should_receive('make_delete_command').and_return(flexmock())
  222. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  223. flexmock()
  224. )
  225. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  226. flexmock(module.borgmatic.execute).should_receive('execute_command').once()
  227. module.delete_archives(
  228. repository={'path': 'repo'},
  229. config={},
  230. local_borg_version=flexmock(),
  231. delete_arguments=flexmock(archive='archive'),
  232. global_arguments=flexmock(),
  233. )
  234. def test_delete_archives_with_match_archives_calls_borg_delete():
  235. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  236. flexmock(module.borgmatic.borg.repo_delete).should_receive('delete_repository').never()
  237. flexmock(module).should_receive('make_delete_command').and_return(flexmock())
  238. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  239. flexmock()
  240. )
  241. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  242. flexmock(module.borgmatic.execute).should_receive('execute_command').once()
  243. module.delete_archives(
  244. repository={'path': 'repo'},
  245. config={},
  246. local_borg_version=flexmock(),
  247. delete_arguments=flexmock(match_archives='sh:foo*'),
  248. global_arguments=flexmock(),
  249. )
  250. @pytest.mark.parametrize('argument_name', module.ARCHIVE_RELATED_ARGUMENT_NAMES[2:])
  251. def test_delete_archives_with_archive_related_argument_calls_borg_delete(argument_name):
  252. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  253. flexmock(module.borgmatic.borg.repo_delete).should_receive('delete_repository').never()
  254. flexmock(module).should_receive('make_delete_command').and_return(flexmock())
  255. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  256. flexmock()
  257. )
  258. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  259. flexmock(module.borgmatic.execute).should_receive('execute_command').once()
  260. module.delete_archives(
  261. repository={'path': 'repo'},
  262. config={},
  263. local_borg_version=flexmock(),
  264. delete_arguments=flexmock(archive='archive', **{argument_name: 'value'}),
  265. global_arguments=flexmock(),
  266. )
  267. def test_delete_archives_without_archive_related_argument_calls_borg_repo_delete():
  268. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  269. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  270. flexmock(module.borgmatic.borg.repo_delete).should_receive('delete_repository').once()
  271. flexmock(module).should_receive('make_delete_command').never()
  272. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').never()
  273. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  274. flexmock(module.borgmatic.execute).should_receive('execute_command').never()
  275. module.delete_archives(
  276. repository={'path': 'repo'},
  277. config={},
  278. local_borg_version=flexmock(),
  279. delete_arguments=flexmock(
  280. list_archives=True, force=False, cache_only=False, keep_security_info=False
  281. ),
  282. global_arguments=flexmock(),
  283. )
  284. def test_delete_archives_calls_borg_delete_with_working_directory():
  285. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  286. flexmock(module.borgmatic.borg.repo_delete).should_receive('delete_repository').never()
  287. command = flexmock()
  288. flexmock(module).should_receive('make_delete_command').and_return(command)
  289. extra_environment = flexmock()
  290. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  291. extra_environment
  292. )
  293. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  294. '/working/dir'
  295. )
  296. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  297. command,
  298. output_log_level=logging.ANSWER,
  299. extra_environment=extra_environment,
  300. working_directory='/working/dir',
  301. borg_local_path='borg',
  302. borg_exit_codes=None,
  303. ).once()
  304. module.delete_archives(
  305. repository={'path': 'repo'},
  306. config={'working_directory': '/working/dir'},
  307. local_borg_version=flexmock(),
  308. delete_arguments=flexmock(archive='archive'),
  309. global_arguments=flexmock(),
  310. )