Skip to content

Commit

Permalink
removed 2 exposed unused rpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg committed Aug 29, 2023
1 parent cc4ef0b commit f66eb93
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,3 @@ async def get_or_create_cluster(
)

return cluster_get


@router.expose()
async def create_cluster(
app: FastAPI, *, user_id: UserID, wallet_id: WalletID
) -> list[EC2InstanceData]:
return await clusters.create_cluster(app, user_id=user_id, wallet_id=wallet_id)


@router.expose()
async def cluster_heartbeat(
app: FastAPI, *, user_id: UserID, wallet_id: WalletID
) -> None:
return await clusters.cluster_heartbeat(app, user_id=user_id, wallet_id=wallet_id)
108 changes: 22 additions & 86 deletions services/clusters-keeper/tests/unit/test_rpc_clusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
# pylint: disable=unused-variable


import asyncio
import datetime
from collections.abc import Callable
from dataclasses import dataclass
from typing import Final
from unittest.mock import MagicMock

import arrow
import pytest
Expand All @@ -16,9 +17,9 @@
from models_library.wallets import WalletID
from parse import Result, search
from pydantic import parse_obj_as
from pytest_mock.plugin import MockerFixture
from servicelib.rabbitmq import RabbitMQClient
from servicelib.rabbitmq_utils import RPCMethodName, RPCNamespace
from simcore_service_clusters_keeper.core.errors import Ec2InstanceNotFoundError
from simcore_service_clusters_keeper.models import ClusterGet
from simcore_service_clusters_keeper.utils.ec2 import HEARTBEAT_TAG_KEY
from types_aiobotocore_ec2 import EC2Client
Expand Down Expand Up @@ -100,36 +101,6 @@ async def _assert_cluster_instance_created(
assert parse_result["wallet_id"] == wallet_id


async def _create_cluster(
clusters_keeper_rabbitmq_rpc_client: RabbitMQClient,
ec2_client: EC2Client,
user_id: UserID,
wallet_id: WalletID,
) -> None:
# send rabbitmq rpc to create_cluster
rpc_response = await clusters_keeper_rabbitmq_rpc_client.rpc_request(
CLUSTERS_KEEPER_NAMESPACE,
RPCMethodName("create_cluster"),
user_id=user_id,
wallet_id=wallet_id,
)
assert rpc_response
# check we do have a new machine in AWS
await _assert_cluster_instance_created(ec2_client, user_id, wallet_id)


async def test_create_cluster(
_base_configuration: None,
clusters_keeper_rabbitmq_rpc_client: RabbitMQClient,
ec2_client: EC2Client,
user_id: UserID,
wallet_id: WalletID,
):
await _create_cluster(
clusters_keeper_rabbitmq_rpc_client, ec2_client, user_id, wallet_id
)


async def _assert_cluster_heartbeat_on_instance(
ec2_client: EC2Client,
) -> datetime.datetime:
Expand All @@ -151,66 +122,29 @@ async def _assert_cluster_heartbeat_on_instance(
return this_heartbeat_time


async def _send_cluster_heartbeat(
clusters_keeper_rabbitmq_rpc_client: RabbitMQClient,
ec2_client: EC2Client,
user_id: UserID,
wallet_id: WalletID,
) -> datetime.datetime:
rpc_response = await clusters_keeper_rabbitmq_rpc_client.rpc_request(
CLUSTERS_KEEPER_NAMESPACE,
RPCMethodName("cluster_heartbeat"),
user_id=user_id,
wallet_id=wallet_id,
)
assert rpc_response is None

return await _assert_cluster_heartbeat_on_instance(ec2_client)

@dataclass
class MockedDaskModule:
ping_gateway: MagicMock

async def test_cluster_heartbeat(
_base_configuration: None,
clusters_keeper_rabbitmq_rpc_client: RabbitMQClient,
ec2_client: EC2Client,
user_id: UserID,
wallet_id: WalletID,
):
await _create_cluster(
clusters_keeper_rabbitmq_rpc_client, ec2_client, user_id, wallet_id
)

first_heartbeat_time = await _send_cluster_heartbeat(
clusters_keeper_rabbitmq_rpc_client, ec2_client, user_id, wallet_id
)

await asyncio.sleep(1)

next_heartbeat_time = await _send_cluster_heartbeat(
clusters_keeper_rabbitmq_rpc_client, ec2_client, user_id, wallet_id
@pytest.fixture
def mocked_dask_ping_gateway(mocker: MockerFixture) -> MockedDaskModule:
return MockedDaskModule(
ping_gateway=mocker.patch(
"simcore_service_clusters_keeper.rpc.clusters.ping_gateway",
autospec=True,
return_value=True,
),
)

assert next_heartbeat_time > first_heartbeat_time


async def test_cluster_heartbeat_on_non_existing_cluster_raises(
_base_configuration: None,
clusters_keeper_rabbitmq_rpc_client: RabbitMQClient,
ec2_client: EC2Client,
user_id: UserID,
wallet_id: WalletID,
):
with pytest.raises(Ec2InstanceNotFoundError):
await _send_cluster_heartbeat(
clusters_keeper_rabbitmq_rpc_client, ec2_client, user_id, wallet_id
)


async def test_get_or_create_cluster(
_base_configuration: None,
clusters_keeper_rabbitmq_rpc_client: RabbitMQClient,
ec2_client: EC2Client,
user_id: UserID,
wallet_id: WalletID,
mocked_dask_ping_gateway: MockedDaskModule,
):
# send rabbitmq rpc to create_cluster
rpc_response = await clusters_keeper_rabbitmq_rpc_client.rpc_request(
Expand All @@ -220,10 +154,12 @@ async def test_get_or_create_cluster(
wallet_id=wallet_id,
)
assert rpc_response
created_cluster = rpc_response
assert isinstance(created_cluster, ClusterGet)
created_cluster = ClusterGet.parse_raw(rpc_response)
# check we do have a new machine in AWS
await _assert_cluster_instance_created(ec2_client, user_id, wallet_id)
# it is called once as moto server creates instances instantly
mocked_dask_ping_gateway.ping_gateway.assert_called_once()
mocked_dask_ping_gateway.ping_gateway.reset_mock()

# calling it again returns the existing cluster
rpc_response = await clusters_keeper_rabbitmq_rpc_client.rpc_request(
Expand All @@ -233,9 +169,9 @@ async def test_get_or_create_cluster(
wallet_id=wallet_id,
)
assert rpc_response
returned_cluster = rpc_response
assert isinstance(created_cluster, ClusterGet)
returned_cluster = ClusterGet.parse_raw(rpc_response)
# check we still have only 1 instance
await _assert_cluster_instance_created(ec2_client, user_id, wallet_id)
await _assert_cluster_heartbeat_on_instance(ec2_client)
mocked_dask_ping_gateway.ping_gateway.assert_called_once()

assert created_cluster == returned_cluster

0 comments on commit f66eb93

Please sign in to comment.