Add even more missing test coverage (#962).

This commit is contained in:
Dan Helfman
2025-01-16 15:11:59 -08:00
parent 3bda843139
commit 2467518d4e
10 changed files with 173 additions and 22 deletions
+29 -9
View File
@@ -154,22 +154,42 @@ def test_make_snapshot_path_includes_stripped_subvolume_path(
@pytest.mark.parametrize(
'subvolume_path,pattern_path,expected_path',
'subvolume_path,pattern,expected_pattern',
(
('/foo/bar', '/foo/bar/baz', '/foo/bar/.borgmatic-snapshot-1234/./foo/bar/baz'),
('/foo/bar', '/foo/bar', '/foo/bar/.borgmatic-snapshot-1234/./foo/bar'),
('/', '/foo', '/.borgmatic-snapshot-1234/./foo'),
('/', '/', '/.borgmatic-snapshot-1234/./'),
(
'/foo/bar',
Pattern('/foo/bar/baz'),
Pattern('/foo/bar/.borgmatic-snapshot-1234/./foo/bar/baz'),
),
('/foo/bar', Pattern('/foo/bar'), Pattern('/foo/bar/.borgmatic-snapshot-1234/./foo/bar')),
(
'/foo/bar',
Pattern('^/foo/bar', Pattern_type.INCLUDE, Pattern_style.REGULAR_EXPRESSION),
Pattern(
'^/foo/bar/.borgmatic-snapshot-1234/./foo/bar',
Pattern_type.INCLUDE,
Pattern_style.REGULAR_EXPRESSION,
),
),
(
'/foo/bar',
Pattern('/foo/bar', Pattern_type.INCLUDE, Pattern_style.REGULAR_EXPRESSION),
Pattern(
'/foo/bar/.borgmatic-snapshot-1234/./foo/bar',
Pattern_type.INCLUDE,
Pattern_style.REGULAR_EXPRESSION,
),
),
('/', Pattern('/foo'), Pattern('/.borgmatic-snapshot-1234/./foo')),
('/', Pattern('/'), Pattern('/.borgmatic-snapshot-1234/./')),
),
)
def test_make_borg_snapshot_pattern_includes_slashdot_hack_and_stripped_pattern_path(
subvolume_path, pattern_path, expected_path
subvolume_path, pattern, expected_pattern
):
flexmock(module.os).should_receive('getpid').and_return(1234)
assert module.make_borg_snapshot_pattern(subvolume_path, Pattern(pattern_path)) == Pattern(
expected_path
)
assert module.make_borg_snapshot_pattern(subvolume_path, pattern) == expected_pattern
def test_dump_data_sources_snapshots_each_subvolume_and_updates_patterns():
+59 -1
View File
@@ -1,7 +1,7 @@
import pytest
from flexmock import flexmock
from borgmatic.borg.pattern import Pattern
from borgmatic.borg.pattern import Pattern, Pattern_style, Pattern_type
from borgmatic.hooks.data_source import lvm as module
@@ -133,6 +133,40 @@ def test_snapshot_logical_volume_with_non_percentage_snapshot_name_uses_lvcreate
module.snapshot_logical_volume('lvcreate', 'snap', '/dev/snap', '10TB')
@pytest.mark.parametrize(
'pattern,expected_pattern',
(
(
Pattern('/foo/bar/baz'),
Pattern('/run/borgmatic/lvm_snapshots/./foo/bar/baz'),
),
(Pattern('/foo/bar'), Pattern('/run/borgmatic/lvm_snapshots/./foo/bar')),
(
Pattern('^/foo/bar', Pattern_type.INCLUDE, Pattern_style.REGULAR_EXPRESSION),
Pattern(
'^/run/borgmatic/lvm_snapshots/./foo/bar',
Pattern_type.INCLUDE,
Pattern_style.REGULAR_EXPRESSION,
),
),
(
Pattern('/foo/bar', Pattern_type.INCLUDE, Pattern_style.REGULAR_EXPRESSION),
Pattern(
'/run/borgmatic/lvm_snapshots/./foo/bar',
Pattern_type.INCLUDE,
Pattern_style.REGULAR_EXPRESSION,
),
),
(Pattern('/foo'), Pattern('/run/borgmatic/lvm_snapshots/./foo')),
(Pattern('/'), Pattern('/run/borgmatic/lvm_snapshots/./')),
),
)
def test_make_borg_snapshot_pattern_includes_slashdot_hack_and_stripped_pattern_path(
pattern, expected_pattern
):
assert module.make_borg_snapshot_pattern(pattern, '/run/borgmatic') == expected_pattern
def test_dump_data_sources_snapshots_and_mounts_and_updates_patterns():
config = {'lvm': {}}
patterns = [Pattern('/mnt/lvolume1/subdir'), Pattern('/mnt/lvolume2')]
@@ -175,6 +209,12 @@ def test_dump_data_sources_snapshots_and_mounts_and_updates_patterns():
flexmock(module).should_receive('mount_snapshot').with_args(
'mount', '/dev/lvolume2_snap', '/run/borgmatic/lvm_snapshots/mnt/lvolume2'
).once()
flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
Pattern('/mnt/lvolume1/subdir'), '/run/borgmatic'
).and_return(Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume1/subdir'))
flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
Pattern('/mnt/lvolume2'), '/run/borgmatic'
).and_return(Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume2'))
assert (
module.dump_data_sources(
@@ -266,6 +306,12 @@ def test_dump_data_sources_uses_snapshot_size_for_snapshot():
flexmock(module).should_receive('mount_snapshot').with_args(
'mount', '/dev/lvolume2_snap', '/run/borgmatic/lvm_snapshots/mnt/lvolume2'
).once()
flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
Pattern('/mnt/lvolume1/subdir'), '/run/borgmatic'
).and_return(Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume1/subdir'))
flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
Pattern('/mnt/lvolume2'), '/run/borgmatic'
).and_return(Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume2'))
assert (
module.dump_data_sources(
@@ -341,6 +387,12 @@ def test_dump_data_sources_uses_custom_commands():
flexmock(module).should_receive('mount_snapshot').with_args(
'/usr/local/bin/mount', '/dev/lvolume2_snap', '/run/borgmatic/lvm_snapshots/mnt/lvolume2'
).once()
flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
Pattern('/mnt/lvolume1/subdir'), '/run/borgmatic'
).and_return(Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume1/subdir'))
flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
Pattern('/mnt/lvolume2'), '/run/borgmatic'
).and_return(Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume2'))
assert (
module.dump_data_sources(
@@ -455,6 +507,12 @@ def test_dump_data_sources_ignores_mismatch_between_given_patterns_and_contained
flexmock(module).should_receive('mount_snapshot').with_args(
'mount', '/dev/lvolume2_snap', '/run/borgmatic/lvm_snapshots/mnt/lvolume2'
).once()
flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
Pattern('/mnt/lvolume1/subdir'), '/run/borgmatic'
).and_return(Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume1/subdir'))
flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
Pattern('/mnt/lvolume2'), '/run/borgmatic'
).and_return(Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume2'))
assert (
module.dump_data_sources(
@@ -13,6 +13,13 @@ def test_get_contained_patterns_with_self_candidate_returns_self():
assert candidates == {Pattern('/foo'), Pattern('/bar')}
def test_get_contained_patterns_with_self_candidate_and_caret_prefix_returns_self():
candidates = {Pattern('^/foo'), Pattern('^/mnt'), Pattern('^/bar')}
assert module.get_contained_patterns('/mnt', candidates) == (Pattern('^/mnt'),)
assert candidates == {Pattern('^/foo'), Pattern('^/bar')}
def test_get_contained_patterns_with_child_candidate_returns_child():
candidates = {Pattern('/foo'), Pattern('/mnt/subdir'), Pattern('/bar')}
+45 -4
View File
@@ -3,7 +3,7 @@ import os
import pytest
from flexmock import flexmock
from borgmatic.borg.pattern import Pattern
from borgmatic.borg.pattern import Pattern, Pattern_style, Pattern_type
from borgmatic.hooks.data_source import zfs as module
@@ -89,6 +89,40 @@ def test_get_all_dataset_mount_points_does_not_filter_datasets():
)
@pytest.mark.parametrize(
'pattern,expected_pattern',
(
(
Pattern('/foo/bar/baz'),
Pattern('/run/borgmatic/zfs_snapshots/./foo/bar/baz'),
),
(Pattern('/foo/bar'), Pattern('/run/borgmatic/zfs_snapshots/./foo/bar')),
(
Pattern('^/foo/bar', Pattern_type.INCLUDE, Pattern_style.REGULAR_EXPRESSION),
Pattern(
'^/run/borgmatic/zfs_snapshots/./foo/bar',
Pattern_type.INCLUDE,
Pattern_style.REGULAR_EXPRESSION,
),
),
(
Pattern('/foo/bar', Pattern_type.INCLUDE, Pattern_style.REGULAR_EXPRESSION),
Pattern(
'/run/borgmatic/zfs_snapshots/./foo/bar',
Pattern_type.INCLUDE,
Pattern_style.REGULAR_EXPRESSION,
),
),
(Pattern('/foo'), Pattern('/run/borgmatic/zfs_snapshots/./foo')),
(Pattern('/'), Pattern('/run/borgmatic/zfs_snapshots/./')),
),
)
def test_make_borg_snapshot_pattern_includes_slashdot_hack_and_stripped_pattern_path(
pattern, expected_pattern
):
assert module.make_borg_snapshot_pattern(pattern, '/run/borgmatic') == expected_pattern
def test_dump_data_sources_snapshots_and_mounts_and_updates_patterns():
flexmock(module).should_receive('get_datasets_to_backup').and_return(
(
@@ -111,6 +145,9 @@ def test_dump_data_sources_snapshots_and_mounts_and_updates_patterns():
full_snapshot_name,
module.os.path.normpath(snapshot_mount_path),
).once()
flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
Pattern('/mnt/dataset/subdir'), '/run/borgmatic'
).and_return(Pattern('/run/borgmatic/zfs_snapshots/./mnt/dataset/subdir'))
patterns = [Pattern('/mnt/dataset/subdir')]
assert (
@@ -174,6 +211,9 @@ def test_dump_data_sources_uses_custom_commands():
full_snapshot_name,
module.os.path.normpath(snapshot_mount_path),
).once()
flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
Pattern('/mnt/dataset/subdir'), '/run/borgmatic'
).and_return(Pattern('/run/borgmatic/zfs_snapshots/./mnt/dataset/subdir'))
patterns = [Pattern('/mnt/dataset/subdir')]
hook_config = {
'zfs_command': '/usr/local/bin/zfs',
@@ -246,9 +286,10 @@ def test_dump_data_sources_ignores_mismatch_between_given_patterns_and_contained
full_snapshot_name,
module.os.path.normpath(snapshot_mount_path),
).once()
patterns = [
Pattern('/hmm'),
]
flexmock(module).should_receive('make_borg_snapshot_pattern').with_args(
Pattern('/mnt/dataset/subdir'), '/run/borgmatic'
).and_return(Pattern('/run/borgmatic/zfs_snapshots/./mnt/dataset/subdir'))
patterns = [Pattern('/hmm')]
assert (
module.dump_data_sources(