test_create.py 8.9 KB

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