diff --git a/borgmatic/execute.py b/borgmatic/execute.py index f775b2f..4b791b9 100644 --- a/borgmatic/execute.py +++ b/borgmatic/execute.py @@ -79,7 +79,11 @@ def execute_command( Raise subprocesses.CalledProcessError if an error occurs while running the command. ''' - logger.debug(' '.join(full_command)) + logger.debug( + ' '.join(full_command) + + (' < {}'.format(input_file.name) if input_file else '') + + (' > {}'.format(output_file.name) if output_file else '') + ) environment = {**os.environ, **extra_environment} if extra_environment else None if output_log_level is None: diff --git a/tests/unit/hooks/test_mysql.py b/tests/unit/hooks/test_mysql.py index 354be73..c134413 100644 --- a/tests/unit/hooks/test_mysql.py +++ b/tests/unit/hooks/test_mysql.py @@ -116,3 +116,90 @@ def test_dump_databases_runs_mysqldump_for_all_databases(): ).once() module.dump_databases(databases, 'test.yaml', dry_run=False) + + +def test_make_database_dump_patterns_converts_names_to_glob_paths(): + flexmock(module.dump).should_receive('make_database_dump_filename').and_return( + 'databases/*/foo' + ).and_return('databases/*/bar') + + assert module.make_database_dump_patterns(flexmock(), flexmock(), ('foo', 'bar')) == [ + 'databases/*/foo', + 'databases/*/bar', + ] + + +def test_make_database_dump_patterns_treats_empty_names_as_matching_all_databases(): + flexmock(module.dump).should_receive('make_database_dump_filename').with_args( + module.DUMP_PATH, '*', '*' + ).and_return('databases/*/*') + + assert module.make_database_dump_patterns(flexmock(), flexmock(), ()) == ['databases/*/*'] + + +def test_restore_database_dumps_restores_each_database(): + databases = [{'name': 'foo'}, {'name': 'bar'}] + flexmock(module.dump).should_receive('make_database_dump_filename').and_return( + 'databases/localhost/foo' + ).and_return('databases/localhost/bar') + + for name in ('foo', 'bar'): + dump_filename = 'databases/localhost/{}'.format(name) + input_file = flexmock() + flexmock(sys.modules['builtins']).should_receive('open').with_args( + dump_filename + ).and_return(input_file) + flexmock(module).should_receive('execute_command').with_args( + ('mysql', '--batch'), input_file=input_file, extra_environment=None + ).once() + + module.restore_database_dumps(databases, 'test.yaml', dry_run=False) + + +def test_restore_database_dumps_runs_mysql_with_hostname_and_port(): + databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}] + flexmock(module.dump).should_receive('make_database_dump_filename').and_return( + 'databases/localhost/foo' + ) + dump_filename = 'databases/localhost/foo' + input_file = flexmock() + flexmock(sys.modules['builtins']).should_receive('open').with_args(dump_filename).and_return( + input_file + ) + + flexmock(module).should_receive('execute_command').with_args( + ( + 'mysql', + '--batch', + '--host', + 'database.example.org', + '--port', + '5433', + '--protocol', + 'tcp', + ), + input_file=input_file, + extra_environment=None, + ).once() + + module.restore_database_dumps(databases, 'test.yaml', dry_run=False) + + +def test_restore_database_dumps_runs_mysql_with_username_and_password(): + databases = [{'name': 'foo', 'username': 'root', 'password': 'trustsome1'}] + flexmock(module.dump).should_receive('make_database_dump_filename').and_return( + 'databases/localhost/foo' + ) + dump_filename = 'databases/localhost/foo' + input_file = flexmock() + flexmock(sys.modules['builtins']).should_receive('open').with_args(dump_filename).and_return( + input_file + ) + + flexmock(module).should_receive('execute_command').with_args( + ('mysql', '--batch', '--user', 'root'), + input_file=input_file, + extra_environment={'MYSQL_PWD': 'trustsome1'}, + ).once() + + module.restore_database_dumps(databases, 'test.yaml', dry_run=False) diff --git a/tests/unit/hooks/test_postgresql.py b/tests/unit/hooks/test_postgresql.py index 8ff58e9..470a63e 100644 --- a/tests/unit/hooks/test_postgresql.py +++ b/tests/unit/hooks/test_postgresql.py @@ -208,7 +208,7 @@ def test_restore_database_dumps_runs_pg_restore_with_hostname_and_port(): databases = [{'name': 'foo', 'hostname': 'database.example.org', 'port': 5433}] flexmock(module.dump).should_receive('make_database_dump_filename').and_return( 'databases/localhost/foo' - ).and_return('databases/localhost/bar') + ) flexmock(module).should_receive('execute_command').with_args( ( @@ -251,7 +251,7 @@ def test_restore_database_dumps_runs_pg_restore_with_username_and_password(): databases = [{'name': 'foo', 'username': 'postgres', 'password': 'trustsome1'}] flexmock(module.dump).should_receive('make_database_dump_filename').and_return( 'databases/localhost/foo' - ).and_return('databases/localhost/bar') + ) flexmock(module).should_receive('execute_command').with_args( ( diff --git a/tests/unit/test_execute.py b/tests/unit/test_execute.py index cae54c5..f29feb5 100644 --- a/tests/unit/test_execute.py +++ b/tests/unit/test_execute.py @@ -63,7 +63,7 @@ def test_execute_command_calls_full_command(): def test_execute_command_calls_full_command_with_output_file(): full_command = ['foo', 'bar'] - output_file = flexmock() + output_file = flexmock(name='test') flexmock(module.os, environ={'a': 'b'}) flexmock(module.subprocess).should_receive('Popen').with_args( full_command, @@ -83,7 +83,7 @@ def test_execute_command_calls_full_command_with_output_file(): def test_execute_command_calls_full_command_with_input_file(): full_command = ['foo', 'bar'] - input_file = flexmock() + input_file = flexmock(name='test') flexmock(module.os, environ={'a': 'b'}) flexmock(module.subprocess).should_receive('Popen').with_args( full_command,