Improved describe instance types + tests

This commit is contained in:
Nuwan Goonasekera 2020-11-22 21:14:01 +05:30
parent a69bad57ef
commit 114a8efac8
No known key found for this signature in database
GPG Key ID: 4C28B687668239D8
4 changed files with 43 additions and 10 deletions

View File

@ -198,6 +198,13 @@ class InvalidInstanceIdError(EC2ClientError):
) )
class InvalidInstanceTypeError(EC2ClientError):
def __init__(self, instance_type):
super(InvalidInstanceTypeError, self).__init__(
"InvalidInstanceType.NotFound",
"The instance type '{0}' does not exist".format(instance_type),
)
class InvalidAMIIdError(EC2ClientError): class InvalidAMIIdError(EC2ClientError):
def __init__(self, ami_id): def __init__(self, ami_id):
super(InvalidAMIIdError, self).__init__( super(InvalidAMIIdError, self).__init__(

View File

@ -51,6 +51,7 @@ from .exceptions import (
InvalidDomainError, InvalidDomainError,
InvalidID, InvalidID,
InvalidInstanceIdError, InvalidInstanceIdError,
InvalidInstanceTypeError,
InvalidInternetGatewayIdError, InvalidInternetGatewayIdError,
InvalidKeyPairDuplicateError, InvalidKeyPairDuplicateError,
InvalidKeyPairFormatError, InvalidKeyPairFormatError,
@ -1126,8 +1127,6 @@ class InstanceBackend(object):
return reservations return reservations
<<<<<<< HEAD
=======
class InstanceTypeBackend(object): class InstanceTypeBackend(object):
def __init__(self): def __init__(self):
super(InstanceTypeBackend, self).__init__() super(InstanceTypeBackend, self).__init__()

View File

@ -144,9 +144,8 @@ class InstanceResponse(BaseResponse):
return template.render(instances=instances) return template.render(instances=instances)
def describe_instance_types(self): def describe_instance_types(self):
instance_types = [ instance_type_filters = self._get_multi_param("InstanceType")
InstanceType(name="t1.micro", cores=1, memory=644874240, disk=0) instance_types = self.ec2_backend.describe_instance_types(instance_type_filters)
]
template = self.response_template(EC2_DESCRIBE_INSTANCE_TYPES) template = self.response_template(EC2_DESCRIBE_INSTANCE_TYPES)
return template.render(instance_types=instance_types) return template.render(instance_types=instance_types)
@ -826,17 +825,17 @@ EC2_DESCRIBE_INSTANCE_TYPES = """<?xml version="1.0" encoding="UTF-8"?>
<instanceTypeSet> <instanceTypeSet>
{% for instance_type in instance_types %} {% for instance_type in instance_types %}
<item> <item>
<instanceType>{{ instance_type.name }}</instanceType> <instanceType>{{ instance_type.apiname }}</instanceType>
<vCpuInfo> <vCpuInfo>
<defaultVCpus>{{ instance_type.cores }}</defaultVCpus> <defaultVCpus>{{ instance_type.vcpus|int }}</defaultVCpus>
<defaultCores>{{ instance_type.cores }}</defaultCores> <defaultCores>{{ instance_type.vcpus|int }}</defaultCores>
<defaultThreadsPerCore>1</defaultThreadsPerCore> <defaultThreadsPerCore>1</defaultThreadsPerCore>
</vCpuInfo> </vCpuInfo>
<memoryInfo> <memoryInfo>
<sizeInMiB>{{ instance_type.memory }}</sizeInMiB> <sizeInMiB>{{ instance_type.memory|int }}</sizeInMiB>
</memoryInfo> </memoryInfo>
<instanceStorageInfo> <instanceStorageInfo>
<totalSizeInGB>{{ instance_type.disk }}</totalSizeInGB> <totalSizeInGB>{{ instance_type.storage|int }}</totalSizeInGB>
</instanceStorageInfo> </instanceStorageInfo>
<processorInfo> <processorInfo>
<supportedArchitectures> <supportedArchitectures>

View File

@ -2,6 +2,9 @@ from __future__ import unicode_literals
import boto3 import boto3
import sure # noqa import sure # noqa
import pytest
from botocore.exceptions import ClientError
from moto import mock_ec2 from moto import mock_ec2
@ -16,3 +19,28 @@ def test_describe_instance_types():
instance_types["InstanceTypes"][0].should.have.key("InstanceType") instance_types["InstanceTypes"][0].should.have.key("InstanceType")
instance_types["InstanceTypes"][0].should.have.key("MemoryInfo") instance_types["InstanceTypes"][0].should.have.key("MemoryInfo")
instance_types["InstanceTypes"][0]["MemoryInfo"].should.have.key("SizeInMiB") instance_types["InstanceTypes"][0]["MemoryInfo"].should.have.key("SizeInMiB")
@mock_ec2
def test_describe_instance_types_filter_by_type():
client = boto3.client("ec2", "us-east-1")
instance_types = client.describe_instance_types(InstanceTypes=['t1.micro', 't2.nano'])
instance_types.should.have.key("InstanceTypes")
instance_types["InstanceTypes"].should_not.be.empty
instance_types["InstanceTypes"].should.have.length_of(2)
instance_types["InstanceTypes"][0]['InstanceType'].should.equal('t1.micro')
instance_types["InstanceTypes"][1]['InstanceType'].should.equal('t2.nano')
@mock_ec2
def test_describe_instance_types_unknown_type():
client = boto3.client("ec2", "us-east-1")
with pytest.raises(ClientError) as err:
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)