2
0

test_borgmatic.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 injecting the given repository path).
  11. '''
  12. subprocess.check_call(
  13. 'generate-borgmatic-config --destination {}'.format(config_path).split(' ')
  14. )
  15. config = (
  16. open(config_path)
  17. .read()
  18. .replace('user@backupserver:sourcehostname.borg', repository_path)
  19. .replace('- /home', '- {}'.format(config_path))
  20. .replace('- /etc', '')
  21. .replace('- /var/log/syslog*', '')
  22. )
  23. config_file = open(config_path, 'w')
  24. config_file.write(config)
  25. config_file.close()
  26. def test_borgmatic_command():
  27. # Create a Borg repository.
  28. temporary_directory = tempfile.mkdtemp()
  29. repository_path = os.path.join(temporary_directory, 'test.borg')
  30. try:
  31. subprocess.check_call(
  32. 'borg init --encryption repokey {}'.format(repository_path).split(' '),
  33. env={'BORG_PASSPHRASE': '', **os.environ},
  34. )
  35. config_path = os.path.join(temporary_directory, 'test.yaml')
  36. generate_configuration(config_path, repository_path)
  37. # Run borgmatic to generate a backup archive, and then list it to make sure it exists.
  38. subprocess.check_call('borgmatic --config {}'.format(config_path).split(' '))
  39. output = subprocess.check_output(
  40. 'borgmatic --config {} --list --json'.format(config_path).split(' ')
  41. ).decode(sys.stdout.encoding)
  42. parsed_output = json.loads(output)
  43. assert len(parsed_output) == 1
  44. assert len(parsed_output[0]['archives']) == 1
  45. # Also exercise the info flag.
  46. output = subprocess.check_output(
  47. 'borgmatic --config {} --info --json'.format(config_path).split(' ')
  48. ).decode(sys.stdout.encoding)
  49. parsed_output = json.loads(output)
  50. assert len(parsed_output) == 1
  51. assert 'repository' in parsed_output[0]
  52. finally:
  53. shutil.rmtree(temporary_directory)