test_borg.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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_flags').and_return(())
  9. flexmock(module.environment).should_receive('make_environment')
  10. flexmock(module).should_receive('execute_command').with_args(
  11. ('borg', 'break-lock', '::'),
  12. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  13. borg_local_path='borg',
  14. shell=True,
  15. extra_environment={'BORG_REPO': 'repo', 'ARCHIVE': ''},
  16. )
  17. module.run_arbitrary_borg(
  18. repository_path='repo',
  19. 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_flags').and_return(())
  27. flexmock(module.environment).should_receive('make_environment')
  28. flexmock(module).should_receive('execute_command').with_args(
  29. ('borg', 'break-lock', '--info', '::'),
  30. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  31. borg_local_path='borg',
  32. shell=True,
  33. extra_environment={'BORG_REPO': 'repo', 'ARCHIVE': ''},
  34. )
  35. insert_logging_mock(logging.INFO)
  36. module.run_arbitrary_borg(
  37. repository_path='repo',
  38. 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_flags').and_return(())
  46. flexmock(module.environment).should_receive('make_environment')
  47. flexmock(module).should_receive('execute_command').with_args(
  48. ('borg', 'break-lock', '--debug', '--show-rc', '::'),
  49. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  50. borg_local_path='borg',
  51. shell=True,
  52. extra_environment={'BORG_REPO': 'repo', 'ARCHIVE': ''},
  53. )
  54. insert_logging_mock(logging.DEBUG)
  55. module.run_arbitrary_borg(
  56. repository_path='repo',
  57. 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. config = {'lock_wait': 5}
  65. flexmock(module.flags).should_receive('make_flags').and_return(()).and_return(
  66. ('--lock-wait', '5')
  67. )
  68. flexmock(module.environment).should_receive('make_environment')
  69. flexmock(module).should_receive('execute_command').with_args(
  70. ('borg', 'break-lock', '--lock-wait', '5', '::'),
  71. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  72. borg_local_path='borg',
  73. shell=True,
  74. extra_environment={'BORG_REPO': 'repo', 'ARCHIVE': ''},
  75. )
  76. module.run_arbitrary_borg(
  77. repository_path='repo',
  78. config=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_flags').and_return(())
  86. flexmock(module.environment).should_receive('make_environment')
  87. flexmock(module).should_receive('execute_command').with_args(
  88. ('borg', 'break-lock', '::$ARCHIVE'),
  89. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  90. borg_local_path='borg',
  91. shell=True,
  92. extra_environment={'BORG_REPO': 'repo', 'ARCHIVE': 'archive'},
  93. )
  94. module.run_arbitrary_borg(
  95. repository_path='repo',
  96. config={},
  97. local_borg_version='1.2.3',
  98. options=['break-lock', '::$ARCHIVE'],
  99. archive='archive',
  100. )
  101. def test_run_arbitrary_borg_with_local_path_calls_borg_via_local_path():
  102. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  103. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  104. flexmock(module.flags).should_receive('make_flags').and_return(())
  105. flexmock(module.environment).should_receive('make_environment')
  106. flexmock(module).should_receive('execute_command').with_args(
  107. ('borg1', 'break-lock', '::'),
  108. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  109. borg_local_path='borg1',
  110. shell=True,
  111. extra_environment={'BORG_REPO': 'repo', 'ARCHIVE': ''},
  112. )
  113. module.run_arbitrary_borg(
  114. repository_path='repo',
  115. config={},
  116. local_borg_version='1.2.3',
  117. options=['break-lock', '::'],
  118. local_path='borg1',
  119. )
  120. def test_run_arbitrary_borg_with_remote_path_calls_borg_with_remote_path_flags():
  121. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  122. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  123. flexmock(module.flags).should_receive('make_flags').and_return(
  124. ('--remote-path', 'borg1')
  125. ).and_return(())
  126. flexmock(module.environment).should_receive('make_environment')
  127. flexmock(module).should_receive('execute_command').with_args(
  128. ('borg', 'break-lock', '--remote-path', 'borg1', '::'),
  129. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  130. borg_local_path='borg',
  131. shell=True,
  132. extra_environment={'BORG_REPO': 'repo', 'ARCHIVE': ''},
  133. )
  134. module.run_arbitrary_borg(
  135. repository_path='repo',
  136. config={},
  137. local_borg_version='1.2.3',
  138. options=['break-lock', '::'],
  139. remote_path='borg1',
  140. )
  141. def test_run_arbitrary_borg_passes_borg_specific_flags_to_borg():
  142. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  143. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  144. flexmock(module.flags).should_receive('make_flags').and_return(())
  145. flexmock(module.environment).should_receive('make_environment')
  146. flexmock(module).should_receive('execute_command').with_args(
  147. ('borg', 'list', '--progress', '::'),
  148. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  149. borg_local_path='borg',
  150. shell=True,
  151. extra_environment={'BORG_REPO': 'repo', 'ARCHIVE': ''},
  152. )
  153. module.run_arbitrary_borg(
  154. repository_path='repo',
  155. config={},
  156. local_borg_version='1.2.3',
  157. options=['list', '--progress', '::'],
  158. )
  159. def test_run_arbitrary_borg_omits_dash_dash_in_flags_passed_to_borg():
  160. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  161. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  162. flexmock(module.flags).should_receive('make_flags').and_return(())
  163. flexmock(module.environment).should_receive('make_environment')
  164. flexmock(module).should_receive('execute_command').with_args(
  165. ('borg', 'break-lock', '::'),
  166. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  167. borg_local_path='borg',
  168. shell=True,
  169. extra_environment={'BORG_REPO': 'repo', 'ARCHIVE': ''},
  170. )
  171. module.run_arbitrary_borg(
  172. repository_path='repo',
  173. config={},
  174. local_borg_version='1.2.3',
  175. options=['--', 'break-lock', '::'],
  176. )
  177. def test_run_arbitrary_borg_without_borg_specific_flags_does_not_raise():
  178. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  179. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  180. flexmock(module.flags).should_receive('make_flags').and_return(())
  181. flexmock(module.environment).should_receive('make_environment')
  182. flexmock(module).should_receive('execute_command').with_args(
  183. ('borg',),
  184. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  185. borg_local_path='borg',
  186. shell=True,
  187. extra_environment={'BORG_REPO': 'repo', 'ARCHIVE': ''},
  188. )
  189. module.run_arbitrary_borg(
  190. repository_path='repo',
  191. config={},
  192. local_borg_version='1.2.3',
  193. options=[],
  194. )
  195. def test_run_arbitrary_borg_passes_key_sub_command_to_borg_before_injected_flags():
  196. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  197. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  198. flexmock(module.flags).should_receive('make_flags').and_return(())
  199. flexmock(module.environment).should_receive('make_environment')
  200. flexmock(module).should_receive('execute_command').with_args(
  201. ('borg', 'key', 'export', '--info', '::'),
  202. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  203. borg_local_path='borg',
  204. shell=True,
  205. extra_environment={'BORG_REPO': 'repo', 'ARCHIVE': ''},
  206. )
  207. insert_logging_mock(logging.INFO)
  208. module.run_arbitrary_borg(
  209. repository_path='repo',
  210. config={},
  211. local_borg_version='1.2.3',
  212. options=['key', 'export', '::'],
  213. )
  214. def test_run_arbitrary_borg_passes_debug_sub_command_to_borg_before_injected_flags():
  215. flexmock(module.borgmatic.logger).should_receive('add_custom_log_levels')
  216. flexmock(module.logging).ANSWER = module.borgmatic.logger.ANSWER
  217. flexmock(module.flags).should_receive('make_flags').and_return(())
  218. flexmock(module.environment).should_receive('make_environment')
  219. flexmock(module).should_receive('execute_command').with_args(
  220. ('borg', 'debug', 'dump-manifest', '--info', '::', 'path'),
  221. output_file=module.borgmatic.execute.DO_NOT_CAPTURE,
  222. borg_local_path='borg',
  223. shell=True,
  224. extra_environment={'BORG_REPO': 'repo', 'ARCHIVE': ''},
  225. )
  226. insert_logging_mock(logging.INFO)
  227. module.run_arbitrary_borg(
  228. repository_path='repo',
  229. config={},
  230. local_borg_version='1.2.3',
  231. options=['debug', 'dump-manifest', '::', 'path'],
  232. )