2
0

test_bootstrap.py 4.5 KB

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