2
0

test_create.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import os
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.actions import create as module
  5. def test_run_create_executes_and_calls_hooks_for_configured_repository():
  6. flexmock(module.logger).answer = lambda message: None
  7. flexmock(module.borgmatic.config.validate).should_receive('repositories_match').never()
  8. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  9. flexmock()
  10. )
  11. flexmock(module.borgmatic.borg.create).should_receive('create_archive').once()
  12. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return({})
  13. flexmock(module.borgmatic.hooks.dispatch).should_receive(
  14. 'call_hooks_even_if_unconfigured'
  15. ).and_return({})
  16. flexmock(module.borgmatic.actions.pattern).should_receive('collect_patterns').and_return(())
  17. flexmock(module.borgmatic.actions.pattern).should_receive('process_patterns').and_return([])
  18. flexmock(os.path).should_receive('join').and_return('/run/borgmatic/bootstrap')
  19. create_arguments = flexmock(
  20. repository=None,
  21. progress=flexmock(),
  22. statistics=flexmock(),
  23. json=False,
  24. list_details=flexmock(),
  25. )
  26. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  27. list(
  28. module.run_create(
  29. config_filename='test.yaml',
  30. repository={'path': 'repo'},
  31. config={},
  32. config_paths=['/tmp/test.yaml'],
  33. local_borg_version=None,
  34. create_arguments=create_arguments,
  35. global_arguments=global_arguments,
  36. dry_run_label='',
  37. local_path=None,
  38. remote_path=None,
  39. )
  40. )
  41. def test_run_create_runs_with_selected_repository():
  42. flexmock(module.logger).answer = lambda message: None
  43. flexmock(module.borgmatic.config.validate).should_receive(
  44. 'repositories_match'
  45. ).once().and_return(True)
  46. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  47. flexmock()
  48. )
  49. flexmock(module.borgmatic.borg.create).should_receive('create_archive').once()
  50. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return({})
  51. flexmock(module.borgmatic.hooks.dispatch).should_receive(
  52. 'call_hooks_even_if_unconfigured'
  53. ).and_return({})
  54. flexmock(module.borgmatic.actions.pattern).should_receive('collect_patterns').and_return(())
  55. flexmock(module.borgmatic.actions.pattern).should_receive('process_patterns').and_return([])
  56. flexmock(os.path).should_receive('join').and_return('/run/borgmatic/bootstrap')
  57. create_arguments = flexmock(
  58. repository=flexmock(),
  59. progress=flexmock(),
  60. statistics=flexmock(),
  61. json=False,
  62. list_details=flexmock(),
  63. )
  64. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  65. list(
  66. module.run_create(
  67. config_filename='test.yaml',
  68. repository={'path': 'repo'},
  69. config={},
  70. config_paths=['/tmp/test.yaml'],
  71. local_borg_version=None,
  72. create_arguments=create_arguments,
  73. global_arguments=global_arguments,
  74. dry_run_label='',
  75. local_path=None,
  76. remote_path=None,
  77. )
  78. )
  79. def test_run_create_bails_if_repository_does_not_match():
  80. flexmock(module.logger).answer = lambda message: None
  81. flexmock(module.borgmatic.config.validate).should_receive(
  82. 'repositories_match'
  83. ).once().and_return(False)
  84. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').never()
  85. flexmock(module.borgmatic.borg.create).should_receive('create_archive').never()
  86. create_arguments = flexmock(
  87. repository=flexmock(),
  88. progress=flexmock(),
  89. statistics=flexmock(),
  90. json=False,
  91. list_details=flexmock(),
  92. )
  93. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  94. list(
  95. module.run_create(
  96. config_filename='test.yaml',
  97. repository='repo',
  98. config={},
  99. config_paths=['/tmp/test.yaml'],
  100. local_borg_version=None,
  101. create_arguments=create_arguments,
  102. global_arguments=global_arguments,
  103. dry_run_label='',
  104. local_path=None,
  105. remote_path=None,
  106. )
  107. )
  108. def test_run_create_with_both_list_and_json_errors():
  109. flexmock(module.logger).answer = lambda message: None
  110. flexmock(module.borgmatic.config.validate).should_receive(
  111. 'repositories_match'
  112. ).once().and_return(True)
  113. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').never()
  114. flexmock(module.borgmatic.borg.create).should_receive('create_archive').never()
  115. create_arguments = flexmock(
  116. repository=flexmock(),
  117. progress=flexmock(),
  118. statistics=flexmock(),
  119. json=True,
  120. list_details=flexmock(),
  121. )
  122. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  123. with pytest.raises(ValueError):
  124. list(
  125. module.run_create(
  126. config_filename='test.yaml',
  127. repository={'path': 'repo'},
  128. config={'list_details': True},
  129. config_paths=['/tmp/test.yaml'],
  130. local_borg_version=None,
  131. create_arguments=create_arguments,
  132. global_arguments=global_arguments,
  133. dry_run_label='',
  134. local_path=None,
  135. remote_path=None,
  136. )
  137. )
  138. def test_run_create_with_both_list_and_progress_errors():
  139. flexmock(module.logger).answer = lambda message: None
  140. flexmock(module.borgmatic.config.validate).should_receive(
  141. 'repositories_match'
  142. ).once().and_return(True)
  143. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').never()
  144. flexmock(module.borgmatic.borg.create).should_receive('create_archive').never()
  145. create_arguments = flexmock(
  146. repository=flexmock(),
  147. progress=flexmock(),
  148. statistics=flexmock(),
  149. json=False,
  150. list_details=flexmock(),
  151. )
  152. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  153. with pytest.raises(ValueError):
  154. list(
  155. module.run_create(
  156. config_filename='test.yaml',
  157. repository={'path': 'repo'},
  158. config={'list_details': True, 'progress': True},
  159. config_paths=['/tmp/test.yaml'],
  160. local_borg_version=None,
  161. create_arguments=create_arguments,
  162. global_arguments=global_arguments,
  163. dry_run_label='',
  164. local_path=None,
  165. remote_path=None,
  166. )
  167. )
  168. def test_run_create_produces_json():
  169. flexmock(module.logger).answer = lambda message: None
  170. flexmock(module.borgmatic.config.validate).should_receive(
  171. 'repositories_match'
  172. ).once().and_return(True)
  173. flexmock(module.borgmatic.config.paths).should_receive('Runtime_directory').and_return(
  174. flexmock()
  175. )
  176. flexmock(module.borgmatic.borg.create).should_receive('create_archive').once().and_return(
  177. flexmock()
  178. )
  179. parsed_json = flexmock()
  180. flexmock(module.borgmatic.actions.json).should_receive('parse_json').and_return(parsed_json)
  181. flexmock(module.borgmatic.hooks.dispatch).should_receive('call_hooks').and_return({})
  182. flexmock(module.borgmatic.hooks.dispatch).should_receive(
  183. 'call_hooks_even_if_unconfigured'
  184. ).and_return({})
  185. flexmock(module.borgmatic.actions.pattern).should_receive('collect_patterns').and_return(())
  186. flexmock(module.borgmatic.actions.pattern).should_receive('process_patterns').and_return([])
  187. flexmock(os.path).should_receive('join').and_return('/run/borgmatic/bootstrap')
  188. create_arguments = flexmock(
  189. repository=flexmock(),
  190. progress=flexmock(),
  191. statistics=flexmock(),
  192. json=True,
  193. list_details=flexmock(),
  194. )
  195. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False)
  196. assert list(
  197. module.run_create(
  198. config_filename='test.yaml',
  199. repository={'path': 'repo'},
  200. config={},
  201. config_paths=['/tmp/test.yaml'],
  202. local_borg_version=None,
  203. create_arguments=create_arguments,
  204. global_arguments=global_arguments,
  205. dry_run_label='',
  206. local_path=None,
  207. remote_path=None,
  208. )
  209. ) == [parsed_json]