Skip to content

Commit 86c3062

Browse files
committed
test getting greener
1 parent 54a2614 commit 86c3062

File tree

3 files changed

+46
-45
lines changed

3 files changed

+46
-45
lines changed

services/director/tests/unit/api/test_rest_health.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
from fastapi import status
88

99

10-
async def test_healthcheck(client: httpx.AsyncClient, api_version_prefix: str):
10+
async def test_healthcheck(
11+
configure_registry_access,
12+
client: httpx.AsyncClient,
13+
api_version_prefix: str,
14+
):
1115
resp = await client.get(f"/{api_version_prefix}/")
1216

1317
assert resp.is_success

services/director/tests/unit/api/test_rest_running_interactive_services.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77

88
import httpx
99
import pytest
10+
from faker import Faker
1011

1112
# from aioresponses.core import CallbackResult, aioresponses
1213
from fastapi import status
14+
from models_library.projects import ProjectID
15+
from models_library.users import UserID
1316

1417

1518
def _assert_response_and_unwrap_envelope(got: httpx.Response):
@@ -50,26 +53,28 @@ async def test_running_services_post_and_delete_no_swarm(
5053
)
5154
async def test_running_services_post_and_delete(
5255
configure_swarm_stack_name,
56+
configure_registry_access,
5357
client: httpx.AsyncClient,
5458
push_services,
55-
docker_swarm,
56-
user_id,
57-
project_id,
58-
api_version_prefix,
59+
docker_swarm: None,
60+
user_id: UserID,
61+
project_id: ProjectID,
62+
api_version_prefix: str,
5963
save_state: bool | None,
6064
expected_save_state_call: bool,
6165
mocker,
66+
faker: Faker,
6267
):
6368
params = {}
6469
resp = await client.post(
6570
f"/{api_version_prefix}/running_interactive_services", params=params
6671
)
67-
assert resp.status_code == status.HTTP_400_BAD_REQUEST
72+
assert resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
6873

6974
params = {
70-
"user_id": "None",
71-
"project_id": "None",
72-
"service_uuid": "sdlfkj4",
75+
"user_id": f"{faker.pyint(min_value=1)}",
76+
"project_id": f"{faker.uuid4()}",
77+
"service_uuid": f"{faker.uuid4()}",
7378
"service_key": "None",
7479
"service_tag": "None", # optional
7580
"service_basepath": "None", # optional
@@ -78,31 +83,39 @@ async def test_running_services_post_and_delete(
7883
f"/{api_version_prefix}/running_interactive_services", params=params
7984
)
8085
data = resp.json()
81-
assert resp.status_code == status.HTTP_400_BAD_REQUEST, data
86+
assert resp.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY, data
87+
88+
fake_headers = {"x-simcore-user-agent": faker.pystr()}
8289

8390
params["service_key"] = "simcore/services/comp/somfunkyname-nhsd"
8491
params["service_tag"] = "1.2.3"
8592
resp = await client.post(
86-
f"/{api_version_prefix}/running_interactive_services", params=params
93+
f"/{api_version_prefix}/running_interactive_services",
94+
params=params,
95+
headers=fake_headers,
8796
)
8897
data = resp.json()
8998
assert resp.status_code == status.HTTP_404_NOT_FOUND, data
9099

91-
created_services = await push_services(0, 2)
100+
created_services = await push_services(
101+
number_of_computational_services=0, number_of_interactive_services=2
102+
)
92103
assert len(created_services) == 2
93104
for created_service in created_services:
94105
service_description = created_service["service_description"]
95-
params["user_id"] = user_id
96-
params["project_id"] = project_id
106+
params["user_id"] = f"{user_id}"
107+
params["project_id"] = f"{project_id}"
97108
params["service_key"] = service_description["key"]
98109
params["service_tag"] = service_description["version"]
99110
service_port = created_service["internal_port"]
100111
service_entry_point = created_service["entry_point"]
101112
params["service_basepath"] = "/i/am/a/basepath"
102-
params["service_uuid"] = str(uuid.uuid4())
113+
params["service_uuid"] = f"{faker.uuid4()}"
103114
# start the service
104115
resp = await client.post(
105-
f"/{api_version_prefix}/running_interactive_services", params=params
116+
f"/{api_version_prefix}/running_interactive_services",
117+
params=params,
118+
headers=fake_headers,
106119
)
107120
assert resp.status_code == status.HTTP_201_CREATED
108121
assert resp.encoding == "application/json"

services/director/tests/unit/conftest.py

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,20 @@ def common_schemas_specs_dir(osparc_simcore_root_dir: Path) -> Path:
5757
return specs_dir
5858

5959

60-
@pytest.fixture(scope="session")
61-
def configure_swarm_stack_name(monkeypatch: pytest.MonkeyPatch) -> EnvVarsDict:
62-
return setenvs_from_dict(monkeypatch, envs={"SWARM_STACK_NAME": "test_stack"})
60+
@pytest.fixture
61+
def configure_swarm_stack_name(
62+
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch
63+
) -> EnvVarsDict:
64+
return app_environment | setenvs_from_dict(
65+
monkeypatch, envs={"SWARM_STACK_NAME": "test_stack"}
66+
)
6367

6468

6569
@pytest.fixture
6670
def configure_registry_access(
67-
monkeypatch: pytest.MonkeyPatch, docker_registry: str
71+
app_environment: EnvVarsDict, monkeypatch: pytest.MonkeyPatch, docker_registry: str
6872
) -> EnvVarsDict:
69-
return setenvs_from_dict(
73+
return app_environment | setenvs_from_dict(
7074
monkeypatch,
7175
envs={
7276
"REGISTRY_URL": docker_registry,
@@ -79,7 +83,9 @@ def configure_registry_access(
7983

8084
@pytest.fixture(scope="session")
8185
def configure_custom_registry(
82-
monkeypatch: pytest.MonkeyPatch, pytestconfig: pytest.Config
86+
app_environment: EnvVarsDict,
87+
monkeypatch: pytest.MonkeyPatch,
88+
pytestconfig: pytest.Config,
8389
) -> EnvVarsDict:
8490
# to set these values call
8591
# pytest --registry_url myregistry --registry_user username --registry_pw password
@@ -92,7 +98,7 @@ def configure_custom_registry(
9298
registry_pw = pytestconfig.getoption("registry_pw")
9399
assert registry_pw
94100
assert isinstance(registry_pw, str)
95-
return setenvs_from_dict(
101+
return app_environment | setenvs_from_dict(
96102
monkeypatch,
97103
envs={
98104
"REGISTRY_URL": registry_url,
@@ -144,25 +150,3 @@ async def app(
144150
shutdown_timeout=None if is_pdb_enabled else MAX_TIME_FOR_APP_TO_SHUTDOWN,
145151
):
146152
yield the_test_app
147-
148-
149-
# @pytest.fixture
150-
# async def aiohttp_mock_app(loop, mocker):
151-
# print("client session started ...")
152-
# session = ClientSession()
153-
154-
# mock_app_storage = {
155-
# config.APP_CLIENT_SESSION_KEY: session,
156-
# config.APP_REGISTRY_CACHE_DATA_KEY: {},
157-
# }
158-
159-
# def _get_item(self, key):
160-
# return mock_app_storage[key]
161-
162-
# aiohttp_app = mocker.patch("aiohttp.web.Application")
163-
# aiohttp_app.__getitem__ = _get_item
164-
165-
# yield aiohttp_app
166-
167-
# # cleanup session
168-
# await session.close()

0 commit comments

Comments
 (0)