test_lvm.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 = [Pattern('/mnt/lvolume1/subdir'), Pattern('/mnt/lvolume2')]
  7. logical_volumes = (
  8. module.Logical_volume(
  9. name='lvolume1',
  10. device_path='/dev/lvolume1',
  11. mount_point='/mnt/lvolume1',
  12. contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
  13. ),
  14. module.Logical_volume(
  15. name='lvolume2',
  16. device_path='/dev/lvolume2',
  17. mount_point='/mnt/lvolume2',
  18. contained_patterns=(Pattern('/mnt/lvolume2'),),
  19. ),
  20. )
  21. flexmock(module).should_receive('get_logical_volumes').and_return(logical_volumes)
  22. flexmock(module.os).should_receive('getpid').and_return(1234)
  23. flexmock(module).should_receive('snapshot_logical_volume').with_args(
  24. 'lvcreate',
  25. 'lvolume1_borgmatic-1234',
  26. '/dev/lvolume1',
  27. module.DEFAULT_SNAPSHOT_SIZE,
  28. ).once()
  29. flexmock(module).should_receive('snapshot_logical_volume').with_args(
  30. 'lvcreate',
  31. 'lvolume2_borgmatic-1234',
  32. '/dev/lvolume2',
  33. module.DEFAULT_SNAPSHOT_SIZE,
  34. ).once()
  35. flexmock(module).should_receive('get_snapshots').with_args(
  36. 'lvs',
  37. snapshot_name='lvolume1_borgmatic-1234',
  38. ).and_return(
  39. (module.Snapshot(name='lvolume1_borgmatic-1234', device_path='/dev/lvolume1_snap'),),
  40. )
  41. flexmock(module).should_receive('get_snapshots').with_args(
  42. 'lvs',
  43. snapshot_name='lvolume2_borgmatic-1234',
  44. ).and_return(
  45. (module.Snapshot(name='lvolume2_borgmatic-1234', device_path='/dev/lvolume2_snap'),),
  46. )
  47. flexmock(module.hashlib).should_receive('shake_256').and_return(
  48. flexmock(hexdigest=lambda length: 'b33f'),
  49. )
  50. flexmock(module).should_receive('mount_snapshot').with_args(
  51. 'mount',
  52. '/dev/lvolume1_snap',
  53. '/run/borgmatic/lvm_snapshots/b33f/mnt/lvolume1',
  54. ).once()
  55. flexmock(module).should_receive('mount_snapshot').with_args(
  56. 'mount',
  57. '/dev/lvolume2_snap',
  58. '/run/borgmatic/lvm_snapshots/b33f/mnt/lvolume2',
  59. ).once()
  60. assert (
  61. module.dump_data_sources(
  62. hook_config=config['lvm'],
  63. config=config,
  64. config_paths=('test.yaml',),
  65. borgmatic_runtime_directory='/run/borgmatic',
  66. patterns=patterns,
  67. dry_run=False,
  68. )
  69. == []
  70. )
  71. assert patterns == [
  72. Pattern('/run/borgmatic/lvm_snapshots/b33f/./mnt/lvolume1/subdir'),
  73. Pattern('/run/borgmatic/lvm_snapshots/b33f/./mnt/lvolume1/subdir', Pattern_type.INCLUDE),
  74. Pattern('/run/borgmatic/lvm_snapshots/b33f/./mnt/lvolume2'),
  75. Pattern('/run/borgmatic/lvm_snapshots/b33f/./mnt/lvolume2', Pattern_type.INCLUDE),
  76. ]