test_create.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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=flexmock(),
  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=flexmock(),
  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=flexmock(),
  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=flexmock(),
  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_create_borgmatic_manifest_creates_manifest_file():
  132. flexmock(module.os.path).should_receive('join').with_args(
  133. module.borgmatic.borg.state.DEFAULT_BORGMATIC_SOURCE_DIRECTORY, 'bootstrap', 'manifest.json'
  134. ).and_return('/home/user/.borgmatic/bootstrap/manifest.json')
  135. flexmock(module.os.path).should_receive('exists').and_return(False)
  136. flexmock(module.os).should_receive('makedirs').and_return(True)
  137. flexmock(module.importlib_metadata).should_receive('version').and_return('1.0.0')
  138. flexmock(sys.modules['builtins']).should_receive('open').with_args(
  139. '/home/user/.borgmatic/bootstrap/manifest.json', 'w'
  140. ).and_return(
  141. flexmock(
  142. __enter__=lambda *args: flexmock(write=lambda *args: None, close=lambda *args: None),
  143. __exit__=lambda *args: None,
  144. )
  145. )
  146. flexmock(module.json).should_receive('dump').and_return(True).once()
  147. module.create_borgmatic_manifest({}, 'test.yaml', False)
  148. def test_create_borgmatic_manifest_creates_manifest_file_with_custom_borgmatic_source_directory():
  149. flexmock(module.os.path).should_receive('join').with_args(
  150. '/borgmatic', 'bootstrap', 'manifest.json'
  151. ).and_return('/borgmatic/bootstrap/manifest.json')
  152. flexmock(module.os.path).should_receive('exists').and_return(False)
  153. flexmock(module.os).should_receive('makedirs').and_return(True)
  154. flexmock(module.importlib_metadata).should_receive('version').and_return('1.0.0')
  155. flexmock(sys.modules['builtins']).should_receive('open').with_args(
  156. '/borgmatic/bootstrap/manifest.json', 'w'
  157. ).and_return(
  158. flexmock(
  159. __enter__=lambda *args: flexmock(write=lambda *args: None, close=lambda *args: None),
  160. __exit__=lambda *args: None,
  161. )
  162. )
  163. flexmock(module.json).should_receive('dump').and_return(True).once()
  164. module.create_borgmatic_manifest(
  165. {'borgmatic_source_directory': '/borgmatic'}, 'test.yaml', False
  166. )
  167. def test_create_borgmatic_manifest_does_not_create_manifest_file_on_dry_run():
  168. flexmock(module.os.path).should_receive('expanduser').never()
  169. module.create_borgmatic_manifest({}, 'test.yaml', True)