From 90aa87d53eea2f2924314f7f3c5abdfed99fe21c Mon Sep 17 00:00:00 2001 From: Hugo Lopes Tavares Date: Tue, 28 Jan 2014 18:14:00 -0500 Subject: [PATCH] Fix DescribeImages to throw error if AMI requested does not exist --- moto/ec2/exceptions.py | 4 ++-- moto/ec2/models.py | 2 ++ moto/ec2/responses/amis.py | 17 ++++++++++++++--- moto/ec2/responses/instances.py | 2 +- tests/test_ec2/test_amis.py | 6 ++++++ 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/moto/ec2/exceptions.py b/moto/ec2/exceptions.py index 07122d2b2..7b8b1cdb8 100644 --- a/moto/ec2/exceptions.py +++ b/moto/ec2/exceptions.py @@ -1,4 +1,4 @@ class InvalidIdError(RuntimeError): - def __init__(self, instance_id): + def __init__(self, id_value): super(InvalidIdError, self).__init__() - self.instance_id = instance_id + self.id = id_value diff --git a/moto/ec2/models.py b/moto/ec2/models.py index fb09bddc5..497e57b31 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -222,6 +222,8 @@ class AmiBackend(object): def describe_images(self, ami_ids=None): if ami_ids: images = [image for image in self.amis.values() if image.id in ami_ids] + for missing_ami in set(ami_ids) - set(ami.id for ami in images): + raise InvalidIdError(missing_ami) else: images = self.amis.values() return images diff --git a/moto/ec2/responses/amis.py b/moto/ec2/responses/amis.py index 74375e1b0..fc1e95db2 100644 --- a/moto/ec2/responses/amis.py +++ b/moto/ec2/responses/amis.py @@ -2,6 +2,7 @@ from jinja2 import Template from moto.core.responses import BaseResponse from moto.ec2.models import ec2_backend +from moto.ec2.exceptions import InvalidIdError from moto.ec2.utils import instance_ids_from_querystring, image_ids_from_querystring @@ -35,9 +36,14 @@ class AmisResponse(BaseResponse): def describe_images(self): ami_ids = image_ids_from_querystring(self.querystring) - images = ec2_backend.describe_images(ami_ids=ami_ids) - template = Template(DESCRIBE_IMAGES_RESPONSE) - return template.render(images=images) + try: + images = ec2_backend.describe_images(ami_ids=ami_ids) + except InvalidIdError as exc: + template = Template(DESCRIBE_IMAGES_INVALID_IMAGE_ID_RESPONSE) + return template.render(image_id=exc.id), dict(status=400) + else: + template = Template(DESCRIBE_IMAGES_RESPONSE) + return template.render(images=images) def modify_image_attribute(self): raise NotImplementedError('AMIs.modify_image_attribute is not yet implemented') @@ -101,6 +107,11 @@ DESCRIBE_IMAGE_RESPONSE = """ +InvalidAMIID.NotFoundThe image id '[{{ image_id }}]' does not exist59dbff89-35bd-4eac-99ed-be587EXAMPLE +""" + DEREGISTER_IMAGE_RESPONSE = """ 59dbff89-35bd-4eac-99ed-be587EXAMPLE {{ success }} diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py index cc2c002bf..62f406b07 100644 --- a/moto/ec2/responses/instances.py +++ b/moto/ec2/responses/instances.py @@ -15,7 +15,7 @@ class InstanceResponse(BaseResponse): reservations = ec2_backend.get_reservations_by_instance_ids(instance_ids) except InvalidIdError as exc: template = Template(EC2_INVALID_INSTANCE_ID) - return template.render(instance_id=exc.instance_id), dict(status=400) + return template.render(instance_id=exc.id), dict(status=400) else: reservations = ec2_backend.all_reservations(make_copy=True) diff --git a/tests/test_ec2/test_amis.py b/tests/test_ec2/test_amis.py index 6cd3812e5..365b0c242 100644 --- a/tests/test_ec2/test_amis.py +++ b/tests/test_ec2/test_amis.py @@ -39,3 +39,9 @@ def test_ami_pulls_attributes_from_instance(): image_id = conn.create_image(instance.id, "test-ami", "this is a test ami") image = conn.get_image(image_id) image.kernel_id.should.equal('test-kernel') + + +@mock_ec2 +def test_getting_missing_ami(): + conn = boto.connect_ec2('the_key', 'the_secret') + conn.get_image.when.called_with('ami-missing').should.throw(EC2ResponseError)