test_lvm.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 and tacking on an encryption
  11. passphrase).
  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/./sourcehostname.borg', repository_path)
  18. .replace('- path: /e2e/mnt/backup', '')
  19. .replace('label: local', '')
  20. .replace('- /home', f'- {config_path}')
  21. .replace('- /etc', '- /e2e/mnt/lvolume/subdir')
  22. .replace('- /var/log/syslog*', '')
  23. + 'encryption_passphrase: "test"\n'
  24. + 'lvm:\n'
  25. + ' lsblk_command: python3 /app/tests/end-to-end/commands/fake_lsblk.py\n'
  26. + ' lvcreate_command: python3 /app/tests/end-to-end/commands/fake_lvcreate.py\n'
  27. + ' lvremove_command: python3 /app/tests/end-to-end/commands/fake_lvremove.py\n'
  28. + ' lvs_command: python3 /app/tests/end-to-end/commands/fake_lvs.py\n'
  29. + ' mount_command: python3 /app/tests/end-to-end/commands/fake_mount.py\n'
  30. + ' umount_command: python3 /app/tests/end-to-end/commands/fake_umount.py\n'
  31. )
  32. config_file = open(config_path, 'w')
  33. config_file.write(config)
  34. config_file.close()
  35. def test_lvm_create_and_list():
  36. temporary_directory = tempfile.mkdtemp()
  37. repository_path = os.path.join(temporary_directory, 'test.borg')
  38. try:
  39. config_path = os.path.join(temporary_directory, 'test.yaml')
  40. generate_configuration(config_path, repository_path)
  41. subprocess.check_call(
  42. f'borgmatic -v 2 --config {config_path} repo-create --encryption repokey'.split(' ')
  43. )
  44. # Run a create action to exercise LVM snapshotting and backup.
  45. subprocess.check_call(f'borgmatic --config {config_path} create'.split(' '))
  46. # List the resulting archive and assert that the snapshotted files are there.
  47. output = subprocess.check_output(
  48. f'borgmatic --config {config_path} list --archive latest'.split(' ')
  49. ).decode(sys.stdout.encoding)
  50. assert 'e2e/mnt/lvolume/subdir/file.txt' in output
  51. # Assert that the snapshot has been deleted.
  52. assert not json.loads(
  53. subprocess.check_output(
  54. 'python3 /app/tests/end-to-end/commands/fake_lvs.py --report-format json --options lv_name,lv_path --select'.split(
  55. ' '
  56. )
  57. + ['lv_attr =~ ^s']
  58. )
  59. )['report'][0]['lv']
  60. finally:
  61. shutil.rmtree(temporary_directory)