test_zfs.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import os
  2. from flexmock import flexmock
  3. from borgmatic.borg.pattern import Pattern, Pattern_type
  4. from borgmatic.hooks.data_source import zfs as module
  5. def test_dump_data_sources_snapshots_and_mounts_and_updates_patterns():
  6. dataset = flexmock(
  7. name='dataset',
  8. mount_point='/mnt/dataset',
  9. contained_patterns=(Pattern('/mnt/dataset/subdir'),),
  10. )
  11. flexmock(module).should_receive('get_datasets_to_backup').and_return((dataset,))
  12. flexmock(module.os).should_receive('getpid').and_return(1234)
  13. full_snapshot_name = 'dataset@borgmatic-1234'
  14. flexmock(module).should_receive('snapshot_dataset').with_args(
  15. 'zfs',
  16. full_snapshot_name,
  17. ).once()
  18. flexmock(module.hashlib).should_receive('shake_256').and_return(
  19. flexmock(hexdigest=lambda length: 'b33f'),
  20. )
  21. snapshot_mount_path = '/run/borgmatic/zfs_snapshots/b33f/./mnt/dataset'
  22. flexmock(module).should_receive('mount_snapshot').with_args(
  23. 'mount',
  24. full_snapshot_name,
  25. module.os.path.normpath(snapshot_mount_path),
  26. ).once()
  27. patterns = [Pattern('/mnt/dataset/subdir')]
  28. assert (
  29. module.dump_data_sources(
  30. hook_config={},
  31. config={'source_directories': '/mnt/dataset', 'zfs': {}},
  32. config_paths=('test.yaml',),
  33. borgmatic_runtime_directory='/run/borgmatic',
  34. patterns=patterns,
  35. dry_run=False,
  36. )
  37. == []
  38. )
  39. assert patterns == [
  40. Pattern(os.path.join(snapshot_mount_path, 'subdir')),
  41. Pattern(os.path.join(snapshot_mount_path, 'subdir'), Pattern_type.INCLUDE),
  42. ]