From 13acaa47e451087896e048fa08179c245bd1a73e Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Thu, 30 Jan 2025 22:50:13 -0800 Subject: [PATCH] Add an end-to-end test for the passcommand hook (#961). --- tests/end-to-end/hooks/credential/__init__.py | 0 .../hooks/credential/test_passcommand.py | 65 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 tests/end-to-end/hooks/credential/__init__.py create mode 100644 tests/end-to-end/hooks/credential/test_passcommand.py diff --git a/tests/end-to-end/hooks/credential/__init__.py b/tests/end-to-end/hooks/credential/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/end-to-end/hooks/credential/test_passcommand.py b/tests/end-to-end/hooks/credential/test_passcommand.py new file mode 100644 index 00000000..e6c1c810 --- /dev/null +++ b/tests/end-to-end/hooks/credential/test_passcommand.py @@ -0,0 +1,65 @@ +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 passcommand. + ''' + 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_passcommand: "echo test"' + ) + 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) + + 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(' ') + ) + + # Run borgmatic to generate a backup archive, and then list it to make sure it exists. + subprocess.check_call(f'borgmatic -v 2 --config {config_path}'.split(' ')) + output = subprocess.check_output( + f'borgmatic --config {config_path} list --json'.split(' ') + ).decode(sys.stdout.encoding) + parsed_output = json.loads(output) + + assert len(parsed_output) == 1 + assert len(parsed_output[0]['archives']) == 1 + archive_name = parsed_output[0]['archives'][0]['archive'] + finally: + os.chdir(original_working_directory) + shutil.rmtree(temporary_directory)