2016-07-25 03:38:28 +00:00
|
|
|
import os
|
2023-07-16 22:47:36 +00:00
|
|
|
from typing import Optional
|
2016-07-25 03:38:28 +00:00
|
|
|
|
2016-10-13 21:59:28 +00:00
|
|
|
import pytest
|
|
|
|
|
2023-07-04 23:38:10 +00:00
|
|
|
from borg.testsuite.archiver import BORG_EXES
|
2023-07-04 20:35:51 +00:00
|
|
|
|
2016-12-17 14:58:51 +00:00
|
|
|
if hasattr(pytest, "register_assert_rewrite"):
|
|
|
|
pytest.register_assert_rewrite("borg.testsuite")
|
2016-10-13 22:46:43 +00:00
|
|
|
|
2017-06-06 20:37:53 +00:00
|
|
|
|
2021-03-15 13:52:39 +00:00
|
|
|
import borg.cache # noqa: E402
|
2023-07-04 20:35:51 +00:00
|
|
|
from borg.archiver import Archiver
|
2021-03-15 13:52:39 +00:00
|
|
|
from borg.logger import setup_logging # noqa: E402
|
2016-05-17 22:22:49 +00:00
|
|
|
|
|
|
|
# Ensure that the loggers exist for all tests
|
|
|
|
setup_logging()
|
|
|
|
|
2021-03-15 13:52:39 +00:00
|
|
|
from borg.testsuite import has_lchflags, has_llfuse, has_pyfuse3 # noqa: E402
|
|
|
|
from borg.testsuite import are_symlinks_supported, are_hardlinks_supported, is_utime_fully_supported # noqa: E402
|
|
|
|
from borg.testsuite.platform import fakeroot_detected # noqa: E402
|
2016-05-17 22:22:49 +00:00
|
|
|
|
|
|
|
|
2016-10-13 21:59:28 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def clean_env(tmpdir_factory, monkeypatch):
|
|
|
|
# also avoid to use anything from the outside environment:
|
2020-10-10 21:12:47 +00:00
|
|
|
keys = [key for key in os.environ if key.startswith("BORG_") and key not in ("BORG_FUSE_IMPL",)]
|
2016-10-13 21:59:28 +00:00
|
|
|
for key in keys:
|
|
|
|
monkeypatch.delenv(key, raising=False)
|
2023-02-03 22:47:28 +00:00
|
|
|
# avoid that we access / modify the user's normal .config / .cache directory:
|
|
|
|
monkeypatch.setenv("BORG_BASE_DIR", str(tmpdir_factory.mktemp("borg-base-dir")))
|
2022-04-07 14:22:34 +00:00
|
|
|
# Speed up tests
|
|
|
|
monkeypatch.setenv("BORG_TESTONLY_WEAKEN_KDF", "1")
|
2016-10-13 21:59:28 +00:00
|
|
|
|
|
|
|
|
2016-05-17 22:22:49 +00:00
|
|
|
def pytest_report_header(config, startdir):
|
2016-07-25 03:38:28 +00:00
|
|
|
tests = {
|
|
|
|
"BSD flags": has_lchflags,
|
2020-10-10 21:12:47 +00:00
|
|
|
"fuse2": has_llfuse,
|
|
|
|
"fuse3": has_pyfuse3,
|
2016-07-25 03:38:28 +00:00
|
|
|
"root": not fakeroot_detected(),
|
|
|
|
"symlinks": are_symlinks_supported(),
|
|
|
|
"hardlinks": are_hardlinks_supported(),
|
|
|
|
"atime/mtime": is_utime_fully_supported(),
|
|
|
|
"modes": "BORG_TESTS_IGNORE_MODES" not in os.environ,
|
|
|
|
}
|
|
|
|
enabled = []
|
|
|
|
disabled = []
|
|
|
|
for test in tests:
|
|
|
|
if tests[test]:
|
|
|
|
enabled.append(test)
|
|
|
|
else:
|
|
|
|
disabled.append(test)
|
|
|
|
output = "Tests enabled: " + ", ".join(enabled) + "\n"
|
|
|
|
output += "Tests disabled: " + ", ".join(disabled)
|
|
|
|
return output
|
2017-06-06 20:37:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DefaultPatches:
|
|
|
|
def __init__(self, request):
|
2017-06-10 15:59:41 +00:00
|
|
|
self.org_cache_wipe_cache = borg.cache.LocalCache.wipe_cache
|
2017-06-06 20:37:53 +00:00
|
|
|
|
|
|
|
def wipe_should_not_be_called(*a, **kw):
|
2021-03-15 13:52:39 +00:00
|
|
|
raise AssertionError(
|
|
|
|
"Cache wipe was triggered, if this is part of the test add " "@pytest.mark.allow_cache_wipe"
|
|
|
|
)
|
2022-07-06 13:37:27 +00:00
|
|
|
|
2017-06-06 20:37:53 +00:00
|
|
|
if "allow_cache_wipe" not in request.keywords:
|
2017-06-10 15:59:41 +00:00
|
|
|
borg.cache.LocalCache.wipe_cache = wipe_should_not_be_called
|
2017-06-06 20:37:53 +00:00
|
|
|
request.addfinalizer(self.undo)
|
|
|
|
|
|
|
|
def undo(self):
|
2017-06-10 15:59:41 +00:00
|
|
|
borg.cache.LocalCache.wipe_cache = self.org_cache_wipe_cache
|
2017-06-06 20:37:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def default_patches(request):
|
|
|
|
return DefaultPatches(request)
|
2023-07-04 20:35:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def set_env_variables():
|
|
|
|
os.environ["BORG_CHECK_I_KNOW_WHAT_I_AM_DOING"] = "YES"
|
|
|
|
os.environ["BORG_DELETE_I_KNOW_WHAT_I_AM_DOING"] = "YES"
|
|
|
|
os.environ["BORG_PASSPHRASE"] = "waytooeasyonlyfortests"
|
|
|
|
os.environ["BORG_SELFTEST"] = "disabled"
|
|
|
|
|
|
|
|
|
|
|
|
class ArchiverSetup:
|
|
|
|
EXE: str = None # python source based
|
|
|
|
FORK_DEFAULT = False
|
|
|
|
BORG_EXES = []
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.archiver = None
|
2023-07-16 22:47:36 +00:00
|
|
|
self.tmpdir: Optional[str] = None
|
|
|
|
self.repository_path: Optional[str] = None
|
|
|
|
self.repository_location: Optional[str] = None
|
|
|
|
self.input_path: Optional[str] = None
|
|
|
|
self.output_path: Optional[str] = None
|
|
|
|
self.keys_path: Optional[str] = None
|
|
|
|
self.cache_path: Optional[str] = None
|
|
|
|
self.exclude_file_path: Optional[str] = None
|
|
|
|
self.patterns_file_path: Optional[str] = None
|
2023-07-04 20:35:51 +00:00
|
|
|
|
2023-07-21 04:32:23 +00:00
|
|
|
def get_kind(self) -> str:
|
|
|
|
if self.repository_location.startswith("ssh://__testsuite__"):
|
|
|
|
return "remote"
|
|
|
|
elif self.EXE == "borg.exe":
|
|
|
|
return "binary"
|
|
|
|
else:
|
|
|
|
return "local"
|
|
|
|
|
2023-07-04 20:35:51 +00:00
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def archiver(tmp_path, set_env_variables):
|
|
|
|
archiver = ArchiverSetup()
|
|
|
|
archiver.archiver = not archiver.FORK_DEFAULT and Archiver() or None
|
|
|
|
archiver.tmpdir = tmp_path
|
|
|
|
archiver.repository_path = os.fspath(tmp_path / "repository")
|
2023-07-20 00:24:15 +00:00
|
|
|
archiver.repository_location = archiver.repository_path
|
2023-07-04 20:35:51 +00:00
|
|
|
archiver.input_path = os.fspath(tmp_path / "input")
|
|
|
|
archiver.output_path = os.fspath(tmp_path / "output")
|
|
|
|
archiver.keys_path = os.fspath(tmp_path / "keys")
|
|
|
|
archiver.cache_path = os.fspath(tmp_path / "cache")
|
|
|
|
archiver.exclude_file_path = os.fspath(tmp_path / "excludes")
|
|
|
|
archiver.patterns_file_path = os.fspath(tmp_path / "patterns")
|
|
|
|
os.environ["BORG_KEYS_DIR"] = archiver.keys_path
|
|
|
|
os.environ["BORG_CACHE_DIR"] = archiver.cache_path
|
|
|
|
os.mkdir(archiver.input_path)
|
|
|
|
os.chmod(archiver.input_path, 0o777) # avoid troubles with fakeroot / FUSE
|
|
|
|
os.mkdir(archiver.output_path)
|
|
|
|
os.mkdir(archiver.keys_path)
|
|
|
|
os.mkdir(archiver.cache_path)
|
|
|
|
with open(archiver.exclude_file_path, "wb") as fd:
|
|
|
|
fd.write(b"input/file2\n# A comment line, then a blank line\n\n")
|
|
|
|
with open(archiver.patterns_file_path, "wb") as fd:
|
|
|
|
fd.write(b"+input/file_important\n- input/file*\n# A comment line, then a blank line\n\n")
|
2023-07-16 20:33:54 +00:00
|
|
|
old_wd = os.getcwd()
|
2023-07-04 20:35:51 +00:00
|
|
|
os.chdir(archiver.tmpdir)
|
|
|
|
yield archiver
|
2023-07-16 20:33:54 +00:00
|
|
|
os.chdir(old_wd)
|
2023-07-04 20:35:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def remote_archiver(archiver):
|
2023-07-20 00:24:15 +00:00
|
|
|
archiver.repository_location = "ssh://__testsuite__" + str(archiver.repository_path)
|
2023-07-04 23:38:10 +00:00
|
|
|
yield archiver
|
2023-07-04 20:35:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2023-07-04 23:38:10 +00:00
|
|
|
def binary_archiver(archiver):
|
|
|
|
if "binary" not in BORG_EXES:
|
2023-07-04 20:35:51 +00:00
|
|
|
pytest.skip("No borg.exe binary available")
|
|
|
|
archiver.EXE = "borg.exe"
|
|
|
|
archiver.FORK_DEFAULT = True
|
|
|
|
yield archiver
|