forked from mlflow/mlflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
388 lines (335 loc) · 13.1 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import json
import os
import posixpath
import re
import shutil
import subprocess
import sys
import threading
import traceback
import click
import pytest
from mlflow.environment_variables import _MLFLOW_TESTING, MLFLOW_TRACKING_URI
from mlflow.utils.os import is_windows
from mlflow.version import VERSION
from tests.helper_functions import get_safe_port
def pytest_addoption(parser):
parser.addoption(
"--requires-ssh",
action="store_true",
dest="requires_ssh",
default=False,
help="Run tests decorated with 'requires_ssh' annotation. "
"These tests require keys to be configured locally "
"for SSH authentication.",
)
parser.addoption(
"--ignore-flavors",
action="store_true",
dest="ignore_flavors",
default=False,
help="Ignore tests for model flavors.",
)
parser.addoption(
"--splits",
default=None,
type=int,
help="The number of groups to split tests into.",
)
parser.addoption(
"--group",
default=None,
type=int,
help="The group of tests to run.",
)
parser.addoption(
"--serve-wheel",
action="store_true",
default=os.getenv("CI", "false").lower() == "true",
help="Serve a wheel for the dev version of MLflow. True by default in CI, False otherwise.",
)
def pytest_configure(config):
config.addinivalue_line("markers", "requires_ssh")
config.addinivalue_line("markers", "notrackingurimock")
config.addinivalue_line("markers", "allow_infer_pip_requirements_fallback")
config.addinivalue_line(
"markers", "do_not_disable_new_import_hook_firing_if_module_already_exists"
)
config.addinivalue_line("markers", "classification")
labels = fetch_pr_labels() or []
if "fail-fast" in labels:
config.option.maxfail = 1
@pytest.hookimpl(tryfirst=True)
def pytest_cmdline_main(config):
group = config.getoption("group")
splits = config.getoption("splits")
if splits is None and group is None:
return None
if splits and group is None:
raise pytest.UsageError("`--group` is required")
if group and splits is None:
raise pytest.UsageError("`--splits` is required")
if splits < 0:
raise pytest.UsageError("`--splits` must be >= 1")
if group < 1 or group > splits:
raise pytest.UsageError("`--group` must be between 1 and {splits}")
return None
def pytest_sessionstart(session):
if uri := MLFLOW_TRACKING_URI.get():
click.echo(
click.style(
(
f"Environment variable {MLFLOW_TRACKING_URI} is set to {uri!r}, "
"which may interfere with tests."
),
fg="red",
)
)
def pytest_runtest_setup(item):
markers = [mark.name for mark in item.iter_markers()]
if "requires_ssh" in markers and not item.config.getoption("--requires-ssh"):
pytest.skip("use `--requires-ssh` to run this test")
def fetch_pr_labels():
"""
Returns the labels associated with the current pull request.
"""
if "GITHUB_ACTIONS" not in os.environ:
return None
if os.environ.get("GITHUB_EVENT_NAME") != "pull_request":
return None
with open(os.environ["GITHUB_EVENT_PATH"]) as f:
pr_data = json.load(f)
return [label["name"] for label in pr_data["pull_request"]["labels"]]
@pytest.hookimpl(hookwrapper=True)
def pytest_report_teststatus(report, config):
outcome = yield
if report.when == "call":
try:
import psutil
except ImportError:
return
(*rest, result) = outcome.get_result()
mem = psutil.virtual_memory()
mem_used = mem.used / 1024**3
mem_total = mem.total / 1024**3
disk = psutil.disk_usage("/")
disk_used = disk.used / 1024**3
disk_total = disk.total / 1024**3
outcome.force_result(
(
*rest,
(
f"{result} | "
f"MEM {mem_used:.1f}/{mem_total:.1f} GB | "
f"DISK {disk_used:.1f}/{disk_total:.1f} GB"
),
)
)
@pytest.hookimpl(hookwrapper=True)
def pytest_ignore_collect(collection_path, config):
outcome = yield
if not outcome.get_result() and config.getoption("ignore_flavors"):
# If not ignored by the default hook and `--ignore-flavors` specified
# Ignored files and directories must be included in dev/run-python-flavor-tests.sh
model_flavors = [
# Tests of flavor modules.
"tests/autogen",
"tests/azureml",
"tests/catboost",
"tests/diviner",
"tests/dspy",
"tests/fastai",
"tests/gluon",
"tests/h2o",
"tests/johnsnowlabs",
"tests/keras",
"tests/keras_core",
"tests/llama_index",
"tests/langchain",
"tests/lightgbm",
"tests/mleap",
"tests/models",
"tests/onnx",
"tests/openai",
"tests/paddle",
"tests/pmdarima",
"tests/promptflow",
"tests/prophet",
"tests/pyfunc",
"tests/pytorch",
"tests/sagemaker",
"tests/sentence_transformers",
"tests/shap",
"tests/sklearn",
"tests/spacy",
"tests/spark",
"tests/statsmodels",
"tests/tensorflow",
"tests/transformers",
"tests/xgboost",
# Lazy loading test.
"tests/test_mlflow_lazily_imports_ml_packages.py",
# Tests of utils.
"tests/utils/test_model_utils.py",
# This test is included here because it imports many big libraries like tf, keras, etc.
"tests/tracking/fluent/test_fluent_autolog.py",
# Cross flavor autologging related tests.
"tests/autologging/test_autologging_safety_unit.py",
"tests/autologging/test_autologging_behaviors_unit.py",
"tests/autologging/test_autologging_behaviors_integration.py",
"tests/autologging/test_autologging_utils.py",
"tests/autologging/test_training_session.py",
# Opt in authentication feature.
"tests/server/auth",
"tests/gateway",
]
relpath = os.path.relpath(str(collection_path))
relpath = relpath.replace(os.sep, posixpath.sep) # for Windows
if relpath in model_flavors:
outcome.force_result(True)
@pytest.hookimpl(trylast=True)
def pytest_collection_modifyitems(session, config, items):
# Executing `tests.server.test_prometheus_exporter` after `tests.server.test_handlers`
# results in an error because Flask >= 2.2.0 doesn't allow calling setup method such as
# `before_request` on the application after the first request. To avoid this issue,
# execute `tests.server.test_prometheus_exporter` first by reordering the test items.
items.sort(key=lambda item: item.module.__name__ != "tests.server.test_prometheus_exporter")
# Select the tests to run based on the group and splits
if (splits := config.getoption("--splits")) and (group := config.getoption("--group")):
items[:] = items[(group - 1) :: splits]
@pytest.hookimpl(hookwrapper=True)
def pytest_terminal_summary(terminalreporter, exitstatus, config):
yield
failed_test_reports = terminalreporter.stats.get("failed", [])
if failed_test_reports:
if len(failed_test_reports) <= 30:
terminalreporter.section("command to run failed test cases")
ids = [repr(report.nodeid) for report in failed_test_reports]
else:
terminalreporter.section("command to run failed test suites")
# Use dict.fromkeys to preserve the order
ids = list(dict.fromkeys(report.fspath for report in failed_test_reports))
terminalreporter.write(" ".join(["pytest"] + ids))
terminalreporter.write("\n" * 2)
# If some tests failed at installing mlflow, we suggest using `--serve-wheel` flag.
# Some test cases try to install mlflow via pip e.g. model loading. They pins
# mlflow version to install based on local environment i.e. dev version ahead of
# the latest release, hence it's not found on PyPI. `--serve-wheel` flag was
# introduced to resolve this issue, which starts local PyPI server and serve
# an mlflow wheel based on local source code.
# Ref: https://github.com/mlflow/mlflow/pull/10247
msg = f"No matching distribution found for mlflow=={VERSION}"
for rep in failed_test_reports:
if any(msg in t for t in (rep.longreprtext, rep.capstdout, rep.capstderr)):
terminalreporter.section("HINTS", yellow=True)
terminalreporter.write(
f"Found test(s) that failed with {msg!r}. Adding"
" --serve-wheel` flag to your pytest command may help.\n\n",
yellow=True,
)
break
main_thread = threading.main_thread()
if threads := [t for t in threading.enumerate() if t is not main_thread]:
terminalreporter.section("Remaining threads", yellow=True)
for idx, thread in enumerate(threads, start=1):
terminalreporter.write(f"{idx}: {thread}\n")
if non_daemon_threads := [t for t in threads if not t.daemon]:
frames = sys._current_frames()
terminalreporter.section("Tracebacks of non-daemon threads", yellow=True)
for thread in non_daemon_threads:
thread.join(timeout=1)
if thread.is_alive() and (frame := frames.get(thread.ident)):
terminalreporter.section(repr(thread), sep="~")
terminalreporter.write("".join(traceback.format_stack(frame)))
try:
import psutil
except ImportError:
pass
else:
current_process = psutil.Process()
if children := current_process.children(recursive=True):
terminalreporter.section("Remaining child processes", yellow=True)
for idx, child in enumerate(children, start=1):
terminalreporter.write(f"{idx}: {child}\n")
@pytest.fixture(scope="module", autouse=True)
def clean_up_envs():
"""
Clean up virtualenvs and conda environments created during tests to save disk space.
"""
yield
if "GITHUB_ACTIONS" in os.environ:
from mlflow.utils.virtualenv import _get_mlflow_virtualenv_root
shutil.rmtree(_get_mlflow_virtualenv_root(), ignore_errors=True)
if not is_windows():
conda_info = json.loads(subprocess.check_output(["conda", "info", "--json"], text=True))
root_prefix = conda_info["root_prefix"]
regex = re.compile(r"mlflow-\w{32,}")
for env in conda_info["envs"]:
if env == root_prefix:
continue
if regex.fullmatch(os.path.basename(env)):
shutil.rmtree(env, ignore_errors=True)
@pytest.fixture(scope="session", autouse=True)
def enable_mlflow_testing():
with pytest.MonkeyPatch.context() as mp:
mp.setenv(_MLFLOW_TESTING.name, "TRUE")
yield
@pytest.fixture(scope="session", autouse=True)
def serve_wheel(request, tmp_path_factory):
"""
Models logged during tests have a dependency on the dev version of MLflow built from
source (e.g., mlflow==1.20.0.dev0) and cannot be served because the dev version is not
available on PyPI. This fixture serves a wheel for the dev version from a temporary
PyPI repository running on localhost and appends the repository URL to the
`PIP_EXTRA_INDEX_URL` environment variable to make the wheel available to pip.
"""
if not request.config.getoption("--serve-wheel"):
yield # pytest expects a generator fixture to yield
return
root = tmp_path_factory.mktemp("root")
mlflow_dir = root.joinpath("mlflow")
mlflow_dir.mkdir()
port = get_safe_port()
try:
repo_root = subprocess.check_output(
[
"git",
"rev-parse",
"--show-toplevel",
],
text=True,
).strip()
except subprocess.CalledProcessError:
# Some tests run in a Docker container where git is not installed.
# In this case, assume we're in the root of the repo.
repo_root = "."
subprocess.run(
[
sys.executable,
"-m",
"pip",
"wheel",
"--wheel-dir",
mlflow_dir,
"--no-deps",
repo_root,
],
check=True,
)
with subprocess.Popen(
[
sys.executable,
"-m",
"http.server",
str(port),
],
cwd=root,
) as prc:
try:
url = f"http://localhost:{port}"
if existing_url := os.environ.get("PIP_EXTRA_INDEX_URL"):
url = f"{existing_url} {url}"
os.environ["PIP_EXTRA_INDEX_URL"] = url
yield
finally:
prc.terminate()