2
0

test_borg.py 12 KB

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