Skip to content

Commit 198855d

Browse files
committed
fixes webserver tests
1 parent ab8aa5a commit 198855d

File tree

6 files changed

+20
-195
lines changed

6 files changed

+20
-195
lines changed

packages/models-library/src/models_library/api_schemas_webserver/computations.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
from pydantic import BaseModel
22

3-
from ..clusters import ClusterID
4-
53

64
class ComputationStart(BaseModel):
75
force_restart: bool = False
8-
cluster_id: ClusterID = 0
96
subgraph: set[str] = set()
107

118

packages/models-library/src/models_library/clusters.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class Cluster(BaseCluster):
113113
json_schema_extra={
114114
"examples": [
115115
{
116-
"id": 432,
116+
"id": DEFAULT_CLUSTER_ID,
117117
"name": "My awesome cluster",
118118
"type": ClusterTypeInModel.ON_PREMISE,
119119
"owner": 12,
@@ -126,7 +126,7 @@ class Cluster(BaseCluster):
126126
},
127127
},
128128
{
129-
"id": 432546,
129+
"id": DEFAULT_CLUSTER_ID,
130130
"name": "My AWS cluster",
131131
"description": "a AWS cluster administered by me",
132132
"type": ClusterTypeInModel.AWS,
@@ -144,25 +144,6 @@ class Cluster(BaseCluster):
144144
7899: CLUSTER_USER_RIGHTS, # type: ignore[dict-item]
145145
},
146146
},
147-
{
148-
"id": 325436,
149-
"name": "My AWS cluster",
150-
"description": "a AWS cluster administered by me",
151-
"type": ClusterTypeInModel.AWS,
152-
"owner": 2321,
153-
"endpoint": "https://registry.osparc-development.fake2.dev",
154-
"authentication": {
155-
"type": "tls",
156-
"tls_ca_file": "/path/to/ca_file",
157-
"tls_client_cert": "/path/to/cert_file",
158-
"tls_client_key": "/path/to/key_file",
159-
},
160-
"access_rights": {
161-
154: CLUSTER_ADMIN_RIGHTS, # type: ignore[dict-item]
162-
12: CLUSTER_MANAGER_RIGHTS, # type: ignore[dict-item]
163-
7899: CLUSTER_USER_RIGHTS, # type: ignore[dict-item]
164-
},
165-
},
166147
]
167148
},
168149
)

packages/models-library/src/models_library/projects_pipeline.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ class ComputationTask(BaseModel):
8282
"progress": 0.0,
8383
},
8484
"iteration": None,
85-
"cluster_id": None,
8685
"started": arrow.utcnow().shift(minutes=-50).datetime, # type: ignore[dict-item]
8786
"stopped": None,
8887
"submitted": arrow.utcnow().shift(hours=-1).datetime, # type: ignore[dict-item]
@@ -114,7 +113,6 @@ class ComputationTask(BaseModel):
114113
"progress": 1.0,
115114
},
116115
"iteration": 2,
117-
"cluster_id": 0,
118116
"started": arrow.utcnow().shift(minutes=-50).datetime, # type: ignore[dict-item]
119117
"stopped": arrow.utcnow().shift(minutes=-20).datetime, # type: ignore[dict-item]
120118
"submitted": arrow.utcnow().shift(hours=-1).datetime, # type: ignore[dict-item]

packages/pytest-simcore/src/pytest_simcore/services_api_mocks_for_aiohttp_clients.py

Lines changed: 9 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
# pylint: disable=unused-argument
33
# pylint: disable=unused-variable
44

