test_btrfs.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import os
  2. import shutil
  3. import subprocess
  4. import sys
  5. import tempfile
  6. def generate_configuration(config_path, repository_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).
  11. '''
  12. subprocess.check_call(f'borgmatic config generate --destination {config_path}'.split(' '))
  13. config = (
  14. open(config_path)
  15. .read()
  16. .replace('ssh://user@backupserver/./sourcehostname.borg', repository_path)
  17. .replace('- path: /e2e/mnt/backup', '')
  18. .replace('label: local', '')
  19. .replace('- /home', f'- {config_path}')
  20. .replace('- /etc', '- /e2e/mnt/subvolume/subdir')
  21. .replace('- /var/log/syslog*', '')
  22. + 'encryption_passphrase: "test"\n'
  23. + 'btrfs:\n'
  24. + ' btrfs_command: python3 /app/tests/end-to-end/commands/fake_btrfs.py\n'
  25. + ' findmnt_command: python3 /app/tests/end-to-end/commands/fake_findmnt.py\n'
  26. )
  27. config_file = open(config_path, 'w')
  28. config_file.write(config)
  29. config_file.close()
  30. def test_btrfs_create_and_list():
  31. temporary_directory = tempfile.mkdtemp()
  32. repository_path = os.path.join(temporary_directory, 'test.borg')
  33. try:
  34. config_path = os.path.join(temporary_directory, 'test.yaml')
  35. generate_configuration(config_path, repository_path)
  36. subprocess.check_call(
  37. f'borgmatic -v 2 --config {config_path} repo-create --encryption repokey'.split(' ')
  38. )
  39. # Run a create action to exercise Btrfs snapshotting and backup.
  40. subprocess.check_call(f'borgmatic --config {config_path} create'.split(' '))
  41. # List the resulting archive and assert that the snapshotted files are there.
  42. output = subprocess.check_output(
  43. f'borgmatic --config {config_path} list --archive latest'.split(' ')
  44. ).decode(sys.stdout.encoding)
  45. assert 'e2e/mnt/subvolume/subdir/file.txt' in output
  46. # Assert that the snapshot has been deleted.
  47. assert not subprocess.check_output(
  48. 'python3 /app/tests/end-to-end/commands/fake_btrfs.py subvolume list -s /e2e/mnt/subvolume'.split(
  49. ' '
  50. )
  51. )
  52. finally:
  53. shutil.rmtree(temporary_directory)