131 lines
4.9 KiB
Python
131 lines
4.9 KiB
Python
import pytest
|
|
from flexmock import flexmock
|
|
|
|
from borgmatic.hooks import dump as module
|
|
|
|
|
|
def test_make_database_dump_filename_uses_name_and_hostname():
|
|
flexmock(module.os.path).should_receive('expanduser').and_return('databases')
|
|
|
|
assert (
|
|
module.make_database_dump_filename('databases', 'test', 'hostname')
|
|
== 'databases/hostname/test'
|
|
)
|
|
|
|
|
|
def test_make_database_dump_filename_without_hostname_defaults_to_localhost():
|
|
flexmock(module.os.path).should_receive('expanduser').and_return('databases')
|
|
|
|
assert module.make_database_dump_filename('databases', 'test') == 'databases/localhost/test'
|
|
|
|
|
|
def test_make_database_dump_filename_with_invalid_name_raises():
|
|
flexmock(module.os.path).should_receive('expanduser').and_return('databases')
|
|
|
|
with pytest.raises(ValueError):
|
|
module.make_database_dump_filename('databases', 'invalid/name')
|
|
|
|
|
|
def test_remove_database_dumps_removes_dump_for_each_database():
|
|
databases = [{'name': 'foo'}, {'name': 'bar'}]
|
|
flexmock(module).should_receive('make_database_dump_filename').and_return(
|
|
'databases/localhost/foo'
|
|
).and_return('databases/localhost/bar')
|
|
flexmock(module.os).should_receive('listdir').and_return([])
|
|
flexmock(module.os).should_receive('rmdir')
|
|
|
|
for name in ('foo', 'bar'):
|
|
flexmock(module.os).should_receive('remove').with_args(
|
|
'databases/localhost/{}'.format(name)
|
|
).once()
|
|
|
|
module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=False)
|
|
|
|
|
|
def test_remove_database_dumps_with_dry_run_skips_removal():
|
|
databases = [{'name': 'foo'}, {'name': 'bar'}]
|
|
flexmock(module.os).should_receive('rmdir').never()
|
|
flexmock(module.os).should_receive('remove').never()
|
|
|
|
module.remove_database_dumps('databases', databases, 'SuperDB', 'test.yaml', dry_run=True)
|
|
|
|
|
|
def test_remove_database_dumps_without_databases_does_not_raise():
|
|
module.remove_database_dumps('databases', [], 'SuperDB', 'test.yaml', dry_run=False)
|
|
|
|
|
|
def test_convert_glob_patterns_to_borg_patterns_removes_leading_slash():
|
|
assert module.convert_glob_patterns_to_borg_patterns(('/etc/foo/bar',)) == ['sh:etc/foo/bar']
|
|
|
|
|
|
def test_get_database_names_from_dumps_gets_names_from_filenames_matching_globs():
|
|
flexmock(module.glob).should_receive('glob').and_return(
|
|
('databases/localhost/foo',)
|
|
).and_return(('databases/localhost/bar',)).and_return(())
|
|
|
|
assert module.get_database_names_from_dumps(
|
|
('databases/*/foo', 'databases/*/bar', 'databases/*/baz')
|
|
) == ['foo', 'bar']
|
|
|
|
|
|
def test_get_database_configurations_only_produces_named_databases():
|
|
databases = [
|
|
{'name': 'foo', 'hostname': 'example.org'},
|
|
{'name': 'bar', 'hostname': 'example.com'},
|
|
{'name': 'baz', 'hostname': 'example.org'},
|
|
]
|
|
|
|
assert list(module.get_database_configurations(databases, ('foo', 'baz'))) == [
|
|
{'name': 'foo', 'hostname': 'example.org'},
|
|
{'name': 'baz', 'hostname': 'example.org'},
|
|
]
|
|
|
|
|
|
def test_get_database_configurations_matches_all_database():
|
|
databases = [
|
|
{'name': 'foo', 'hostname': 'example.org'},
|
|
{'name': 'all', 'hostname': 'example.com'},
|
|
]
|
|
|
|
assert list(module.get_database_configurations(databases, ('foo', 'bar', 'baz'))) == [
|
|
{'name': 'foo', 'hostname': 'example.org'},
|
|
{'name': 'bar', 'hostname': 'example.com'},
|
|
{'name': 'baz', 'hostname': 'example.com'},
|
|
]
|
|
|
|
|
|
def test_get_database_configurations_with_unknown_database_name_raises():
|
|
databases = [{'name': 'foo', 'hostname': 'example.org'}]
|
|
|
|
with pytest.raises(ValueError):
|
|
list(module.get_database_configurations(databases, ('foo', 'bar')))
|
|
|
|
|
|
def test_get_per_hook_database_configurations_partitions_by_hook():
|
|
hooks = {'postgresql_databases': [flexmock()]}
|
|
names = ('foo', 'bar')
|
|
dump_patterns = flexmock()
|
|
expected_config = {'postgresql_databases': [flexmock()]}
|
|
flexmock(module).should_receive('get_database_configurations').with_args(
|
|
hooks['postgresql_databases'], names
|
|
).and_return(expected_config['postgresql_databases'])
|
|
|
|
config = module.get_per_hook_database_configurations(hooks, names, dump_patterns)
|
|
|
|
assert config == expected_config
|
|
|
|
|
|
def test_get_per_hook_database_configurations_defaults_to_detected_database_names():
|
|
hooks = {'postgresql_databases': [flexmock()]}
|
|
names = ()
|
|
detected_names = flexmock()
|
|
dump_patterns = {'postgresql_databases': [flexmock()]}
|
|
expected_config = {'postgresql_databases': [flexmock()]}
|
|
flexmock(module).should_receive('get_database_names_from_dumps').and_return(detected_names)
|
|
flexmock(module).should_receive('get_database_configurations').with_args(
|
|
hooks['postgresql_databases'], detected_names
|
|
).and_return(expected_config['postgresql_databases'])
|
|
|
|
config = module.get_per_hook_database_configurations(hooks, names, dump_patterns)
|
|
|
|
assert config == expected_config
|