test_override.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os
  2. import shutil
  3. import subprocess
  4. import tempfile
  5. def generate_configuration(config_path, repository_path):
  6. '''
  7. Generate borgmatic configuration into a file at the config path, and update the defaults so as
  8. to work for testing (including injecting the given repository path and tacking on an encryption
  9. passphrase).
  10. '''
  11. subprocess.check_call(f'borgmatic config generate --destination {config_path}'.split(' '))
  12. config = (
  13. open(config_path)
  14. .read()
  15. .replace('ssh://user@backupserver/./sourcehostname.borg', repository_path)
  16. .replace('- ssh://user@backupserver/./{fqdn}', '') # noqa: FS003
  17. .replace('- /var/local/backups/local.borg', '')
  18. .replace('- /home/user/path with spaces', '')
  19. .replace('- /home', f'- {config_path}')
  20. .replace('- /etc', '')
  21. .replace('- /var/log/syslog*', '')
  22. + 'encryption_passphrase: "test"'
  23. )
  24. config_file = open(config_path, 'w')
  25. config_file.write(config)
  26. config_file.close()
  27. def test_override_gets_normalized():
  28. temporary_directory = tempfile.mkdtemp()
  29. repository_path = os.path.join(temporary_directory, 'test.borg')
  30. original_working_directory = os.getcwd()
  31. try:
  32. config_path = os.path.join(temporary_directory, 'test.yaml')
  33. generate_configuration(config_path, repository_path)
  34. subprocess.check_call(
  35. f'borgmatic -v 2 --config {config_path} repo-create --encryption repokey'.split(' ')
  36. )
  37. # Run borgmatic with an override structured for an outdated config file format. If
  38. # normalization is working, it should get normalized and shouldn't error.
  39. subprocess.check_call(
  40. f'borgmatic create --config {config_path} --override hooks.healthchecks=http://localhost:8888/someuuid'.split(
  41. ' '
  42. )
  43. )
  44. finally:
  45. os.chdir(original_working_directory)
  46. shutil.rmtree(temporary_directory)