From 51ef9c770880eee0ee1f67068b03fe44e0e29b07 Mon Sep 17 00:00:00 2001 From: bockulator Date: Tue, 12 Aug 2025 23:32:32 +0200 Subject: [PATCH 1/2] Fix database dumps hooks not respecting working_directory --- borgmatic/hooks/data_source/mariadb.py | 8 ++++- borgmatic/hooks/data_source/mongodb.py | 14 ++++++-- borgmatic/hooks/data_source/mysql.py | 8 ++++- borgmatic/hooks/data_source/postgresql.py | 15 ++++++-- borgmatic/hooks/data_source/sqlite.py | 8 ++++- tests/unit/hooks/data_source/test_mariadb.py | 26 ++++++++++++++ tests/unit/hooks/data_source/test_mongodb.py | 17 +++++++++ tests/unit/hooks/data_source/test_mysql.py | 26 ++++++++++++++ .../unit/hooks/data_source/test_postgresql.py | 36 +++++++++++++++++++ tests/unit/hooks/data_source/test_sqlite.py | 8 +++++ 10 files changed, 159 insertions(+), 7 deletions(-) diff --git a/borgmatic/hooks/data_source/mariadb.py b/borgmatic/hooks/data_source/mariadb.py index 606eb3a4..78975f52 100644 --- a/borgmatic/hooks/data_source/mariadb.py +++ b/borgmatic/hooks/data_source/mariadb.py @@ -155,7 +155,11 @@ def database_names_to_dump(database, config, username, password, environment, dr if skip_names: logger.debug(f'Skipping database names: {", ".join(skip_names)}') - show_output = execute_command_and_capture_output(show_command, environment=environment) + show_output = execute_command_and_capture_output( + show_command, + environment=environment, + working_directory=borgmatic.config.paths.get_working_directory(config), + ) return tuple( show_name @@ -239,6 +243,7 @@ def execute_dump_command( dump_command, environment=environment, run_to_completion=False, + working_directory=borgmatic.config.paths.get_working_directory(config), ) @@ -495,4 +500,5 @@ def restore_data_source_dump( output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment=environment, + working_directory=borgmatic.config.paths.get_working_directory(config), ) diff --git a/borgmatic/hooks/data_source/mongodb.py b/borgmatic/hooks/data_source/mongodb.py index 7c94d5f7..c4dceed5 100644 --- a/borgmatic/hooks/data_source/mongodb.py +++ b/borgmatic/hooks/data_source/mongodb.py @@ -90,11 +90,20 @@ def dump_data_sources( if dump_format == 'directory': dump.create_parent_directory_for_dump(dump_filename) - execute_command(command, shell=True) # noqa: S604 + execute_command( # noqa: S604 + command, + shell=True, + working_directory=borgmatic.config.paths.get_working_directory(config), + ) else: dump.create_named_pipe_for_dump(dump_filename) processes.append( - execute_command(command, shell=True, run_to_completion=False), # noqa: S604 + execute_command( # noqa: S604 + command, + shell=True, + run_to_completion=False, + working_directory=borgmatic.config.paths.get_working_directory(config), + ), ) if not dry_run: @@ -273,6 +282,7 @@ def restore_data_source_dump( [extract_process] if extract_process else [], output_log_level=logging.DEBUG, input_file=extract_process.stdout if extract_process else None, + working_directory=borgmatic.config.paths.get_working_directory(config), ) diff --git a/borgmatic/hooks/data_source/mysql.py b/borgmatic/hooks/data_source/mysql.py index a176a7ad..b725814b 100644 --- a/borgmatic/hooks/data_source/mysql.py +++ b/borgmatic/hooks/data_source/mysql.py @@ -84,7 +84,11 @@ def database_names_to_dump(database, config, username, password, environment, dr if skip_names: logger.debug(f'Skipping database names: {", ".join(skip_names)}') - show_output = execute_command_and_capture_output(show_command, environment=environment) + show_output = execute_command_and_capture_output( + show_command, + environment=environment, + working_directory=borgmatic.config.paths.get_working_directory(config), + ) return tuple( show_name @@ -170,6 +174,7 @@ def execute_dump_command( dump_command, environment=environment, run_to_completion=False, + working_directory=borgmatic.config.paths.get_working_directory(config), ) @@ -432,4 +437,5 @@ def restore_data_source_dump( output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment=environment, + working_directory=borgmatic.config.paths.get_working_directory(config), ) diff --git a/borgmatic/hooks/data_source/postgresql.py b/borgmatic/hooks/data_source/postgresql.py index 3ef8e39a..6c746417 100644 --- a/borgmatic/hooks/data_source/postgresql.py +++ b/borgmatic/hooks/data_source/postgresql.py @@ -103,7 +103,11 @@ def database_names_to_dump(database, config, environment, dry_run): + (tuple(database['list_options'].split(' ')) if 'list_options' in database else ()) ) logger.debug('Querying for "all" PostgreSQL databases to dump') - list_output = execute_command_and_capture_output(list_command, environment=environment) + list_output = execute_command_and_capture_output( + list_command, + environment=environment, + working_directory=borgmatic.config.paths.get_working_directory(config), + ) return tuple( row[0] @@ -245,6 +249,7 @@ def dump_data_sources( command, shell=True, environment=environment, + working_directory=borgmatic.config.paths.get_working_directory(config), ) else: dump.create_named_pipe_for_dump(dump_filename) @@ -254,6 +259,7 @@ def dump_data_sources( shell=True, environment=environment, run_to_completion=False, + working_directory=borgmatic.config.paths.get_working_directory(config), ), ) @@ -422,5 +428,10 @@ def restore_data_source_dump( output_log_level=logging.DEBUG, input_file=extract_process.stdout if extract_process else None, environment=environment, + working_directory=borgmatic.config.paths.get_working_directory(config), + ) + execute_command( + analyze_command, + environment=environment, + working_directory=borgmatic.config.paths.get_working_directory(config), ) - execute_command(analyze_command, environment=environment) diff --git a/borgmatic/hooks/data_source/sqlite.py b/borgmatic/hooks/data_source/sqlite.py index d14804ef..a31a8abf 100644 --- a/borgmatic/hooks/data_source/sqlite.py +++ b/borgmatic/hooks/data_source/sqlite.py @@ -101,7 +101,12 @@ def dump_data_sources( dump.create_named_pipe_for_dump(dump_filename) processes.append( - execute_command(command, shell=True, run_to_completion=False), # noqa: S604 + execute_command( # noqa: S604 + command, + shell=True, + run_to_completion=False, + working_directory=borgmatic.config.paths.get_working_directory(config), + ), ) if not dry_run: @@ -205,4 +210,5 @@ def restore_data_source_dump( [extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=borgmatic.config.paths.get_working_directory(config), ) diff --git a/tests/unit/hooks/data_source/test_mariadb.py b/tests/unit/hooks/data_source/test_mariadb.py index b74c7482..bcb7f571 100644 --- a/tests/unit/hooks/data_source/test_mariadb.py +++ b/tests/unit/hooks/data_source/test_mariadb.py @@ -196,6 +196,7 @@ def test_database_names_to_dump_queries_mariadb_for_database_names(): 'show schemas', ), environment=environment, + working_directory=None, ).and_return('foo\nbar\nmysql\n').once() names = module.database_names_to_dump( @@ -231,6 +232,7 @@ def test_database_names_to_dump_with_database_name_all_and_skip_names_filters_ou 'show schemas', ), environment=environment, + working_directory=None, ).and_return('foo\nbar\nbaz\nmysql\n').once() names = module.database_names_to_dump( @@ -263,6 +265,7 @@ def test_database_names_to_dump_with_environment_password_transport_skips_defaul 'show schemas', ), environment=environment, + working_directory=None, ).and_return('foo\nbar\nmysql\n').once() names = module.database_names_to_dump( @@ -299,6 +302,7 @@ def test_database_names_to_dump_runs_mariadb_with_tls(): 'show schemas', ), environment=environment, + working_directory=None, ).and_return('foo\nbar\nmysql\n').once() names = module.database_names_to_dump( @@ -335,6 +339,7 @@ def test_database_names_to_dump_runs_mariadb_without_tls(): 'show schemas', ), environment=environment, + working_directory=None, ).and_return('foo\nbar\nmysql\n').once() names = module.database_names_to_dump( @@ -710,6 +715,7 @@ def test_database_names_to_dump_runs_mariadb_with_list_options(): 'show schemas', ), environment=None, + working_directory=None, ).and_return('foo\nbar').once() assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == ( @@ -744,6 +750,7 @@ def test_database_names_to_dump_runs_non_default_mariadb_with_list_options(): '--execute', 'show schemas', ), + working_directory=None, ).and_return('foo\nbar').once() assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == ( @@ -779,6 +786,7 @@ def test_execute_dump_command_runs_mariadb_dump(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -821,6 +829,7 @@ def test_execute_dump_command_with_environment_password_transport_skips_defaults ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -865,6 +874,7 @@ def test_execute_dump_command_runs_mariadb_dump_without_add_drop_database(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -916,6 +926,7 @@ def test_execute_dump_command_runs_mariadb_dump_with_hostname_and_port(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -962,6 +973,7 @@ def test_execute_dump_command_runs_mariadb_dump_with_tls(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -1008,6 +1020,7 @@ def test_execute_dump_command_runs_mariadb_dump_without_tls(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -1053,6 +1066,7 @@ def test_execute_dump_command_runs_mariadb_dump_with_username_and_password(): ), environment={}, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -1099,6 +1113,7 @@ def test_execute_dump_command_runs_mariadb_dump_with_options(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -1145,6 +1160,7 @@ def test_execute_dump_command_runs_non_default_mariadb_dump_with_options(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -1247,6 +1263,7 @@ def test_restore_data_source_dump_runs_mariadb_to_restore(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1285,6 +1302,7 @@ def test_restore_data_source_dump_runs_mariadb_with_options(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1325,6 +1343,7 @@ def test_restore_data_source_dump_runs_non_default_mariadb_with_options(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1372,6 +1391,7 @@ def test_restore_data_source_dump_runs_mariadb_with_hostname_and_port(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1414,6 +1434,7 @@ def test_restore_data_source_dump_runs_mariadb_with_tls(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1456,6 +1477,7 @@ def test_restore_data_source_dump_runs_mariadb_without_tls(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1494,6 +1516,7 @@ def test_restore_data_source_dump_runs_mariadb_with_username_and_password(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1537,6 +1560,7 @@ def test_restore_data_source_with_environment_password_transport_skips_defaults_ output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root', 'MYSQL_PWD': 'trustsome1'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1595,6 +1619,7 @@ def test_restore_data_source_dump_with_connection_params_uses_connection_params_ output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1658,6 +1683,7 @@ def test_restore_data_source_dump_without_connection_params_uses_restore_params_ output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( diff --git a/tests/unit/hooks/data_source/test_mongodb.py b/tests/unit/hooks/data_source/test_mongodb.py index 801c1e18..dc474176 100644 --- a/tests/unit/hooks/data_source/test_mongodb.py +++ b/tests/unit/hooks/data_source/test_mongodb.py @@ -37,6 +37,7 @@ def test_dump_data_sources_runs_mongodump_for_each_database(): ('mongodump', '--db', name, '--archive', '>', f'databases/localhost/{name}'), shell=True, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( @@ -116,6 +117,7 @@ def test_dump_data_sources_runs_mongodump_with_hostname_and_port(): ), shell=True, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -183,6 +185,7 @@ def test_dump_data_sources_runs_mongodump_with_username_and_password(): ), shell=True, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -221,6 +224,7 @@ def test_dump_data_sources_runs_mongodump_with_directory_format(): flexmock(module).should_receive('execute_command').with_args( ('mongodump', '--out', 'databases/localhost/foo', '--db', 'foo'), shell=True, + working_directory=None, ).and_return(flexmock()).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -271,6 +275,7 @@ def test_dump_data_sources_runs_mongodump_with_options(): ), shell=True, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -310,6 +315,7 @@ def test_dump_data_sources_runs_mongodumpall_for_all_databases(): ('mongodump', '--archive', '>', 'databases/localhost/all'), shell=True, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -378,6 +384,7 @@ def test_restore_data_source_dump_runs_mongorestore(): processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() module.restore_data_source_dump( @@ -420,6 +427,7 @@ def test_restore_data_source_dump_runs_mongorestore_with_hostname_and_port(): processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() module.restore_data_source_dump( @@ -473,6 +481,7 @@ def test_restore_data_source_dump_runs_mongorestore_with_username_and_password() processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() module.restore_data_source_dump( @@ -534,6 +543,7 @@ def test_restore_data_source_dump_with_connection_params_uses_connection_params_ processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() module.restore_data_source_dump( @@ -595,6 +605,7 @@ def test_restore_data_source_dump_without_connection_params_uses_restore_params_ processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() module.restore_data_source_dump( @@ -627,6 +638,7 @@ def test_restore_data_source_dump_runs_mongorestore_with_options(): processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() module.restore_data_source_dump( @@ -667,6 +679,7 @@ def test_restore_databases_dump_runs_mongorestore_with_schemas(): processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() module.restore_data_source_dump( @@ -699,6 +712,7 @@ def test_restore_data_source_dump_runs_psql_for_all_database_dump(): processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() module.restore_data_source_dump( @@ -756,6 +770,7 @@ def test_restore_data_source_dump_without_extract_process_restores_from_disk(): processes=[], output_log_level=logging.DEBUG, input_file=None, + working_directory=None, ).once() module.restore_data_source_dump( @@ -797,6 +812,7 @@ def test_dump_data_sources_uses_custom_mongodump_command(): ), shell=True, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -865,6 +881,7 @@ def test_restore_data_source_dump_uses_custom_mongorestore_command(): processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() module.restore_data_source_dump( diff --git a/tests/unit/hooks/data_source/test_mysql.py b/tests/unit/hooks/data_source/test_mysql.py index 9b745196..a62d766d 100644 --- a/tests/unit/hooks/data_source/test_mysql.py +++ b/tests/unit/hooks/data_source/test_mysql.py @@ -78,6 +78,7 @@ def test_database_names_to_dump_queries_mysql_for_database_names(): 'show schemas', ), environment=environment, + working_directory=None, ).and_return('foo\nbar\nmysql\n').once() names = module.database_names_to_dump( @@ -117,6 +118,7 @@ def test_database_names_to_dump_with_database_name_all_and_skip_names_filters_ou 'show schemas', ), environment=environment, + working_directory=None, ).and_return('foo\nbar\nbaz\nmysql\n').once() names = module.database_names_to_dump( @@ -153,6 +155,7 @@ def test_database_names_to_dump_with_environment_password_transport_skips_defaul 'show schemas', ), environment=environment, + working_directory=None, ).and_return('foo\nbar\nmysql\n').once() names = module.database_names_to_dump( @@ -189,6 +192,7 @@ def test_database_names_to_dump_runs_mysql_with_tls(): 'show schemas', ), environment=environment, + working_directory=None, ).and_return('foo\nbar\nmysql\n').once() names = module.database_names_to_dump( @@ -225,6 +229,7 @@ def test_database_names_to_dump_runs_mysql_without_tls(): 'show schemas', ), environment=environment, + working_directory=None, ).and_return('foo\nbar\nmysql\n').once() names = module.database_names_to_dump( @@ -595,6 +600,7 @@ def test_database_names_to_dump_runs_mysql_with_list_options(): 'show schemas', ), environment=None, + working_directory=None, ).and_return('foo\nbar').once() assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == ( @@ -626,6 +632,7 @@ def test_database_names_to_dump_runs_non_default_mysql_with_list_options(): '--execute', 'show schemas', ), + working_directory=None, ).and_return('foo\nbar').once() assert module.database_names_to_dump(database, {}, 'root', 'trustsome1', None, '') == ( @@ -661,6 +668,7 @@ def test_execute_dump_command_runs_mysqldump(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -707,6 +715,7 @@ def test_execute_dump_command_with_environment_password_transport_skips_defaults ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -751,6 +760,7 @@ def test_execute_dump_command_runs_mysqldump_without_add_drop_database(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -802,6 +812,7 @@ def test_execute_dump_command_runs_mysqldump_with_hostname_and_port(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -848,6 +859,7 @@ def test_execute_dump_command_runs_mysqldump_with_tls(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -894,6 +906,7 @@ def test_execute_dump_command_runs_mysqldump_without_tls(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -939,6 +952,7 @@ def test_execute_dump_command_runs_mysqldump_with_username_and_password(): ), environment={}, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -985,6 +999,7 @@ def test_execute_dump_command_runs_mysqldump_with_options(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -1030,6 +1045,7 @@ def test_execute_dump_command_runs_non_default_mysqldump(): ), environment=None, run_to_completion=False, + working_directory=None, ).and_return(process).once() assert ( @@ -1131,6 +1147,7 @@ def test_restore_data_source_dump_runs_mysql_to_restore(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1169,6 +1186,7 @@ def test_restore_data_source_dump_runs_mysql_with_options(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1207,6 +1225,7 @@ def test_restore_data_source_dump_runs_non_default_mysql_with_options(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1254,6 +1273,7 @@ def test_restore_data_source_dump_runs_mysql_with_hostname_and_port(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1296,6 +1316,7 @@ def test_restore_data_source_dump_runs_mysql_with_tls(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1338,6 +1359,7 @@ def test_restore_data_source_dump_runs_mysql_without_tls(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1376,6 +1398,7 @@ def test_restore_data_source_dump_runs_mysql_with_username_and_password(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1423,6 +1446,7 @@ def test_restore_data_source_with_environment_password_transport_skips_defaults_ output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root', 'MYSQL_PWD': 'trustsome1'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1483,6 +1507,7 @@ def test_restore_data_source_dump_with_connection_params_uses_connection_params_ output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1548,6 +1573,7 @@ def test_restore_data_source_dump_without_connection_params_uses_restore_params_ output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'USER': 'root'}, + working_directory=None, ).once() module.restore_data_source_dump( diff --git a/tests/unit/hooks/data_source/test_postgresql.py b/tests/unit/hooks/data_source/test_postgresql.py index a49cc78f..c70a8bd0 100644 --- a/tests/unit/hooks/data_source/test_postgresql.py +++ b/tests/unit/hooks/data_source/test_postgresql.py @@ -131,6 +131,7 @@ def test_database_names_to_dump_with_all_and_format_lists_databases_with_hostnam '1234', ), environment=object, + working_directory=None, ).and_return('foo,test,\nbar,test,"stuff and such"') assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == ( @@ -156,6 +157,7 @@ def test_database_names_to_dump_with_all_and_format_lists_databases_with_usernam 'postgres', ), environment=object, + working_directory=None, ).and_return('foo,test,\nbar,test,"stuff and such"') assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == ( @@ -172,6 +174,7 @@ def test_database_names_to_dump_with_all_and_format_lists_databases_with_options flexmock(module).should_receive('execute_command_and_capture_output').with_args( ('psql', '--list', '--no-password', '--no-psqlrc', '--csv', '--tuples-only', '--harder'), environment=object, + working_directory=None, ).and_return('foo,test,\nbar,test,"stuff and such"') assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == ( @@ -216,6 +219,7 @@ def test_database_names_to_dump_with_all_and_psql_command_uses_custom_command(): '--tuples-only', ), environment=object, + working_directory=None, ).and_return('foo,text').once() assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == ('foo',) @@ -272,6 +276,7 @@ def test_dump_data_sources_runs_pg_dump_for_each_database(): shell=True, environment={'PGSSLMODE': 'disable'}, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -448,6 +453,7 @@ def test_dump_data_sources_runs_pg_dump_with_hostname_and_port(): shell=True, environment={'PGSSLMODE': 'disable'}, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -510,6 +516,7 @@ def test_dump_data_sources_runs_pg_dump_with_username_and_password(): shell=True, environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -570,6 +577,7 @@ def test_dump_data_sources_with_username_injection_attack_gets_escaped(): shell=True, environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -625,6 +633,7 @@ def test_dump_data_sources_runs_pg_dump_with_directory_format(): ), shell=True, environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).and_return(flexmock()).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -686,6 +695,7 @@ def test_dump_data_sources_runs_pg_dump_with_string_compression(): shell=True, environment={'PGSSLMODE': 'disable'}, run_to_completion=False, + working_directory=None, ).and_return(processes[0]).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -747,6 +757,7 @@ def test_dump_data_sources_runs_pg_dump_with_integer_compression(): shell=True, environment={'PGSSLMODE': 'disable'}, run_to_completion=False, + working_directory=None, ).and_return(processes[0]).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -807,6 +818,7 @@ def test_dump_data_sources_runs_pg_dump_with_options(): shell=True, environment={'PGSSLMODE': 'disable'}, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -853,6 +865,7 @@ def test_dump_data_sources_runs_pg_dumpall_for_all_databases(): shell=True, environment={'PGSSLMODE': 'disable'}, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -911,6 +924,7 @@ def test_dump_data_sources_runs_non_default_pg_dump(): shell=True, environment={'PGSSLMODE': 'disable'}, run_to_completion=False, + working_directory=None, ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -961,6 +975,7 @@ def test_restore_data_source_dump_runs_pg_restore(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() flexmock(module).should_receive('execute_command').with_args( ( @@ -974,6 +989,7 @@ def test_restore_data_source_dump_runs_pg_restore(): 'ANALYZE', ), environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1022,6 +1038,7 @@ def test_restore_data_source_dump_runs_pg_restore_with_hostname_and_port(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() flexmock(module).should_receive('execute_command').with_args( ( @@ -1039,6 +1056,7 @@ def test_restore_data_source_dump_runs_pg_restore_with_hostname_and_port(): 'ANALYZE', ), environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1087,6 +1105,7 @@ def test_restore_data_source_dump_runs_pg_restore_with_username_and_password(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}, + working_directory=None, ).once() flexmock(module).should_receive('execute_command').with_args( ( @@ -1102,6 +1121,7 @@ def test_restore_data_source_dump_runs_pg_restore_with_username_and_password(): 'ANALYZE', ), environment={'PGPASSWORD': 'trustsome1', 'PGSSLMODE': 'disable'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1165,6 +1185,7 @@ def test_restore_data_source_dump_with_connection_params_uses_connection_params_ output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'}, + working_directory=None, ).once() flexmock(module).should_receive('execute_command').with_args( ( @@ -1184,6 +1205,7 @@ def test_restore_data_source_dump_with_connection_params_uses_connection_params_ 'ANALYZE', ), environment={'PGPASSWORD': 'clipassword', 'PGSSLMODE': 'disable'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1247,6 +1269,7 @@ def test_restore_data_source_dump_without_connection_params_uses_restore_params_ output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'}, + working_directory=None, ).once() flexmock(module).should_receive('execute_command').with_args( ( @@ -1266,6 +1289,7 @@ def test_restore_data_source_dump_without_connection_params_uses_restore_params_ 'ANALYZE', ), environment={'PGPASSWORD': 'restorepassword', 'PGSSLMODE': 'disable'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1316,6 +1340,7 @@ def test_restore_data_source_dump_runs_pg_restore_with_options(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() flexmock(module).should_receive('execute_command').with_args( ( @@ -1330,6 +1355,7 @@ def test_restore_data_source_dump_runs_pg_restore_with_options(): 'ANALYZE', ), environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1368,10 +1394,12 @@ def test_restore_data_source_dump_runs_psql_for_all_database_dump(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() flexmock(module).should_receive('execute_command').with_args( ('psql', '--no-password', '--no-psqlrc', '--quiet', '--command', 'ANALYZE'), environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1406,6 +1434,7 @@ def test_restore_data_source_dump_runs_psql_for_plain_database_dump(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() flexmock(module).should_receive('execute_command').with_args( ( @@ -1419,6 +1448,7 @@ def test_restore_data_source_dump_runs_psql_for_plain_database_dump(): 'ANALYZE', ), environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1473,6 +1503,7 @@ def test_restore_data_source_dump_runs_non_default_pg_restore_and_psql(): output_log_level=logging.DEBUG, input_file=extract_process.stdout, environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() flexmock(module).should_receive('execute_command').with_args( ( @@ -1491,6 +1522,7 @@ def test_restore_data_source_dump_runs_non_default_pg_restore_and_psql(): 'ANALYZE', ), environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1560,6 +1592,7 @@ def test_restore_data_source_dump_without_extract_process_restores_from_disk(): output_log_level=logging.DEBUG, input_file=None, environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() flexmock(module).should_receive('execute_command').with_args( ( @@ -1573,6 +1606,7 @@ def test_restore_data_source_dump_without_extract_process_restores_from_disk(): 'ANALYZE', ), environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() module.restore_data_source_dump( @@ -1619,6 +1653,7 @@ def test_restore_data_source_dump_with_schemas_restores_schemas(): output_log_level=logging.DEBUG, input_file=None, environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() flexmock(module).should_receive('execute_command').with_args( ( @@ -1632,6 +1667,7 @@ def test_restore_data_source_dump_with_schemas_restores_schemas(): 'ANALYZE', ), environment={'PGSSLMODE': 'disable'}, + working_directory=None, ).once() module.restore_data_source_dump( diff --git a/tests/unit/hooks/data_source/test_sqlite.py b/tests/unit/hooks/data_source/test_sqlite.py index 060eed8c..32090a41 100644 --- a/tests/unit/hooks/data_source/test_sqlite.py +++ b/tests/unit/hooks/data_source/test_sqlite.py @@ -122,6 +122,7 @@ def test_dump_data_sources_with_path_injection_attack_gets_escaped(): ), shell=True, run_to_completion=False, + working_directory=None, ).and_return(processes[0]) flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -179,6 +180,7 @@ def test_dump_data_sources_runs_non_default_sqlite_with_path_injection_attack_ge ), shell=True, run_to_completion=False, + working_directory=None, ).and_return(processes[0]) flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -333,6 +335,7 @@ def test_restore_data_source_dump_restores_database(): processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() flexmock(module.os).should_receive('remove').once() @@ -369,6 +372,7 @@ def test_restore_data_source_dump_runs_non_default_sqlite_restores_database(): processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() flexmock(module.os).should_receive('remove').once() @@ -403,6 +407,7 @@ def test_restore_data_source_dump_with_connection_params_uses_connection_params_ processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() flexmock(module.os).should_receive('remove').once() @@ -437,6 +442,7 @@ def test_restore_data_source_dump_runs_non_default_sqlite_with_connection_params processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() flexmock(module.os).should_receive('remove').once() @@ -474,6 +480,7 @@ def test_restore_data_source_dump_without_connection_params_uses_restore_params_ processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() flexmock(module.os).should_receive('remove').once() @@ -509,6 +516,7 @@ def test_restore_data_source_dump_runs_non_default_sqlite_without_connection_par processes=[extract_process], output_log_level=logging.DEBUG, input_file=extract_process.stdout, + working_directory=None, ).once() flexmock(module.os).should_receive('remove').once() From ce7f0226be36a4eb1418588f54faf57d6abc9c32 Mon Sep 17 00:00:00 2001 From: bockulator Date: Tue, 11 Nov 2025 15:08:01 +0100 Subject: [PATCH 2/2] Mock `get_working_directory()` calls in data source hook tests --- tests/unit/hooks/data_source/test_mariadb.py | 30 ++++++++++++++++++- tests/unit/hooks/data_source/test_mongodb.py | 21 ++++++++++++- tests/unit/hooks/data_source/test_mysql.py | 26 ++++++++++++++++ .../unit/hooks/data_source/test_postgresql.py | 29 +++++++++++++++++- tests/unit/hooks/data_source/test_sqlite.py | 11 ++++++- 5 files changed, 113 insertions(+), 4 deletions(-) diff --git a/tests/unit/hooks/data_source/test_mariadb.py b/tests/unit/hooks/data_source/test_mariadb.py index bcb7f571..995b13be 100644 --- a/tests/unit/hooks/data_source/test_mariadb.py +++ b/tests/unit/hooks/data_source/test_mariadb.py @@ -186,6 +186,9 @@ def test_database_names_to_dump_queries_mariadb_for_database_names(): 'trustsome1', None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return( + '/path/to/working/dir' + ) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'mariadb', @@ -196,7 +199,7 @@ def test_database_names_to_dump_queries_mariadb_for_database_names(): 'show schemas', ), environment=environment, - working_directory=None, + working_directory='/path/to/working/dir', ).and_return('foo\nbar\nmysql\n').once() names = module.database_names_to_dump( @@ -222,6 +225,7 @@ def test_database_names_to_dump_with_database_name_all_and_skip_names_filters_ou 'trustsome1', None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'mariadb', @@ -254,6 +258,7 @@ def test_database_names_to_dump_with_environment_password_transport_skips_defaul ).replace_with(lambda value, config: value) flexmock(module).should_receive('parse_extra_options').and_return((), None) flexmock(module).should_receive('make_defaults_file_options').never() + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'mariadb', @@ -291,6 +296,7 @@ def test_database_names_to_dump_runs_mariadb_with_tls(): 'trustsome1', None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'mariadb', @@ -328,6 +334,7 @@ def test_database_names_to_dump_runs_mariadb_without_tls(): 'trustsome1', None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'mariadb', @@ -704,6 +711,7 @@ def test_database_names_to_dump_runs_mariadb_with_list_options(): 'trustsome1', 'mariadb.cnf', ).and_return(('--defaults-extra-file=/dev/fd/99',)) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'mariadb', @@ -739,6 +747,7 @@ def test_database_names_to_dump_runs_non_default_mariadb_with_list_options(): 'trustsome1', 'mariadb.cnf', ).and_return(('--defaults-extra-file=/dev/fd/99',)) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( environment=None, full_command=( @@ -773,6 +782,7 @@ def test_execute_dump_command_runs_mariadb_dump(): None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -815,6 +825,7 @@ def test_execute_dump_command_with_environment_password_transport_skips_defaults flexmock(module).should_receive('parse_extra_options').and_return((), None) flexmock(module).should_receive('make_defaults_file_options').never() flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -862,6 +873,7 @@ def test_execute_dump_command_runs_mariadb_dump_without_add_drop_database(): None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -907,6 +919,7 @@ def test_execute_dump_command_runs_mariadb_dump_with_hostname_and_port(): None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -959,6 +972,7 @@ def test_execute_dump_command_runs_mariadb_dump_with_tls(): None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -1006,6 +1020,7 @@ def test_execute_dump_command_runs_mariadb_dump_without_tls(): None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -1053,6 +1068,7 @@ def test_execute_dump_command_runs_mariadb_dump_with_username_and_password(): None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -1099,6 +1115,7 @@ def test_execute_dump_command_runs_mariadb_dump_with_options(): None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -1146,6 +1163,7 @@ def test_execute_dump_command_runs_non_default_mariadb_dump_with_options(): None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -1257,6 +1275,7 @@ def test_restore_data_source_dump_runs_mariadb_to_restore(): None, ).and_return(()) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ('mariadb', '--batch'), processes=[extract_process], @@ -1296,6 +1315,7 @@ def test_restore_data_source_dump_runs_mariadb_with_options(): None, ).and_return(()) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ('mariadb', '--harder', '--batch'), processes=[extract_process], @@ -1337,6 +1357,7 @@ def test_restore_data_source_dump_runs_non_default_mariadb_with_options(): None, ).and_return(()) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ('custom_mariadb', '--harder', '--batch'), processes=[extract_process], @@ -1376,6 +1397,7 @@ def test_restore_data_source_dump_runs_mariadb_with_hostname_and_port(): None, ).and_return(()) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'mariadb', @@ -1424,6 +1446,7 @@ def test_restore_data_source_dump_runs_mariadb_with_tls(): None, ).and_return(()) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'mariadb', @@ -1467,6 +1490,7 @@ def test_restore_data_source_dump_runs_mariadb_without_tls(): None, ).and_return(()) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'mariadb', @@ -1510,6 +1534,7 @@ def test_restore_data_source_dump_runs_mariadb_with_username_and_password(): None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ('mariadb', '--defaults-extra-file=/dev/fd/99', '--batch'), processes=[extract_process], @@ -1554,6 +1579,7 @@ def test_restore_data_source_with_environment_password_transport_skips_defaults_ flexmock(module.os).should_receive('environ').and_return( {'USER': 'root', 'MYSQL_PWD': 'trustsome1'}, ) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ('mariadb', '--batch', '--user', 'root'), processes=[extract_process], @@ -1603,6 +1629,7 @@ def test_restore_data_source_dump_with_connection_params_uses_connection_params_ None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'mariadb', @@ -1666,6 +1693,7 @@ def test_restore_data_source_dump_without_connection_params_uses_restore_params_ None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'mariadb', diff --git a/tests/unit/hooks/data_source/test_mongodb.py b/tests/unit/hooks/data_source/test_mongodb.py index dc474176..1eaaaeeb 100644 --- a/tests/unit/hooks/data_source/test_mongodb.py +++ b/tests/unit/hooks/data_source/test_mongodb.py @@ -31,13 +31,16 @@ def test_dump_data_sources_runs_mongodump_for_each_database(): 'databases/localhost/foo', ).and_return('databases/localhost/bar') flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return( + '/path/to/working/dir' + ) for name, process in zip(('foo', 'bar'), processes): flexmock(module).should_receive('execute_command').with_args( ('mongodump', '--db', name, '--archive', '>', f'databases/localhost/{name}'), shell=True, run_to_completion=False, - working_directory=None, + working_directory='/path/to/working/dir', ).and_return(process).once() flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( @@ -101,6 +104,7 @@ def test_dump_data_sources_runs_mongodump_with_hostname_and_port(): 'databases/database.example.org/foo', ) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -167,6 +171,7 @@ def test_dump_data_sources_runs_mongodump_with_username_and_password(): '/dev/fd/99', ) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -220,6 +225,7 @@ def test_dump_data_sources_runs_mongodump_with_directory_format(): ) flexmock(module.dump).should_receive('create_parent_directory_for_dump') flexmock(module.dump).should_receive('create_named_pipe_for_dump').never() + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ('mongodump', '--out', 'databases/localhost/foo', '--db', 'foo'), @@ -262,6 +268,7 @@ def test_dump_data_sources_runs_mongodump_with_options(): 'databases/localhost/foo', ) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -310,6 +317,7 @@ def test_dump_data_sources_runs_mongodumpall_for_all_databases(): 'databases/localhost/all', ) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ('mongodump', '--archive', '>', 'databases/localhost/all'), @@ -379,6 +387,7 @@ def test_restore_data_source_dump_runs_mongorestore(): flexmock(module.borgmatic.hooks.credential.parse).should_receive( 'resolve_credential', ).replace_with(lambda value, config: value) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ['mongorestore', '--archive', '--drop'], processes=[extract_process], @@ -414,6 +423,7 @@ def test_restore_data_source_dump_runs_mongorestore_with_hostname_and_port(): flexmock(module.borgmatic.hooks.credential.parse).should_receive( 'resolve_credential', ).replace_with(lambda value, config: value) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( [ 'mongorestore', @@ -466,6 +476,7 @@ def test_restore_data_source_dump_runs_mongorestore_with_username_and_password() flexmock(module).should_receive('make_password_config_file').with_args('trustsome1').and_return( '/dev/fd/99', ) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( [ 'mongorestore', @@ -524,6 +535,7 @@ def test_restore_data_source_dump_with_connection_params_uses_connection_params_ flexmock(module).should_receive('make_password_config_file').with_args( 'clipassword', ).and_return('/dev/fd/99') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( [ 'mongorestore', @@ -586,6 +598,7 @@ def test_restore_data_source_dump_without_connection_params_uses_restore_params_ flexmock(module).should_receive('make_password_config_file').with_args( 'restorepass', ).and_return('/dev/fd/99') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( [ 'mongorestore', @@ -633,6 +646,7 @@ def test_restore_data_source_dump_runs_mongorestore_with_options(): flexmock(module.borgmatic.hooks.credential.parse).should_receive( 'resolve_credential', ).replace_with(lambda value, config: value) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ['mongorestore', '--archive', '--drop', '--harder'], processes=[extract_process], @@ -666,6 +680,7 @@ def test_restore_databases_dump_runs_mongorestore_with_schemas(): flexmock(module.borgmatic.hooks.credential.parse).should_receive( 'resolve_credential', ).replace_with(lambda value, config: value) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( [ 'mongorestore', @@ -707,6 +722,7 @@ def test_restore_data_source_dump_runs_psql_for_all_database_dump(): flexmock(module.borgmatic.hooks.credential.parse).should_receive( 'resolve_credential', ).replace_with(lambda value, config: value) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ['mongorestore', '--archive'], processes=[extract_process], @@ -765,6 +781,7 @@ def test_restore_data_source_dump_without_extract_process_restores_from_disk(): flexmock(module.borgmatic.hooks.credential.parse).should_receive( 'resolve_credential', ).replace_with(lambda value, config: value) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ['mongorestore', '--dir', '/dump/path', '--drop'], processes=[], @@ -800,6 +817,7 @@ def test_dump_data_sources_uses_custom_mongodump_command(): 'databases/localhost/foo', ) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -871,6 +889,7 @@ def test_restore_data_source_dump_uses_custom_mongorestore_command(): flexmock(module.borgmatic.hooks.credential.parse).should_receive( 'resolve_credential', ).replace_with(lambda value, config: value) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( [ 'custom_mongorestore', # Should use custom command instead of default diff --git a/tests/unit/hooks/data_source/test_mysql.py b/tests/unit/hooks/data_source/test_mysql.py index a62d766d..be24de1e 100644 --- a/tests/unit/hooks/data_source/test_mysql.py +++ b/tests/unit/hooks/data_source/test_mysql.py @@ -68,6 +68,7 @@ def test_database_names_to_dump_queries_mysql_for_database_names(): flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive( 'make_defaults_file_options', ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',)) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'mysql', @@ -108,6 +109,7 @@ def test_database_names_to_dump_with_database_name_all_and_skip_names_filters_ou 'trustsome1', None, ).and_return(('--defaults-extra-file=/dev/fd/99',)) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'mysql', @@ -144,6 +146,7 @@ def test_database_names_to_dump_with_environment_password_transport_skips_defaul flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive( 'make_defaults_file_options', ).never() + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'mysql', @@ -181,6 +184,7 @@ def test_database_names_to_dump_runs_mysql_with_tls(): flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive( 'make_defaults_file_options', ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',)) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'mysql', @@ -218,6 +222,7 @@ def test_database_names_to_dump_runs_mysql_without_tls(): flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive( 'make_defaults_file_options', ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',)) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'mysql', @@ -589,6 +594,7 @@ def test_database_names_to_dump_runs_mysql_with_list_options(): flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive( 'make_defaults_file_options', ).with_args('root', 'trustsome1', 'my.cnf').and_return(('--defaults-extra-file=/dev/fd/99',)) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'mysql', @@ -621,6 +627,7 @@ def test_database_names_to_dump_runs_non_default_mysql_with_list_options(): flexmock(module.borgmatic.hooks.data_source.mariadb).should_receive( 'make_defaults_file_options', ).with_args('root', 'trustsome1', 'my.cnf').and_return(('--defaults-extra-file=/dev/fd/99',)) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( environment=None, full_command=( @@ -655,6 +662,7 @@ def test_execute_dump_command_runs_mysqldump(): 'make_defaults_file_options', ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -701,6 +709,7 @@ def test_execute_dump_command_with_environment_password_transport_skips_defaults 'make_defaults_file_options', ).never() flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -748,6 +757,7 @@ def test_execute_dump_command_runs_mysqldump_without_add_drop_database(): 'make_defaults_file_options', ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -793,6 +803,7 @@ def test_execute_dump_command_runs_mysqldump_with_hostname_and_port(): 'make_defaults_file_options', ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -845,6 +856,7 @@ def test_execute_dump_command_runs_mysqldump_with_tls(): 'make_defaults_file_options', ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -892,6 +904,7 @@ def test_execute_dump_command_runs_mysqldump_without_tls(): 'make_defaults_file_options', ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -939,6 +952,7 @@ def test_execute_dump_command_runs_mysqldump_with_username_and_password(): 'make_defaults_file_options', ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -985,6 +999,7 @@ def test_execute_dump_command_runs_mysqldump_with_options(): 'make_defaults_file_options', ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -1032,6 +1047,7 @@ def test_execute_dump_command_runs_non_default_mysqldump(): 'make_defaults_file_options', ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -1141,6 +1157,7 @@ def test_restore_data_source_dump_runs_mysql_to_restore(): 'make_defaults_file_options', ).with_args(None, None, None).and_return(()) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ('mysql', '--batch'), processes=[extract_process], @@ -1180,6 +1197,7 @@ def test_restore_data_source_dump_runs_mysql_with_options(): 'make_defaults_file_options', ).with_args(None, None, None).and_return(()) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ('mysql', '--harder', '--batch'), processes=[extract_process], @@ -1219,6 +1237,7 @@ def test_restore_data_source_dump_runs_non_default_mysql_with_options(): 'make_defaults_file_options', ).with_args(None, None, None).and_return(()) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ('custom_mysql', '--harder', '--batch'), processes=[extract_process], @@ -1258,6 +1277,7 @@ def test_restore_data_source_dump_runs_mysql_with_hostname_and_port(): 'make_defaults_file_options', ).with_args(None, None, None).and_return(()) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'mysql', @@ -1306,6 +1326,7 @@ def test_restore_data_source_dump_runs_mysql_with_tls(): 'make_defaults_file_options', ).with_args(None, None, None).and_return(()) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'mysql', @@ -1349,6 +1370,7 @@ def test_restore_data_source_dump_runs_mysql_without_tls(): 'make_defaults_file_options', ).with_args(None, None, None).and_return(()) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'mysql', @@ -1392,6 +1414,7 @@ def test_restore_data_source_dump_runs_mysql_with_username_and_password(): 'make_defaults_file_options', ).with_args('root', 'trustsome1', None).and_return(('--defaults-extra-file=/dev/fd/99',)) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ('mysql', '--defaults-extra-file=/dev/fd/99', '--batch'), processes=[extract_process], @@ -1440,6 +1463,7 @@ def test_restore_data_source_with_environment_password_transport_skips_defaults_ flexmock(module.os).should_receive('environ').and_return( {'USER': 'root', 'MYSQL_PWD': 'trustsome1'}, ) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ('mysql', '--batch', '--user', 'root'), processes=[extract_process], @@ -1491,6 +1515,7 @@ def test_restore_data_source_dump_with_connection_params_uses_connection_params_ ('--defaults-extra-file=/dev/fd/99',), ) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'mysql', @@ -1556,6 +1581,7 @@ def test_restore_data_source_dump_without_connection_params_uses_restore_params_ ('--defaults-extra-file=/dev/fd/99',), ) flexmock(module.os).should_receive('environ').and_return({'USER': 'root'}) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'mysql', diff --git a/tests/unit/hooks/data_source/test_postgresql.py b/tests/unit/hooks/data_source/test_postgresql.py index c70a8bd0..03bd8f8c 100644 --- a/tests/unit/hooks/data_source/test_postgresql.py +++ b/tests/unit/hooks/data_source/test_postgresql.py @@ -117,6 +117,9 @@ def test_database_names_to_dump_with_all_and_format_lists_databases_with_hostnam flexmock(module.borgmatic.hooks.credential.parse).should_receive( 'resolve_credential', ).replace_with(lambda value, config: value) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return( + '/path/to/working/dir' + ) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'psql', @@ -131,7 +134,7 @@ def test_database_names_to_dump_with_all_and_format_lists_databases_with_hostnam '1234', ), environment=object, - working_directory=None, + working_directory='/path/to/working/dir', ).and_return('foo,test,\nbar,test,"stuff and such"') assert module.database_names_to_dump(database, {}, flexmock(), dry_run=False) == ( @@ -145,6 +148,7 @@ def test_database_names_to_dump_with_all_and_format_lists_databases_with_usernam flexmock(module.borgmatic.hooks.credential.parse).should_receive( 'resolve_credential', ).replace_with(lambda value, config: value) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'psql', @@ -171,6 +175,7 @@ def test_database_names_to_dump_with_all_and_format_lists_databases_with_options flexmock(module.borgmatic.hooks.credential.parse).should_receive( 'resolve_credential', ).replace_with(lambda value, config: value) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ('psql', '--list', '--no-password', '--no-psqlrc', '--csv', '--tuples-only', '--harder'), environment=object, @@ -204,6 +209,7 @@ def test_database_names_to_dump_with_all_and_psql_command_uses_custom_command(): flexmock(module.borgmatic.hooks.credential.parse).should_receive( 'resolve_credential', ).replace_with(lambda value, config: value) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_and_capture_output').with_args( ( 'docker', @@ -259,6 +265,7 @@ def test_dump_data_sources_runs_pg_dump_for_each_database(): 'resolve_credential', ).replace_with(lambda value, config: value) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) for name, process in zip(('foo', 'bar'), processes): flexmock(module).should_receive('execute_command').with_args( @@ -433,6 +440,7 @@ def test_dump_data_sources_runs_pg_dump_with_hostname_and_port(): 'resolve_credential', ).replace_with(lambda value, config: value) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -498,6 +506,7 @@ def test_dump_data_sources_runs_pg_dump_with_username_and_password(): 'resolve_credential', ).replace_with(lambda value, config: value) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -559,6 +568,7 @@ def test_dump_data_sources_with_username_injection_attack_gets_escaped(): 'resolve_credential', ).replace_with(lambda value, config: value) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -618,6 +628,7 @@ def test_dump_data_sources_runs_pg_dump_with_directory_format(): ).replace_with(lambda value, config: value) flexmock(module.dump).should_receive('create_parent_directory_for_dump') flexmock(module.dump).should_receive('create_named_pipe_for_dump').never() + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -677,6 +688,7 @@ def test_dump_data_sources_runs_pg_dump_with_string_compression(): 'resolve_credential', ).replace_with(lambda value, config: value) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -739,6 +751,7 @@ def test_dump_data_sources_runs_pg_dump_with_integer_compression(): 'resolve_credential', ).replace_with(lambda value, config: value) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -801,6 +814,7 @@ def test_dump_data_sources_runs_pg_dump_with_options(): 'resolve_credential', ).replace_with(lambda value, config: value) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -859,6 +873,7 @@ def test_dump_data_sources_runs_pg_dumpall_for_all_databases(): 'resolve_credential', ).replace_with(lambda value, config: value) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ('pg_dumpall', '--no-password', '--clean', '--if-exists', '>', 'databases/localhost/all'), @@ -906,6 +921,7 @@ def test_dump_data_sources_runs_non_default_pg_dump(): 'resolve_credential', ).replace_with(lambda value, config: value) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( @@ -961,6 +977,7 @@ def test_restore_data_source_dump_runs_pg_restore(): flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_data_source_dump_filename') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'pg_restore', @@ -1020,6 +1037,7 @@ def test_restore_data_source_dump_runs_pg_restore_with_hostname_and_port(): flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_data_source_dump_filename') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'pg_restore', @@ -1089,6 +1107,7 @@ def test_restore_data_source_dump_runs_pg_restore_with_username_and_password(): ) flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_data_source_dump_filename') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'pg_restore', @@ -1165,6 +1184,7 @@ def test_restore_data_source_dump_with_connection_params_uses_connection_params_ ) flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_data_source_dump_filename') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'pg_restore', @@ -1249,6 +1269,7 @@ def test_restore_data_source_dump_without_connection_params_uses_restore_params_ ) flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_data_source_dump_filename') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'pg_restore', @@ -1325,6 +1346,7 @@ def test_restore_data_source_dump_runs_pg_restore_with_options(): flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_data_source_dump_filename') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'pg_restore', @@ -1384,6 +1406,7 @@ def test_restore_data_source_dump_runs_psql_for_all_database_dump(): flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_data_source_dump_filename') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'psql', @@ -1428,6 +1451,7 @@ def test_restore_data_source_dump_runs_psql_for_plain_database_dump(): flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_data_source_dump_filename') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ('psql', '--no-password', '--no-psqlrc', '--dbname', 'foo'), processes=[extract_process], @@ -1484,6 +1508,7 @@ def test_restore_data_source_dump_runs_non_default_pg_restore_and_psql(): flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_data_source_dump_filename') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'docker', @@ -1577,6 +1602,7 @@ def test_restore_data_source_dump_without_extract_process_restores_from_disk(): flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'pg_restore', @@ -1634,6 +1660,7 @@ def test_restore_data_source_dump_with_schemas_restores_schemas(): flexmock(module).should_receive('make_environment').and_return({'PGSSLMODE': 'disable'}) flexmock(module).should_receive('make_dump_path') flexmock(module.dump).should_receive('make_data_source_dump_filename').and_return('/dump/path') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'pg_restore', diff --git a/tests/unit/hooks/data_source/test_sqlite.py b/tests/unit/hooks/data_source/test_sqlite.py index 32090a41..9feab9db 100644 --- a/tests/unit/hooks/data_source/test_sqlite.py +++ b/tests/unit/hooks/data_source/test_sqlite.py @@ -111,6 +111,9 @@ def test_dump_data_sources_with_path_injection_attack_gets_escaped(): ) flexmock(module.os.path).should_receive('exists').and_return(False) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return( + '/path/to/working/dir' + ) flexmock(module).should_receive('execute_command').with_args( ( 'sqlite3', @@ -122,7 +125,7 @@ def test_dump_data_sources_with_path_injection_attack_gets_escaped(): ), shell=True, run_to_completion=False, - working_directory=None, + working_directory='/path/to/working/dir', ).and_return(processes[0]) flexmock(module.dump).should_receive('write_data_source_dumps_metadata').with_args( '/run/borgmatic', @@ -168,6 +171,7 @@ def test_dump_data_sources_runs_non_default_sqlite_with_path_injection_attack_ge ) flexmock(module.os.path).should_receive('exists').and_return(False) flexmock(module.dump).should_receive('create_named_pipe_for_dump') + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command').with_args( ( 'custom_sqlite', # custom sqlite command @@ -326,6 +330,7 @@ def test_restore_data_source_dump_restores_database(): hook_config = [{'path': '/path/to/database', 'name': 'database'}, {'name': 'other'}] extract_process = flexmock(stdout=flexmock()) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'sqlite3', @@ -362,6 +367,7 @@ def test_restore_data_source_dump_runs_non_default_sqlite_restores_database(): ] extract_process = flexmock(stdout=flexmock()) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'custom_sqlite', @@ -398,6 +404,7 @@ def test_restore_data_source_dump_with_connection_params_uses_connection_params_ ] extract_process = flexmock(stdout=flexmock()) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'sqlite3', @@ -433,6 +440,7 @@ def test_restore_data_source_dump_runs_non_default_sqlite_with_connection_params ] extract_process = flexmock(stdout=flexmock()) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'custom_sqlite', @@ -507,6 +515,7 @@ def test_restore_data_source_dump_runs_non_default_sqlite_without_connection_par ] extract_process = flexmock(stdout=flexmock()) + flexmock(module.borgmatic.config.paths).should_receive('get_working_directory').and_return(None) flexmock(module).should_receive('execute_command_with_processes').with_args( ( 'custom_sqlite',