conftest.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import os
  2. from typing import Optional
  3. import pytest
  4. from borg.testsuite.archiver import BORG_EXES
  5. if hasattr(pytest, "register_assert_rewrite"):
  6. pytest.register_assert_rewrite("borg.testsuite")
  7. import borg.cache # noqa: E402
  8. from borg.archiver import Archiver
  9. from borg.logger import setup_logging # noqa: E402
  10. # Ensure that the loggers exist for all tests
  11. setup_logging()
  12. from borg.testsuite import has_lchflags, has_llfuse, has_pyfuse3 # noqa: E402
  13. from borg.testsuite import are_symlinks_supported, are_hardlinks_supported, is_utime_fully_supported # noqa: E402
  14. from borg.testsuite.platform import fakeroot_detected # noqa: E402
  15. @pytest.fixture(autouse=True)
  16. def clean_env(tmpdir_factory, monkeypatch):
  17. # also avoid to use anything from the outside environment:
  18. keys = [key for key in os.environ if key.startswith("BORG_") and key not in ("BORG_FUSE_IMPL",)]
  19. for key in keys:
  20. monkeypatch.delenv(key, raising=False)
  21. # avoid that we access / modify the user's normal .config / .cache directory:
  22. monkeypatch.setenv("BORG_BASE_DIR", str(tmpdir_factory.mktemp("borg-base-dir")))
  23. # Speed up tests
  24. monkeypatch.setenv("BORG_TESTONLY_WEAKEN_KDF", "1")
  25. def pytest_report_header(config, startdir):
  26. tests = {
  27. "BSD flags": has_lchflags,
  28. "fuse2": has_llfuse,
  29. "fuse3": has_pyfuse3,
  30. "root": not fakeroot_detected(),
  31. "symlinks": are_symlinks_supported(),
  32. "hardlinks": are_hardlinks_supported(),
  33. "atime/mtime": is_utime_fully_supported(),
  34. "modes": "BORG_TESTS_IGNORE_MODES" not in os.environ,
  35. }
  36. enabled = []
  37. disabled = []
  38. for test in tests:
  39. if tests[test]:
  40. enabled.append(test)
  41. else:
  42. disabled.append(test)
  43. output = "Tests enabled: " + ", ".join(enabled) + "\n"
  44. output += "Tests disabled: " + ", ".join(disabled)
  45. return output
  46. class DefaultPatches:
  47. def __init__(self, request):
  48. self.org_cache_wipe_cache = borg.cache.LocalCache.wipe_cache
  49. def wipe_should_not_be_called(*a, **kw):
  50. raise AssertionError(
  51. "Cache wipe was triggered, if this is part of the test add " "@pytest.mark.allow_cache_wipe"
  52. )
  53. if "allow_cache_wipe" not in request.keywords:
  54. borg.cache.LocalCache.wipe_cache = wipe_should_not_be_called
  55. request.addfinalizer(self.undo)
  56. def undo(self):
  57. borg.cache.LocalCache.wipe_cache = self.org_cache_wipe_cache
  58. @pytest.fixture(autouse=True)
  59. def default_patches(request):
  60. return DefaultPatches(request)
  61. @pytest.fixture()
  62. def set_env_variables():
  63. os.environ["BORG_CHECK_I_KNOW_WHAT_I_AM_DOING"] = "YES"
  64. os.environ["BORG_DELETE_I_KNOW_WHAT_I_AM_DOING"] = "YES"
  65. os.environ["BORG_PASSPHRASE"] = "waytooeasyonlyfortests"
  66. os.environ["BORG_SELFTEST"] = "disabled"
  67. class ArchiverSetup:
  68. EXE: str = None # python source based
  69. FORK_DEFAULT = False
  70. BORG_EXES = []
  71. def __init__(self):
  72. self.archiver = None
  73. self.tmpdir: Optional[str] = None
  74. self.repository_path: Optional[str] = None
  75. self.repository_location: Optional[str] = None
  76. self.input_path: Optional[str] = None
  77. self.output_path: Optional[str] = None
  78. self.keys_path: Optional[str] = None
  79. self.cache_path: Optional[str] = None
  80. self.exclude_file_path: Optional[str] = None
  81. self.patterns_file_path: Optional[str] = None
  82. def get_kind(self) -> str:
  83. if self.repository_location.startswith("ssh://__testsuite__"):
  84. return "remote"
  85. elif self.EXE == "borg.exe":
  86. return "binary"
  87. else:
  88. return "local"
  89. @pytest.fixture()
  90. def archiver(tmp_path, set_env_variables):
  91. archiver = ArchiverSetup()
  92. archiver.archiver = not archiver.FORK_DEFAULT and Archiver() or None
  93. archiver.tmpdir = tmp_path
  94. archiver.repository_path = os.fspath(tmp_path / "repository")
  95. archiver.repository_location = archiver.repository_path
  96. archiver.input_path = os.fspath(tmp_path / "input")
  97. archiver.output_path = os.fspath(tmp_path / "output")
  98. archiver.keys_path = os.fspath(tmp_path / "keys")
  99. archiver.cache_path = os.fspath(tmp_path / "cache")
  100. archiver.exclude_file_path = os.fspath(tmp_path / "excludes")
  101. archiver.patterns_file_path = os.fspath(tmp_path / "patterns")
  102. os.environ["BORG_KEYS_DIR"] = archiver.keys_path
  103. os.environ["BORG_CACHE_DIR"] = archiver.cache_path
  104. os.mkdir(archiver.input_path)
  105. os.chmod(archiver.input_path, 0o777) # avoid troubles with fakeroot / FUSE
  106. os.mkdir(archiver.output_path)
  107. os.mkdir(archiver.keys_path)
  108. os.mkdir(archiver.cache_path)
  109. with open(archiver.exclude_file_path, "wb") as fd:
  110. fd.write(b"input/file2\n# A comment line, then a blank line\n\n")
  111. with open(archiver.patterns_file_path, "wb") as fd:
  112. fd.write(b"+input/file_important\n- input/file*\n# A comment line, then a blank line\n\n")
  113. old_wd = os.getcwd()
  114. os.chdir(archiver.tmpdir)
  115. yield archiver
  116. os.chdir(old_wd)
  117. @pytest.fixture()
  118. def remote_archiver(archiver):
  119. archiver.repository_location = "ssh://__testsuite__" + str(archiver.repository_path)
  120. yield archiver
  121. @pytest.fixture()
  122. def binary_archiver(archiver):
  123. if "binary" not in BORG_EXES:
  124. pytest.skip("No borg.exe binary available")
  125. archiver.EXE = "borg.exe"
  126. archiver.FORK_DEFAULT = True
  127. yield archiver