5-
import json
6-
import random
75
import re
86
from pathlib import Path
97
from typing import Any
@@ -13,6 +11,7 @@
1311
from aioresponses import aioresponses as AioResponsesMock
1412
from aioresponses.core import CallbackResult
1513
from faker import Faker
14+
from models_library.api_schemas_directorv2.comp_tasks import ComputationGet
1615
from models_library.api_schemas_storage import (
1716
FileMetaDataGet,
1817
FileUploadCompleteFutureResponse,
@@ -23,7 +22,6 @@
2322
LinkType,
2423
PresignedLink,
2524
)
26-
from models_library.clusters import Cluster
2725
from models_library.generics import Envelope
2826
from models_library.projects_pipeline import ComputationTask
2927
from models_library.projects_state import RunningState
@@ -81,7 +79,7 @@ def create_computation_cb(url, **kwargs) -> CallbackResult:
8179
assert param in body, f"{param} is missing from body: {body}"
8280
state = (
8381
RunningState.PUBLISHED
84-
if "start_pipeline" in body and body["start_pipeline"]
82+
if body.get("start_pipeline")
8583
else RunningState.NOT_STARTED
8684
)
8785
pipeline: dict[str, list[str]] = FULL_PROJECT_PIPELINE_ADJACENCY
@@ -131,8 +129,13 @@ def get_computation_cb(url, **kwargs) -> CallbackResult:
131129
state = RunningState.NOT_STARTED
132130
pipeline: dict[str, list[str]] = FULL_PROJECT_PIPELINE_ADJACENCY
133131
node_states = FULL_PROJECT_NODE_STATES
134-
returned_computation = ComputationTask.model_validate(
135-
ComputationTask.model_config["json_schema_extra"]["examples"][0]
132+
assert "json_schema_extra" in ComputationGet.model_config
133+
assert isinstance(ComputationGet.model_config["json_schema_extra"], dict)
134+
assert isinstance(
135+
ComputationGet.model_config["json_schema_extra"]["examples"], list
136+
)
137+
returned_computation = ComputationGet.model_validate(
138+
ComputationGet.model_config["json_schema_extra"]["examples"][0]
136139
).model_copy(
137140
update={
138141
"id": Path(url.path).name,
@@ -151,85 +154,6 @@ def get_computation_cb(url, **kwargs) -> CallbackResult:
151154
)
152155

153156

154-
def create_cluster_cb(url, **kwargs) -> CallbackResult:
155-
assert "json" in kwargs, f"missing body in call to {url}"
156-
assert url.query.get("user_id")
157-
random_cluster = Cluster.model_validate(
158-
random.choice(Cluster.model_config["json_schema_extra"]["examples"])
159-
)
160-
return CallbackResult(
161-
status=201, payload=json.loads(random_cluster.model_dump_json(by_alias=True))
162-
)
163-
164-
165-
def list_clusters_cb(url, **kwargs) -> CallbackResult:
166-
assert url.query.get("user_id")
167-
return CallbackResult(
168-
status=200,
169-
body=json.dumps(
170-
[
171-
json.loads(
172-
Cluster.model_validate(
173-
random.choice(
174-
Cluster.model_config["json_schema_extra"]["examples"]
175-
)
176-
).model_dump_json(by_alias=True)
177-
)
178-
for _ in range(3)
179-
]
180-
),
181-
)
182-
183-
184-
def get_cluster_cb(url, **kwargs) -> CallbackResult:
185-
assert url.query.get("user_id")
186-
cluster_id = url.path.split("/")[-1]
187-
return CallbackResult(
188-
status=200,
189-
payload=json.loads(
190-
Cluster.model_validate(
191-
{
192-
**random.choice(
193-
Cluster.model_config["json_schema_extra"]["examples"]
194-
),
195-
**{"id": cluster_id},
196-
}
197-
).model_dump_json(by_alias=True)
198-
),
199-
)
200-
201-
202-
def get_cluster_details_cb(url, **kwargs) -> CallbackResult:
203-
assert url.query.get("user_id")
204-
cluster_id = url.path.split("/")[-1]
205-
assert cluster_id
206-
return CallbackResult(
207-
status=200,
208-
payload={
209-
"scheduler": {"status": "RUNNING"},
210-
"dashboard_link": "https://dashboard.link.com",
211-
},
212-
)
213-
214-
215-
def patch_cluster_cb(url, **kwargs) -> CallbackResult:
216-
assert url.query.get("user_id")
217-
cluster_id = url.path.split("/")[-1]
218-
return CallbackResult(
219-
status=200,
220-
payload=json.loads(
221-
Cluster.model_validate(
222-
{
223-
**random.choice(
224-
Cluster.model_config["json_schema_extra"]["examples"]
225-
),
226-
**{"id": cluster_id},
227-
}
228-
).model_dump_json(by_alias=True)
229-
),
230-
)
231-
232-
233157
@pytest.fixture
234158
async def director_v2_service_mock(
235159
aioresponses_mocker: AioResponsesMock,
@@ -280,73 +204,6 @@ async def director_v2_service_mock(
280204
aioresponses_mocker.delete(delete_computation_pattern, status=204, repeat=True)
281205
aioresponses_mocker.patch(projects_networks_pattern, status=204, repeat=True)
282206

283-
# clusters
284-
aioresponses_mocker.post(
285-
re.compile(
286-
r"^http://[a-z\-_]*director-v2:[0-9]+/v2/clusters\?(\w+(?:=\w+)?\&?){1,}$"
287-
),
288-
callback=create_cluster_cb,
289-
status=status.HTTP_201_CREATED,
290-
repeat=True,
291-
)
292-
293-
aioresponses_mocker.get(
294-
re.compile(
295-
r"^http://[a-z\-_]*director-v2:[0-9]+/v2/clusters\?(\w+(?:=\w+)?\&?){1,}$"
296-
),
297-
callback=list_clusters_cb,
298-
status=status.HTTP_201_CREATED,
299-
repeat=True,
300-
)
301-
302-
aioresponses_mocker.get(
303-
re.compile(
304-
r"^http://[a-z\-_]*director-v2:[0-9]+/v2/clusters(/[0-9]+)\?(\w+(?:=\w+)?\&?){1,}$"
305-
),
306-
callback=get_cluster_cb,
307-
status=status.HTTP_201_CREATED,
308-
repeat=True,
309-
)
310-
311-
aioresponses_mocker.get(
312-
re.compile(
313-
r"^http://[a-z\-_]*director-v2:[0-9]+/v2/clusters/[0-9]+/details\?(\w+(?:=\w+)?\&?){1,}$"
314-
),
315-
callback=get_cluster_details_cb,
316-
status=status.HTTP_201_CREATED,
317-
repeat=True,
318-
)
319-
320-
aioresponses_mocker.patch(
321-
re.compile(
322-
r"^http://[a-z\-_]*director-v2:[0-9]+/v2/clusters(/[0-9]+)\?(\w+(?:=\w+)?\&?){1,}$"
323-
),
324-
callback=patch_cluster_cb,
325-
status=status.HTTP_201_CREATED,
326-
repeat=True,
327-
)
328-
aioresponses_mocker.delete(
329-
re.compile(
330-
r"^http://[a-z\-_]*director-v2:[0-9]+/v2/clusters(/[0-9]+)\?(\w+(?:=\w+)?\&?){1,}$"
331-
),
332-
status=status.HTTP_204_NO_CONTENT,
333-
repeat=True,
334-
)
335-
336-
aioresponses_mocker.post(
337-
re.compile(r"^http://[a-z\-_]*director-v2:[0-9]+/v2/clusters:ping$"),
338-
status=status.HTTP_204_NO_CONTENT,
339-
repeat=True,
340-
)
341-
342-
aioresponses_mocker.post(
343-
re.compile(
344-
r"^http://[a-z\-_]*director-v2:[0-9]+/v2/clusters(/[0-9]+):ping\?(\w+(?:=\w+)?\&?){1,}$"
345-
),
346-
status=status.HTTP_204_NO_CONTENT,
347-
repeat=True,
348-
)
349-
350207
return aioresponses_mocker
351208

352209

services/web/server/src/simcore_service_webserver/director_v2/_handlers.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
from aiohttp import web
66
from common_library.json_serialization import json_dumps
7+
from models_library.api_schemas_directorv2.comp_tasks import ComputationGet
78
from models_library.api_schemas_webserver.computations import ComputationStart
8-
from models_library.clusters import ClusterID
99
from models_library.projects import ProjectID
10-
from models_library.users import UserID
1110
from pydantic import BaseModel, Field, TypeAdapter, ValidationError
1211
from servicelib.aiohttp import status
1312
from servicelib.aiohttp.rest_responses import create_http_error, exception_to_response
@@ -206,10 +205,6 @@ async def stop_computation(request: web.Request) -> web.Response:
206205
)
207206

208207

209-
class ComputationTaskGet(BaseModel):
210-
cluster_id: ClusterID | None
211-
212-
213208
@routes.get(f"/{VTAG}/computations/{{project_id}}", name="get_computation")
214209
@login_required
215210
@permission_required("services.pipeline.*")
@@ -219,15 +214,15 @@ async def get_computation(request: web.Request) -> web.Response:
219214
run_policy = get_project_run_policy(request.app)
220215
assert run_policy # nosec
221216

222-
user_id = UserID(request[RQT_USERID_KEY])
217+
user_id = request[RQT_USERID_KEY]
223218
project_id = ProjectID(request.match_info["project_id"])
224219

225220
try:
226221
project_ids: list[ProjectID] = await run_policy.get_runnable_projects_ids(
227222
request, project_id
228223
)
229224
_logger.debug("Project %s will get %d variants", project_id, len(project_ids))
230-
list_computation_tasks = TypeAdapter(list[ComputationTaskGet]).validate_python(
225+
list_computation_tasks = TypeAdapter(list[ComputationGet]).validate_python(
231226
await asyncio.gather(
232227
*[
233228
computations.get(project_id=pid, user_id=user_id)
@@ -236,12 +231,7 @@ async def get_computation(request: web.Request) -> web.Response:
236231
),
237232
)
238233
assert len(list_computation_tasks) == len(project_ids) # nosec
239-
# NOTE: until changed all the versions of a meta project shall use the same cluster
240-
# this should fail the day that changes
241-
assert all(
242-
c.cluster_id == list_computation_tasks[0].cluster_id
243-
for c in list_computation_tasks
244-
)
234+
245235
return web.json_response(
246236
data={"data": list_computation_tasks[0].model_dump(by_alias=True)},
247237
dumps=json_dumps,

services/web/server/tests/unit/with_dbs/01/test_director_v2_handlers.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ async def test_stop_computation(
111111
rsp = await client.post(f"{url}")
112112
await assert_status(
113113
rsp,
114-
status.HTTP_204_NO_CONTENT
115-
if user_role == UserRole.GUEST
116-
else expected.no_content,
114+
(
115+
status.HTTP_204_NO_CONTENT
116+
if user_role == UserRole.GUEST
117+
else expected.no_content
118+
),
117119
)
118120

119121

0 commit comments

Comments
 (0)