test_bootstrap.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import sys
  2. from flexmock import flexmock
  3. from borgmatic.hooks.data_source import bootstrap as module
  4. def test_dump_data_sources_creates_manifest_file():
  5. flexmock(module.borgmatic.hooks.command).should_receive('Before_after_hooks').and_return(
  6. flexmock()
  7. )
  8. flexmock(module.os).should_receive('makedirs')
  9. flexmock(module.importlib.metadata).should_receive('version').and_return('1.0.0')
  10. manifest_file = flexmock(
  11. __enter__=lambda *args: flexmock(write=lambda *args: None, close=lambda *args: None),
  12. __exit__=lambda *args: None,
  13. )
  14. flexmock(sys.modules['builtins']).should_receive('open').with_args(
  15. '/run/borgmatic/bootstrap/manifest.json', 'w'
  16. ).and_return(manifest_file)
  17. flexmock(module.json).should_receive('dump').with_args(
  18. {'borgmatic_version': '1.0.0', 'config_paths': ('test.yaml',)},
  19. manifest_file,
  20. ).once()
  21. module.dump_data_sources(
  22. hook_config=None,
  23. config={},
  24. config_paths=('test.yaml',),
  25. borgmatic_runtime_directory='/run/borgmatic',
  26. patterns=[],
  27. dry_run=False,
  28. )
  29. def test_dump_data_sources_with_store_config_files_false_does_not_create_manifest_file():
  30. flexmock(module.borgmatic.hooks.command).should_receive('Before_after_hooks').never()
  31. flexmock(module.os).should_receive('makedirs').never()
  32. flexmock(module.json).should_receive('dump').never()
  33. hook_config = {'store_config_files': False}
  34. module.dump_data_sources(
  35. hook_config=hook_config,
  36. config={'bootstrap': hook_config},
  37. config_paths=('test.yaml',),
  38. borgmatic_runtime_directory='/run/borgmatic',
  39. patterns=[],
  40. dry_run=True,
  41. )
  42. def test_dump_data_sources_with_dry_run_does_not_create_manifest_file():
  43. flexmock(module.borgmatic.hooks.command).should_receive('Before_after_hooks').and_return(
  44. flexmock()
  45. )
  46. flexmock(module.os).should_receive('makedirs').never()
  47. flexmock(module.json).should_receive('dump').never()
  48. module.dump_data_sources(
  49. hook_config=None,
  50. config={},
  51. config_paths=('test.yaml',),
  52. borgmatic_runtime_directory='/run/borgmatic',
  53. patterns=[],
  54. dry_run=True,
  55. )
  56. def test_remove_data_source_dumps_deletes_manifest_and_parent_directory():
  57. flexmock(module.borgmatic.config.paths).should_receive(
  58. 'replace_temporary_subdirectory_with_glob'
  59. ).and_return('/run/borgmatic')
  60. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  61. flexmock(module.os).should_receive('remove').with_args(
  62. '/run/borgmatic/bootstrap/manifest.json'
  63. ).once()
  64. flexmock(module.os).should_receive('rmdir').with_args('/run/borgmatic/bootstrap').once()
  65. module.remove_data_source_dumps(
  66. hook_config=None,
  67. config={},
  68. borgmatic_runtime_directory='/run/borgmatic',
  69. dry_run=False,
  70. )
  71. def test_remove_data_source_dumps_with_dry_run_bails():
  72. flexmock(module.borgmatic.config.paths).should_receive(
  73. 'replace_temporary_subdirectory_with_glob'
  74. ).and_return('/run/borgmatic')
  75. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  76. flexmock(module.os).should_receive('remove').never()
  77. flexmock(module.os).should_receive('rmdir').never()
  78. module.remove_data_source_dumps(
  79. hook_config=None,
  80. config={},
  81. borgmatic_runtime_directory='/run/borgmatic',
  82. dry_run=True,
  83. )
  84. def test_remove_data_source_dumps_swallows_manifest_file_not_found_error():
  85. flexmock(module.borgmatic.config.paths).should_receive(
  86. 'replace_temporary_subdirectory_with_glob'
  87. ).and_return('/run/borgmatic')
  88. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  89. flexmock(module.os).should_receive('remove').with_args(
  90. '/run/borgmatic/bootstrap/manifest.json'
  91. ).and_raise(FileNotFoundError).once()
  92. flexmock(module.os).should_receive('rmdir').with_args('/run/borgmatic/bootstrap').once()
  93. module.remove_data_source_dumps(
  94. hook_config=None,
  95. config={},
  96. borgmatic_runtime_directory='/run/borgmatic',
  97. dry_run=False,
  98. )
  99. def test_remove_data_source_dumps_swallows_manifest_parent_directory_not_found_error():
  100. flexmock(module.borgmatic.config.paths).should_receive(
  101. 'replace_temporary_subdirectory_with_glob'
  102. ).and_return('/run/borgmatic')
  103. flexmock(module.glob).should_receive('glob').replace_with(lambda path: [path])
  104. flexmock(module.os).should_receive('remove').with_args(
  105. '/run/borgmatic/bootstrap/manifest.json'
  106. ).once()
  107. flexmock(module.os).should_receive('rmdir').with_args('/run/borgmatic/bootstrap').and_raise(
  108. FileNotFoundError
  109. ).once()
  110. module.remove_data_source_dumps(
  111. hook_config=None,
  112. config={},
  113. borgmatic_runtime_directory='/run/borgmatic',
  114. dry_run=False,
  115. )