test_rdelete.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import rdelete as module
  4. from ..test_verbosity import insert_logging_mock
  5. def test_make_rdelete_command_with_feature_available_runs_borg_rdelete():
  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_rdelete_command(
  13. repository={'path': 'repo'},
  14. config={},
  15. local_borg_version='1.2.3',
  16. rdelete_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', 'rdelete', 'repo')
  22. def test_make_rdelete_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_rdelete_command(
  30. repository={'path': 'repo'},
  31. config={},
  32. local_borg_version='1.2.3',
  33. rdelete_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_rdelete_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_rdelete_command(
  48. repository={'path': 'repo'},
  49. config={},
  50. local_borg_version='1.2.3',
  51. rdelete_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', 'rdelete', '--info', 'repo')
  57. def test_make_rdelete_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_rdelete_command(
  66. repository={'path': 'repo'},
  67. config={},
  68. local_borg_version='1.2.3',
  69. rdelete_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', 'rdelete', '--debug', '--show-rc', 'repo')
  75. def test_make_rdelete_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_rdelete_command(
  86. repository={'path': 'repo'},
  87. config={},
  88. local_borg_version='1.2.3',
  89. rdelete_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', 'rdelete', '--dry-run', 'repo')
  95. def test_make_rdelete_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_rdelete_command(
  106. repository={'path': 'repo'},
  107. config={},
  108. local_borg_version='1.2.3',
  109. rdelete_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', 'rdelete', '--remote-path', 'borg1', 'repo')
  115. def test_make_rdelete_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_rdelete_command(
  126. repository={'path': 'repo'},
  127. config={},
  128. local_borg_version='1.2.3',
  129. rdelete_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', 'rdelete', '--log-json', 'repo')
  135. def test_make_rdelete_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_rdelete_command(
  146. repository={'path': 'repo'},
  147. config={'lock_wait': 5},
  148. local_borg_version='1.2.3',
  149. rdelete_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', 'rdelete', '--lock-wait', '5', 'repo')
  155. def test_make_rdelete_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_rdelete_command(
  166. repository={'path': 'repo'},
  167. config={},
  168. local_borg_version='1.2.3',
  169. rdelete_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', 'rdelete', '--list', 'repo')
  175. def test_make_rdelete_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_rdelete_command(
  183. repository={'path': 'repo'},
  184. config={},
  185. local_borg_version='1.2.3',
  186. rdelete_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', 'rdelete', '--force', 'repo')
  192. def test_make_rdelete_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_rdelete_command(
  200. repository={'path': 'repo'},
  201. config={},
  202. local_borg_version='1.2.3',
  203. rdelete_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', 'rdelete', '--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_rdelete_command').and_return(command)
  213. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  214. flexmock()
  215. )
  216. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  217. command,
  218. output_log_level=module.logging.ANSWER,
  219. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  220. extra_environment=object,
  221. borg_local_path='borg',
  222. borg_exit_codes=None,
  223. ).once()
  224. module.delete_repository(
  225. repository={'path': 'repo'},
  226. config={},
  227. local_borg_version=flexmock(),
  228. rdelete_arguments=flexmock(force=False, cache_only=False),
  229. global_arguments=flexmock(),
  230. local_path='borg',
  231. remote_path=None,
  232. )
  233. def test_delete_repository_with_force_captures_output():
  234. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  235. command = flexmock()
  236. flexmock(module).should_receive('make_rdelete_command').and_return(command)
  237. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  238. flexmock()
  239. )
  240. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  241. command,
  242. output_log_level=module.logging.ANSWER,
  243. output_file=None,
  244. extra_environment=object,
  245. borg_local_path='borg',
  246. borg_exit_codes=None,
  247. ).once()
  248. module.delete_repository(
  249. repository={'path': 'repo'},
  250. config={},
  251. local_borg_version=flexmock(),
  252. rdelete_arguments=flexmock(force=True, cache_only=False),
  253. global_arguments=flexmock(),
  254. local_path='borg',
  255. remote_path=None,
  256. )
  257. def test_delete_repository_with_cache_only_captures_output():
  258. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  259. command = flexmock()
  260. flexmock(module).should_receive('make_rdelete_command').and_return(command)
  261. flexmock(module.borgmatic.borg.environment).should_receive('make_environment').and_return(
  262. flexmock()
  263. )
  264. flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
  265. command,
  266. output_log_level=module.logging.ANSWER,
  267. output_file=None,
  268. extra_environment=object,
  269. borg_local_path='borg',
  270. borg_exit_codes=None,
  271. ).once()
  272. module.delete_repository(
  273. repository={'path': 'repo'},
  274. config={},
  275. local_borg_version=flexmock(),
  276. rdelete_arguments=flexmock(force=False, cache_only=True),
  277. global_arguments=flexmock(),
  278. local_path='borg',
  279. remote_path=None,
  280. )