test_borg.py 12 KB

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