Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderegg committed Aug 15, 2024
1 parent 7494359 commit 2abacff
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class TaskRequirementsAboveRequiredEC2InstanceTypeError(AutoscalingRuntimeError)
)


class TaskBestFittingInstanceNotFoundError(AutoscalingRuntimeError):
msg_template: str = (
"Task requires {resources} but no instance type fits the requirements. "
"TIP: Ensure task resources requirements fit available instance types."
)


class Ec2InvalidDnsNameError(AutoscalingRuntimeError):
msg_template: str = "Invalid EC2 private DNS name {aws_private_dns_name}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@
EC2Tags,
Resources,
)
from aws_library.ec2._errors import (
EC2InstanceNotFoundError,
EC2InstanceTypeInvalidError,
EC2TooManyInstancesError,
)
from aws_library.ec2._errors import EC2TooManyInstancesError
from fastapi import FastAPI
from models_library.generated_models.docker_rest_api import Node, NodeState
from servicelib.logging_utils import log_catch, log_context
from servicelib.utils_formatting import timedelta_as_minute_second
from types_aiobotocore_ec2.literals import InstanceTypeType

from ..core.errors import Ec2InvalidDnsNameError
from ..core.errors import (
Ec2InvalidDnsNameError,
TaskBestFittingInstanceNotFoundError,
TaskRequirementsAboveRequiredEC2InstanceTypeError,
TaskRequiresUnauthorizedEC2InstanceTypeError,
)
from ..core.settings import ApplicationSettings, get_application_settings
from ..models import (
AssignedTasksToInstanceType,
Expand Down Expand Up @@ -497,13 +498,12 @@ async def _find_needed_instances(
- task_required_resources,
)
)
except EC2InstanceNotFoundError:
_logger.exception(
"Task %s needs more resources than any EC2 instance "
"can provide with the current configuration. Please check!",
f"{task}",
)
except EC2InstanceTypeInvalidError:
except TaskBestFittingInstanceNotFoundError:
_logger.exception("Task %s needs more resources: ", f"{task}")
except (
TaskRequirementsAboveRequiredEC2InstanceTypeError,
TaskRequiresUnauthorizedEC2InstanceTypeError,
):
_logger.exception("Unexpected error:")

_logger.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
from textwrap import dedent

from aws_library.ec2 import AWSTagKey, AWSTagValue, EC2InstanceType, EC2Tags, Resources
from aws_library.ec2._errors import EC2InstanceNotFoundError

from .._meta import VERSION
from ..core.errors import ConfigurationError
from ..core.errors import ConfigurationError, TaskBestFittingInstanceNotFoundError
from ..core.settings import ApplicationSettings

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -105,7 +104,6 @@ def find_best_fitting_ec2_instance(

score, instance = next(iter(score_to_ec2_candidate.items()))
if score == 0:
raise EC2InstanceNotFoundError(
needed_resources=resources, msg="no adequate EC2 instance found!"
)
raise TaskBestFittingInstanceNotFoundError(needed_resources=resources)

return instance
8 changes: 5 additions & 3 deletions services/autoscaling/tests/unit/test_utils_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

import pytest
from aws_library.ec2 import EC2InstanceType, Resources
from aws_library.ec2._errors import EC2InstanceNotFoundError
from faker import Faker
from pydantic import ByteSize
from simcore_service_autoscaling.core.errors import ConfigurationError
from simcore_service_autoscaling.core.errors import (
ConfigurationError,
TaskBestFittingInstanceNotFoundError,
)
from simcore_service_autoscaling.utils.utils_ec2 import (
closest_instance_policy,
compose_user_data,
Expand All @@ -28,7 +30,7 @@ async def test_find_best_fitting_ec2_instance_with_no_instances_raises():
async def test_find_best_fitting_ec2_instance_closest_instance_policy_with_resource_0_raises(
random_fake_available_instances: list[EC2InstanceType],
):
with pytest.raises(EC2InstanceNotFoundError):
with pytest.raises(TaskBestFittingInstanceNotFoundError):
find_best_fitting_ec2_instance(
allowed_ec2_instances=random_fake_available_instances,
resources=Resources(cpus=0, ram=ByteSize(0)),
Expand Down

0 comments on commit 2abacff

Please sign in to comment.