123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152 |
- import pytest
- from flexmock import flexmock
- from borgmatic.borg.pattern import Pattern, Pattern_style, Pattern_type
- from borgmatic.hooks.data_source import lvm as module
- def test_get_logical_volumes_filters_by_patterns():
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return(
- '''
- {
- "blockdevices": [
- {
- "name": "vgroup-notmounted",
- "path": "/dev/mapper/vgroup-notmounted",
- "mountpoint": null,
- "type": "lvm"
- }, {
- "name": "vgroup-lvolume",
- "path": "/dev/mapper/vgroup-lvolume",
- "mountpoint": "/mnt/lvolume",
- "type": "lvm"
- }, {
- "name": "vgroup-other",
- "path": "/dev/mapper/vgroup-other",
- "mountpoint": "/mnt/other",
- "type": "lvm"
- }, {
- "name": "vgroup-notlvm",
- "path": "/dev/mapper/vgroup-notlvm",
- "mountpoint": "/mnt/notlvm",
- "type": "notlvm"
- }
- ]
- }
- '''
- )
- contained = {Pattern('/mnt/lvolume'), Pattern('/mnt/lvolume/subdir')}
- flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
- 'get_contained_patterns'
- ).with_args(None, contained).never()
- flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
- 'get_contained_patterns'
- ).with_args('/mnt/lvolume', contained).and_return(
- (Pattern('/mnt/lvolume'), Pattern('/mnt/lvolume/subdir'))
- )
- flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
- 'get_contained_patterns'
- ).with_args('/mnt/other', contained).and_return(())
- flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
- 'get_contained_patterns'
- ).with_args('/mnt/notlvm', contained).never()
- assert module.get_logical_volumes(
- 'lsblk', patterns=(Pattern('/mnt/lvolume'), Pattern('/mnt/lvolume/subdir'))
- ) == (
- module.Logical_volume(
- name='vgroup-lvolume',
- device_path='/dev/mapper/vgroup-lvolume',
- mount_point='/mnt/lvolume',
- contained_patterns=(Pattern('/mnt/lvolume'), Pattern('/mnt/lvolume/subdir')),
- ),
- )
- def test_get_logical_volumes_with_invalid_lsblk_json_errors():
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return('{')
- flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
- 'get_contained_patterns'
- ).never()
- with pytest.raises(ValueError):
- module.get_logical_volumes(
- 'lsblk', patterns=(Pattern('/mnt/lvolume'), Pattern('/mnt/lvolume/subdir'))
- )
- def test_get_logical_volumes_with_lsblk_json_missing_keys_errors():
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return('{"block_devices": [{}]}')
- flexmock(module.borgmatic.hooks.data_source.snapshot).should_receive(
- 'get_contained_patterns'
- ).never()
- with pytest.raises(ValueError):
- module.get_logical_volumes(
- 'lsblk', patterns=(Pattern('/mnt/lvolume'), Pattern('/mnt/lvolume/subdir'))
- )
- def test_snapshot_logical_volume_with_percentage_snapshot_name_uses_lvcreate_extents_flag():
- flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
- (
- 'lvcreate',
- '--snapshot',
- '--extents',
- '10%ORIGIN',
- '--permission',
- 'r',
- '--name',
- 'snap',
- '/dev/snap',
- ),
- output_log_level=object,
- )
- module.snapshot_logical_volume('lvcreate', 'snap', '/dev/snap', '10%ORIGIN')
- def test_snapshot_logical_volume_with_non_percentage_snapshot_name_uses_lvcreate_size_flag():
- flexmock(module.borgmatic.execute).should_receive('execute_command').with_args(
- (
- 'lvcreate',
- '--snapshot',
- '--size',
- '10TB',
- '--permission',
- 'r',
- '--name',
- 'snap',
- '/dev/snap',
- ),
- output_log_level=object,
- )
- 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')]
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- flexmock(module.os).should_receive('getpid').and_return(1234)
- flexmock(module).should_receive('snapshot_logical_volume').with_args(
- 'lvcreate', 'lvolume1_borgmatic-1234', '/dev/lvolume1', module.DEFAULT_SNAPSHOT_SIZE
- ).once()
- flexmock(module).should_receive('snapshot_logical_volume').with_args(
- 'lvcreate', 'lvolume2_borgmatic-1234', '/dev/lvolume2', module.DEFAULT_SNAPSHOT_SIZE
- ).once()
- flexmock(module).should_receive('get_snapshots').with_args(
- 'lvs', snapshot_name='lvolume1_borgmatic-1234'
- ).and_return(
- (module.Snapshot(name='lvolume1_borgmatic-1234', device_path='/dev/lvolume1_snap'),)
- )
- flexmock(module).should_receive('get_snapshots').with_args(
- 'lvs', snapshot_name='lvolume2_borgmatic-1234'
- ).and_return(
- (module.Snapshot(name='lvolume2_borgmatic-1234', device_path='/dev/lvolume2_snap'),)
- )
- flexmock(module).should_receive('mount_snapshot').with_args(
- 'mount', '/dev/lvolume1_snap', '/run/borgmatic/lvm_snapshots/mnt/lvolume1'
- ).once()
- 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(
- hook_config=config['lvm'],
- config=config,
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=patterns,
- dry_run=False,
- )
- == []
- )
- assert patterns == [
- Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume1/subdir'),
- Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume2'),
- ]
- def test_dump_data_sources_with_no_logical_volumes_skips_snapshots():
- config = {'lvm': {}}
- patterns = [Pattern('/mnt/lvolume1/subdir'), Pattern('/mnt/lvolume2')]
- flexmock(module).should_receive('get_logical_volumes').and_return(())
- flexmock(module).should_receive('snapshot_logical_volume').never()
- flexmock(module).should_receive('mount_snapshot').never()
- assert (
- module.dump_data_sources(
- hook_config=config['lvm'],
- config=config,
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=patterns,
- dry_run=False,
- )
- == []
- )
- assert patterns == [Pattern('/mnt/lvolume1/subdir'), Pattern('/mnt/lvolume2')]
- def test_dump_data_sources_uses_snapshot_size_for_snapshot():
- config = {'lvm': {'snapshot_size': '1000PB'}}
- patterns = [Pattern('/mnt/lvolume1/subdir'), Pattern('/mnt/lvolume2')]
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- flexmock(module.os).should_receive('getpid').and_return(1234)
- flexmock(module).should_receive('snapshot_logical_volume').with_args(
- 'lvcreate',
- 'lvolume1_borgmatic-1234',
- '/dev/lvolume1',
- '1000PB',
- ).once()
- flexmock(module).should_receive('snapshot_logical_volume').with_args(
- 'lvcreate',
- 'lvolume2_borgmatic-1234',
- '/dev/lvolume2',
- '1000PB',
- ).once()
- flexmock(module).should_receive('get_snapshots').with_args(
- 'lvs', snapshot_name='lvolume1_borgmatic-1234'
- ).and_return(
- (module.Snapshot(name='lvolume1_borgmatic-1234', device_path='/dev/lvolume1_snap'),)
- )
- flexmock(module).should_receive('get_snapshots').with_args(
- 'lvs', snapshot_name='lvolume2_borgmatic-1234'
- ).and_return(
- (module.Snapshot(name='lvolume2_borgmatic-1234', device_path='/dev/lvolume2_snap'),)
- )
- flexmock(module).should_receive('mount_snapshot').with_args(
- 'mount', '/dev/lvolume1_snap', '/run/borgmatic/lvm_snapshots/mnt/lvolume1'
- ).once()
- 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(
- hook_config=config['lvm'],
- config=config,
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=patterns,
- dry_run=False,
- )
- == []
- )
- assert patterns == [
- Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume1/subdir'),
- Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume2'),
- ]
- def test_dump_data_sources_uses_custom_commands():
- config = {
- 'lvm': {
- 'lsblk_command': '/usr/local/bin/lsblk',
- 'lvcreate_command': '/usr/local/bin/lvcreate',
- 'lvs_command': '/usr/local/bin/lvs',
- 'mount_command': '/usr/local/bin/mount',
- },
- }
- patterns = [Pattern('/mnt/lvolume1/subdir'), Pattern('/mnt/lvolume2')]
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- flexmock(module.os).should_receive('getpid').and_return(1234)
- flexmock(module).should_receive('snapshot_logical_volume').with_args(
- '/usr/local/bin/lvcreate',
- 'lvolume1_borgmatic-1234',
- '/dev/lvolume1',
- module.DEFAULT_SNAPSHOT_SIZE,
- ).once()
- flexmock(module).should_receive('snapshot_logical_volume').with_args(
- '/usr/local/bin/lvcreate',
- 'lvolume2_borgmatic-1234',
- '/dev/lvolume2',
- module.DEFAULT_SNAPSHOT_SIZE,
- ).once()
- flexmock(module).should_receive('get_snapshots').with_args(
- '/usr/local/bin/lvs', snapshot_name='lvolume1_borgmatic-1234'
- ).and_return(
- (module.Snapshot(name='lvolume1_borgmatic-1234', device_path='/dev/lvolume1_snap'),)
- )
- flexmock(module).should_receive('get_snapshots').with_args(
- '/usr/local/bin/lvs', snapshot_name='lvolume2_borgmatic-1234'
- ).and_return(
- (module.Snapshot(name='lvolume2_borgmatic-1234', device_path='/dev/lvolume2_snap'),)
- )
- flexmock(module).should_receive('mount_snapshot').with_args(
- '/usr/local/bin/mount', '/dev/lvolume1_snap', '/run/borgmatic/lvm_snapshots/mnt/lvolume1'
- ).once()
- 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(
- hook_config=config['lvm'],
- config=config,
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=patterns,
- dry_run=False,
- )
- == []
- )
- assert patterns == [
- Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume1/subdir'),
- Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume2'),
- ]
- def test_dump_data_sources_with_dry_run_skips_snapshots_and_does_not_touch_patterns():
- config = {'lvm': {}}
- patterns = [Pattern('/mnt/lvolume1/subdir'), Pattern('/mnt/lvolume2')]
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- flexmock(module.os).should_receive('getpid').and_return(1234)
- flexmock(module).should_receive('snapshot_logical_volume').never()
- flexmock(module).should_receive('get_snapshots').with_args(
- 'lvs', snapshot_name='lvolume1_borgmatic-1234'
- ).and_return(
- (module.Snapshot(name='lvolume1_borgmatic-1234', device_path='/dev/lvolume1_snap'),)
- )
- flexmock(module).should_receive('get_snapshots').with_args(
- 'lvs', snapshot_name='lvolume2_borgmatic-1234'
- ).and_return(
- (module.Snapshot(name='lvolume2_borgmatic-1234', device_path='/dev/lvolume2_snap'),)
- )
- flexmock(module).should_receive('mount_snapshot').never()
- assert (
- module.dump_data_sources(
- hook_config=config['lvm'],
- config=config,
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=patterns,
- dry_run=True,
- )
- == []
- )
- assert patterns == [
- Pattern('/mnt/lvolume1/subdir'),
- Pattern('/mnt/lvolume2'),
- ]
- def test_dump_data_sources_ignores_mismatch_between_given_patterns_and_contained_patterns():
- config = {'lvm': {}}
- patterns = [Pattern('/hmm')]
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- flexmock(module.os).should_receive('getpid').and_return(1234)
- flexmock(module).should_receive('snapshot_logical_volume').with_args(
- 'lvcreate', 'lvolume1_borgmatic-1234', '/dev/lvolume1', module.DEFAULT_SNAPSHOT_SIZE
- ).once()
- flexmock(module).should_receive('snapshot_logical_volume').with_args(
- 'lvcreate', 'lvolume2_borgmatic-1234', '/dev/lvolume2', module.DEFAULT_SNAPSHOT_SIZE
- ).once()
- flexmock(module).should_receive('get_snapshots').with_args(
- 'lvs', snapshot_name='lvolume1_borgmatic-1234'
- ).and_return(
- (module.Snapshot(name='lvolume1_borgmatic-1234', device_path='/dev/lvolume1_snap'),)
- )
- flexmock(module).should_receive('get_snapshots').with_args(
- 'lvs', snapshot_name='lvolume2_borgmatic-1234'
- ).and_return(
- (module.Snapshot(name='lvolume2_borgmatic-1234', device_path='/dev/lvolume2_snap'),)
- )
- flexmock(module).should_receive('mount_snapshot').with_args(
- 'mount', '/dev/lvolume1_snap', '/run/borgmatic/lvm_snapshots/mnt/lvolume1'
- ).once()
- 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(
- hook_config=config['lvm'],
- config=config,
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=patterns,
- dry_run=False,
- )
- == []
- )
- assert patterns == [
- Pattern('/hmm'),
- Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume1/subdir'),
- Pattern('/run/borgmatic/lvm_snapshots/./mnt/lvolume2'),
- ]
- def test_dump_data_sources_with_missing_snapshot_errors():
- config = {'lvm': {}}
- patterns = [Pattern('/mnt/lvolume1/subdir'), Pattern('/mnt/lvolume2')]
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- flexmock(module.os).should_receive('getpid').and_return(1234)
- flexmock(module).should_receive('snapshot_logical_volume').with_args(
- 'lvcreate', 'lvolume1_borgmatic-1234', '/dev/lvolume1', module.DEFAULT_SNAPSHOT_SIZE
- ).once()
- flexmock(module).should_receive('snapshot_logical_volume').with_args(
- 'lvcreate', 'lvolume2_borgmatic-1234', '/dev/lvolume2', module.DEFAULT_SNAPSHOT_SIZE
- ).never()
- flexmock(module).should_receive('get_snapshots').with_args(
- 'lvs', snapshot_name='lvolume1_borgmatic-1234'
- ).and_return(())
- flexmock(module).should_receive('get_snapshots').with_args(
- 'lvs', snapshot_name='lvolume2_borgmatic-1234'
- ).never()
- flexmock(module).should_receive('mount_snapshot').never()
- with pytest.raises(ValueError):
- module.dump_data_sources(
- hook_config=config['lvm'],
- config=config,
- config_paths=('test.yaml',),
- borgmatic_runtime_directory='/run/borgmatic',
- patterns=patterns,
- dry_run=False,
- )
- def test_get_snapshots_lists_all_snapshots():
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return(
- '''
- {
- "report": [
- {
- "lv": [
- {"lv_name": "snap1", "lv_path": "/dev/snap1"},
- {"lv_name": "snap2", "lv_path": "/dev/snap2"}
- ]
- }
- ],
- "log": [
- ]
- }
- '''
- )
- assert module.get_snapshots('lvs') == (
- module.Snapshot('snap1', '/dev/snap1'),
- module.Snapshot('snap2', '/dev/snap2'),
- )
- def test_get_snapshots_with_snapshot_name_lists_just_that_snapshot():
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return(
- '''
- {
- "report": [
- {
- "lv": [
- {"lv_name": "snap1", "lv_path": "/dev/snap1"},
- {"lv_name": "snap2", "lv_path": "/dev/snap2"}
- ]
- }
- ],
- "log": [
- ]
- }
- '''
- )
- assert module.get_snapshots('lvs', snapshot_name='snap2') == (
- module.Snapshot('snap2', '/dev/snap2'),
- )
- def test_get_snapshots_with_invalid_lvs_json_errors():
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return('{')
- with pytest.raises(ValueError):
- assert module.get_snapshots('lvs')
- def test_get_snapshots_with_lvs_json_missing_report_errors():
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return(
- '''
- {
- "report": [],
- "log": [
- ]
- }
- '''
- )
- with pytest.raises(ValueError):
- assert module.get_snapshots('lvs')
- def test_get_snapshots_with_lvs_json_missing_keys_errors():
- flexmock(module.borgmatic.execute).should_receive(
- 'execute_command_and_capture_output'
- ).and_return(
- '''
- {
- "report": [
- {
- "lv": [
- {}
- ]
- }
- ],
- "log": [
- ]
- }
- '''
- )
- with pytest.raises(ValueError):
- assert module.get_snapshots('lvs')
- def test_remove_data_source_dumps_unmounts_and_remove_snapshots():
- config = {'lvm': {}}
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- 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])
- flexmock(module.os.path).should_receive('isdir').and_return(True)
- flexmock(module.shutil).should_receive('rmtree')
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume1',
- ).once()
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume2',
- ).once()
- flexmock(module).should_receive('get_snapshots').and_return(
- (
- module.Snapshot('lvolume1_borgmatic-1234', '/dev/lvolume1'),
- module.Snapshot('lvolume2_borgmatic-1234', '/dev/lvolume2'),
- module.Snapshot('nonborgmatic', '/dev/nonborgmatic'),
- ),
- )
- flexmock(module).should_receive('remove_snapshot').with_args('lvremove', '/dev/lvolume1').once()
- flexmock(module).should_receive('remove_snapshot').with_args('lvremove', '/dev/lvolume2').once()
- flexmock(module).should_receive('remove_snapshot').with_args(
- 'nonborgmatic', '/dev/nonborgmatic'
- ).never()
- module.remove_data_source_dumps(
- hook_config=config['lvm'],
- config=config,
- borgmatic_runtime_directory='/run/borgmatic',
- dry_run=False,
- )
- def test_remove_data_source_dumps_bails_for_missing_lvm_configuration():
- flexmock(module).should_receive('get_logical_volumes').never()
- flexmock(module.borgmatic.config.paths).should_receive(
- 'replace_temporary_subdirectory_with_glob'
- ).never()
- flexmock(module).should_receive('unmount_snapshot').never()
- flexmock(module).should_receive('remove_snapshot').never()
- module.remove_data_source_dumps(
- hook_config=None,
- config={'source_directories': '/mnt/lvolume'},
- borgmatic_runtime_directory='/run/borgmatic',
- dry_run=False,
- )
- def test_remove_data_source_dumps_bails_for_missing_lsblk_command():
- config = {'lvm': {}}
- flexmock(module).should_receive('get_logical_volumes').and_raise(FileNotFoundError)
- flexmock(module.borgmatic.config.paths).should_receive(
- 'replace_temporary_subdirectory_with_glob'
- ).never()
- flexmock(module).should_receive('unmount_snapshot').never()
- flexmock(module).should_receive('remove_snapshot').never()
- module.remove_data_source_dumps(
- hook_config=config['lvm'],
- config=config,
- borgmatic_runtime_directory='/run/borgmatic',
- dry_run=False,
- )
- def test_remove_data_source_dumps_bails_for_lsblk_command_error():
- config = {'lvm': {}}
- flexmock(module).should_receive('get_logical_volumes').and_raise(
- module.subprocess.CalledProcessError(1, 'wtf')
- )
- flexmock(module.borgmatic.config.paths).should_receive(
- 'replace_temporary_subdirectory_with_glob'
- ).never()
- flexmock(module).should_receive('unmount_snapshot').never()
- flexmock(module).should_receive('remove_snapshot').never()
- module.remove_data_source_dumps(
- hook_config=config['lvm'],
- config=config,
- borgmatic_runtime_directory='/run/borgmatic',
- dry_run=False,
- )
- def test_remove_data_source_dumps_with_missing_snapshot_directory_skips_unmount():
- config = {'lvm': {}}
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- 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])
- flexmock(module.os.path).should_receive('isdir').with_args(
- '/run/borgmatic/lvm_snapshots'
- ).and_return(False)
- flexmock(module.shutil).should_receive('rmtree').never()
- flexmock(module).should_receive('unmount_snapshot').never()
- flexmock(module).should_receive('get_snapshots').and_return(
- (
- module.Snapshot('lvolume1_borgmatic-1234', '/dev/lvolume1'),
- module.Snapshot('lvolume2_borgmatic-1234', '/dev/lvolume2'),
- ),
- )
- flexmock(module).should_receive('remove_snapshot').with_args('lvremove', '/dev/lvolume1').once()
- flexmock(module).should_receive('remove_snapshot').with_args('lvremove', '/dev/lvolume2').once()
- module.remove_data_source_dumps(
- hook_config=config['lvm'],
- config=config,
- borgmatic_runtime_directory='/run/borgmatic',
- dry_run=False,
- )
- def test_remove_data_source_dumps_with_missing_snapshot_mount_path_skips_unmount():
- config = {'lvm': {}}
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- 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])
- flexmock(module.os.path).should_receive('isdir').with_args(
- '/run/borgmatic/lvm_snapshots'
- ).and_return(True)
- flexmock(module.os.path).should_receive('isdir').with_args(
- '/run/borgmatic/lvm_snapshots/mnt/lvolume1'
- ).and_return(False)
- flexmock(module.os.path).should_receive('isdir').with_args(
- '/run/borgmatic/lvm_snapshots/mnt/lvolume2'
- ).and_return(True)
- flexmock(module.shutil).should_receive('rmtree')
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume1',
- ).never()
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume2',
- ).once()
- flexmock(module).should_receive('get_snapshots').and_return(
- (
- module.Snapshot('lvolume1_borgmatic-1234', '/dev/lvolume1'),
- module.Snapshot('lvolume2_borgmatic-1234', '/dev/lvolume2'),
- ),
- )
- flexmock(module).should_receive('remove_snapshot').with_args('lvremove', '/dev/lvolume1').once()
- flexmock(module).should_receive('remove_snapshot').with_args('lvremove', '/dev/lvolume2').once()
- module.remove_data_source_dumps(
- hook_config=config['lvm'],
- config=config,
- borgmatic_runtime_directory='/run/borgmatic',
- dry_run=False,
- )
- def test_remove_data_source_dumps_with_successful_mount_point_removal_skips_unmount():
- config = {'lvm': {}}
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- 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])
- flexmock(module.os.path).should_receive('isdir').with_args(
- '/run/borgmatic/lvm_snapshots'
- ).and_return(True)
- flexmock(module.os.path).should_receive('isdir').with_args(
- '/run/borgmatic/lvm_snapshots/mnt/lvolume1'
- ).and_return(True).and_return(False)
- flexmock(module.os.path).should_receive('isdir').with_args(
- '/run/borgmatic/lvm_snapshots/mnt/lvolume2'
- ).and_return(True).and_return(True)
- flexmock(module.shutil).should_receive('rmtree')
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume1',
- ).never()
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume2',
- ).once()
- flexmock(module).should_receive('get_snapshots').and_return(
- (
- module.Snapshot('lvolume1_borgmatic-1234', '/dev/lvolume1'),
- module.Snapshot('lvolume2_borgmatic-1234', '/dev/lvolume2'),
- ),
- )
- flexmock(module).should_receive('remove_snapshot').with_args('lvremove', '/dev/lvolume1').once()
- flexmock(module).should_receive('remove_snapshot').with_args('lvremove', '/dev/lvolume2').once()
- module.remove_data_source_dumps(
- hook_config=config['lvm'],
- config=config,
- borgmatic_runtime_directory='/run/borgmatic',
- dry_run=False,
- )
- def test_remove_data_source_dumps_bails_for_missing_umount_command():
- config = {'lvm': {}}
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- 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])
- flexmock(module.os.path).should_receive('isdir').and_return(True)
- flexmock(module.shutil).should_receive('rmtree')
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume1',
- ).and_raise(FileNotFoundError)
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume2',
- ).never()
- flexmock(module).should_receive('get_snapshots').never()
- flexmock(module).should_receive('remove_snapshot').never()
- module.remove_data_source_dumps(
- hook_config=config['lvm'],
- config=config,
- borgmatic_runtime_directory='/run/borgmatic',
- dry_run=False,
- )
- def test_remove_data_source_dumps_bails_for_umount_command_error():
- config = {'lvm': {}}
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- 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])
- flexmock(module.os.path).should_receive('isdir').and_return(True)
- flexmock(module.shutil).should_receive('rmtree')
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume1',
- ).and_raise(module.subprocess.CalledProcessError(1, 'wtf'))
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume2',
- ).never()
- flexmock(module).should_receive('get_snapshots').never()
- flexmock(module).should_receive('remove_snapshot').never()
- module.remove_data_source_dumps(
- hook_config=config['lvm'],
- config=config,
- borgmatic_runtime_directory='/run/borgmatic',
- dry_run=False,
- )
- def test_remove_data_source_dumps_bails_for_missing_lvs_command():
- config = {'lvm': {}}
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- 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])
- flexmock(module.os.path).should_receive('isdir').and_return(True)
- flexmock(module.shutil).should_receive('rmtree')
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume1',
- ).once()
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume2',
- ).once()
- flexmock(module).should_receive('get_snapshots').and_raise(FileNotFoundError)
- flexmock(module).should_receive('remove_snapshot').never()
- module.remove_data_source_dumps(
- hook_config=config['lvm'],
- config=config,
- borgmatic_runtime_directory='/run/borgmatic',
- dry_run=False,
- )
- def test_remove_data_source_dumps_bails_for_lvs_command_error():
- config = {'lvm': {}}
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- 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])
- flexmock(module.os.path).should_receive('isdir').and_return(True)
- flexmock(module.shutil).should_receive('rmtree')
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume1',
- ).once()
- flexmock(module).should_receive('unmount_snapshot').with_args(
- 'umount',
- '/run/borgmatic/lvm_snapshots/mnt/lvolume2',
- ).once()
- flexmock(module).should_receive('get_snapshots').and_raise(
- module.subprocess.CalledProcessError(1, 'wtf')
- )
- flexmock(module).should_receive('remove_snapshot').never()
- module.remove_data_source_dumps(
- hook_config=config['lvm'],
- config=config,
- borgmatic_runtime_directory='/run/borgmatic',
- dry_run=False,
- )
- def test_remove_data_source_with_dry_run_skips_snapshot_unmount_and_delete():
- config = {'lvm': {}}
- flexmock(module).should_receive('get_logical_volumes').and_return(
- (
- module.Logical_volume(
- name='lvolume1',
- device_path='/dev/lvolume1',
- mount_point='/mnt/lvolume1',
- contained_patterns=(Pattern('/mnt/lvolume1/subdir'),),
- ),
- module.Logical_volume(
- name='lvolume2',
- device_path='/dev/lvolume2',
- mount_point='/mnt/lvolume2',
- contained_patterns=(Pattern('/mnt/lvolume2'),),
- ),
- )
- )
- 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])
- flexmock(module.os.path).should_receive('isdir').and_return(True)
- flexmock(module.shutil).should_receive('rmtree').never()
- flexmock(module).should_receive('unmount_snapshot').never()
- flexmock(module).should_receive('get_snapshots').and_return(
- (
- module.Snapshot('lvolume1_borgmatic-1234', '/dev/lvolume1'),
- module.Snapshot('lvolume2_borgmatic-1234', '/dev/lvolume2'),
- module.Snapshot('nonborgmatic', '/dev/nonborgmatic'),
- ),
- ).once()
- flexmock(module).should_receive('remove_snapshot').never()
- module.remove_data_source_dumps(
- hook_config=config['lvm'],
- config=config,
- borgmatic_runtime_directory='/run/borgmatic',
- dry_run=True,
- )
|