test_borg.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import logging
  2. from flexmock import flexmock
  3. from borgmatic.borg import borg as module
  4. from ..test_verbosity import insert_logging_mock
  5. def test_run_arbitrary_borg_calls_borg_with_parameters():
  6. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  7. flexmock(module.flags).should_receive('make_flags').and_return(())
  8. flexmock(module.environment).should_receive('make_environment')
  9. flexmock(module).should_receive('execute_command').with_args(
  10. ('borg', 'break-lock', 'repo'),
  11. output_log_level=logging.WARNING,
  12. borg_local_path='borg',
  13. extra_environment=None,
  14. )
  15. module.run_arbitrary_borg(
  16. repository='repo', storage_config={}, local_borg_version='1.2.3', options=['break-lock'],
  17. )
  18. def test_run_arbitrary_borg_with_log_info_calls_borg_with_info_parameter():
  19. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  20. flexmock(module.flags).should_receive('make_flags').and_return(())
  21. flexmock(module.environment).should_receive('make_environment')
  22. flexmock(module).should_receive('execute_command').with_args(
  23. ('borg', 'break-lock', 'repo', '--info'),
  24. output_log_level=logging.WARNING,
  25. borg_local_path='borg',
  26. extra_environment=None,
  27. )
  28. insert_logging_mock(logging.INFO)
  29. module.run_arbitrary_borg(
  30. repository='repo', storage_config={}, local_borg_version='1.2.3', options=['break-lock'],
  31. )
  32. def test_run_arbitrary_borg_with_log_debug_calls_borg_with_debug_parameter():
  33. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  34. flexmock(module.flags).should_receive('make_flags').and_return(())
  35. flexmock(module.environment).should_receive('make_environment')
  36. flexmock(module).should_receive('execute_command').with_args(
  37. ('borg', 'break-lock', 'repo', '--debug', '--show-rc'),
  38. output_log_level=logging.WARNING,
  39. borg_local_path='borg',
  40. extra_environment=None,
  41. )
  42. insert_logging_mock(logging.DEBUG)
  43. module.run_arbitrary_borg(
  44. repository='repo', storage_config={}, local_borg_version='1.2.3', options=['break-lock'],
  45. )
  46. def test_run_arbitrary_borg_with_lock_wait_calls_borg_with_lock_wait_parameters():
  47. storage_config = {'lock_wait': 5}
  48. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  49. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(
  50. ('--lock-wait', '5')
  51. )
  52. flexmock(module.environment).should_receive('make_environment')
  53. flexmock(module).should_receive('execute_command').with_args(
  54. ('borg', 'break-lock', 'repo', '--lock-wait', '5'),
  55. output_log_level=logging.WARNING,
  56. borg_local_path='borg',
  57. extra_environment=None,
  58. )
  59. module.run_arbitrary_borg(
  60. repository='repo',
  61. storage_config=storage_config,
  62. local_borg_version='1.2.3',
  63. options=['break-lock'],
  64. )
  65. def test_run_arbitrary_borg_with_archive_calls_borg_with_archive_parameter():
  66. flexmock(module.flags).should_receive('make_repository_archive_flags').and_return(
  67. ('repo::archive',)
  68. )
  69. flexmock(module.flags).should_receive('make_flags').and_return(())
  70. flexmock(module.environment).should_receive('make_environment')
  71. flexmock(module).should_receive('execute_command').with_args(
  72. ('borg', 'break-lock', 'repo::archive'),
  73. output_log_level=logging.WARNING,
  74. borg_local_path='borg',
  75. extra_environment=None,
  76. )
  77. module.run_arbitrary_borg(
  78. repository='repo',
  79. storage_config={},
  80. local_borg_version='1.2.3',
  81. options=['break-lock'],
  82. archive='archive',
  83. )
  84. def test_run_arbitrary_borg_with_local_path_calls_borg_via_local_path():
  85. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  86. flexmock(module.flags).should_receive('make_flags').and_return(())
  87. flexmock(module.environment).should_receive('make_environment')
  88. flexmock(module).should_receive('execute_command').with_args(
  89. ('borg1', 'break-lock', 'repo'),
  90. output_log_level=logging.WARNING,
  91. borg_local_path='borg1',
  92. extra_environment=None,
  93. )
  94. module.run_arbitrary_borg(
  95. repository='repo',
  96. storage_config={},
  97. local_borg_version='1.2.3',
  98. options=['break-lock'],
  99. local_path='borg1',
  100. )
  101. def test_run_arbitrary_borg_with_remote_path_calls_borg_with_remote_path_parameters():
  102. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  103. flexmock(module.flags).should_receive('make_flags').and_return(
  104. ('--remote-path', 'borg1')
  105. ).and_return(())
  106. flexmock(module.environment).should_receive('make_environment')
  107. flexmock(module).should_receive('execute_command').with_args(
  108. ('borg', 'break-lock', 'repo', '--remote-path', 'borg1'),
  109. output_log_level=logging.WARNING,
  110. borg_local_path='borg',
  111. extra_environment=None,
  112. )
  113. module.run_arbitrary_borg(
  114. repository='repo',
  115. storage_config={},
  116. local_borg_version='1.2.3',
  117. options=['break-lock'],
  118. remote_path='borg1',
  119. )
  120. def test_run_arbitrary_borg_passes_borg_specific_parameters_to_borg():
  121. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  122. flexmock(module.flags).should_receive('make_flags').and_return(())
  123. flexmock(module.environment).should_receive('make_environment')
  124. flexmock(module).should_receive('execute_command').with_args(
  125. ('borg', 'list', 'repo', '--progress'),
  126. output_log_level=logging.WARNING,
  127. borg_local_path='borg',
  128. extra_environment=None,
  129. )
  130. module.run_arbitrary_borg(
  131. repository='repo',
  132. storage_config={},
  133. local_borg_version='1.2.3',
  134. options=['list', '--progress'],
  135. )
  136. def test_run_arbitrary_borg_omits_dash_dash_in_parameters_passed_to_borg():
  137. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  138. flexmock(module.flags).should_receive('make_flags').and_return(())
  139. flexmock(module.environment).should_receive('make_environment')
  140. flexmock(module).should_receive('execute_command').with_args(
  141. ('borg', 'break-lock', 'repo'),
  142. output_log_level=logging.WARNING,
  143. borg_local_path='borg',
  144. extra_environment=None,
  145. )
  146. module.run_arbitrary_borg(
  147. repository='repo',
  148. storage_config={},
  149. local_borg_version='1.2.3',
  150. options=['--', 'break-lock'],
  151. )
  152. def test_run_arbitrary_borg_without_borg_specific_parameters_does_not_raise():
  153. flexmock(module.flags).should_receive('make_repository_flags').never()
  154. flexmock(module.flags).should_receive('make_flags').and_return(())
  155. flexmock(module.environment).should_receive('make_environment')
  156. flexmock(module).should_receive('execute_command').with_args(
  157. ('borg',), output_log_level=logging.WARNING, borg_local_path='borg', extra_environment=None,
  158. )
  159. module.run_arbitrary_borg(
  160. repository='repo', storage_config={}, local_borg_version='1.2.3', options=[],
  161. )
  162. def test_run_arbitrary_borg_passes_key_sub_command_to_borg_before_repository():
  163. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  164. flexmock(module.flags).should_receive('make_flags').and_return(())
  165. flexmock(module.environment).should_receive('make_environment')
  166. flexmock(module).should_receive('execute_command').with_args(
  167. ('borg', 'key', 'export', 'repo'),
  168. output_log_level=logging.WARNING,
  169. borg_local_path='borg',
  170. extra_environment=None,
  171. )
  172. module.run_arbitrary_borg(
  173. repository='repo', storage_config={}, local_borg_version='1.2.3', options=['key', 'export'],
  174. )
  175. def test_run_arbitrary_borg_passes_debug_sub_command_to_borg_before_repository():
  176. flexmock(module.flags).should_receive('make_repository_flags').and_return(('repo',))
  177. flexmock(module.flags).should_receive('make_flags').and_return(())
  178. flexmock(module.environment).should_receive('make_environment')
  179. flexmock(module).should_receive('execute_command').with_args(
  180. ('borg', 'debug', 'dump-manifest', 'repo', 'path'),
  181. output_log_level=logging.WARNING,
  182. borg_local_path='borg',
  183. extra_environment=None,
  184. )
  185. module.run_arbitrary_borg(
  186. repository='repo',
  187. storage_config={},
  188. local_borg_version='1.2.3',
  189. options=['debug', 'dump-manifest', 'path'],
  190. )
  191. def test_run_arbitrary_borg_with_debug_info_command_does_not_pass_borg_repository():
  192. flexmock(module.flags).should_receive('make_repository_flags').never()
  193. flexmock(module.flags).should_receive('make_flags').and_return(())
  194. flexmock(module.environment).should_receive('make_environment')
  195. flexmock(module).should_receive('execute_command').with_args(
  196. ('borg', 'debug', 'info'),
  197. output_log_level=logging.WARNING,
  198. borg_local_path='borg',
  199. extra_environment=None,
  200. )
  201. module.run_arbitrary_borg(
  202. repository='repo', storage_config={}, local_borg_version='1.2.3', options=['debug', 'info'],
  203. )
  204. def test_run_arbitrary_borg_with_debug_convert_profile_command_does_not_pass_borg_repository():
  205. flexmock(module.flags).should_receive('make_repository_flags').never()
  206. flexmock(module.flags).should_receive('make_flags').and_return(())
  207. flexmock(module.environment).should_receive('make_environment')
  208. flexmock(module).should_receive('execute_command').with_args(
  209. ('borg', 'debug', 'convert-profile', 'in', 'out'),
  210. output_log_level=logging.WARNING,
  211. borg_local_path='borg',
  212. extra_environment=None,
  213. )
  214. module.run_arbitrary_borg(
  215. repository='repo',
  216. storage_config={},
  217. local_borg_version='1.2.3',
  218. options=['debug', 'convert-profile', 'in', 'out'],
  219. )