test_create.py 8.8 KB

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