2
0

test_bootstrap.py 4.6 KB

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