Преглед на файлове

Fix a regression that broke environment variable interpolation (#1062).

Dan Helfman преди 1 месец
родител
ревизия
2c8dc5858f
променени са 4 файла, в които са добавени 76 реда и са изтрити 2 реда
  1. 1 0
      NEWS
  2. 1 1
      borgmatic/commands/arguments.py
  3. 1 1
      borgmatic/commands/borgmatic.py
  4. 73 0
      tests/end-to-end/test_environment_variable.py

+ 1 - 0
NEWS

@@ -7,6 +7,7 @@
  * #1060: Fix action command hooks getting run too many times when multiple borgmatic actions are
    executed (implicitly or explicitly).
  * #1060: Don't run action command hooks for actions listed in the "skip_actions" option.
+ * #1062: Fix a regression that broke environment variable interpolation.
 
 2.0.1
  * #1057: Fix argument parsing to avoid using Python 3.12+ string features. Now borgmatic will

+ 1 - 1
borgmatic/commands/arguments.py

@@ -587,7 +587,7 @@ def make_parsers(schema, unparsed_arguments):
         '--no-environment-interpolation',
         dest='resolve_env',
         action='store_false',
-        help='Do not resolve environment variables in configuration file',
+        help='Do not resolve environment variables in configuration files',
     )
     global_group.add_argument(
         '--bash-completion',

+ 1 - 1
borgmatic/commands/borgmatic.py

@@ -1040,7 +1040,7 @@ def main(extra_summary_logs=[]):  # pragma: no cover
         config_filenames,
         arguments,
         global_arguments.overrides,
-        resolve_env=global_arguments.resolve_env and not validate,
+        resolve_env=global_arguments.resolve_env and not arguments.get('validate'),
     )
 
     # Use the helper function to check and show help on no arguments, passing the preloaded configs

+ 73 - 0
tests/end-to-end/test_environment_variable.py

@@ -0,0 +1,73 @@
+import json
+import os
+import shutil
+import subprocess
+import sys
+import tempfile
+
+import pytest
+
+
+def generate_configuration(config_path, repository_path):
+    '''
+    Generate borgmatic configuration into a file at the config path, and update the defaults so as
+    to work for testing, including updating the source directories, injecting the given repository
+    path, and tacking on an encryption passphrase environment variable (which will get passed in
+    when the Borg command is called below).
+    '''
+    subprocess.check_call(f'borgmatic config generate --destination {config_path}'.split(' '))
+    config = (
+        open(config_path)
+        .read()
+        .replace('ssh://user@backupserver/./sourcehostname.borg', repository_path)
+        .replace('- path: /mnt/backup', '')
+        .replace('label: local', '')
+        .replace('- /home/user/path with spaces', '')
+        .replace('- /home', f'- {config_path}')
+        .replace('- /etc', '')
+        .replace('- /var/log/syslog*', '')
+        + '\nencryption_passphrase: "${PASSPHRASE}"'
+        # Disable automatic storage of config files so we can test storage and extraction manually.
+        + '\nbootstrap:\n  store_config_files: false'
+    )
+    config_file = open(config_path, 'w')
+    config_file.write(config)
+    config_file.close()
+
+
+def test_borgmatic_command():
+    # Create a Borg repository.
+    temporary_directory = tempfile.mkdtemp()
+    repository_path = os.path.join(temporary_directory, 'test.borg')
+    extract_path = os.path.join(temporary_directory, 'extract')
+
+    original_working_directory = os.getcwd()
+    os.mkdir(extract_path)
+    os.chdir(extract_path)
+    environment = dict(os.environ, **{'PASSPHRASE': 'test'})
+
+    try:
+        config_path = os.path.join(temporary_directory, 'test.yaml')
+        generate_configuration(config_path, repository_path)
+
+        subprocess.check_call(
+            f'borgmatic -v 2 --config {config_path} repo-create --encryption repokey'.split(' '),
+            env=environment,
+        )
+
+        # Run borgmatic to generate a backup archive, and then list it to make sure it exists.
+        subprocess.check_call(
+            f'borgmatic --config {config_path}'.split(' '),
+            env=environment,
+        )
+        output = subprocess.check_output(
+            f'borgmatic --config {config_path} list --json'.split(' '),
+            env=environment,
+        ).decode(sys.stdout.encoding)
+        parsed_output = json.loads(output)
+
+        assert len(parsed_output) == 1
+        assert len(parsed_output[0]['archives']) == 1
+    finally:
+        os.chdir(original_working_directory)
+        shutil.rmtree(temporary_directory)