test_zfs.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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: /mnt/backup', '')
  18. .replace('label: local', '')
  19. .replace('- /home', f'- {config_path}')
  20. .replace('- /etc', '- /e2e/pool/dataset/subdir')
  21. .replace('- /var/log/syslog*', '')
  22. + 'encryption_passphrase: "test"\n'
  23. + 'zfs:\n'
  24. + ' zfs_command: python3 /app/tests/end-to-end/commands/fake_zfs.py\n'
  25. + ' mount_command: python3 /app/tests/end-to-end/commands/fake_mount.py\n'
  26. + ' umount_command: python3 /app/tests/end-to-end/commands/fake_umount.py'
  27. )
  28. config_file = open(config_path, 'w')
  29. config_file.write(config)
  30. config_file.close()
  31. def test_zfs_create_and_list():
  32. temporary_directory = tempfile.mkdtemp()
  33. repository_path = os.path.join(temporary_directory, 'test.borg')
  34. try:
  35. config_path = os.path.join(temporary_directory, 'test.yaml')
  36. generate_configuration(config_path, repository_path)
  37. subprocess.check_call(
  38. f'borgmatic -v 2 --config {config_path} repo-create --encryption repokey'.split(' ')
  39. )
  40. # Run a create action to exercise ZFS snapshotting and backup.
  41. subprocess.check_call(f'borgmatic -v 2 --config {config_path} create'.split(' '))
  42. # List the resulting archive and assert that the snapshotted files are there.
  43. output = subprocess.check_output(
  44. f'borgmatic --config {config_path} list --archive latest'.split(' ')
  45. ).decode(sys.stdout.encoding)
  46. assert 'e2e/pool/dataset/subdir/file.txt' in output
  47. # Assert that the snapshot has been deleted.
  48. assert not subprocess.check_output(
  49. 'python3 /app/tests/end-to-end/commands/fake_zfs.py list -H -t snapshot'.split(' ')
  50. )
  51. finally:
  52. shutil.rmtree(temporary_directory)