2
0

rcreate_cmd.py 3.1 KB

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