|
| 1 | +# pylint: disable=redefined-outer-name |
| 2 | +# pylint: disable=unused-argument |
| 3 | + |
| 4 | +from collections.abc import AsyncIterator |
| 5 | +from uuid import uuid4 |
| 6 | + |
| 7 | +import aiodocker |
| 8 | +import pytest |
| 9 | +from models_library.api_schemas_resource_usage_tracker.pricing_plans import ( |
| 10 | + RutPricingPlanGet, |
| 11 | +) |
| 12 | +from models_library.projects_networks import ProjectsNetworks |
| 13 | +from models_library.services_resources import ( |
| 14 | + ServiceResourcesDict, |
| 15 | + ServiceResourcesDictHelpers, |
| 16 | +) |
| 17 | +from pydantic import TypeAdapter |
| 18 | +from pytest_mock.plugin import MockerFixture |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(scope="session") |
| 22 | +def network_name() -> str: |
| 23 | + return "pytest-simcore_interactive_services_subnet" |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +async def ensure_swarm_and_networks( |
| 28 | + network_name: str, docker_swarm: None |
| 29 | +) -> AsyncIterator[None]: |
| 30 | + """ |
| 31 | + Make sure to always have a docker swarm network. |
| 32 | + If one is not present crete one. There can not be more then one. |
| 33 | + """ |
| 34 | + |
| 35 | + async with aiodocker.Docker() as docker_client: |
| 36 | + # if network dose not exist create and remove it |
| 37 | + create_and_remove_network = True |
| 38 | + for network_data in await docker_client.networks.list(): |
| 39 | + if network_data["Name"] == network_name: |
| 40 | + create_and_remove_network = False |
| 41 | + break |
| 42 | + docker_network = None |
| 43 | + if create_and_remove_network: |
| 44 | + network_config = { |
| 45 | + "Name": network_name, |
| 46 | + "Driver": "overlay", |
| 47 | + "Attachable": True, |
| 48 | + "Internal": False, |
| 49 | + "Scope": "swarm", |
| 50 | + } |
| 51 | + docker_network = await docker_client.networks.create(network_config) |
| 52 | + |
| 53 | + yield |
| 54 | + |
| 55 | + if create_and_remove_network and docker_network: |
| 56 | + network = await docker_client.networks.get(docker_network.id) |
| 57 | + assert await network.delete() is True |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture |
| 61 | +def mock_projects_networks_repository(mocker: MockerFixture) -> None: |
| 62 | + mocker.patch( |
| 63 | + ( |
| 64 | + "simcore_service_director_v2.modules.db.repositories." |
| 65 | + "projects_networks.ProjectsNetworksRepository.get_projects_networks" |
| 66 | + ), |
| 67 | + return_value=ProjectsNetworks.model_validate( |
| 68 | + {"project_uuid": uuid4(), "networks_with_aliases": {}} |
| 69 | + ), |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture |
| 74 | +def service_resources() -> ServiceResourcesDict: |
| 75 | + return TypeAdapter(ServiceResourcesDict).validate_python( |
| 76 | + ServiceResourcesDictHelpers.model_config["json_schema_extra"]["examples"][0], |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.fixture |
| 81 | +def mock_resource_usage_tracker(mocker: MockerFixture) -> None: |
| 82 | + base_module = "simcore_service_director_v2.modules.resource_usage_tracker_client" |
| 83 | + service_pricing_plan = RutPricingPlanGet.model_validate( |
| 84 | + RutPricingPlanGet.model_config["json_schema_extra"]["examples"][1] |
| 85 | + ) |
| 86 | + for unit in service_pricing_plan.pricing_units: |
| 87 | + unit.specific_info.aws_ec2_instances.clear() |
| 88 | + mocker.patch( |
| 89 | + f"{base_module}.ResourceUsageTrackerClient.get_default_service_pricing_plan", |
| 90 | + return_value=service_pricing_plan, |
| 91 | + ) |
0 commit comments