test_create.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. location={},
  27. storage={},
  28. hooks={},
  29. hook_context={},
  30. local_borg_version=None,
  31. create_arguments=create_arguments,
  32. global_arguments=global_arguments,
  33. dry_run_label='',
  34. local_path=None,
  35. remote_path=None,
  36. )
  37. )
  38. def test_run_create_runs_with_selected_repository():
  39. flexmock(module.logger).answer = lambda message: None
  40. flexmock(module.borgmatic.config.validate).should_receive(
  41. 'repositories_match'
  42. ).once().and_return(True)
  43. flexmock(module.borgmatic.borg.create).should_receive('create_archive').once()
  44. flexmock(module).should_receive('create_borgmatic_manifest').once()
  45. create_arguments = flexmock(
  46. repository=flexmock(),
  47. progress=flexmock(),
  48. stats=flexmock(),
  49. json=flexmock(),
  50. list_files=flexmock(),
  51. )
  52. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False, used_config_paths=[])
  53. list(
  54. module.run_create(
  55. config_filename='test.yaml',
  56. repository={'path': 'repo'},
  57. location={},
  58. storage={},
  59. hooks={},
  60. hook_context={},
  61. local_borg_version=None,
  62. create_arguments=create_arguments,
  63. global_arguments=global_arguments,
  64. dry_run_label='',
  65. local_path=None,
  66. remote_path=None,
  67. )
  68. )
  69. def test_run_create_bails_if_repository_does_not_match():
  70. flexmock(module.logger).answer = lambda message: None
  71. flexmock(module.borgmatic.config.validate).should_receive(
  72. 'repositories_match'
  73. ).once().and_return(False)
  74. flexmock(module.borgmatic.borg.create).should_receive('create_archive').never()
  75. flexmock(module).should_receive('create_borgmatic_manifest').never()
  76. create_arguments = flexmock(
  77. repository=flexmock(),
  78. progress=flexmock(),
  79. stats=flexmock(),
  80. json=flexmock(),
  81. list_files=flexmock(),
  82. )
  83. global_arguments = flexmock(monitoring_verbosity=1, dry_run=False, used_config_paths=[])
  84. list(
  85. module.run_create(
  86. config_filename='test.yaml',
  87. repository='repo',
  88. location={},
  89. storage={},
  90. hooks={},
  91. hook_context={},
  92. local_borg_version=None,
  93. create_arguments=create_arguments,
  94. global_arguments=global_arguments,
  95. dry_run_label='',
  96. local_path=None,
  97. remote_path=None,
  98. )
  99. )
  100. def test_create_borgmatic_manifest_creates_manifest_file():
  101. flexmock(module.os.path).should_receive('join').with_args(
  102. module.borgmatic.borg.state.DEFAULT_BORGMATIC_SOURCE_DIRECTORY, 'bootstrap', 'manifest.json'
  103. ).and_return('/home/user/.borgmatic/bootstrap/manifest.json')
  104. flexmock(module.os.path).should_receive('exists').and_return(False)
  105. flexmock(module.os).should_receive('makedirs').and_return(True)
  106. flexmock(module.importlib_metadata).should_receive('version').and_return('1.0.0')
  107. flexmock(sys.modules['builtins']).should_receive('open').with_args(
  108. '/home/user/.borgmatic/bootstrap/manifest.json', 'w'
  109. ).and_return(
  110. flexmock(
  111. __enter__=lambda *args: flexmock(write=lambda *args: None, close=lambda *args: None),
  112. __exit__=lambda *args: None,
  113. )
  114. )
  115. flexmock(module.json).should_receive('dump').and_return(True).once()
  116. module.create_borgmatic_manifest({}, 'test.yaml', False)
  117. def test_create_borgmatic_manifest_creates_manifest_file_with_custom_borgmatic_source_directory():
  118. flexmock(module.os.path).should_receive('join').with_args(
  119. '/borgmatic', 'bootstrap', 'manifest.json'
  120. ).and_return('/borgmatic/bootstrap/manifest.json')
  121. flexmock(module.os.path).should_receive('exists').and_return(False)
  122. flexmock(module.os).should_receive('makedirs').and_return(True)
  123. flexmock(module.importlib_metadata).should_receive('version').and_return('1.0.0')
  124. flexmock(sys.modules['builtins']).should_receive('open').with_args(
  125. '/borgmatic/bootstrap/manifest.json', 'w'
  126. ).and_return(
  127. flexmock(
  128. __enter__=lambda *args: flexmock(write=lambda *args: None, close=lambda *args: None),
  129. __exit__=lambda *args: None,
  130. )
  131. )
  132. flexmock(module.json).should_receive('dump').and_return(True).once()
  133. module.create_borgmatic_manifest(
  134. {'borgmatic_source_directory': '/borgmatic'}, 'test.yaml', False
  135. )
  136. def test_create_borgmatic_manifest_does_not_create_manifest_file_on_dry_run():
  137. flexmock(module.os.path).should_receive('expanduser').never()
  138. module.create_borgmatic_manifest({}, 'test.yaml', True)