test_repo_delete.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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', True
  80. ).and_return(('--dry-run',))
  81. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  82. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  83. ('repo',)
  84. )
  85. command = module.make_repo_delete_command(
  86. repository={'path': 'repo'},
  87. config={},
  88. local_borg_version='1.2.3',
  89. repo_delete_arguments=flexmock(list_details=False, force=0),
  90. global_arguments=flexmock(dry_run=True),
  91. local_path='borg',
  92. remote_path=None,
  93. )
  94. assert command == ('borg', 'repo-delete', '--dry-run', 'repo')
  95. def test_make_repo_delete_command_includes_remote_path():
  96. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  97. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  98. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  99. 'remote-path', 'borg1'
  100. ).and_return(('--remote-path', 'borg1'))
  101. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  102. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  103. ('repo',)
  104. )
  105. command = module.make_repo_delete_command(
  106. repository={'path': 'repo'},
  107. config={},
  108. local_borg_version='1.2.3',
  109. repo_delete_arguments=flexmock(list_details=False, force=0),
  110. global_arguments=flexmock(dry_run=False),
  111. local_path='borg',
  112. remote_path='borg1',
  113. )
  114. assert command == ('borg', 'repo-delete', '--remote-path', 'borg1', 'repo')
  115. def test_make_repo_delete_command_includes_umask():
  116. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  117. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').replace_with(
  118. lambda name, value: (f'--{name}', value) if value else ()
  119. )
  120. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  121. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  122. ('repo',)
  123. )
  124. command = module.make_repo_delete_command(
  125. repository={'path': 'repo'},
  126. config={'umask': '077'},
  127. local_borg_version='1.2.3',
  128. repo_delete_arguments=flexmock(list_details=False, force=0),
  129. global_arguments=flexmock(dry_run=False),
  130. local_path='borg',
  131. remote_path=None,
  132. )
  133. assert command == ('borg', 'repo-delete', '--umask', '077', 'repo')
  134. def test_make_repo_delete_command_includes_log_json():
  135. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  136. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  137. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  138. 'log-json', True
  139. ).and_return(('--log-json',))
  140. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  141. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  142. ('repo',)
  143. )
  144. command = module.make_repo_delete_command(
  145. repository={'path': 'repo'},
  146. config={'log_json': True},
  147. local_borg_version='1.2.3',
  148. repo_delete_arguments=flexmock(list_details=False, force=0),
  149. global_arguments=flexmock(dry_run=False),
  150. local_path='borg',
  151. remote_path=None,
  152. )
  153. assert command == ('borg', 'repo-delete', '--log-json', 'repo')
  154. def test_make_repo_delete_command_includes_lock_wait():
  155. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  156. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  157. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  158. 'lock-wait', 5
  159. ).and_return(('--lock-wait', '5'))
  160. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  161. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  162. ('repo',)
  163. )
  164. command = module.make_repo_delete_command(
  165. repository={'path': 'repo'},
  166. config={'lock_wait': 5},
  167. local_borg_version='1.2.3',
  168. repo_delete_arguments=flexmock(list_details=False, force=0),
  169. global_arguments=flexmock(dry_run=False),
  170. local_path='borg',
  171. remote_path=None,
  172. )
  173. assert command == ('borg', 'repo-delete', '--lock-wait', '5', 'repo')
  174. def test_make_repo_delete_command_includes_list():
  175. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  176. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  177. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  178. 'list', True
  179. ).and_return(('--list',))
  180. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  181. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  182. ('repo',)
  183. )
  184. command = module.make_repo_delete_command(
  185. repository={'path': 'repo'},
  186. config={'list_details': True},
  187. local_borg_version='1.2.3',
  188. repo_delete_arguments=flexmock(list_details=True, force=0),
  189. global_arguments=flexmock(dry_run=False),
  190. local_path='borg',
  191. remote_path=None,
  192. )
  193. assert command == ('borg', 'repo-delete', '--list', 'repo')
  194. def test_make_repo_delete_command_includes_force():
  195. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  196. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  197. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  198. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  199. ('repo',)
  200. )
  201. command = module.make_repo_delete_command(
  202. repository={'path': 'repo'},
  203. config={},
  204. local_borg_version='1.2.3',
  205. repo_delete_arguments=flexmock(list_details=False, force=1),
  206. global_arguments=flexmock(dry_run=False),
  207. local_path='borg',
  208. remote_path=None,
  209. )
  210. assert command == ('borg', 'repo-delete', '--force', 'repo')
  211. def test_make_repo_delete_command_includes_force_twice():
  212. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  213. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  214. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  215. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  216. ('repo',)
  217. )
  218. command = module.make_repo_delete_command(
  219. repository={'path': 'repo'},
  220. config={},
  221. local_borg_version='1.2.3',
  222. repo_delete_arguments=flexmock(list_details=False, force=2),
  223. global_arguments=flexmock(dry_run=False),
  224. local_path='borg',
  225. remote_path=None,
  226. )
  227. assert command == ('borg', 'repo-delete', '--force', '--force', 'repo')
  228. def test_delete_repository_with_defaults_does_not_capture_output():
  229. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  230. command = flexmock()
  231. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  232. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  233. flexmock()
  234. )
  235. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  236. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  237. command,
  238. output_log_level=module.logging.ANSWER,
  239. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  240. environment=object,
  241. working_directory=None,
  242. borg_local_path='borg',
  243. borg_exit_codes=None,
  244. ).once()
  245. module.delete_repository(
  246. repository={'path': 'repo'},
  247. config={},
  248. local_borg_version=flexmock(),
  249. repo_delete_arguments=flexmock(force=False, cache_only=False),
  250. global_arguments=flexmock(),
  251. local_path='borg',
  252. remote_path=None,
  253. )
  254. def test_delete_repository_with_force_captures_output():
  255. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  256. command = flexmock()
  257. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  258. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  259. flexmock()
  260. )
  261. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  262. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  263. command,
  264. output_log_level=module.logging.ANSWER,
  265. output_file=None,
  266. environment=object,
  267. working_directory=None,
  268. borg_local_path='borg',
  269. borg_exit_codes=None,
  270. ).once()
  271. module.delete_repository(
  272. repository={'path': 'repo'},
  273. config={},
  274. local_borg_version=flexmock(),
  275. repo_delete_arguments=flexmock(force=True, cache_only=False),
  276. global_arguments=flexmock(),
  277. local_path='borg',
  278. remote_path=None,
  279. )
  280. def test_delete_repository_with_cache_only_captures_output():
  281. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  282. command = flexmock()
  283. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  284. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  285. flexmock()
  286. )
  287. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  288. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  289. command,
  290. output_log_level=module.logging.ANSWER,
  291. output_file=None,
  292. environment=object,
  293. working_directory=None,
  294. borg_local_path='borg',
  295. borg_exit_codes=None,
  296. ).once()
  297. module.delete_repository(
  298. repository={'path': 'repo'},
  299. config={},
  300. local_borg_version=flexmock(),
  301. repo_delete_arguments=flexmock(force=False, cache_only=True),
  302. global_arguments=flexmock(),
  303. local_path='borg',
  304. remote_path=None,
  305. )
  306. def test_delete_repository_calls_borg_with_working_directory():
  307. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  308. command = flexmock()
  309. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  310. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  311. flexmock()
  312. )
  313. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  314. '/working/dir',
  315. )
  316. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  317. command,
  318. output_log_level=module.logging.ANSWER,
  319. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  320. environment=object,
  321. working_directory='/working/dir',
  322. borg_local_path='borg',
  323. borg_exit_codes=None,
  324. ).once()
  325. module.delete_repository(
  326. repository={'path': 'repo'},
  327. config={'working_directory': '/working/dir'},
  328. local_borg_version=flexmock(),
  329. repo_delete_arguments=flexmock(force=False, cache_only=False),
  330. global_arguments=flexmock(),
  331. local_path='borg',
  332. remote_path=None,
  333. )