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):
def __init__(self, ami_id):
super(InvalidAMIIdError, self).__init__(

View File

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

View File

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

View File

@ -2,6 +2,9 @@ from __future__ import unicode_literals
import boto3
import sure # noqa
import pytest
from botocore.exceptions import ClientError
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("MemoryInfo")
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)