2019-11-08 20:53:27 +01:00
|
|
|
import pytest
|
|
|
|
from flexmock import flexmock
|
|
|
|
|
|
|
|
from borgmatic.hooks import dump as module
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_make_data_source_dump_path_joins_arguments():
|
|
|
|
assert module.make_data_source_dump_path('/tmp', 'super_databases') == '/tmp/super_databases'
|
2019-12-11 01:04:34 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_make_data_source_dump_path_defaults_without_source_directory():
|
|
|
|
assert (
|
|
|
|
module.make_data_source_dump_path(None, 'super_databases') == '~/.borgmatic/super_databases'
|
|
|
|
)
|
2019-12-11 01:04:34 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_make_data_source_dump_filename_uses_name_and_hostname():
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.os.path).should_receive('expanduser').and_return('databases')
|
|
|
|
|
|
|
|
assert (
|
2023-08-24 22:50:10 +02:00
|
|
|
module.make_data_source_dump_filename('databases', 'test', 'hostname')
|
2019-11-08 20:53:27 +01:00
|
|
|
== 'databases/hostname/test'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_make_data_source_dump_filename_without_hostname_defaults_to_localhost():
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.os.path).should_receive('expanduser').and_return('databases')
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
assert module.make_data_source_dump_filename('databases', 'test') == 'databases/localhost/test'
|
2019-11-08 20:53:27 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_make_data_source_dump_filename_with_invalid_name_raises():
|
2019-11-08 20:53:27 +01:00
|
|
|
flexmock(module.os.path).should_receive('expanduser').and_return('databases')
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
2023-08-24 22:50:10 +02:00
|
|
|
module.make_data_source_dump_filename('databases', 'invalid/name')
|
2019-11-08 20:53:27 +01:00
|
|
|
|
|
|
|
|
2020-05-18 20:31:29 +02:00
|
|
|
def test_create_parent_directory_for_dump_does_not_raise():
|
2020-05-07 21:14:27 +02:00
|
|
|
flexmock(module.os).should_receive('makedirs')
|
2020-05-18 20:31:29 +02:00
|
|
|
|
|
|
|
module.create_parent_directory_for_dump('/path/to/parent')
|
|
|
|
|
|
|
|
|
|
|
|
def test_create_named_pipe_for_dump_does_not_raise():
|
|
|
|
flexmock(module).should_receive('create_parent_directory_for_dump')
|
2020-05-07 21:14:27 +02:00
|
|
|
flexmock(module.os).should_receive('mkfifo')
|
|
|
|
|
|
|
|
module.create_named_pipe_for_dump('/path/to/pipe')
|
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_remove_data_source_dumps_removes_dump_path():
|
2020-06-02 21:40:32 +02:00
|
|
|
flexmock(module.os.path).should_receive('expanduser').and_return('databases/localhost')
|
2020-05-18 20:31:29 +02:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(True)
|
2020-06-02 21:40:32 +02:00
|
|
|
flexmock(module.shutil).should_receive('rmtree').with_args('databases/localhost').once()
|
2019-11-08 20:53:27 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
module.remove_data_source_dumps('databases', 'SuperDB', 'test.yaml', dry_run=False)
|
2020-01-21 19:34:46 +01:00
|
|
|
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_remove_data_source_dumps_with_dry_run_skips_removal():
|
2020-06-02 21:40:32 +02:00
|
|
|
flexmock(module.os.path).should_receive('expanduser').and_return('databases/localhost')
|
|
|
|
flexmock(module.os.path).should_receive('exists').never()
|
|
|
|
flexmock(module.shutil).should_receive('rmtree').never()
|
2019-11-08 20:53:27 +01:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
module.remove_data_source_dumps('databases', 'SuperDB', 'test.yaml', dry_run=True)
|
2019-11-08 20:53:27 +01:00
|
|
|
|
2020-05-18 20:31:29 +02:00
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
def test_remove_data_source_dumps_without_dump_path_present_skips_removal():
|
2020-06-02 21:40:32 +02:00
|
|
|
flexmock(module.os.path).should_receive('expanduser').and_return('databases/localhost')
|
2020-05-18 20:31:29 +02:00
|
|
|
flexmock(module.os.path).should_receive('exists').and_return(False)
|
|
|
|
flexmock(module.shutil).should_receive('rmtree').never()
|
|
|
|
|
2023-08-24 22:50:10 +02:00
|
|
|
module.remove_data_source_dumps('databases', 'SuperDB', 'test.yaml', dry_run=False)
|
2019-11-12 06:59:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
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']
|