Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
189 lines (157 loc) · 5.61 KB
/
Copy pathconftest.py
File metadata and controls
189 lines (157 loc) · 5.61 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
"""Pytest configuration and fixtures for minilake tests."""
import asyncio
import logging
import os
import subprocess
import time
import urllib.request
from pathlib import Path
from typing import Generator
import pytest
from databricks.sdk import WorkspaceClient
logger = logging.getLogger(__name__)
# Test server configuration
TEST_SERVER_PORT = 8123
TEST_SERVER_HOST = "127.0.0.1"
TEST_AUTH_TOKEN = "test-token"
# Detect if running in Docker (docker-compose.test.yml sets MINILAKE_DATA_DIR)
DOCKER_ENV = os.getenv("MINILAKE_DATA_DIR") is not None and os.getenv("MINILAKE_DATA_DIR") == "/data"
if DOCKER_ENV:
# Running in Docker Compose - server is on separate container
TEST_SERVER_URL = "http://minilake-test-server:8000"
else:
# Running locally - use localhost
TEST_SERVER_URL = f"http://{TEST_SERVER_HOST}:{TEST_SERVER_PORT}"
@pytest.fixture(scope="session")
def event_loop():
"""Create an event loop for async tests."""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="session")
def minilake_server() -> Generator[str, None, None]:
"""Start minilake server as subprocess (or use existing Docker service).
Yields the base URL (http://localhost:8123 or http://minilake-test-server:8000).
"""
# If running in Docker Compose, server is already running
if DOCKER_ENV:
logger.info(f"Using Docker Compose server at {TEST_SERVER_URL}")
# Just wait for it to be ready
max_retries = 30
retry_count = 0
while retry_count < max_retries:
try:
response = urllib.request.urlopen(f"{TEST_SERVER_URL}/_minilake/health", timeout=2)
if response.status == 200:
logger.info("Server is ready")
break
except Exception:
retry_count += 1
if retry_count % 5 == 0:
logger.debug(f"Waiting for server... (attempt {retry_count}/{max_retries})")
time.sleep(0.5)
if retry_count >= max_retries:
raise RuntimeError(f"Server at {TEST_SERVER_URL} failed to respond after {max_retries} attempts.")
yield TEST_SERVER_URL
return
# Local mode - start server as subprocess
logger.info(f"Starting minilake server on {TEST_SERVER_URL}")
# Get project root (relative to this conftest.py)
project_root = str(Path(__file__).parent.parent)
# MCP is off by default in production; the test server always enables it so
# tests/mcp/ has an endpoint to talk to (docker-compose.test.yml does the same for
# Docker mode).
server_env = {**os.environ, "MINILAKE_MCP": "1"}
proc = subprocess.Popen(
[
"uv",
"run",
"minilake",
"--port",
str(TEST_SERVER_PORT),
"--host",
TEST_SERVER_HOST,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=project_root,
env=server_env,
)
# Wait for server to be ready
max_retries = 30
retry_count = 0
while retry_count < max_retries:
try:
response = urllib.request.urlopen(f"{TEST_SERVER_URL}/_minilake/health", timeout=2)
if response.status == 200:
logger.info("Server is ready")
break
except Exception:
retry_count += 1
if retry_count % 5 == 0:
logger.debug(f"Waiting for server... (attempt {retry_count}/{max_retries})")
time.sleep(0.5)
if retry_count >= max_retries:
proc.terminate()
stdout, stderr = proc.communicate(timeout=5)
raise RuntimeError(
f"Server failed to start after {max_retries} attempts.\n"
f"stdout: {stdout.decode()}\nstderr: {stderr.decode()}"
)
yield TEST_SERVER_URL
# Cleanup
logger.info("Shutting down minilake server")
proc.terminate()
try:
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
logger.warning("Server did not shut down gracefully, killing it")
proc.kill()
proc.wait()
@pytest.fixture
def workspace_client(minilake_server: str) -> WorkspaceClient:
"""Create a databricks-sdk WorkspaceClient pointing at minilake."""
return WorkspaceClient(
host=minilake_server,
token=TEST_AUTH_TOKEN,
auth_type="pat",
)
@pytest.fixture
async def reset_state(minilake_server: str):
"""Reset minilake state before each test.
This is an async fixture that clears all service state
but preserves the DuckDB warehouse/UC data structures.
"""
# Reset before test
try:
urllib.request.urlopen(
urllib.request.Request(
f"{minilake_server}/_minilake/reset",
method="POST",
),
timeout=5,
)
logger.debug("State reset successful")
except Exception as e:
logger.warning(f"Failed to reset state: {e}")
yield
# Cleanup after test (optional)
@pytest.fixture(autouse=True)
def reset_state_sync(minilake_server: str):
"""Synchronous version of reset_state for non-async tests.
This is autouse so every test automatically resets state before running.
"""
# Reset before test
try:
urllib.request.urlopen(
urllib.request.Request(
f"{minilake_server}/_minilake/reset",
method="POST",
),
timeout=5,
)
logger.debug("State reset successful")
except Exception as e:
logger.warning(f"Failed to reset state: {e}")
yield
# Cleanup after test (optional)