test_create.py 9.4 KB

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