Преглед изворни кода

Fix a few tests to mock more accurately.

Dan Helfman пре 3 месеци
родитељ
комит
2280bb26b6
2 измењених фајлова са 29 додато и 25 уклоњено
  1. 3 3
      borgmatic/borg/environment.py
  2. 26 22
      tests/unit/borg/test_environment.py

+ 3 - 3
borgmatic/borg/environment.py

@@ -27,7 +27,7 @@ DEFAULT_BOOL_OPTION_TO_UPPERCASE_ENVIRONMENT_VARIABLE = {
 def make_environment(config):
     '''
     Given a borgmatic configuration dict, convert it to a Borg environment variable dict, merge it
-    with a copy of the current environment variables and return the result.
+    with a copy of the current environment variables, and return the result.
 
     Do not reuse this environment across multiple Borg invocations, because it can include
     references to resources like anonymous pipes for passphrases—which can only be consumed once.
@@ -40,8 +40,8 @@ def make_environment(config):
 
     In borgmatic, we want to simulate this precedence order, but there are some additional
     complications. First, values can come from either configuration or from environment variables
-    set outside borgmatic and configured options should take precedence. Second, when borgmatic gets
-    a passphrase—directly from configuration or indirectly via a credential hook or a passcommand—we
+    set outside borgmatic; configured options should take precedence. Second, when borgmatic gets a
+    passphrase—directly from configuration or indirectly via a credential hook or a passcommand—we
     want to pass that passphrase to Borg via an anonymous pipe (+ BORG_PASSPHRASE_FD), since that's
     more secure than using an environment variable (BORG_PASSPHRASE).
     '''

+ 26 - 22
tests/unit/borg/test_environment.py

@@ -5,6 +5,9 @@ from borgmatic.borg import environment as module
 
 def test_make_environment_with_passcommand_should_call_it_and_set_passphrase_file_descriptor_in_environment():
     flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
+    flexmock(module.borgmatic.hooks.credential.parse).should_receive(
+        'resolve_credential'
+    ).and_return(None)
     flexmock(module.borgmatic.borg.passcommand).should_receive(
         'get_passphrase_from_passcommand'
     ).and_return('passphrase')
@@ -21,12 +24,12 @@ def test_make_environment_with_passcommand_should_call_it_and_set_passphrase_fil
 
 def test_make_environment_with_passphrase_should_set_passphrase_file_descriptor_in_environment():
     flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
-    flexmock(module.borgmatic.borg.passcommand).should_receive(
-        'get_passphrase_from_passcommand'
-    ).and_return(None)
     flexmock(module.borgmatic.hooks.credential.parse).should_receive(
         'resolve_credential'
     ).replace_with(lambda value, config: value)
+    flexmock(module.borgmatic.borg.passcommand).should_receive(
+        'get_passphrase_from_passcommand'
+    ).and_return(None)
     flexmock(module.os).should_receive('pipe').and_return((3, 4))
     flexmock(module.os).should_receive('write')
     flexmock(module.os).should_receive('close')
@@ -41,12 +44,12 @@ def test_make_environment_with_passphrase_should_set_passphrase_file_descriptor_
 def test_make_environment_with_credential_tag_passphrase_should_load_it_and_set_passphrase_file_descriptor_in_environment():
     flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
     config = {'encryption_passphrase': '{credential systemd pass}'}
-    flexmock(module.borgmatic.borg.passcommand).should_receive(
-        'get_passphrase_from_passcommand'
-    ).and_return(None)
     flexmock(module.borgmatic.hooks.credential.parse).should_receive(
         'resolve_credential',
     ).with_args('{credential systemd pass}', config).and_return('pass')
+    flexmock(module.borgmatic.borg.passcommand).should_receive(
+        'get_passphrase_from_passcommand'
+    ).never()
     flexmock(module.os).should_receive('pipe').and_return((3, 4))
     flexmock(module.os).should_receive('write')
     flexmock(module.os).should_receive('close')
@@ -60,8 +63,8 @@ def test_make_environment_with_credential_tag_passphrase_should_load_it_and_set_
 
 def test_make_environment_with_ssh_command_should_set_environment():
     flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
-    flexmock(module.borgmatic.borg.passcommand).should_receive(
-        'get_passphrase_from_passcommand'
+    flexmock(module.borgmatic.hooks.credential.parse).should_receive(
+        'resolve_credential'
     ).and_return(None)
     flexmock(module.os).should_receive('pipe').never()
     environment = module.make_environment({'ssh_command': 'ssh -C'})
@@ -71,8 +74,8 @@ def test_make_environment_with_ssh_command_should_set_environment():
 
 def test_make_environment_without_configuration_sets_certain_environment_variables():
     flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
-    flexmock(module.borgmatic.borg.passcommand).should_receive(
-        'get_passphrase_from_passcommand'
+    flexmock(module.borgmatic.hooks.credential.parse).should_receive(
+        'resolve_credential'
     ).and_return(None)
     flexmock(module.os).should_receive('pipe').never()
     environment = module.make_environment({})
@@ -94,8 +97,8 @@ def test_make_environment_without_configuration_passes_through_default_environme
             'BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK': 'nah',
         }
     )
-    flexmock(module.borgmatic.borg.passcommand).should_receive(
-        'get_passphrase_from_passcommand'
+    flexmock(module.borgmatic.hooks.credential.parse).should_receive(
+        'resolve_credential'
     ).and_return(None)
     flexmock(module.os).should_receive('pipe').never()
     environment = module.make_environment({})
@@ -110,8 +113,8 @@ def test_make_environment_without_configuration_passes_through_default_environme
 
 def test_make_environment_with_relocated_repo_access_true_should_set_environment_yes():
     flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
-    flexmock(module.borgmatic.borg.passcommand).should_receive(
-        'get_passphrase_from_passcommand'
+    flexmock(module.borgmatic.hooks.credential.parse).should_receive(
+        'resolve_credential'
     ).and_return(None)
     flexmock(module.os).should_receive('pipe').never()
     environment = module.make_environment({'relocated_repo_access_is_ok': True})
@@ -121,8 +124,8 @@ def test_make_environment_with_relocated_repo_access_true_should_set_environment
 
 def test_make_environment_with_relocated_repo_access_false_should_set_environment_no():
     flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
-    flexmock(module.borgmatic.borg.passcommand).should_receive(
-        'get_passphrase_from_passcommand'
+    flexmock(module.borgmatic.hooks.credential.parse).should_receive(
+        'resolve_credential'
     ).and_return(None)
     flexmock(module.os).should_receive('pipe').never()
     environment = module.make_environment({'relocated_repo_access_is_ok': False})
@@ -132,8 +135,8 @@ def test_make_environment_with_relocated_repo_access_false_should_set_environmen
 
 def test_make_environment_check_i_know_what_i_am_doing_true_should_set_environment_YES():
     flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
-    flexmock(module.borgmatic.borg.passcommand).should_receive(
-        'get_passphrase_from_passcommand'
+    flexmock(module.borgmatic.hooks.credential.parse).should_receive(
+        'resolve_credential'
     ).and_return(None)
     flexmock(module.os).should_receive('pipe').never()
     environment = module.make_environment({'check_i_know_what_i_am_doing': True})
@@ -143,8 +146,8 @@ def test_make_environment_check_i_know_what_i_am_doing_true_should_set_environme
 
 def test_make_environment_check_i_know_what_i_am_doing_false_should_set_environment_NO():
     flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
-    flexmock(module.borgmatic.borg.passcommand).should_receive(
-        'get_passphrase_from_passcommand'
+    flexmock(module.borgmatic.hooks.credential.parse).should_receive(
+        'resolve_credential'
     ).and_return(None)
     flexmock(module.os).should_receive('pipe').never()
     environment = module.make_environment({'check_i_know_what_i_am_doing': False})
@@ -154,9 +157,10 @@ def test_make_environment_check_i_know_what_i_am_doing_false_should_set_environm
 
 def test_make_environment_with_integer_variable_value():
     flexmock(module.os).should_receive('environ').and_return({'USER': 'root'})
-    flexmock(module.borgmatic.borg.passcommand).should_receive(
-        'get_passphrase_from_passcommand'
+    flexmock(module.borgmatic.hooks.credential.parse).should_receive(
+        'resolve_credential'
     ).and_return(None)
     flexmock(module.os).should_receive('pipe').never()
     environment = module.make_environment({'borg_files_cache_ttl': 40})
+
     assert environment.get('BORG_FILES_CACHE_TTL') == '40'