| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794 | import osimport pytestfrom flexmock import flexmockfrom borgmatic.borg.pattern import Pattern, Pattern_source, Pattern_style, Pattern_typefrom borgmatic.hooks.data_source import zfs as moduledef test_get_datasets_to_backup_filters_datasets_by_patterns():    flexmock(module.borgmatic.execute).should_receive(        'execute_command_and_capture_output'    ).and_return(        'dataset\t/dataset\ton\t-\nother\t/other\ton\t-',    )    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).with_args('/dataset', object).and_return(        (            Pattern(                '/dataset',                Pattern_type.ROOT,                source=Pattern_source.CONFIG,            ),        )    )    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).with_args('/other', object).and_return(())    assert module.get_datasets_to_backup(        'zfs',        patterns=(            Pattern(                '/foo',                Pattern_type.ROOT,                source=Pattern_source.CONFIG,            ),            Pattern(                '/dataset',                Pattern_type.ROOT,                source=Pattern_source.CONFIG,            ),            Pattern(                '/bar',                Pattern_type.ROOT,                source=Pattern_source.CONFIG,            ),        ),    ) == (        module.Dataset(            name='dataset',            mount_point='/dataset',            contained_patterns=(                Pattern(                    '/dataset',                    Pattern_type.ROOT,                    source=Pattern_source.CONFIG,                ),            ),        ),    )def test_get_datasets_to_backup_skips_non_root_patterns():    flexmock(module.borgmatic.execute).should_receive(        'execute_command_and_capture_output'    ).and_return(        'dataset\t/dataset\ton\t-\nother\t/other\ton\t-',    )    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).with_args('/dataset', object).and_return(        (            Pattern(                '/dataset',                Pattern_type.EXCLUDE,                source=Pattern_source.CONFIG,            ),        )    )    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).with_args('/other', object).and_return(())    assert (        module.get_datasets_to_backup(            'zfs',            patterns=(                Pattern(                    '/foo',                    Pattern_type.ROOT,                    source=Pattern_source.CONFIG,                ),                Pattern(                    '/dataset',                    Pattern_type.EXCLUDE,                    source=Pattern_source.CONFIG,                ),                Pattern(                    '/bar',                    Pattern_type.ROOT,                    source=Pattern_source.CONFIG,                ),            ),        )        == ()    )def test_get_datasets_to_backup_skips_non_config_patterns():    flexmock(module.borgmatic.execute).should_receive(        'execute_command_and_capture_output'    ).and_return(        'dataset\t/dataset\ton\t-\nother\t/other\ton\t-',    )    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).with_args('/dataset', object).and_return(        (            Pattern(                '/dataset',                Pattern_type.ROOT,                source=Pattern_source.HOOK,            ),        )    )    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).with_args('/other', object).and_return(())    assert (        module.get_datasets_to_backup(            'zfs',            patterns=(                Pattern(                    '/foo',                    Pattern_type.ROOT,                    source=Pattern_source.CONFIG,                ),                Pattern(                    '/dataset',                    Pattern_type.ROOT,                    source=Pattern_source.HOOK,                ),                Pattern(                    '/bar',                    Pattern_type.ROOT,                    source=Pattern_source.CONFIG,                ),            ),        )        == ()    )def test_get_datasets_to_backup_filters_datasets_by_user_property():    flexmock(module.borgmatic.execute).should_receive(        'execute_command_and_capture_output'    ).and_return(        'dataset\t/dataset\ton\tauto\nother\t/other\ton\t-',    )    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).with_args('/dataset', object).and_return(())    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).with_args('/other', object).and_return(())    assert module.get_datasets_to_backup(        'zfs',        patterns=(Pattern('/foo'), Pattern('/bar')),    ) == (        module.Dataset(            name='dataset',            mount_point='/dataset',            auto_backup=True,            contained_patterns=(Pattern('/dataset', source=Pattern_source.HOOK),),        ),    )def test_get_datasets_to_backup_filters_datasets_by_canmount_property():    flexmock(module.borgmatic.execute).should_receive(        'execute_command_and_capture_output'    ).and_return(        'dataset\t/dataset\toff\t-\nother\t/other\ton\t-',    )    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).with_args('/dataset', object).and_return((Pattern('/dataset'),))    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).with_args('/other', object).and_return(())    assert (        module.get_datasets_to_backup(            'zfs',            patterns=(                Pattern('/foo'),                Pattern('/dataset'),                Pattern('/bar'),            ),        )        == ()    )def test_get_datasets_to_backup_with_invalid_list_output_raises():    flexmock(module.borgmatic.execute).should_receive(        'execute_command_and_capture_output'    ).and_return(        'dataset',    )    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).never()    with pytest.raises(ValueError, match='zfs'):        module.get_datasets_to_backup('zfs', patterns=(Pattern('/foo'), Pattern('/bar')))def test_get_all_dataset_mount_points_omits_none():    flexmock(module.borgmatic.execute).should_receive(        'execute_command_and_capture_output'    ).and_return(        '/dataset\nnone\n/other',    )    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).and_return((Pattern('/dataset'),))    assert module.get_all_dataset_mount_points('zfs') == (        ('/dataset'),        ('/other'),    )def test_get_all_dataset_mount_points_omits_duplicates():    flexmock(module.borgmatic.execute).should_receive(        'execute_command_and_capture_output'    ).and_return(        '/dataset\n/other\n/dataset\n/other',    )    flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(        'get_contained_patterns'    ).and_return((Pattern('/dataset'),))    assert module.get_all_dataset_mount_points('zfs') == (        ('/dataset'),        ('/other'),    )@pytest.mark.parametrize(    'pattern,expected_pattern',    (        (            Pattern('/foo/bar/baz'),            Pattern('/run/borgmatic/zfs_snapshots/b33f/./foo/bar/baz'),        ),        (Pattern('/foo/bar'), Pattern('/run/borgmatic/zfs_snapshots/b33f/./foo/bar')),        (            Pattern('^/foo/bar', Pattern_type.INCLUDE, Pattern_style.REGULAR_EXPRESSION),            Pattern(                '^/run/borgmatic/zfs_snapshots/b33f/./foo/bar',                Pattern_type.INCLUDE,                Pattern_style.REGULAR_EXPRESSION,            ),        ),        (            Pattern('/foo/bar', Pattern_type.INCLUDE, Pattern_style.REGULAR_EXPRESSION),            Pattern(                '/run/borgmatic/zfs_snapshots/b33f/./foo/bar',                Pattern_type.INCLUDE,                Pattern_style.REGULAR_EXPRESSION,            ),        ),        (Pattern('/foo'), Pattern('/run/borgmatic/zfs_snapshots/b33f/./foo')),        (Pattern('/'), Pattern('/run/borgmatic/zfs_snapshots/b33f/./')),    ),)def test_make_borg_snapshot_pattern_includes_slashdot_hack_and_stripped_pattern_path(    pattern, expected_pattern):    flexmock(module.hashlib).should_receive('shake_256').and_return(        flexmock(hexdigest=lambda length: 'b33f')    )    assert (        module.make_borg_snapshot_pattern(            pattern, flexmock(mount_point='/something'), '/run/borgmatic'        )        == expected_pattern    )def test_dump_data_sources_snapshots_and_mounts_and_updates_patterns():    dataset = flexmock(        name='dataset',        mount_point='/mnt/dataset',        contained_patterns=(Pattern('/mnt/dataset/subdir'),),    )    flexmock(module).should_receive('get_datasets_to_backup').and_return((dataset,))    flexmock(module.os).should_receive('getpid').and_return(1234)    full_snapshot_name = 'dataset@borgmatic-1234'    flexmock(module).should_receive('snapshot_dataset').with_args(        'zfs',        full_snapshot_name,    ).once()    flexmock(module.hashlib).should_receive('shake_256').and_return(        flexmock(hexdigest=lambda length: 'b33f')    )    snapshot_mount_path = '/run/borgmatic/zfs_snapshots/b33f/./mnt/dataset'    flexmock(module).should_receive('mount_snapshot').with_args(        'mount',        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'), dataset, '/run/borgmatic'    ).and_return(Pattern('/run/borgmatic/zfs_snapshots/b33f/./mnt/dataset/subdir'))    patterns = [Pattern('/mnt/dataset/subdir')]    assert (        module.dump_data_sources(            hook_config={},            config={'source_directories': '/mnt/dataset', 'zfs': {}},            config_paths=('test.yaml',),            borgmatic_runtime_directory='/run/borgmatic',            patterns=patterns,            dry_run=False,        )        == []    )    assert patterns == [Pattern(os.path.join(snapshot_mount_path, 'subdir'))]def test_dump_data_sources_with_no_datasets_skips_snapshots():    flexmock(module).should_receive('get_datasets_to_backup').and_return(())    flexmock(module.os).should_receive('getpid').and_return(1234)    flexmock(module).should_receive('snapshot_dataset').never()    flexmock(module).should_receive('mount_snapshot').never()    patterns = [Pattern('/mnt/dataset')]    assert (        module.dump_data_sources(            hook_config={},            config={'patterns': flexmock(), 'zfs': {}},            config_paths=('test.yaml',),            borgmatic_runtime_directory='/run/borgmatic',            patterns=patterns,            dry_run=False,        )        == []    )    assert patterns == [Pattern('/mnt/dataset')]def test_dump_data_sources_uses_custom_commands():    dataset = flexmock(        name='dataset',        mount_point='/mnt/dataset',        contained_patterns=(Pattern('/mnt/dataset/subdir'),),    )    flexmock(module).should_receive('get_datasets_to_backup').and_return((dataset,))    flexmock(module.os).should_receive('getpid').and_return(1234)    full_snapshot_name = 'dataset@borgmatic-1234'    flexmock(module).should_receive('snapshot_dataset').with_args(        '/usr/local/bin/zfs',        full_snapshot_name,    ).once()    flexmock(module.hashlib).should_receive('shake_256').and_return(        flexmock(hexdigest=lambda length: 'b33f')    )    snapshot_mount_path = '/run/borgmatic/zfs_snapshots/b33f/./mnt/dataset'    flexmock(module).should_receive('mount_snapshot').with_args(        '/usr/local/bin/mount',        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'), dataset, '/run/borgmatic'    ).and_return(Pattern('/run/borgmatic/zfs_snapshots/b33f/./mnt/dataset/subdir'))    patterns = [Pattern('/mnt/dataset/subdir')]    hook_config = {        'zfs_command': '/usr/local/bin/zfs',        'mount_command': '/usr/local/bin/mount',    }    assert (        module.dump_data_sources(            hook_config=hook_config,            config={                'patterns': flexmock(),                'zfs': hook_config,            },            config_paths=('test.yaml',),            borgmatic_runtime_directory='/run/borgmatic',            patterns=patterns,            dry_run=False,        )        == []    )    assert patterns == [Pattern(os.path.join(snapshot_mount_path, 'subdir'))]def test_dump_data_sources_with_dry_run_skips_commands_and_does_not_touch_patterns():    flexmock(module).should_receive('get_datasets_to_backup').and_return(        (flexmock(name='dataset', mount_point='/mnt/dataset'),)    )    flexmock(module.os).should_receive('getpid').and_return(1234)    flexmock(module).should_receive('snapshot_dataset').never()    flexmock(module).should_receive('mount_snapshot').never()    patterns = [Pattern('/mnt/dataset')]    assert (        module.dump_data_sources(            hook_config={},            config={'patterns': ('R /mnt/dataset',), 'zfs': {}},            config_paths=('test.yaml',),            borgmatic_runtime_directory='/run/borgmatic',            patterns=patterns,            dry_run=True,        )        == []    )    assert patterns == [Pattern('/mnt/dataset')]def test_dump_data_sources_ignores_mismatch_between_given_patterns_and_contained_patterns():    dataset = flexmock(        name='dataset',        mount_point='/mnt/dataset',        contained_patterns=(Pattern('/mnt/dataset/subdir'),),    )    flexmock(module).should_receive('get_datasets_to_backup').and_return((dataset,))    flexmock(module.os).should_receive('getpid').and_return(1234)    full_snapshot_name = 'dataset@borgmatic-1234'    flexmock(module).should_receive('snapshot_dataset').with_args(        'zfs',        full_snapshot_name,    ).once()    flexmock(module.hashlib).should_receive('shake_256').and_return(        flexmock(hexdigest=lambda length: 'b33f')    )    snapshot_mount_path = '/run/borgmatic/zfs_snapshots/b33f/./mnt/dataset'    flexmock(module).should_receive('mount_snapshot').with_args(        'mount',        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'), dataset, '/run/borgmatic'    ).and_return(Pattern('/run/borgmatic/zfs_snapshots/b33f/./mnt/dataset/subdir'))    patterns = [Pattern('/hmm')]    assert (        module.dump_data_sources(            hook_config={},            config={'patterns': ('R /mnt/dataset',), 'zfs': {}},            config_paths=('test.yaml',),            borgmatic_runtime_directory='/run/borgmatic',            patterns=patterns,            dry_run=False,        )        == []    )    assert patterns == [Pattern('/hmm'), Pattern(os.path.join(snapshot_mount_path, 'subdir'))]def test_get_all_snapshots_parses_list_output():    flexmock(module.borgmatic.execute).should_receive(        'execute_command_and_capture_output'    ).and_return(        'dataset1@borgmatic-1234\ndataset2@borgmatic-4567',    )    assert module.get_all_snapshots('zfs') == ('dataset1@borgmatic-1234', 'dataset2@borgmatic-4567')def test_remove_data_source_dumps_unmounts_and_destroys_snapshots():    flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))    flexmock(module.borgmatic.config.paths).should_receive(        'replace_temporary_subdirectory_with_glob'    ).and_return('/run/borgmatic')    flexmock(module.glob).should_receive('glob').replace_with(        lambda path: [path.replace('*', 'b33f')]    )    flexmock(module.os.path).should_receive('isdir').and_return(True)    flexmock(module.os).should_receive('listdir').and_return(['file.txt'])    flexmock(module.shutil).should_receive('rmtree')    flexmock(module).should_receive('unmount_snapshot').with_args(        'umount', '/run/borgmatic/zfs_snapshots/b33f/mnt/dataset'    ).once()    flexmock(module).should_receive('get_all_snapshots').and_return(        ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')    )    flexmock(module).should_receive('destroy_snapshot').with_args(        'zfs', 'dataset@borgmatic-1234'    ).once()    module.remove_data_source_dumps(        hook_config={},        config={'source_directories': '/mnt/dataset', 'zfs': {}},        borgmatic_runtime_directory='/run/borgmatic',        dry_run=False,    )def test_remove_data_source_dumps_use_custom_commands():    flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))    flexmock(module.borgmatic.config.paths).should_receive(        'replace_temporary_subdirectory_with_glob'    ).and_return('/run/borgmatic')    flexmock(module.glob).should_receive('glob').replace_with(        lambda path: [path.replace('*', 'b33f')]    )    flexmock(module.os.path).should_receive('isdir').and_return(True)    flexmock(module.os).should_receive('listdir').and_return(['file.txt'])    flexmock(module.shutil).should_receive('rmtree')    flexmock(module).should_receive('unmount_snapshot').with_args(        '/usr/local/bin/umount', '/run/borgmatic/zfs_snapshots/b33f/mnt/dataset'    ).once()    flexmock(module).should_receive('get_all_snapshots').and_return(        ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')    )    flexmock(module).should_receive('destroy_snapshot').with_args(        '/usr/local/bin/zfs', 'dataset@borgmatic-1234'    ).once()    hook_config = {'zfs_command': '/usr/local/bin/zfs', 'umount_command': '/usr/local/bin/umount'}    module.remove_data_source_dumps(        hook_config=hook_config,        config={'source_directories': '/mnt/dataset', 'zfs': hook_config},        borgmatic_runtime_directory='/run/borgmatic',        dry_run=False,    )def test_remove_data_source_dumps_bails_for_missing_hook_configuration():    flexmock(module).should_receive('get_all_dataset_mount_points').never()    flexmock(module.borgmatic.config.paths).should_receive(        'replace_temporary_subdirectory_with_glob'    ).never()    module.remove_data_source_dumps(        hook_config=None,        config={'source_directories': '/mnt/dataset'},        borgmatic_runtime_directory='/run/borgmatic',        dry_run=False,    )def test_remove_data_source_dumps_bails_for_missing_zfs_command():    flexmock(module).should_receive('get_all_dataset_mount_points').and_raise(FileNotFoundError)    flexmock(module.borgmatic.config.paths).should_receive(        'replace_temporary_subdirectory_with_glob'    ).never()    hook_config = {'zfs_command': 'wtf'}    module.remove_data_source_dumps(        hook_config=hook_config,        config={'source_directories': '/mnt/dataset', 'zfs': hook_config},        borgmatic_runtime_directory='/run/borgmatic',        dry_run=False,    )def test_remove_data_source_dumps_bails_for_zfs_command_error():    flexmock(module).should_receive('get_all_dataset_mount_points').and_raise(        module.subprocess.CalledProcessError(1, 'wtf')    )    flexmock(module.borgmatic.config.paths).should_receive(        'replace_temporary_subdirectory_with_glob'    ).never()    hook_config = {'zfs_command': 'wtf'}    module.remove_data_source_dumps(        hook_config=hook_config,        config={'source_directories': '/mnt/dataset', 'zfs': hook_config},        borgmatic_runtime_directory='/run/borgmatic',        dry_run=False,    )def test_remove_data_source_dumps_bails_for_missing_umount_command():    flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))    flexmock(module.borgmatic.config.paths).should_receive(        'replace_temporary_subdirectory_with_glob'    ).and_return('/run/borgmatic')    flexmock(module.glob).should_receive('glob').replace_with(        lambda path: [path.replace('*', 'b33f')]    )    flexmock(module.os.path).should_receive('isdir').and_return(True)    flexmock(module.os).should_receive('listdir').and_return(['file.txt'])    flexmock(module.shutil).should_receive('rmtree')    flexmock(module).should_receive('unmount_snapshot').with_args(        '/usr/local/bin/umount', '/run/borgmatic/zfs_snapshots/b33f/mnt/dataset'    ).and_raise(FileNotFoundError)    flexmock(module).should_receive('get_all_snapshots').never()    flexmock(module).should_receive('destroy_snapshot').never()    hook_config = {'zfs_command': '/usr/local/bin/zfs', 'umount_command': '/usr/local/bin/umount'}    module.remove_data_source_dumps(        hook_config=hook_config,        config={'source_directories': '/mnt/dataset', 'zfs': hook_config},        borgmatic_runtime_directory='/run/borgmatic',        dry_run=False,    )def test_remove_data_source_dumps_swallows_umount_command_error():    flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))    flexmock(module.borgmatic.config.paths).should_receive(        'replace_temporary_subdirectory_with_glob'    ).and_return('/run/borgmatic')    flexmock(module.glob).should_receive('glob').replace_with(        lambda path: [path.replace('*', 'b33f')]    )    flexmock(module.os.path).should_receive('isdir').and_return(True)    flexmock(module.os).should_receive('listdir').and_return(['file.txt'])    flexmock(module.shutil).should_receive('rmtree')    flexmock(module).should_receive('unmount_snapshot').with_args(        '/usr/local/bin/umount', '/run/borgmatic/zfs_snapshots/b33f/mnt/dataset'    ).and_raise(module.subprocess.CalledProcessError(1, 'wtf'))    flexmock(module).should_receive('get_all_snapshots').and_return(        ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')    )    flexmock(module).should_receive('destroy_snapshot').with_args(        '/usr/local/bin/zfs', 'dataset@borgmatic-1234'    ).once()    hook_config = {'zfs_command': '/usr/local/bin/zfs', 'umount_command': '/usr/local/bin/umount'}    module.remove_data_source_dumps(        hook_config=hook_config,        config={'source_directories': '/mnt/dataset', 'zfs': hook_config},        borgmatic_runtime_directory='/run/borgmatic',        dry_run=False,    )def test_remove_data_source_dumps_skips_unmount_snapshot_directories_that_are_not_actually_directories():    flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))    flexmock(module.borgmatic.config.paths).should_receive(        'replace_temporary_subdirectory_with_glob'    ).and_return('/run/borgmatic')    flexmock(module.glob).should_receive('glob').replace_with(        lambda path: [path.replace('*', 'b33f')]    )    flexmock(module.os.path).should_receive('isdir').and_return(False)    flexmock(module.shutil).should_receive('rmtree').never()    flexmock(module).should_receive('unmount_snapshot').never()    flexmock(module).should_receive('get_all_snapshots').and_return(        ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')    )    flexmock(module).should_receive('destroy_snapshot').with_args(        'zfs', 'dataset@borgmatic-1234'    ).once()    module.remove_data_source_dumps(        hook_config={},        config={'source_directories': '/mnt/dataset', 'zfs': {}},        borgmatic_runtime_directory='/run/borgmatic',        dry_run=False,    )def test_remove_data_source_dumps_skips_unmount_snapshot_mount_paths_that_are_not_actually_directories():    flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))    flexmock(module.borgmatic.config.paths).should_receive(        'replace_temporary_subdirectory_with_glob'    ).and_return('/run/borgmatic')    flexmock(module.glob).should_receive('glob').replace_with(        lambda path: [path.replace('*', 'b33f')]    )    flexmock(module.os.path).should_receive('isdir').with_args(        '/run/borgmatic/zfs_snapshots/b33f'    ).and_return(True)    flexmock(module.os.path).should_receive('isdir').with_args(        '/run/borgmatic/zfs_snapshots/b33f/mnt/dataset'    ).and_return(False)    flexmock(module.os).should_receive('listdir').and_return(['file.txt'])    flexmock(module.shutil).should_receive('rmtree')    flexmock(module).should_receive('unmount_snapshot').never()    flexmock(module).should_receive('get_all_snapshots').and_return(        ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')    )    flexmock(module).should_receive('destroy_snapshot').with_args(        'zfs', 'dataset@borgmatic-1234'    ).once()    module.remove_data_source_dumps(        hook_config={},        config={'source_directories': '/mnt/dataset', 'zfs': {}},        borgmatic_runtime_directory='/run/borgmatic',        dry_run=False,    )def test_remove_data_source_dumps_skips_unmount_snapshot_mount_paths_that_are_empty():    flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))    flexmock(module.borgmatic.config.paths).should_receive(        'replace_temporary_subdirectory_with_glob'    ).and_return('/run/borgmatic')    flexmock(module.glob).should_receive('glob').replace_with(        lambda path: [path.replace('*', 'b33f')]    )    flexmock(module.os.path).should_receive('isdir').with_args(        '/run/borgmatic/zfs_snapshots/b33f'    ).and_return(True)    flexmock(module.os.path).should_receive('isdir').with_args(        '/run/borgmatic/zfs_snapshots/b33f/mnt/dataset'    ).and_return(True)    flexmock(module.os).should_receive('listdir').with_args(        '/run/borgmatic/zfs_snapshots/b33f/mnt/dataset'    ).and_return([])    flexmock(module.shutil).should_receive('rmtree')    flexmock(module).should_receive('unmount_snapshot').never()    flexmock(module).should_receive('get_all_snapshots').and_return(        ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')    )    flexmock(module).should_receive('destroy_snapshot').with_args(        'zfs', 'dataset@borgmatic-1234'    ).once()    module.remove_data_source_dumps(        hook_config={},        config={'source_directories': '/mnt/dataset', 'zfs': {}},        borgmatic_runtime_directory='/run/borgmatic',        dry_run=False,    )def test_remove_data_source_dumps_skips_unmount_snapshot_mount_paths_after_rmtree_succeeds():    flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))    flexmock(module.borgmatic.config.paths).should_receive(        'replace_temporary_subdirectory_with_glob'    ).and_return('/run/borgmatic')    flexmock(module.glob).should_receive('glob').replace_with(        lambda path: [path.replace('*', 'b33f')]    )    flexmock(module.os.path).should_receive('isdir').with_args(        '/run/borgmatic/zfs_snapshots/b33f'    ).and_return(True)    flexmock(module.os.path).should_receive('isdir').with_args(        '/run/borgmatic/zfs_snapshots/b33f/mnt/dataset'    ).and_return(True).and_return(False)    flexmock(module.os).should_receive('listdir').and_return(['file.txt'])    flexmock(module.shutil).should_receive('rmtree')    flexmock(module).should_receive('unmount_snapshot').never()    flexmock(module).should_receive('get_all_snapshots').and_return(        ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')    )    flexmock(module).should_receive('destroy_snapshot').with_args(        'zfs', 'dataset@borgmatic-1234'    ).once()    module.remove_data_source_dumps(        hook_config={},        config={'source_directories': '/mnt/dataset', 'zfs': {}},        borgmatic_runtime_directory='/run/borgmatic',        dry_run=False,    )def test_remove_data_source_dumps_with_dry_run_skips_unmount_and_destroy():    flexmock(module).should_receive('get_all_dataset_mount_points').and_return(('/mnt/dataset',))    flexmock(module.borgmatic.config.paths).should_receive(        'replace_temporary_subdirectory_with_glob'    ).and_return('/run/borgmatic')    flexmock(module.glob).should_receive('glob').replace_with(        lambda path: [path.replace('*', 'b33f')]    )    flexmock(module.os.path).should_receive('isdir').and_return(True)    flexmock(module.os).should_receive('listdir').and_return(['file.txt'])    flexmock(module.shutil).should_receive('rmtree').never()    flexmock(module).should_receive('unmount_snapshot').never()    flexmock(module).should_receive('get_all_snapshots').and_return(        ('dataset@borgmatic-1234', 'dataset@other', 'other@other', 'invalid')    )    flexmock(module).should_receive('destroy_snapshot').never()    module.remove_data_source_dumps(        hook_config={},        config={'source_directories': '/mnt/dataset', 'zfs': {}},        borgmatic_runtime_directory='/run/borgmatic',        dry_run=True,    )
 |