From 17c2d109e57c7050fcc6003960494b0755135de5 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sun, 21 Jun 2020 14:41:22 -0700 Subject: [PATCH] Add tests for pass-through of BORG_* environment variables. --- NEWS | 1 + borgmatic/borg/environment.py | 5 ++++- tests/unit/borg/test_environment.py | 16 +++++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 4699d3d..de82c4f 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,5 @@ 1.5.7.dev0 + * #327: Fix broken pass-through of BORG_* environment variables to Borg. * #331: Add SSL support to PostgreSQL database configuration. * #333: Fix for potential data loss (data not getting backed up) when borgmatic omitted configured source directories in certain situations. Specifically, this occurred when two source directories diff --git a/borgmatic/borg/environment.py b/borgmatic/borg/environment.py index edd7b9d..a37c1b5 100644 --- a/borgmatic/borg/environment.py +++ b/borgmatic/borg/environment.py @@ -19,8 +19,11 @@ DEFAULT_BOOL_OPTION_TO_ENVIRONMENT_VARIABLE = { def initialize(storage_config): for option_name, environment_variable_name in OPTION_TO_ENVIRONMENT_VARIABLE.items(): - # Options from the config.yaml file have precedence over already set env variables: + + # Options from borgmatic configuration take precedence over already set BORG_* environment + # variables. value = storage_config.get(option_name) or os.environ.get(environment_variable_name) + if value: os.environ[environment_variable_name] = value else: diff --git a/tests/unit/borg/test_environment.py b/tests/unit/borg/test_environment.py index c70d061..5a08e76 100644 --- a/tests/unit/borg/test_environment.py +++ b/tests/unit/borg/test_environment.py @@ -62,13 +62,23 @@ def test_initialize_with_relocated_repo_access_should_override_default(): os.environ = orig_environ -def test_initialize_is_not_affected_by_existing_environment(): +def test_initialize_prefers_configuration_option_over_borg_environment_variable(): orig_environ = os.environ try: - os.environ = {'BORG_PASSPHRASE': 'pass', 'BORG_SSH': 'mosh'} + os.environ = {'BORG_SSH': 'mosh'} module.initialize({'ssh_command': 'ssh -C'}) - assert 'BORG_PASSPHRASE' not in os.environ assert os.environ.get('BORG_RSH') == 'ssh -C' finally: os.environ = orig_environ + + +def test_initialize_passes_through_existing_borg_environment_variable(): + orig_environ = os.environ + + try: + os.environ = {'BORG_PASSPHRASE': 'pass'} + module.initialize({'ssh_command': 'ssh -C'}) + assert os.environ.get('BORG_PASSPHRASE') == 'pass' + finally: + os.environ = orig_environ