test_repo_delete.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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_archives=False, force=0),
  17. global_arguments=flexmock(dry_run=False, log_json=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_archives=False, force=0),
  34. global_arguments=flexmock(dry_run=False, log_json=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_archives=False, force=0),
  52. global_arguments=flexmock(dry_run=False, log_json=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_archives=False, force=0),
  70. global_arguments=flexmock(dry_run=False, log_json=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_archives=False, force=0),
  90. global_arguments=flexmock(dry_run=True, log_json=False),
  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_archives=False, force=0),
  110. global_arguments=flexmock(dry_run=False, log_json=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_log_json():
  116. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  117. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  118. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  119. 'log-json', True
  120. ).and_return(('--log-json',))
  121. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  122. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  123. ('repo',)
  124. )
  125. command = module.make_repo_delete_command(
  126. repository={'path': 'repo'},
  127. config={},
  128. local_borg_version='1.2.3',
  129. repo_delete_arguments=flexmock(list_archives=False, force=0),
  130. global_arguments=flexmock(dry_run=False, log_json=True),
  131. local_path='borg',
  132. remote_path=None,
  133. )
  134. assert command == ('borg', 'repo-delete', '--log-json', 'repo')
  135. def test_make_repo_delete_command_includes_lock_wait():
  136. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  137. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  138. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  139. 'lock-wait', 5
  140. ).and_return(('--lock-wait', '5'))
  141. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  142. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  143. ('repo',)
  144. )
  145. command = module.make_repo_delete_command(
  146. repository={'path': 'repo'},
  147. config={'lock_wait': 5},
  148. local_borg_version='1.2.3',
  149. repo_delete_arguments=flexmock(list_archives=False, force=0),
  150. global_arguments=flexmock(dry_run=False, log_json=False),
  151. local_path='borg',
  152. remote_path=None,
  153. )
  154. assert command == ('borg', 'repo-delete', '--lock-wait', '5', 'repo')
  155. def test_make_repo_delete_command_includes_list():
  156. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  157. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  158. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').with_args(
  159. 'list', True
  160. ).and_return(('--list',))
  161. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  162. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  163. ('repo',)
  164. )
  165. command = module.make_repo_delete_command(
  166. repository={'path': 'repo'},
  167. config={},
  168. local_borg_version='1.2.3',
  169. repo_delete_arguments=flexmock(list_archives=True, force=0),
  170. global_arguments=flexmock(dry_run=False, log_json=False),
  171. local_path='borg',
  172. remote_path=None,
  173. )
  174. assert command == ('borg', 'repo-delete', '--list', 'repo')
  175. def test_make_repo_delete_command_includes_force():
  176. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  177. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  178. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  179. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  180. ('repo',)
  181. )
  182. command = module.make_repo_delete_command(
  183. repository={'path': 'repo'},
  184. config={},
  185. local_borg_version='1.2.3',
  186. repo_delete_arguments=flexmock(list_archives=False, force=1),
  187. global_arguments=flexmock(dry_run=False, log_json=False),
  188. local_path='borg',
  189. remote_path=None,
  190. )
  191. assert command == ('borg', 'repo-delete', '--force', 'repo')
  192. def test_make_repo_delete_command_includes_force_twice():
  193. flexmock(module.borgmatic.borg.feature).should_receive('available').and_return(True)
  194. flexmock(module.borgmatic.borg.flags).should_receive('make_flags').and_return(())
  195. flexmock(module.borgmatic.borg.flags).should_receive('make_flags_from_arguments').and_return(())
  196. flexmock(module.borgmatic.borg.flags).should_receive('make_repository_flags').and_return(
  197. ('repo',)
  198. )
  199. command = module.make_repo_delete_command(
  200. repository={'path': 'repo'},
  201. config={},
  202. local_borg_version='1.2.3',
  203. repo_delete_arguments=flexmock(list_archives=False, force=2),
  204. global_arguments=flexmock(dry_run=False, log_json=False),
  205. local_path='borg',
  206. remote_path=None,
  207. )
  208. assert command == ('borg', 'repo-delete', '--force', '--force', 'repo')
  209. def test_delete_repository_with_defaults_does_not_capture_output():
  210. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  211. command = flexmock()
  212. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  213. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  214. flexmock()
  215. )
  216. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  217. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  218. command,
  219. output_log_level=module.logging.ANSWER,
  220. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  221. extra_environment=object,
  222. working_directory=None,
  223. borg_local_path='borg',
  224. borg_exit_codes=None,
  225. ).once()
  226. module.delete_repository(
  227. repository={'path': 'repo'},
  228. config={},
  229. local_borg_version=flexmock(),
  230. repo_delete_arguments=flexmock(force=False, cache_only=False),
  231. global_arguments=flexmock(),
  232. local_path='borg',
  233. remote_path=None,
  234. )
  235. def test_delete_repository_with_force_captures_output():
  236. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  237. command = flexmock()
  238. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  239. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  240. flexmock()
  241. )
  242. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  243. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  244. command,
  245. output_log_level=module.logging.ANSWER,
  246. output_file=None,
  247. extra_environment=object,
  248. working_directory=None,
  249. borg_local_path='borg',
  250. borg_exit_codes=None,
  251. ).once()
  252. module.delete_repository(
  253. repository={'path': 'repo'},
  254. config={},
  255. local_borg_version=flexmock(),
  256. repo_delete_arguments=flexmock(force=True, cache_only=False),
  257. global_arguments=flexmock(),
  258. local_path='borg',
  259. remote_path=None,
  260. )
  261. def test_delete_repository_with_cache_only_captures_output():
  262. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  263. command = flexmock()
  264. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  265. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  266. flexmock()
  267. )
  268. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None)
  269. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  270. command,
  271. output_log_level=module.logging.ANSWER,
  272. output_file=None,
  273. extra_environment=object,
  274. working_directory=None,
  275. borg_local_path='borg',
  276. borg_exit_codes=None,
  277. ).once()
  278. module.delete_repository(
  279. repository={'path': 'repo'},
  280. config={},
  281. local_borg_version=flexmock(),
  282. repo_delete_arguments=flexmock(force=False, cache_only=True),
  283. global_arguments=flexmock(),
  284. local_path='borg',
  285. remote_path=None,
  286. )
  287. def test_delete_repository_calls_borg_with_working_directory():
  288. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  289. command = flexmock()
  290. flexmock(module).should_receive('make_repo_delete_command').and_return(command)
  291. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  292. flexmock()
  293. )
  294. flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(
  295. '/working/dir',
  296. )
  297. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  298. command,
  299. output_log_level=module.logging.ANSWER,
  300. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  301. extra_environment=object,
  302. working_directory='/working/dir',
  303. borg_local_path='borg',
  304. borg_exit_codes=None,
  305. ).once()
  306. module.delete_repository(
  307. repository={'path': 'repo'},
  308. config={'working_directory': '/working/dir'},
  309. local_borg_version=flexmock(),
  310. repo_delete_arguments=flexmock(force=False, cache_only=False),
  311. global_arguments=flexmock(),
  312. local_path='borg',
  313. remote_path=None,
  314. )