conftest.py 5.1 KB

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