test_lvm.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. from flexmock import flexmock
  2. from borgmatic.borg.pattern import Pattern, Pattern_type
  3. from borgmatic.hooks.data_source import lvm as module
  4. def test_dump_data_sources_snapshots_and_mounts_and_updates_patterns():
  5. config = {'lvm': {}}
  6. patterns = [
  7. Pattern('/mnt/lvolume1/subdir'),
  8. Pattern('/mnt/lvolume1/subdir/.cache', Pattern_type.EXCLUDE),
  9. Pattern('/mnt/lvolume2'),
  10. ]
  11. logical_volumes = (
  12. module.Logical_volume(
  13. name='lvolume1',
  14. device_path='/dev/lvolume1',
  15. mount_point='/mnt/lvolume1',
  16. contained_patterns=(
  17. Pattern('/mnt/lvolume1/subdir'),
  18. Pattern('/mnt/lvolume1/subdir/.cache', Pattern_type.EXCLUDE),
  19. ),
  20. ),
  21. module.Logical_volume(
  22. name='lvolume2',
  23. device_path='/dev/lvolume2',
  24. mount_point='/mnt/lvolume2',
  25. contained_patterns=(Pattern('/mnt/lvolume2'),),
  26. ),
  27. )
  28. flexmock(module).should_receive('get_logical_volumes').and_return(logical_volumes)
  29. flexmock(module.os).should_receive('getpid').and_return(1234)
  30. flexmock(module).should_receive('snapshot_logical_volume').with_args(
  31. 'lvcreate',
  32. 'lvolume1_borgmatic-1234',
  33. '/dev/lvolume1',
  34. module.DEFAULT_SNAPSHOT_SIZE,
  35. ).once()
  36. flexmock(module).should_receive('snapshot_logical_volume').with_args(
  37. 'lvcreate',
  38. 'lvolume2_borgmatic-1234',
  39. '/dev/lvolume2',
  40. module.DEFAULT_SNAPSHOT_SIZE,
  41. ).once()
  42. flexmock(module).should_receive('get_snapshots').with_args(
  43. 'lvs',
  44. snapshot_name='lvolume1_borgmatic-1234',
  45. ).and_return(
  46. (module.Snapshot(name='lvolume1_borgmatic-1234', device_path='/dev/lvolume1_snap'),),
  47. )
  48. flexmock(module).should_receive('get_snapshots').with_args(
  49. 'lvs',
  50. snapshot_name='lvolume2_borgmatic-1234',
  51. ).and_return(
  52. (module.Snapshot(name='lvolume2_borgmatic-1234', device_path='/dev/lvolume2_snap'),),
  53. )
  54. flexmock(module.hashlib).should_receive('shake_256').and_return(
  55. flexmock(hexdigest=lambda length: 'b33f'),
  56. )
  57. flexmock(module).should_receive('mount_snapshot').with_args(
  58. 'mount',
  59. '/dev/lvolume1_snap',
  60. '/run/borgmatic/lvm_snapshots/b33f/mnt/lvolume1',
  61. ).once()
  62. flexmock(module).should_receive('mount_snapshot').with_args(
  63. 'mount',
  64. '/dev/lvolume2_snap',
  65. '/run/borgmatic/lvm_snapshots/b33f/mnt/lvolume2',
  66. ).once()
  67. assert (
  68. module.dump_data_sources(
  69. hook_config=config['lvm'],
  70. config=config,
  71. config_paths=('test.yaml',),
  72. borgmatic_runtime_directory='/run/borgmatic',
  73. patterns=patterns,
  74. dry_run=False,
  75. )
  76. == []
  77. )
  78. assert patterns == [
  79. Pattern('/run/borgmatic/lvm_snapshots/b33f/./mnt/lvolume1/subdir'),
  80. Pattern(
  81. '/run/borgmatic/lvm_snapshots/b33f/./mnt/lvolume1/subdir/.cache', Pattern_type.EXCLUDE
  82. ),
  83. Pattern('/run/borgmatic/lvm_snapshots/b33f/./mnt/lvolume1/subdir', Pattern_type.INCLUDE),
  84. Pattern('/run/borgmatic/lvm_snapshots/b33f/./mnt/lvolume2'),
  85. Pattern('/run/borgmatic/lvm_snapshots/b33f/./mnt/lvolume2', Pattern_type.INCLUDE),
  86. ]