rcreate_cmd.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import os
  2. import unittest
  3. from unittest.mock import patch
  4. import pytest
  5. from ...helpers.errors import Error
  6. from ...constants import * # NOQA
  7. from ...crypto.key import FlexiKey
  8. from ...repository import Repository
  9. from .. import environment_variable
  10. from . import (
  11. ArchiverTestCaseBase,
  12. ArchiverTestCaseBinaryBase,
  13. RemoteArchiverTestCaseBase,
  14. RK_ENCRYPTION,
  15. KF_ENCRYPTION,
  16. BORG_EXES,
  17. )
  18. class ArchiverTestCase(ArchiverTestCaseBase):
  19. def test_rcreate_parent_dirs(self):
  20. parent_path = os.path.join(self.tmpdir, "parent1", "parent2")
  21. repository_path = os.path.join(parent_path, "repository")
  22. repository_location = self.prefix + repository_path
  23. with pytest.raises(Repository.ParentPathDoesNotExist):
  24. # normal borg rcreate does NOT create missing parent dirs
  25. self.cmd(f"--repo={repository_location}", "rcreate", "--encryption=none")
  26. # but if told so, it does:
  27. self.cmd(f"--repo={repository_location}", "rcreate", "--encryption=none", "--make-parent-dirs")
  28. assert os.path.exists(parent_path)
  29. def test_rcreate_interrupt(self):
  30. def raise_eof(*args, **kwargs):
  31. raise EOFError
  32. with patch.object(FlexiKey, "create", raise_eof):
  33. self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION, exit_code=1)
  34. assert not os.path.exists(self.repository_location)
  35. def test_rcreate_requires_encryption_option(self):
  36. self.cmd(f"--repo={self.repository_location}", "rcreate", exit_code=2)
  37. def test_rcreate_nested_repositories(self):
  38. self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION)
  39. if self.FORK_DEFAULT:
  40. self.cmd(f"--repo={self.repository_location}/nested", "rcreate", RK_ENCRYPTION, exit_code=2)
  41. else:
  42. with pytest.raises(Repository.AlreadyExists):
  43. self.cmd(f"--repo={self.repository_location}/nested", "rcreate", RK_ENCRYPTION)
  44. def test_rcreate_refuse_to_overwrite_keyfile(self):
  45. """BORG_KEY_FILE=something borg rcreate should quit if "something" already exists.
  46. See https://github.com/borgbackup/borg/pull/6046"""
  47. keyfile = os.path.join(self.tmpdir, "keyfile")
  48. with environment_variable(BORG_KEY_FILE=keyfile):
  49. self.cmd(f"--repo={self.repository_location}0", "rcreate", KF_ENCRYPTION)
  50. with open(keyfile) as file:
  51. before = file.read()
  52. arg = (f"--repo={self.repository_location}1", "rcreate", KF_ENCRYPTION)
  53. if self.FORK_DEFAULT:
  54. self.cmd(*arg, exit_code=2)
  55. else:
  56. with pytest.raises(Error):
  57. self.cmd(*arg)
  58. with open(keyfile) as file:
  59. after = file.read()
  60. assert before == after
  61. class RemoteArchiverTestCase(RemoteArchiverTestCaseBase, ArchiverTestCase):
  62. """run the same tests, but with a remote repository"""
  63. @unittest.skipUnless("binary" in BORG_EXES, "no borg.exe available")
  64. class ArchiverTestCaseBinary(ArchiverTestCaseBinaryBase, ArchiverTestCase):
  65. """runs the same tests, but via the borg binary"""
  66. @unittest.skip("does not raise Exception, but sets rc==2")
  67. def test_rcreate_parent_dirs(self):
  68. pass
  69. @unittest.skip("patches objects")
  70. def test_rcreate_interrupt(self):
  71. pass