Răsfoiți Sursa

Fix borgmatic ignoring the "BORG_RELOCATED_REPO_ACCESS_IS_OK" and "BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK" environment variables (#939).

Dan Helfman 6 luni în urmă
părinte
comite
30c821120e
3 a modificat fișierele cu 30 adăugiri și 3 ștergeri
  1. 2 0
      NEWS
  2. 5 2
      borgmatic/borg/environment.py
  3. 23 1
      tests/unit/borg/test_environment.py

+ 2 - 0
NEWS

@@ -9,6 +9,8 @@
    https://torsion.org/borgmatic/docs/how-to/backup-your-databases/#runtime-directory
  * #934: Add the "RuntimeDirectory" and "StateDirectory" options to the sample systemd service
    file to support the new runtime and state directory logic.
+ * #939: Fix borgmatic ignoring the "BORG_RELOCATED_REPO_ACCESS_IS_OK" and
+   "BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK" environment variables.
 
 1.9.1
  * #928: Fix the user runtime directory location on macOS (and possibly Cygwin).

+ 5 - 2
borgmatic/borg/environment.py

@@ -1,3 +1,5 @@
+import os
+
 OPTION_TO_ENVIRONMENT_VARIABLE = {
     'borg_base_directory': 'BORG_BASE_DIR',
     'borg_config_directory': 'BORG_CONFIG_DIR',
@@ -38,8 +40,9 @@ def make_environment(config):
         option_name,
         environment_variable_name,
     ) in DEFAULT_BOOL_OPTION_TO_DOWNCASE_ENVIRONMENT_VARIABLE.items():
-        value = config.get(option_name)
-        environment[environment_variable_name] = 'yes' if value else 'no'
+        if os.environ.get(environment_variable_name) is None:
+            value = config.get(option_name)
+            environment[environment_variable_name] = 'yes' if value else 'no'
 
     for (
         option_name,

+ 23 - 1
tests/unit/borg/test_environment.py

@@ -1,3 +1,5 @@
+from flexmock import flexmock
+
 from borgmatic.borg import environment as module
 
 
@@ -8,21 +10,24 @@ def test_make_environment_with_passcommand_should_set_environment():
 
 
 def test_make_environment_with_passphrase_should_set_environment():
+    flexmock(module.os.environ).should_receive('get').and_return(None)
     environment = module.make_environment({'encryption_passphrase': 'pass'})
 
     assert environment.get('BORG_PASSPHRASE') == 'pass'
 
 
 def test_make_environment_with_ssh_command_should_set_environment():
+    flexmock(module.os.environ).should_receive('get').and_return(None)
     environment = module.make_environment({'ssh_command': 'ssh -C'})
 
     assert environment.get('BORG_RSH') == 'ssh -C'
 
 
 def test_make_environment_without_configuration_sets_certain_environment_variables():
+    flexmock(module.os.environ).should_receive('get').and_return(None)
     environment = module.make_environment({})
 
-    # borgmatic always sets this Borg environment variable.
+    # Default environment variables.
     assert environment == {
         'BORG_EXIT_CODES': 'modern',
         'BORG_RELOCATED_REPO_ACCESS_IS_OK': 'no',
@@ -30,30 +35,47 @@ def test_make_environment_without_configuration_sets_certain_environment_variabl
     }
 
 
+def test_make_environment_without_configuration_does_not_set_certain_environment_variables_if_already_set():
+    flexmock(module.os.environ).should_receive('get').with_args(
+        'BORG_RELOCATED_REPO_ACCESS_IS_OK'
+    ).and_return('yup')
+    flexmock(module.os.environ).should_receive('get').with_args(
+        'BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK'
+    ).and_return('nah')
+    environment = module.make_environment({})
+
+    assert environment == {'BORG_EXIT_CODES': 'modern'}
+
+
 def test_make_environment_with_relocated_repo_access_true_should_set_environment_yes():
+    flexmock(module.os.environ).should_receive('get').and_return(None)
     environment = module.make_environment({'relocated_repo_access_is_ok': True})
 
     assert environment.get('BORG_RELOCATED_REPO_ACCESS_IS_OK') == 'yes'
 
 
 def test_make_environment_with_relocated_repo_access_false_should_set_environment_no():
+    flexmock(module.os.environ).should_receive('get').and_return(None)
     environment = module.make_environment({'relocated_repo_access_is_ok': False})
 
     assert environment.get('BORG_RELOCATED_REPO_ACCESS_IS_OK') == 'no'
 
 
 def test_make_environment_check_i_know_what_i_am_doing_true_should_set_environment_YES():
+    flexmock(module.os.environ).should_receive('get').and_return(None)
     environment = module.make_environment({'check_i_know_what_i_am_doing': True})
 
     assert environment.get('BORG_CHECK_I_KNOW_WHAT_I_AM_DOING') == 'YES'
 
 
 def test_make_environment_check_i_know_what_i_am_doing_false_should_set_environment_NO():
+    flexmock(module.os.environ).should_receive('get').and_return(None)
     environment = module.make_environment({'check_i_know_what_i_am_doing': False})
 
     assert environment.get('BORG_CHECK_I_KNOW_WHAT_I_AM_DOING') == 'NO'
 
 
 def test_make_environment_with_integer_variable_value():
+    flexmock(module.os.environ).should_receive('get').and_return(None)
     environment = module.make_environment({'borg_files_cache_ttl': 40})
     assert environment.get('BORG_FILES_CACHE_TTL') == '40'