test_config_flag.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import os
  2. import shlex
  3. import shutil
  4. import subprocess
  5. import tempfile
  6. def generate_configuration(config_path):
  7. '''
  8. Generate borgmatic configuration into a file at the config path, and update the defaults so as
  9. to work for testing (including injecting the given repository path and tacking on an encryption
  10. passphrase). But don't actually set the repository path, as that's done on the command-line
  11. below.
  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/./{fqdn}', '') # noqa: FS003
  18. .replace('- /var/local/backups/local.borg', '')
  19. .replace('- /home/user/path with spaces', '')
  20. .replace('- /home', f'- {config_path}')
  21. .replace('- /etc', '')
  22. .replace('- /var/log/syslog*', '')
  23. + 'encryption_passphrase: "test"'
  24. )
  25. config_file = open(config_path, 'w')
  26. config_file.write(config)
  27. config_file.close()
  28. def test_config_flags_do_not_error():
  29. temporary_directory = tempfile.mkdtemp()
  30. repository_path = os.path.join(temporary_directory, 'test.borg')
  31. original_working_directory = os.getcwd()
  32. try:
  33. config_path = os.path.join(temporary_directory, 'test.yaml')
  34. generate_configuration(config_path)
  35. subprocess.check_call(
  36. shlex.split(
  37. f'borgmatic -v 2 --config {config_path} --repositories "[{{path: {repository_path}, label: repo}}]" repo-create --encryption repokey'
  38. )
  39. )
  40. subprocess.check_call(
  41. shlex.split(
  42. f'borgmatic create --config {config_path} --repositories[0].path "{repository_path}"'
  43. )
  44. )
  45. finally:
  46. os.chdir(original_working_directory)
  47. shutil.rmtree(temporary_directory)