forked from dremio/dremio-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
313 lines (264 loc) · 9.49 KB
/
conftest.py
File metadata and controls
313 lines (264 loc) · 9.49 KB
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
#
# Copyright (C) 2017-2025 Dremio Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""
Global pytest fixtures for dremio-mcp tests.
"""
import os
import random
import uuid
from typing import AsyncGenerator, NamedTuple
import pytest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
from collections import OrderedDict
from dremioai.config import settings
from dremioai.config.tools import ToolType
from dremioai.servers.mcp import (
Transports,
init,
create_metrics_server,
)
from mocks.http_mock import (
create_pytest_logging_server_fixture,
start_server_with_app,
ServerFixture,
LoggingServerFixture,
)
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
import contextlib
from dremioai.log import set_level
from dremioai.metrics import registry
from prometheus_client import CollectorRegistry
@pytest.fixture(autouse=True)
def reset_uvicorn_logger_propagation():
"""Reset uvicorn logger state between tests.
Uvicorn's configure_logging() sets uvicorn.access.propagate=False via its
default LOGGING_CONFIG when a server starts. This leaks into subsequent tests
that assert stdlib loggers propagate to the root handler.
"""
yield
import logging
for name in ("uvicorn.access", "uvicorn.error", "uvicorn"):
lg = logging.getLogger(name)
lg.propagate = True
for h in lg.handlers[:]:
lg.removeHandler(h)
@pytest.fixture(autouse=True)
def reset_sse_starlette_app_status():
"""
Reset the global AppStatus.should_exit_event from sse_starlette between tests.
This fixes the asyncio event loop binding issue where the global event gets
bound to the first event loop and causes "bound to a different event loop"
errors in subsequent tests.
"""
try:
# Import and reset the global state
from sse_starlette.sse import AppStatus
AppStatus.should_exit_event = None
except ImportError:
# sse_starlette might not be available in all test environments
pass
yield
# Clean up after test
try:
from sse_starlette.sse import AppStatus
AppStatus.should_exit_event = None
except ImportError:
pass
@pytest.fixture(autouse=True)
def reset_metrics_registry():
"""
Reset the global metrics registry between tests.
This ensures that metrics from previous tests don't persist and affect subsequent
test assertions.
"""
registry._registry = CollectorRegistry()
yield
@pytest.fixture
def temp_config_dir():
"""Create a temporary directory for config files"""
with TemporaryDirectory() as temp_dir:
yield Path(temp_dir)
@pytest.fixture
def mock_config_dir(temp_config_dir):
"""Mock the home directory to use our temporary directory"""
with patch.object(Path, "home", return_value=temp_config_dir):
# Also patch XDG_CONFIG_HOME environment variable
old_env = os.environ.get("XDG_CONFIG_HOME")
os.environ["XDG_CONFIG_HOME"] = str(temp_config_dir)
yield temp_config_dir
# Restore original environment
if old_env:
os.environ["XDG_CONFIG_HOME"] = old_env
else:
os.environ.pop("XDG_CONFIG_HOME", None)
@pytest.fixture
def mock_settings_instance():
"""Create a mock settings instance with default values"""
old_settings = settings.instance()
try:
settings._settings.set(
settings.Settings.model_validate(
{
"dremio": {
"uri": "https://test-dremio-uri.com",
"pat": "test-pat",
"project_id": uuid.uuid4(),
},
"tools": {"server_mode": ToolType.FOR_SELF.name},
}
)
)
yield settings.instance()
finally:
settings._settings.set(old_settings)
@pytest.fixture
def temp_config_dir():
"""Create a temporary directory for config files"""
with TemporaryDirectory() as temp_dir:
yield Path(temp_dir)
@pytest.fixture
def mock_config_dir(temp_config_dir):
"""Mock the home directory to use our temporary directory"""
with patch.object(Path, "home", return_value=temp_config_dir):
# Also patch XDG_CONFIG_HOME environment variable
old_env = os.environ.get("XDG_CONFIG_HOME")
os.environ["XDG_CONFIG_HOME"] = str(temp_config_dir)
yield temp_config_dir
# Restore original environment
if old_env:
os.environ["XDG_CONFIG_HOME"] = old_env
else:
os.environ.pop("XDG_CONFIG_HOME", None)
def _create_logging_server(log_level="warning"):
# Mock data for HTTP endpoints that tools will call
mock_data = OrderedDict(
[
(r"/sql", "sql/job_submission.json"), # SQL query submission
(r"/job/test-job-12345$", "sql/job_status.json"), # Job status check
(r"/job/test-job-12345/results$", "sql/job_results.json"), # Job results
(r"/search", "search/search_results.json"), # Search endpoints
(r"/catalog/.*/wiki", "catalog/wiki.json"), # Wiki endpoints
(r"/catalog/.*/tags", "catalog/tags.json"), # Tags endpoints
(r"/catalog/.*/graph", "catalog/lineage.json"), # Lineage endpoints
(r"/catalog(/by-path)?", "catalog/table_schema.json"), # Schema endpoints
]
)
return create_pytest_logging_server_fixture(
mock_data=mock_data, port=8000, log_level=log_level
)
@pytest.fixture
def logging_level(request: pytest.FixtureRequest):
if request.config.get_verbosity() > 2:
return "debug"
if request.config.get_verbosity() > 1:
return "info"
return "warning"
@pytest.fixture
def logging_server(logging_level):
server = _create_logging_server(logging_level)
try:
yield server
finally:
server.close()
class StreamableMcpServerFixture(NamedTuple):
mcp_server: ServerFixture
logging_server: LoggingServerFixture
metrics_port: int
@contextlib.asynccontextmanager
async def http_streamable_mcp_server(
logging_server: LoggingServerFixture,
logging_level: str,
project_id: str = None,
wlm_engine: str = None,
dremio_overrides: dict = None,
) -> AsyncGenerator[StreamableMcpServerFixture, None]:
old = settings.instance()
sf = None
try:
settings.configure(force=True)
host = "127.0.0.1"
port = random.randrange(9000, 12000)
metrics_port = random.randrange(9000, 12000)
# Ensure metrics port is different from main port
while metrics_port == port:
metrics_port = random.randrange(9000, 12000)
config = {
"dremio": {
"uri": logging_server.url,
"project_id": uuid.uuid4(),
"pat": "test-pat",
"enable_search": True,
"metrics_enabled": True,
"metrics": {
"enabled": True,
"port": metrics_port,
},
},
"tools": {"server_mode": ToolType.FOR_DATA_PATTERNS.name},
}
if wlm_engine:
config["dremio"]["wlm"] = {"engine_name": wlm_engine}
if dremio_overrides:
config["dremio"].update(dremio_overrides)
settings._settings.set(settings.Settings.model_validate(config))
settings.write_settings()
set_level(logging_level.upper())
# Start metrics server using asyncio
metrics_server = create_metrics_server(
host=host, port=metrics_port, log_level=logging_level
)
mcp_server = init(
transport=Transports.streamable_http,
port=port,
mode=settings.instance().tools.server_mode,
support_project_id_endpoints=project_id is not None,
)
app = mcp_server.streamable_http_app()
server, stop_event = start_server_with_app(
app,
host=host,
port=port,
log_level=logging_level,
additional_runners=[metrics_server.serve()],
)
sf = ServerFixture(
f"http://{host}:{port}/mcp/{(str(project_id) + '/') if project_id else ''}",
stop_event,
server,
)
yield StreamableMcpServerFixture(sf, logging_server, metrics_port)
finally:
if sf is not None:
sf.close()
print(f"{sf} closed")
settings._settings.set(old)
@contextlib.asynccontextmanager
async def http_streamable_client_server(
sf: ServerFixture, token=None
) -> AsyncGenerator[ClientSession, None]:
headers = {"Authorization": f"Bearer {token}"} if token is not None else None
async with streamablehttp_client(url=sf.url, headers=headers) as (
read_stream,
write_stream,
gid,
):
print(f"Client connected to {sf.url}")
async with ClientSession(read_stream, write_stream) as session:
await session.initialize()
yield session