-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathconftest.py
More file actions
313 lines (259 loc) · 9.56 KB
/
conftest.py
File metadata and controls
313 lines (259 loc) · 9.56 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
"""Fixtures for DeploymentExecutor tests."""
from __future__ import annotations
from datetime import datetime
from unittest.mock import AsyncMock, MagicMock
from uuid import UUID, uuid4
import pytest
from dateutil.tz import tzutc
from ai.backend.common.data.endpoint.types import EndpointLifecycle
from ai.backend.manager.data.deployment.types import (
DeploymentInfo,
DeploymentMetadata,
DeploymentNetworkSpec,
DeploymentState,
ReplicaSpec,
RouteStatus,
)
from ai.backend.manager.data.resource.types import ScalingGroupProxyTarget
from ai.backend.manager.repositories.deployment.types import RouteData
from ai.backend.manager.sokovan.deployment.executor import DeploymentExecutor
from ai.backend.manager.sokovan.deployment.types import DeploymentWithHistory
# =============================================================================
# Mock Dependencies
# =============================================================================
@pytest.fixture
def mock_deployment_repo() -> AsyncMock:
"""Mock DeploymentRepository."""
repo = AsyncMock()
repo.fetch_scaling_group_proxy_targets = AsyncMock(return_value={})
repo.fetch_active_routes_by_endpoint_ids = AsyncMock(return_value={})
repo.update_endpoint_urls_bulk = AsyncMock(return_value=None)
repo.update_desired_replicas_bulk = AsyncMock(return_value=None)
repo.mark_terminating_route_status_bulk = AsyncMock(return_value=None)
repo.scale_routes = AsyncMock(return_value=None)
repo.fetch_auto_scaling_rules_by_endpoint_ids = AsyncMock(return_value={})
repo.fetch_metrics_for_autoscaling = AsyncMock(return_value=MagicMock())
repo.calculate_desired_replicas_for_deployment = AsyncMock(return_value=None)
return repo
@pytest.fixture
def mock_scheduling_controller() -> AsyncMock:
"""Mock SchedulingController."""
return AsyncMock()
@pytest.fixture
def mock_config_provider() -> MagicMock:
"""Mock ManagerConfigProvider."""
provider = MagicMock()
provider.config.deployment.enable_model_definition_override = False
return provider
@pytest.fixture
def mock_client_pool() -> MagicMock:
"""Mock ClientPool."""
pool = MagicMock()
mock_session = MagicMock()
pool.load_client_session = MagicMock(return_value=mock_session)
return pool
@pytest.fixture
def mock_valkey_stat() -> AsyncMock:
"""Mock ValkeyStatClient."""
return AsyncMock()
@pytest.fixture
def deployment_executor(
mock_deployment_repo: AsyncMock,
mock_scheduling_controller: AsyncMock,
mock_config_provider: MagicMock,
mock_client_pool: MagicMock,
mock_valkey_stat: AsyncMock,
) -> DeploymentExecutor:
"""Create DeploymentExecutor with mocked dependencies."""
return DeploymentExecutor(
deployment_repo=mock_deployment_repo,
scheduling_controller=mock_scheduling_controller,
config_provider=mock_config_provider,
client_pool=mock_client_pool,
valkey_stat=mock_valkey_stat,
)
# =============================================================================
# DeploymentInfo Fixtures
# =============================================================================
def _create_deployment_info(
deployment_id: UUID | None = None,
lifecycle: EndpointLifecycle = EndpointLifecycle.PENDING,
desired_replica_count: int | None = None,
replica_count: int = 1,
resource_group: str = "default",
has_revision: bool = True,
) -> DeploymentInfo:
"""Create DeploymentInfo for tests."""
dep_id = deployment_id or uuid4()
rev_id = uuid4()
revision = MagicMock() if has_revision else None
if revision is not None:
revision.revision_id = rev_id
return DeploymentInfo(
id=dep_id,
metadata=DeploymentMetadata(
name="test-deployment",
domain="default",
project=uuid4(),
resource_group=resource_group,
created_user=uuid4(),
session_owner=uuid4(),
created_at=datetime.now(tzutc()),
revision_history_limit=10,
),
state=DeploymentState(
lifecycle=lifecycle,
retry_count=0,
),
replica_spec=ReplicaSpec(
replica_count=replica_count,
desired_replica_count=desired_replica_count,
),
network=DeploymentNetworkSpec(
open_to_public=False,
url=None,
),
model_revisions=[revision] if has_revision else [], # type: ignore[list-item]
current_revision_id=rev_id if has_revision else None,
)
def _create_route_data(
route_id: UUID | None = None,
endpoint_id: UUID | None = None,
status: RouteStatus = RouteStatus.HEALTHY,
) -> RouteData:
"""Create RouteData for tests."""
return RouteData(
route_id=route_id or uuid4(),
endpoint_id=endpoint_id or uuid4(),
session_id=None,
status=status,
traffic_ratio=1.0,
created_at=datetime.now(tzutc()),
)
@pytest.fixture
def pending_deployment() -> DeploymentWithHistory:
"""Single PENDING deployment for check_pending tests."""
return DeploymentWithHistory(
deployment_info=_create_deployment_info(lifecycle=EndpointLifecycle.PENDING),
)
@pytest.fixture
def pending_deployments_multiple() -> list[DeploymentWithHistory]:
"""Multiple PENDING deployments."""
return [
DeploymentWithHistory(
deployment_info=_create_deployment_info(
lifecycle=EndpointLifecycle.PENDING, resource_group="sg-1"
),
),
DeploymentWithHistory(
deployment_info=_create_deployment_info(
lifecycle=EndpointLifecycle.PENDING, resource_group="sg-2"
),
),
]
@pytest.fixture
def pending_deployment_no_revision() -> DeploymentWithHistory:
"""PENDING deployment without target revision."""
return DeploymentWithHistory(
deployment_info=_create_deployment_info(
lifecycle=EndpointLifecycle.PENDING, has_revision=False
),
)
@pytest.fixture
def ready_deployment() -> DeploymentWithHistory:
"""READY deployment for scaling tests."""
return DeploymentWithHistory(
deployment_info=_create_deployment_info(
lifecycle=EndpointLifecycle.READY,
desired_replica_count=2,
replica_count=2,
),
)
@pytest.fixture
def ready_deployment_needs_scale_up() -> DeploymentWithHistory:
"""READY deployment that needs scale up."""
return DeploymentWithHistory(
deployment_info=_create_deployment_info(
lifecycle=EndpointLifecycle.READY,
desired_replica_count=3,
replica_count=2,
),
)
@pytest.fixture
def ready_deployment_needs_scale_down() -> DeploymentWithHistory:
"""READY deployment that needs scale down."""
return DeploymentWithHistory(
deployment_info=_create_deployment_info(
lifecycle=EndpointLifecycle.READY,
desired_replica_count=1,
replica_count=2,
),
)
@pytest.fixture
def destroying_deployment() -> DeploymentWithHistory:
"""DESTROYING deployment for termination tests."""
return DeploymentWithHistory(
deployment_info=_create_deployment_info(lifecycle=EndpointLifecycle.DESTROYING),
)
@pytest.fixture
def destroying_deployments_multiple() -> list[DeploymentWithHistory]:
"""Multiple DESTROYING deployments."""
return [
DeploymentWithHistory(
deployment_info=_create_deployment_info(
lifecycle=EndpointLifecycle.DESTROYING, resource_group="sg-1"
),
),
DeploymentWithHistory(
deployment_info=_create_deployment_info(
lifecycle=EndpointLifecycle.DESTROYING, resource_group="sg-2"
),
),
]
# =============================================================================
# Proxy Target Fixtures
# =============================================================================
@pytest.fixture
def proxy_target_default() -> ScalingGroupProxyTarget:
"""Default proxy target for tests."""
return ScalingGroupProxyTarget(
addr="http://proxy:8080",
api_token="test-token",
)
@pytest.fixture
def proxy_targets_by_scaling_group(
proxy_target_default: ScalingGroupProxyTarget,
) -> dict[str, ScalingGroupProxyTarget]:
"""Proxy targets mapped by scaling group."""
return {
"default": proxy_target_default,
"sg-1": proxy_target_default,
"sg-2": proxy_target_default,
}
# =============================================================================
# Route Fixtures
# =============================================================================
@pytest.fixture
def routes_matching_replicas() -> list[RouteData]:
"""Routes matching target replica count."""
endpoint_id = uuid4()
return [
_create_route_data(endpoint_id=endpoint_id, status=RouteStatus.HEALTHY),
_create_route_data(endpoint_id=endpoint_id, status=RouteStatus.HEALTHY),
]
@pytest.fixture
def routes_fewer_than_replicas() -> list[RouteData]:
"""Routes fewer than target replica count."""
endpoint_id = uuid4()
return [
_create_route_data(endpoint_id=endpoint_id, status=RouteStatus.HEALTHY),
]
@pytest.fixture
def routes_more_than_replicas() -> list[RouteData]:
"""Routes more than target replica count."""
endpoint_id = uuid4()
return [
_create_route_data(endpoint_id=endpoint_id, status=RouteStatus.HEALTHY),
_create_route_data(endpoint_id=endpoint_id, status=RouteStatus.HEALTHY),
_create_route_data(endpoint_id=endpoint_id, status=RouteStatus.HEALTHY),
]