2
0

test_keepassxc.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import json
  2. import os
  3. import shutil
  4. import subprocess
  5. import sys
  6. import tempfile
  7. def generate_configuration(config_path, repository_path):
  8. '''
  9. Generate borgmatic configuration into a file at the config path, and update the defaults so as
  10. to work for testing, including updating the source directories, injecting the given repository
  11. path, and tacking on an encryption passphrase loaded from keepassxc-cli.
  12. '''
  13. subprocess.check_call(f'borgmatic config generate --destination {config_path}'.split(' '))
  14. config = (
  15. open(config_path)
  16. .read()
  17. .replace('ssh://user@backupserver/./sourcehostname.borg', repository_path)
  18. .replace('- path: /mnt/backup', '')
  19. .replace('label: local', '')
  20. .replace('- /home/user/path with spaces', '')
  21. .replace('- /home', f'- {config_path}')
  22. .replace('- /etc', '')
  23. .replace('- /var/log/syslog*', '')
  24. + '\nencryption_passphrase: "{credential keepassxc keys.kdbx mypassword}"'
  25. + '\nkeepassxc:\n keepassxc_cli_command: python3 /app/tests/end-to-end/commands/fake_keepassxc_cli.py'
  26. )
  27. config_file = open(config_path, 'w')
  28. config_file.write(config)
  29. config_file.close()
  30. def test_keepassxc_password():
  31. # Create a Borg repository.
  32. temporary_directory = tempfile.mkdtemp()
  33. repository_path = os.path.join(temporary_directory, 'test.borg')
  34. original_working_directory = os.getcwd()
  35. os.chdir(temporary_directory)
  36. try:
  37. config_path = os.path.join(temporary_directory, 'test.yaml')
  38. generate_configuration(config_path, repository_path)
  39. database_path = os.path.join(temporary_directory, 'keys.kdbx')
  40. with open(database_path, 'w') as database_file:
  41. database_file.write('fake KeePassXC database to pacify file existence check')
  42. subprocess.check_call(
  43. f'borgmatic -v 2 --config {config_path} repo-create --encryption repokey'.split(' '),
  44. )
  45. # Run borgmatic to generate a backup archive, and then list it to make sure it exists.
  46. subprocess.check_call(
  47. f'borgmatic --config {config_path}'.split(' '),
  48. )
  49. output = subprocess.check_output(
  50. f'borgmatic --config {config_path} list --json'.split(' '),
  51. ).decode(sys.stdout.encoding)
  52. parsed_output = json.loads(output)
  53. assert len(parsed_output) == 1
  54. assert len(parsed_output[0]['archives']) == 1
  55. finally:
  56. os.chdir(original_working_directory)
  57. shutil.rmtree(temporary_directory)