-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathconftest.py
167 lines (129 loc) · 4.47 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import asyncio
import os
import subprocess
import sys
from collections.abc import AsyncIterator, Iterator
from typing import Any
import asyncssh
import pygit2
import pytest
from pytest_test_utils import TempDirFactory, TmpDir
from scmrepo.git import Git
TEST_SSH_USER = "user"
TEST_SSH_KEY_PATH = os.path.join(
os.path.abspath(os.path.dirname(__file__)), f"{TEST_SSH_USER}.key"
)
# pylint: disable=redefined-outer-name
def pytest_addoption(parser):
parser.addoption(
"--slow", action="store_true", default=False, help="run slow tests"
)
def pytest_collection_modifyitems(config, items):
if config.getoption("--slow"):
return
skip_slow = pytest.mark.skip(reason="need --slow option to run")
for item in items:
if "slow" in item.keywords:
item.add_marker(skip_slow)
@pytest.fixture(autouse=True)
def _isolate(tmp_dir_factory: TempDirFactory, monkeypatch: pytest.MonkeyPatch) -> None:
path = tmp_dir_factory.mktemp("mock")
home_dir = path / "home"
home_dir.mkdir()
if sys.platform == "win32":
home_drive, home_path = os.path.splitdrive(home_dir)
monkeypatch.setenv("USERPROFILE", str(home_dir))
monkeypatch.setenv("HOMEDRIVE", home_drive)
monkeypatch.setenv("HOMEPATH", home_path)
else:
monkeypatch.setenv("HOME", str(home_dir))
monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1")
contents = b"""
[user]
name=DVC Tester
[init]
defaultBranch=master
"""
(home_dir / ".gitconfig").write_bytes(contents)
pygit2.settings.search_path[pygit2.GIT_CONFIG_LEVEL_GLOBAL] = str(home_dir) # type: ignore[attr-defined]
@pytest.fixture
def scm(tmp_dir: TmpDir) -> Iterator[Git]:
git_ = Git.init(tmp_dir)
sig = git_.pygit2.default_signature
assert sig.email == "[email protected]"
assert sig.name == "DVC Tester"
yield git_
git_.close()
backends = ["gitpython", "dulwich", "pygit2"]
@pytest.fixture(params=backends)
def git_backend(request) -> str:
marker = request.node.get_closest_marker("skip_git_backend")
to_skip = marker.args if marker else []
backend = request.param
if backend in to_skip:
pytest.skip()
return backend
@pytest.fixture
def git(tmp_dir: TmpDir, git_backend: str) -> Iterator[Git]:
git_ = Git(tmp_dir, backends=[git_backend])
yield git_
git_.close()
@pytest.fixture
def remote_git_dir(tmp_dir_factory: TempDirFactory):
git_dir = tmp_dir_factory.mktemp("git-remote")
remote_git = Git.init(git_dir)
remote_git.close()
return git_dir
@pytest.fixture(scope="session")
def docker(request: pytest.FixtureRequest):
for cmd in [("docker", "ps"), ("docker", "compose", "version")]:
try:
subprocess.check_call(
cmd,
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
)
except (subprocess.CalledProcessError, OSError):
pytest.skip(f"no {cmd[0]} installed")
if "CI" in os.environ and os.name == "nt":
pytest.skip("disabled for Windows on Github Actions")
pytest.importorskip("pytest_docker")
return request.getfixturevalue("docker_services")
@pytest.fixture
def ssh_conn_info(
docker, # pylint: disable=unused-argument
) -> dict[str, Any]:
conn_info = {
"host": "127.0.0.1",
"port": docker.port_for("git-server", 2222),
"client_keys": TEST_SSH_KEY_PATH,
"known_hosts": None,
"username": TEST_SSH_USER,
}
async def _check() -> bool:
try:
async with asyncssh.connect(**conn_info) as conn:
result = await conn.run("git --version")
assert result.returncode == 0
async with conn.start_sftp_client() as sftp:
assert await sftp.exists("/")
except Exception: # noqa: BLE001 # pylint: disable=broad-except
return False
return True
def check() -> bool:
return asyncio.run(_check())
docker.wait_until_responsive(timeout=30.0, pause=1, check=check)
return conn_info
@pytest.fixture
async def ssh_connection(
ssh_conn_info: dict[str, Any],
) -> AsyncIterator[asyncssh.connection.SSHClientConnection]:
async with asyncssh.connect(**ssh_conn_info) as conn:
yield conn
@pytest.fixture
async def sftp(
ssh_connection: asyncssh.connection.SSHClientConnection,
) -> AsyncIterator[asyncssh.SFTPClient]:
async with ssh_connection.start_sftp_client() as sftp:
yield sftp