From aefffd7eeed8abdd83171ded4fac533989af7ea4 Mon Sep 17 00:00:00 2001 From: Laurie O Date: Tue, 4 Oct 2022 19:49:47 +1000 Subject: [PATCH] EC2: Match exception to AWS response when describing non-existent EC2 instance types (#5527) --- moto/ec2/exceptions.py | 14 +++++++++----- moto/ec2/models/instance_types.py | 4 +++- tests/test_ec2/test_instance_types.py | 13 +++++++------ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/moto/ec2/exceptions.py b/moto/ec2/exceptions.py index da20305c7..710f62b12 100644 --- a/moto/ec2/exceptions.py +++ b/moto/ec2/exceptions.py @@ -259,11 +259,15 @@ class InvalidInstanceIdError(EC2ClientError): class InvalidInstanceTypeError(EC2ClientError): - def __init__(self, instance_type): - super().__init__( - "InvalidInstanceType.NotFound", - "The instance type '{0}' does not exist".format(instance_type), - ) + def __init__(self, instance_type, error_type="InvalidInstanceType.NotFound"): + if isinstance(instance_type, str): + msg = f"The instance type '{instance_type}' does not exist" + else: + msg = ( + f"The following supplied instance types do not exist: " + f"[{', '.join(instance_type)}]" + ) + super().__init__(error_type, msg) class InvalidAMIIdError(EC2ClientError): diff --git a/moto/ec2/models/instance_types.py b/moto/ec2/models/instance_types.py index 3d17ec9f5..d5dd139c5 100644 --- a/moto/ec2/models/instance_types.py +++ b/moto/ec2/models/instance_types.py @@ -145,7 +145,9 @@ class InstanceTypeBackend: unknown_ids = set(instance_types) - set( t.get("InstanceType") for t in matches ) - raise InvalidInstanceTypeError(unknown_ids) + raise InvalidInstanceTypeError( + unknown_ids, error_type="InvalidInstanceType" + ) if filters: matches = generic_filter(filters, matches) return matches diff --git a/tests/test_ec2/test_instance_types.py b/tests/test_ec2/test_instance_types.py index 28c603b87..3667b44d6 100644 --- a/tests/test_ec2/test_instance_types.py +++ b/tests/test_ec2/test_instance_types.py @@ -82,13 +82,14 @@ def test_describe_instance_types_gpu_instance_types(): def test_describe_instance_types_unknown_type(): client = boto3.client("ec2", "us-east-1") - with pytest.raises(ClientError) as err: + with pytest.raises(ClientError) as exc_info: client.describe_instance_types(InstanceTypes=["t1.non_existent"]) - err.response["Error"]["Code"].should.equal("ValidationException") - err.response["Error"]["Message"].split(":")[0].should.look_like( - "The instance type '{'t1.non_existent'}' does not exist" - ) - err.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) + + exc_info.value.response["Error"]["Code"].should.equal("InvalidInstanceType") + exc_info.value.response["Error"]["Message"].should.equal( + "The following supplied instance types do not exist: [t1.non_existent]", + ) + exc_info.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) @mock_ec2