|
| 1 | +from collections.abc import Callable |
| 2 | + |
| 3 | +import arrow |
| 4 | +from aws_library.ec2 import EC2InstanceData |
| 5 | +from models_library.generated_models.docker_rest_api import ( |
| 6 | + Availability, |
| 7 | + Node, |
| 8 | + NodeState, |
| 9 | +) |
| 10 | +from pytest_mock import MockType |
| 11 | +from simcore_service_autoscaling.models import AssociatedInstance, Cluster |
| 12 | +from simcore_service_autoscaling.utils.utils_docker import ( |
| 13 | + _OSPARC_NODE_TERMINATION_PROCESS_LABEL_KEY, |
| 14 | + _OSPARC_SERVICE_READY_LABEL_KEY, |
| 15 | + _OSPARC_SERVICES_READY_DATETIME_LABEL_KEY, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +def assert_cluster_state( |
| 20 | + spied_cluster_analysis: MockType, *, expected_calls: int, expected_num_machines: int |
| 21 | +) -> Cluster: |
| 22 | + assert spied_cluster_analysis.call_count == expected_calls |
| 23 | + |
| 24 | + assert isinstance(spied_cluster_analysis.spy_return, Cluster) |
| 25 | + assert ( |
| 26 | + spied_cluster_analysis.spy_return.total_number_of_machines() |
| 27 | + == expected_num_machines |
| 28 | + ) |
| 29 | + print("current cluster state:", spied_cluster_analysis.spy_return) |
| 30 | + cluster = spied_cluster_analysis.spy_return |
| 31 | + spied_cluster_analysis.reset_mock() |
| 32 | + return cluster |
| 33 | + |
| 34 | + |
| 35 | +def create_fake_association( |
| 36 | + create_fake_node: Callable[..., Node], |
| 37 | + drained_machine_id: str | None, |
| 38 | + terminating_machine_id: str | None, |
| 39 | +): |
| 40 | + fake_node_to_instance_map = {} |
| 41 | + |
| 42 | + async def _fake_node_creator( |
| 43 | + _nodes: list[Node], ec2_instances: list[EC2InstanceData] |
| 44 | + ) -> tuple[list[AssociatedInstance], list[EC2InstanceData]]: |
| 45 | + def _create_fake_node_with_labels(instance: EC2InstanceData) -> Node: |
| 46 | + if instance not in fake_node_to_instance_map: |
| 47 | + fake_node = create_fake_node() |
| 48 | + assert fake_node.spec |
| 49 | + fake_node.spec.availability = Availability.active |
| 50 | + assert fake_node.status |
| 51 | + fake_node.status.state = NodeState.ready |
| 52 | + assert fake_node.spec.labels |
| 53 | + fake_node.spec.labels |= { |
| 54 | + _OSPARC_SERVICES_READY_DATETIME_LABEL_KEY: arrow.utcnow().isoformat(), |
| 55 | + _OSPARC_SERVICE_READY_LABEL_KEY: ( |
| 56 | + "true" if instance.id != drained_machine_id else "false" |
| 57 | + ), |
| 58 | + } |
| 59 | + if instance.id == terminating_machine_id: |
| 60 | + fake_node.spec.labels |= { |
| 61 | + _OSPARC_NODE_TERMINATION_PROCESS_LABEL_KEY: arrow.utcnow().isoformat() |
| 62 | + } |
| 63 | + fake_node_to_instance_map[instance] = fake_node |
| 64 | + return fake_node_to_instance_map[instance] |
| 65 | + |
| 66 | + associated_instances = [ |
| 67 | + AssociatedInstance(node=_create_fake_node_with_labels(i), ec2_instance=i) |
| 68 | + for i in ec2_instances |
| 69 | + ] |
| 70 | + |
| 71 | + return associated_instances, [] |
| 72 | + |
| 73 | + return _fake_node_creator |
0 commit comments