From 98fc1eeab9ef39205a218805b87d9b3ecf222e91 Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Thu, 28 Feb 2013 00:08:35 -0500 Subject: [PATCH] update ec2 calls for boto 2.5 compat --- moto/ec2/models.py | 24 ++++++-- moto/ec2/responses/instances.py | 102 ++++++------------------------- tests/test_ec2/test_amis.py | 6 +- tests/test_ec2/test_instances.py | 6 +- 4 files changed, 44 insertions(+), 94 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 9455da63..7158139d 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -1,6 +1,6 @@ from collections import defaultdict -from boto.ec2.instance import Instance, InstanceState, Reservation +from boto.ec2.instance import Instance as BotoInstance, Reservation from moto.core import BaseBackend from .utils import ( @@ -13,6 +13,13 @@ from .utils import ( ) +class Instance(BotoInstance): + def __init__(self): + self._state_name = None + self._state_code = None + super(Instance, self).__init__() + + class InstanceBackend(object): def __init__(self): @@ -30,7 +37,8 @@ class InstanceBackend(object): for index in range(count): new_instance = Instance() new_instance.id = random_instance_id() - new_instance._state = InstanceState(0, "pending") + new_instance._state_name = "pending" + new_instance._state_code = 0 new_reservation.instances.append(new_instance) self.reservations[new_reservation.id] = new_reservation return new_reservation @@ -39,7 +47,8 @@ class InstanceBackend(object): started_instances = [] for instance in self.all_instances(): if instance.id in instance_ids: - instance._state = InstanceState(0, 'pending') + instance._state_name = "pending" + instance._state_code = 0 started_instances.append(instance) return started_instances @@ -48,7 +57,8 @@ class InstanceBackend(object): stopped_instances = [] for instance in self.all_instances(): if instance.id in instance_ids: - instance._state = InstanceState(64, 'stopping') + instance._state_name = "stopping" + instance._state_code = 64 stopped_instances.append(instance) return stopped_instances @@ -57,7 +67,8 @@ class InstanceBackend(object): terminated_instances = [] for instance in self.all_instances(): if instance.id in instance_ids: - instance._state = InstanceState(32, 'shutting-down') + instance._state_name = "shutting-down" + instance._state_code = 32 terminated_instances.append(instance) return terminated_instances @@ -67,7 +78,8 @@ class InstanceBackend(object): for instance in self.all_instances(): if instance.id in instance_ids: # TODO double check instances go to pending when reboot - instance._state = InstanceState(0, 'pending') + instance._state_name = "pending" + instance._state_code = 0 rebooted_instances.append(instance) return rebooted_instances diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py index 798b8413..c7c209a3 100644 --- a/moto/ec2/responses/instances.py +++ b/moto/ec2/responses/instances.py @@ -57,7 +57,7 @@ class InstanceResponse(object): value = self.querystring.get(key)[0] normalized_attribute = camelcase_to_underscores(key.split(".")[0]) instance_id = self.instance_ids[0] - instance = ec2_backend.modify_instance_attribute(instance_id, normalized_attribute, value) + ec2_backend.modify_instance_attribute(instance_id, normalized_attribute, value) return EC2_MODIFY_INSTANCE_ATTRIBUTE @@ -77,8 +77,8 @@ EC2_RUN_INSTANCES = """x86_64 ebs /dev/sda1 - - - /dev/sda1 - - vol-1a2b3c4d - attached - YYYY-MM-DDTHH:MM:SS.SSSZ - true - - - + hvm ABCDE1234567890123 - - - Name - Windows Instance - - + xen - - - eni-1a2b3c4d - subnet-1a2b3c4d - vpc-1a2b3c4d - Primary network interface - 111122223333 - in-use - 10.0.0.12 - 1b:2b:3c:4d:5e:6f - true - - - sg-1a2b3c4d - my-security-group - - - - eni-attach-1a2b3c4d - 0 - attached - YYYY-MM-DDTHH:MM:SS+0000 - true - - - 46.51.219.63 - 111122223333 - - - - 10.0.0.12 - true - - 46.51.219.63 - 111122223333 - - - - 10.0.0.14 - false - - 46.51.221.177 - 111122223333 - - - - - + {% endfor %} @@ -246,14 +184,14 @@ EC2_TERMINATE_INSTANCES = """ {% for instance in instances %} {{ instance.id }} - - 32 - shutting-down - 16 running + + {{ instance._state_code }} + {{ instance._state_name }} + {% endfor %} @@ -266,14 +204,14 @@ EC2_STOP_INSTANCES = """ {% for instance in instances %} {{ instance.id }} - - 32 - {{ instance.state }} - 16 running + + {{ instance._state_code }} + {{ instance._state_name }} + {% endfor %} @@ -286,14 +224,14 @@ EC2_START_INSTANCES = """ {% for instance in instances %} {{ instance.id }} - - 32 - {{ instance.state }} - 16 running + + {{ instance._state_code }} + {{ instance._state_name }} + {% endfor %} diff --git a/tests/test_ec2/test_amis.py b/tests/test_ec2/test_amis.py index 7afaddb5..e9726cfb 100644 --- a/tests/test_ec2/test_amis.py +++ b/tests/test_ec2/test_amis.py @@ -11,7 +11,7 @@ def test_ami_create_and_delete(): conn = boto.connect_ec2('the_key', 'the_secret') reservation = conn.run_instances('') instance = reservation.instances[0] - image = instance.create_image("test-ami", "this is a test ami") + image = conn.create_image(instance.id, "test-ami", "this is a test ami") all_images = conn.get_all_images() all_images[0].id.should.equal(image) @@ -33,6 +33,6 @@ def test_ami_pulls_attributes_from_instance(): instance = reservation.instances[0] instance.modify_attribute("kernel", "test-kernel") - image_id = instance.create_image("test-ami", "this is a test ami") + 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') + expect(image.kernel_id).should.equal('test-kernel') diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index 0c07d48a..2640056a 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -21,7 +21,7 @@ def test_instance_launch_and_terminate(): instances[0].id.should.equal(instance.id) instances[0].state.should.equal('pending') - conn.terminate_instances(instances[0].id) + conn.terminate_instances([instances[0].id]) reservations = conn.get_all_instances() instance = reservations[0].instances[0] @@ -40,7 +40,7 @@ def test_instance_start_and_stop(): for instance in stopped_instances: instance.state.should.equal('stopping') - started_instances = conn.start_instances(instances[0].id) + started_instances = conn.start_instances([instances[0].id]) started_instances[0].state.should.equal('pending') @@ -76,4 +76,4 @@ def test_instance_attribute_user_data(): instance_attribute = instance.get_attribute("userData") instance_attribute.should.be.a(InstanceAttribute) - instance_attribute.get("userData").should.equal("this is my user data") + expect(instance_attribute.get("userData")).should.equal("this is my user data")