test_repo_delete.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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_includes_list():
  179. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  180. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  181. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  182. 'list',
  183. True,
  184. ).and_return(('--list',))
  185. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  186. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  187. ('repo',),
  188. )
  189. command = module.make_repo_delete_command(
  190. repository={'path': 'repo'},
  191. config={'list_details': True},
  192. local_borg_version='1.2.3',
  193. repo_delete_arguments=flexmock(list_details=True, force=0),
  194. global_arguments=flexmock(dry_run=False),
  195. local_path='borg',
  196. remote_path=None,
  197. )
  198. assert command == ('borg', 'repo-delete', '--list', 'repo')
  199. def test_make_repo_delete_command_includes_force():
  200. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  201. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  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_repo_delete_command(
  207. repository={'path': 'repo'},
  208. config={},
  209. local_borg_version='1.2.3',
  210. repo_delete_arguments=flexmock(list_details=False, force=1),
  211. global_arguments=flexmock(dry_run=False),
  212. local_path='borg',
  213. remote_path=None,
  214. )
  215. assert command == ('borg', 'repo-delete', '--force', 'repo')
  216. def test_make_repo_delete_command_includes_force_twice():
  217. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  218. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  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={},
  226. local_borg_version='1.2.3',
  227. repo_delete_arguments=flexmock(list_details=False, force=2),
  228. global_arguments=flexmock(dry_run=False),
  229. local_path='borg',
  230. remote_path=None,
  231. )
  232. assert command == ('borg', 'repo-delete', '--force', '--force', 'repo')
  233. def test_delete_repository_with_defaults_does_not_capture_output():
  234. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  235. command = flexmock()
  236. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  237. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  238. flexmock(),
  239. )
  240. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  241. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  242. command,
  243. output_log_level=module.logging.ANSWER,
  244. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  245. environment=object,
  246. working_directory=None,
  247. borg_local_path='borg',
  248. borg_exit_codes=None,
  249. ).once()
  250. module.delete_repository(
  251. repository={'path': 'repo'},
  252. config={},
  253. local_borg_version=flexmock(),
  254. repo_delete_arguments=flexmock(force=False, cache_only=False),
  255. global_arguments=flexmock(),
  256. local_path='borg',
  257. remote_path=None,
  258. )
  259. def test_delete_repository_with_force_captures_output():
  260. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  261. command = flexmock()
  262. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  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').with_args(
  268. command,
  269. output_log_level=module.logging.ANSWER,
  270. output_file=None,
  271. environment=object,
  272. working_directory=None,
  273. borg_local_path='borg',
  274. borg_exit_codes=None,
  275. ).once()
  276. module.delete_repository(
  277. repository={'path': 'repo'},
  278. config={},
  279. local_borg_version=flexmock(),
  280. repo_delete_arguments=flexmock(force=True, cache_only=False),
  281. global_arguments=flexmock(),
  282. local_path='borg',
  283. remote_path=None,
  284. )
  285. def test_delete_repository_with_cache_only_captures_output():
  286. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  287. command = flexmock()
  288. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  289. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  290. flexmock(),
  291. )
  292. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  293. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  294. command,
  295. output_log_level=module.logging.ANSWER,
  296. output_file=None,
  297. environment=object,
  298. working_directory=None,
  299. borg_local_path='borg',
  300. borg_exit_codes=None,
  301. ).once()
  302. module.delete_repository(
  303. repository={'path': 'repo'},
  304. config={},
  305. local_borg_version=flexmock(),
  306. repo_delete_arguments=flexmock(force=False, cache_only=True),
  307. global_arguments=flexmock(),
  308. local_path='borg',
  309. remote_path=None,
  310. )
  311. def test_delete_repository_calls_borg_with_working_directory():
  312. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  313. command = flexmock()
  314. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  315. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  316. flexmock(),
  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=module.logging.ANSWER,
  324. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  325. environment=object,
  326. working_directory='/working/dir',
  327. borg_local_path='borg',
  328. borg_exit_codes=None,
  329. ).once()
  330. module.delete_repository(
  331. repository={'path': 'repo'},
  332. config={'working_directory': '/working/dir'},
  333. local_borg_version=flexmock(),
  334. repo_delete_arguments=flexmock(force=False, cache_only=False),
  335. global_arguments=flexmock(),
  336. local_path='borg',
  337. remote_path=None,
  338. )